Repository navigation
Expand file tree
/
Copy pathapp.go
More file actions
256 lines (221 loc) · 7.41 KB
/
Copy pathapp.go
File metadata and controls
256 lines (221 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package main
import (
"context"
"fmt"
"log"
"MCA-Maintenance/internal/config"
"MCA-Maintenance/internal/logic"
"MCA-Maintenance/internal/models"
)
// App struct
type App struct {
ctx context.Context
svc *logic.MaintenanceService
cfg *models.Config
}
// NewApp creates a new App application struct
func NewApp() *App {
cfg, err := config.LoadConfig("config.yaml")
if err != nil {
log.Printf("Warning: Failed to load config: %v", err)
}
svc := logic.NewMaintenanceService(cfg)
return &App{
svc: svc,
cfg: cfg,
}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
// GetConfig 获取当前配置
func (a *App) GetConfig() *models.Config {
return a.cfg
}
// GetUsers 获取用户列表
func (a *App) GetUsers() ([]models.User, error) {
if a.svc == nil {
return nil, fmt.Errorf("service not initialized")
}
return a.svc.GetUsers(a.ctx)
}
// UpdateUserRankScore 更新用户分数
func (a *App) UpdateUserRankScore(uid int64, score int) error {
if a.svc == nil {
return fmt.Errorf("service not initialized")
}
return a.svc.UpdateUserRankScore(a.ctx, uid, score)
}
// SimulateRating 积分模拟
func (a *App) SimulateRating(input models.RatingSimInput) models.RatingSimResult {
return logic.SimulateRating(input)
}
// GetSystemStats 获取系统统计信息
func (a *App) GetSystemStats() (models.SystemStats, error) {
if a.svc == nil {
return models.SystemStats{}, fmt.Errorf("service not initialized")
}
return a.svc.GetSystemStats(a.ctx)
}
// UploadMatchResult 手动上传对局战绩
func (a *App) UploadMatchResult(result models.MatchResult) error {
if a.svc == nil {
return fmt.Errorf("service not initialized")
}
return a.svc.UploadMatchResult(a.ctx, result)
}
// ─── MCA 业务代理(OPS 凭据由后端持有,前端不接触令牌)───
// ReportMatch 以 OPS 身份上报一局对战(正常端点 POST /api/matches)
func (a *App) ReportMatch(input models.TeamReportInput) (models.Match, error) {
if a.svc == nil {
return models.Match{}, fmt.Errorf("service not initialized")
}
return a.svc.ReportMatch(a.ctx, input)
}
// GetMatchByID 获取对局详情(含参与者评分变动明细)
func (a *App) GetMatchByID(id int64) (models.Match, error) {
if a.svc == nil {
return models.Match{}, fmt.Errorf("service not initialized")
}
return a.svc.GetMatchByID(a.ctx, id)
}
// ListMatches 全局对局查询(仅 OPS,分页 + 过滤,按结束时间倒序)
func (a *App) ListMatches(q models.MatchListQuery) ([]models.Match, error) {
if a.svc == nil {
return nil, fmt.Errorf("service not initialized")
}
return a.svc.ListMatches(a.ctx, q)
}
// GetMatchByGameID 按幂等键 game_id 直查对局(仅 OPS)
func (a *App) GetMatchByGameID(gameID string) (models.Match, error) {
if a.svc == nil {
return models.Match{}, fmt.Errorf("service not initialized")
}
return a.svc.GetMatchByGameID(a.ctx, gameID)
}
// GetMatchRejections 获取服务端拒绝记录快照(仅 OPS;服务端进程内
// 200 条环形,重启清空)
func (a *App) GetMatchRejections() ([]models.RejectionRecord, error) {
if a.svc == nil {
return nil, fmt.Errorf("service not initialized")
}
return a.svc.GetMatchRejections(a.ctx)
}
// JoinMatchmaking 加入匹配队列(身份为后端服务令牌对应的 UID);
// partyMembers 为组队预留字段,服务端契约确定前仅在非空时随请求发送
func (a *App) JoinMatchmaking(matchType string, partyMembers []string) (models.QueueEntry, error) {
if a.svc == nil {
return models.QueueEntry{}, fmt.Errorf("service not initialized")
}
return a.svc.JoinMatchmaking(a.ctx, matchType, partyMembers)
}
// LeaveMatchmaking 离开匹配队列
func (a *App) LeaveMatchmaking() error {
if a.svc == nil {
return fmt.Errorf("service not initialized")
}
return a.svc.LeaveMatchmaking(a.ctx)
}
// GetMatchmakingStatus 查询排队状态与近期成局记录
func (a *App) GetMatchmakingStatus() (models.MatchmakingStatus, error) {
if a.svc == nil {
return models.MatchmakingStatus{}, fmt.Errorf("service not initialized")
}
return a.svc.GetMatchmakingStatus(a.ctx)
}
// GetMatchAssignment 查询玩家当前的成局路由分配(仅 OPS,只读)
func (a *App) GetMatchAssignment(uuid string) (models.MatchAssignment, error) {
if a.svc == nil {
return models.MatchAssignment{}, fmt.Errorf("service not initialized")
}
return a.svc.GetAssignment(a.ctx, uuid)
}
// GetMetrics 获取服务端指标快照(仅 OPS:计数器 + 按模式队列深度)
func (a *App) GetMetrics() (models.MetricsSnapshot, error) {
if a.svc == nil {
return models.MetricsSnapshot{}, fmt.Errorf("service not initialized")
}
return a.svc.GetMetrics(a.ctx)
}
// GetTopRankings 获取指定类型的 Top N 排行榜(只读)
func (a *App) GetTopRankings(rankType string, limit int) ([]models.Ranking, error) {
if a.svc == nil {
return nil, fmt.Errorf("service not initialized")
}
return a.svc.GetTopRankings(a.ctx, rankType, limit)
}
// GetPlayerHistory 按玩家 UID 查询历史战绩(只读)
func (a *App) GetPlayerHistory(uid int64, limit int) ([]models.PlayerMatchRow, error) {
if a.svc == nil {
return nil, fmt.Errorf("service not initialized")
}
return a.svc.GetPlayerHistory(a.ctx, uid, limit)
}
// GetUpstreamStatus 探测上游 MCA 服务状态
func (a *App) GetUpstreamStatus() models.UpstreamStatus {
if a.svc == nil {
return models.UpstreamStatus{Reachable: false, Message: "service not initialized"}
}
return a.svc.GetUpstreamStatus(a.ctx)
}
// GetRequestLog 获取代理层请求日志(最新在前,最近 50 条)
func (a *App) GetRequestLog() []models.RequestLogEntry {
if a.svc == nil {
return []models.RequestLogEntry{}
}
return a.svc.GetRequestLog()
}
// GetOpsIdentity 获取 OPS 凭据状态(不含令牌明文)
func (a *App) GetOpsIdentity() models.OpsIdentity {
if a.svc == nil {
return models.OpsIdentity{}
}
return a.svc.GetOpsIdentity()
}
// ─── Seasons(契约 1.12.0:手动更季,OPS-only)───
// GetSeasons 获取全部赛季记录(新到旧,首条即当前赛季)。
func (a *App) GetSeasons() ([]models.SeasonRecord, error) {
if a.svc == nil {
return nil, fmt.Errorf("service not initialized")
}
return a.svc.GetSeasons(a.ctx)
}
// RollSeason 手动触发更季(OPS-only)。target_season 必须为当前赛季 +1;
// reset_scores 决定是否全部重置 rank_score(默认 false)。
func (a *App) RollSeason(input models.RollSeasonInput) (models.SeasonRecord, error) {
if a.svc == nil {
return models.SeasonRecord{}, fmt.Errorf("service not initialized")
}
return a.svc.RollSeason(a.ctx, input)
}
// SaveConfig 保存配置
func (a *App) SaveConfig(cfg models.Config) error {
if a.svc == nil {
return fmt.Errorf("service not initialized")
}
err := a.svc.SaveConfig(a.ctx, cfg)
if err == nil {
a.cfg = &cfg // 同步更新 App 中的配置缓存
}
return err
}
// GetConfigHistory 获取配置历史
func (a *App) GetConfigHistory() ([]models.ConfigHistoryItem, error) {
if a.svc == nil {
return nil, fmt.Errorf("service not initialized")
}
return a.svc.GetConfigHistory(a.ctx)
}
// RestoreConfig 还原配置
func (a *App) RestoreConfig(filename string) error {
if a.svc == nil {
return fmt.Errorf("service not initialized")
}
err := a.svc.RestoreConfig(a.ctx, filename)
if err == nil {
a.cfg = a.svc.GetConfig() // 同步更新 App 中的配置缓存
}
return err
}