-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathserver.go
620 lines (546 loc) · 19.3 KB
/
server.go
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/EndlessCheng/mahjong-helper/platform/tenhou"
"github.com/EndlessCheng/mahjong-helper/util"
"github.com/EndlessCheng/mahjong-helper/util/debug"
"github.com/EndlessCheng/mahjong-helper/util/model"
"github.com/fatih/color"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
"io/ioutil"
stdLog "log"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"time"
)
const defaultPort = 12121
func newLogFilePath() (filePath string, err error) {
const logDir = "log"
if err = os.MkdirAll(logDir, os.ModePerm); err != nil {
return
}
fileName := fmt.Sprintf("gamedata-%s.log", time.Now().Format("20060102-150405"))
filePath = filepath.Join(logDir, fileName)
return filepath.Abs(filePath)
}
type mjHandler struct {
log echo.Logger
analysing bool
tenhouMessageReceiver *tenhou.MessageReceiver
tenhouRoundData *tenhouRoundData
majsoulMessageQueue chan []byte
majsoulRoundData *majsoulRoundData
majsoulRecordMap map[string]*majsoulRecordBaseInfo
majsoulCurrentRecordUUID string
majsoulCurrentRecordActionsList []majsoulRoundActions
majsoulCurrentRoundIndex int
majsoulCurrentActionIndex int
majsoulCurrentRoundActions majsoulRoundActions
}
func (h *mjHandler) logError(err error) {
fmt.Fprintln(os.Stderr, err)
if !debugMode {
h.log.Error(err)
}
}
// 调试用
func (h *mjHandler) index(c echo.Context) error {
data, err := ioutil.ReadAll(c.Request().Body)
if err != nil {
h.log.Error("[mjHandler.index.ioutil.ReadAll]", err)
return c.NoContent(http.StatusInternalServerError)
}
fmt.Println(data, string(data))
h.log.Info(data)
return c.String(http.StatusOK, time.Now().Format("2006-01-02 15:04:05"))
}
// 打一摸一分析器
func (h *mjHandler) analysis(c echo.Context) error {
if h.analysing {
return c.NoContent(http.StatusForbidden)
}
h.analysing = true
defer func() { h.analysing = false }()
d := struct {
Reset bool `json:"reset"`
Tiles string `json:"tiles"`
}{}
if err := c.Bind(&d); err != nil {
fmt.Println(err)
return c.String(http.StatusBadRequest, err.Error())
}
if _, err := analysisHumanTiles(model.NewSimpleHumanTilesInfo(d.Tiles)); err != nil {
fmt.Println(err)
return c.String(http.StatusBadRequest, err.Error())
}
return c.NoContent(http.StatusOK)
}
// 分析天凤 WebSocket 数据
func (h *mjHandler) analysisTenhou(c echo.Context) error {
data, err := ioutil.ReadAll(c.Request().Body)
if err != nil {
h.logError(err)
return c.String(http.StatusBadRequest, err.Error())
}
h.tenhouMessageReceiver.Put(data)
return c.NoContent(http.StatusOK)
}
func (h *mjHandler) runAnalysisTenhouMessageTask() {
if !debugMode {
defer func() {
if err := recover(); err != nil {
fmt.Println("内部错误:", err)
}
}()
}
for {
msg := h.tenhouMessageReceiver.Get()
d := tenhouMessage{}
if err := json.Unmarshal(msg, &d); err != nil {
h.logError(err)
continue
}
originJSON := string(msg)
if h.log != nil {
h.log.Info(originJSON)
}
h.tenhouRoundData.msg = &d
h.tenhouRoundData.originJSON = originJSON
if err := h.tenhouRoundData.analysis(); err != nil {
h.logError(err)
}
}
}
// 分析雀魂 WebSocket 数据
func (h *mjHandler) analysisMajsoul(c echo.Context) error {
data, err := ioutil.ReadAll(c.Request().Body)
if err != nil {
h.logError(err)
return c.String(http.StatusBadRequest, err.Error())
}
h.majsoulMessageQueue <- data
return c.NoContent(http.StatusOK)
}
func (h *mjHandler) runAnalysisMajsoulMessageTask() {
if !debugMode {
defer func() {
if err := recover(); err != nil {
fmt.Println("内部错误:", err)
}
}()
}
for msg := range h.majsoulMessageQueue {
d := &majsoulMessage{}
if err := json.Unmarshal(msg, d); err != nil {
h.logError(err)
continue
}
originJSON := string(msg)
if h.log != nil && debug.Lo == 0 {
h.log.Info(originJSON)
} else {
if len(originJSON) > 500 {
originJSON = originJSON[:500]
}
fmt.Println(originJSON)
}
switch {
case len(d.Friends) > 0:
// 好友列表
fmt.Println(d.Friends)
case len(d.RecordBaseInfoList) > 0:
// 牌谱基本信息列表
for _, record := range d.RecordBaseInfoList {
h.majsoulRecordMap[record.UUID] = record
}
color.HiGreen("收到 %2d 个雀魂牌谱(已收集 %d 个),请在网页上点击「查看」", len(d.RecordBaseInfoList), len(h.majsoulRecordMap))
case d.SharedRecordBaseInfo != nil:
// 处理分享的牌谱基本信息
// FIXME: 观看自己的牌谱也会有 d.SharedRecordBaseInfo
record := d.SharedRecordBaseInfo
h.majsoulRecordMap[record.UUID] = record
if err := h._loadMajsoulRecordBaseInfo(record.UUID); err != nil {
h.logError(err)
break
}
case d.CurrentRecordUUID != "":
// 载入某个牌谱
resetAnalysisCache()
h.majsoulCurrentRecordActionsList = nil
if err := h._loadMajsoulRecordBaseInfo(d.CurrentRecordUUID); err != nil {
// 看的是分享的牌谱(先收到 CurrentRecordUUID 和 AccountID,然后收到 SharedRecordBaseInfo)
// 或者是比赛场的牌谱
// 记录主视角 ID(可能是 0)
gameConf.setMajsoulAccountID(d.AccountID)
break
}
// 看的是自己的牌谱
// 更新当前使用的账号
gameConf.addMajsoulAccountID(d.AccountID)
if gameConf.currentActiveMajsoulAccountID != d.AccountID {
fmt.Println()
printAccountInfo(d.AccountID)
gameConf.setMajsoulAccountID(d.AccountID)
}
case len(d.RecordActions) > 0:
if h.majsoulCurrentRecordActionsList != nil {
// TODO: 网页发送更恰当的信息?
break
}
if h.majsoulCurrentRecordUUID == "" {
h.logError(fmt.Errorf("错误:程序未收到所观看的雀魂牌谱的 UUID"))
break
}
baseInfo, ok := h.majsoulRecordMap[h.majsoulCurrentRecordUUID]
if !ok {
h.logError(fmt.Errorf("错误:找不到雀魂牌谱 %s", h.majsoulCurrentRecordUUID))
break
}
selfAccountID := gameConf.currentActiveMajsoulAccountID
if selfAccountID == -1 {
h.logError(fmt.Errorf("错误:当前雀魂账号为空"))
break
}
h.majsoulRoundData.newGame()
h.majsoulRoundData.gameMode = gameModeRecord
// 获取并设置主视角初始座位
selfSeat, err := baseInfo.getSelfSeat(selfAccountID)
if err != nil {
h.logError(err)
break
}
h.majsoulRoundData.selfSeat = selfSeat
// 准备分析……
majsoulCurrentRecordActions, err := parseMajsoulRecordAction(d.RecordActions)
if err != nil {
h.logError(err)
break
}
h.majsoulCurrentRecordActionsList = majsoulCurrentRecordActions
h.majsoulCurrentRoundIndex = 0
h.majsoulCurrentActionIndex = 0
actions := h.majsoulCurrentRecordActionsList[h.majsoulCurrentRoundIndex]
// 创建分析任务
analysisCache := newGameAnalysisCache(h.majsoulCurrentRecordUUID, selfSeat)
setAnalysisCache(analysisCache)
go analysisCache.runMajsoulRecordAnalysisTask(actions)
// 分析第一局的起始信息
data := actions[0].Action
h._analysisMajsoulRoundData(data, originJSON)
case d.RecordClickAction != "":
// 处理网页上的牌谱点击:上一局/跳到某局/下一局/上一巡/跳到某巡/下一巡/上一步/播放/暂停/下一步/点击桌面
// 暂不能分析他家手牌
h._onRecordClick(d.RecordClickAction, d.RecordClickActionIndex, d.FastRecordTo)
case d.LiveBaseInfo != nil:
// 观战
gameConf.setMajsoulAccountID(1) // TODO: 重构
h.majsoulRoundData.newGame()
h.majsoulRoundData.selfSeat = 0 // 观战进来后看的是东起的玩家
h.majsoulRoundData.gameMode = gameModeLive
clearConsole()
fmt.Printf("正在载入对战:%s", d.LiveBaseInfo.String())
case d.LiveFastAction != nil:
if err := h._loadLiveAction(d.LiveFastAction, true); err != nil {
h.logError(err)
break
}
case d.LiveAction != nil:
if err := h._loadLiveAction(d.LiveAction, false); err != nil {
h.logError(err)
break
}
case d.ChangeSeatTo != nil:
// 切换座位
changeSeatTo := *(d.ChangeSeatTo)
h.majsoulRoundData.selfSeat = changeSeatTo
if debugMode {
fmt.Println("座位已切换至", changeSeatTo)
}
var actions majsoulRoundActions
if h.majsoulRoundData.gameMode == gameModeLive { // 观战
actions = h.majsoulCurrentRoundActions
} else { // 牌谱
fullActions := h.majsoulCurrentRecordActionsList[h.majsoulCurrentRoundIndex]
actions = fullActions[:h.majsoulCurrentActionIndex+1]
analysisCache := getAnalysisCache(changeSeatTo)
if analysisCache == nil {
analysisCache = newGameAnalysisCache(h.majsoulCurrentRecordUUID, changeSeatTo)
}
setAnalysisCache(analysisCache)
// 创建分析任务
go analysisCache.runMajsoulRecordAnalysisTask(fullActions)
}
h._fastLoadActions(actions)
case len(d.SyncGameActions) > 0:
h._fastLoadActions(d.SyncGameActions)
default:
// 其他:AI 分析
h._analysisMajsoulRoundData(d, originJSON)
}
}
}
func (h *mjHandler) _loadMajsoulRecordBaseInfo(majsoulRecordUUID string) error {
baseInfo, ok := h.majsoulRecordMap[majsoulRecordUUID]
if !ok {
return fmt.Errorf("错误:找不到雀魂牌谱 %s", majsoulRecordUUID)
}
// 标记当前正在观看的牌谱
h.majsoulCurrentRecordUUID = majsoulRecordUUID
clearConsole()
fmt.Printf("正在解析雀魂牌谱:%s", baseInfo.String())
// 标记古役模式
isGuyiMode := baseInfo.Config.isGuyiMode()
util.SetConsiderOldYaku(isGuyiMode)
if isGuyiMode {
fmt.Println()
color.HiGreen("古役模式已开启")
}
return nil
}
func (h *mjHandler) _loadLiveAction(action *majsoulRecordAction, isFast bool) error {
if debugMode {
fmt.Println("[_loadLiveAction] 收到", action, isFast)
}
newActions, err := h.majsoulCurrentRoundActions.append(action)
if err != nil {
return err
}
h.majsoulCurrentRoundActions = newActions
h.majsoulRoundData.skipOutput = isFast
h._analysisMajsoulRoundData(action.Action, "")
return nil
}
func (h *mjHandler) _analysisMajsoulRoundData(data *majsoulMessage, originJSON string) {
//if originJSON == "{}" {
// return
//}
h.majsoulRoundData.msg = data
h.majsoulRoundData.originJSON = originJSON
if err := h.majsoulRoundData.analysis(); err != nil {
h.logError(err)
}
}
func (h *mjHandler) _fastLoadActions(actions []*majsoulRecordAction) {
if len(actions) == 0 {
return
}
fastRecordEnd := util.MaxInt(0, len(actions)-3)
h.majsoulRoundData.skipOutput = true
// 留最后三个刷新,这样确保会刷新界面
for _, action := range actions[:fastRecordEnd] {
h._analysisMajsoulRoundData(action.Action, "")
}
h.majsoulRoundData.skipOutput = false
for _, action := range actions[fastRecordEnd:] {
h._analysisMajsoulRoundData(action.Action, "")
}
}
func (h *mjHandler) _onRecordClick(clickAction string, clickActionIndex int, fastRecordTo int) {
if debugMode {
fmt.Println("[_onRecordClick] 收到", clickAction, clickActionIndex, fastRecordTo)
}
analysisCache := getCurrentAnalysisCache()
switch clickAction {
case "nextStep", "update":
newActionIndex := h.majsoulCurrentActionIndex + 1
if newActionIndex >= len(h.majsoulCurrentRecordActionsList[h.majsoulCurrentRoundIndex]) {
return
}
h.majsoulCurrentActionIndex = newActionIndex
case "nextRound":
h.majsoulCurrentRoundIndex = (h.majsoulCurrentRoundIndex + 1) % len(h.majsoulCurrentRecordActionsList)
h.majsoulCurrentActionIndex = 0
go analysisCache.runMajsoulRecordAnalysisTask(h.majsoulCurrentRecordActionsList[h.majsoulCurrentRoundIndex])
case "preRound":
h.majsoulCurrentRoundIndex = (h.majsoulCurrentRoundIndex - 1 + len(h.majsoulCurrentRecordActionsList)) % len(h.majsoulCurrentRecordActionsList)
h.majsoulCurrentActionIndex = 0
go analysisCache.runMajsoulRecordAnalysisTask(h.majsoulCurrentRecordActionsList[h.majsoulCurrentRoundIndex])
case "jumpRound":
h.majsoulCurrentRoundIndex = clickActionIndex % len(h.majsoulCurrentRecordActionsList)
h.majsoulCurrentActionIndex = 0
go analysisCache.runMajsoulRecordAnalysisTask(h.majsoulCurrentRecordActionsList[h.majsoulCurrentRoundIndex])
case "nextXun", "preXun", "jumpXun", "preStep", "jumpToLastRoundXun":
if clickAction == "jumpToLastRoundXun" {
h.majsoulCurrentRoundIndex = (h.majsoulCurrentRoundIndex - 1 + len(h.majsoulCurrentRecordActionsList)) % len(h.majsoulCurrentRecordActionsList)
go analysisCache.runMajsoulRecordAnalysisTask(h.majsoulCurrentRecordActionsList[h.majsoulCurrentRoundIndex])
}
h.majsoulRoundData.skipOutput = true
currentRoundActions := h.majsoulCurrentRecordActionsList[h.majsoulCurrentRoundIndex]
startActionIndex := 0
endActionIndex := fastRecordTo
if clickAction == "nextXun" {
startActionIndex = h.majsoulCurrentActionIndex + 1
}
if debugMode {
fmt.Printf("快速处理牌谱中的操作:局 %d 动作 %d-%d\n", h.majsoulCurrentRoundIndex, startActionIndex, endActionIndex)
}
for i, action := range currentRoundActions[startActionIndex : endActionIndex+1] {
if debugMode {
fmt.Printf("快速处理牌谱中的操作:局 %d 动作 %d\n", h.majsoulCurrentRoundIndex, startActionIndex+i)
}
h._analysisMajsoulRoundData(action.Action, "")
}
h.majsoulRoundData.skipOutput = false
h.majsoulCurrentActionIndex = endActionIndex + 1
default:
return
}
if debugMode {
fmt.Printf("处理牌谱中的操作:局 %d 动作 %d\n", h.majsoulCurrentRoundIndex, h.majsoulCurrentActionIndex)
}
action := h.majsoulCurrentRecordActionsList[h.majsoulCurrentRoundIndex][h.majsoulCurrentActionIndex]
h._analysisMajsoulRoundData(action.Action, "")
if action.Name == "RecordHule" || action.Name == "RecordLiuJu" || action.Name == "RecordNoTile" {
// 播放和牌/流局动画,进入下一局或显示终局动画
h.majsoulCurrentRoundIndex++
h.majsoulCurrentActionIndex = 0
if h.majsoulCurrentRoundIndex == len(h.majsoulCurrentRecordActionsList) {
h.majsoulCurrentRoundIndex = 0
return
}
time.Sleep(time.Second)
actions := h.majsoulCurrentRecordActionsList[h.majsoulCurrentRoundIndex]
go analysisCache.runMajsoulRecordAnalysisTask(actions)
// 分析下一局的起始信息
data := actions[h.majsoulCurrentActionIndex].Action
h._analysisMajsoulRoundData(data, "")
}
}
var h *mjHandler
func getMajsoulCurrentRecordUUID() string {
return h.majsoulCurrentRecordUUID
}
func runServer(isHTTPS bool, port int) (err error) {
e := echo.New()
// 移除 echo.Echo 和 http.Server 在控制台上打印的信息
e.HideBanner = true
e.HidePort = true
e.StdLogger = stdLog.New(ioutil.Discard, "", 0)
// 默认是 log.ERROR
e.Logger.SetLevel(log.INFO)
// 设置日志输出到 log/gamedata-xxx.log
filePath, err := newLogFilePath()
if err != nil {
return
}
logFile, err := os.OpenFile(filePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
return
}
e.Logger.SetOutput(logFile)
e.Logger.Info("============================================================================================")
e.Logger.Info("服务启动")
h = &mjHandler{
log: e.Logger,
tenhouMessageReceiver: tenhou.NewMessageReceiver(),
tenhouRoundData: &tenhouRoundData{isRoundEnd: true},
majsoulMessageQueue: make(chan []byte, 100),
majsoulRoundData: &majsoulRoundData{selfSeat: -1},
majsoulRecordMap: map[string]*majsoulRecordBaseInfo{},
}
h.tenhouRoundData.roundData = newGame(h.tenhouRoundData)
h.majsoulRoundData.roundData = newGame(h.majsoulRoundData)
go h.runAnalysisTenhouMessageTask()
go h.runAnalysisMajsoulMessageTask()
e.Use(middleware.Recover())
e.Use(middleware.CORS())
e.GET("/", h.index)
e.POST("/debug", h.index)
e.POST("/analysis", h.analysis)
e.POST("/tenhou", h.analysisTenhou)
e.POST("/majsoul", h.analysisMajsoul)
// code.js 也用的该端口
if port == 0 {
port = defaultPort
}
addr := ":" + strconv.Itoa(port)
if !isHTTPS {
e.POST("/", h.analysisTenhou)
err = e.Start(addr)
} else {
e.POST("/", h.analysisMajsoul)
err = startTLS(e, addr)
}
if err != nil {
// 检查是否为端口占用错误
if opErr, ok := err.(*net.OpError); ok && opErr.Op == "listen" {
if syscallErr, ok := opErr.Err.(*os.SyscallError); ok && syscallErr.Syscall == "bind" {
color.HiRed(addr + " 端口已被占用,程序无法启动(是否已经开启了本程序?)")
}
}
return
}
return nil
}
const (
certText = `-----BEGIN CERTIFICATE-----
MII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mtaN6Wd9CmLRZ7oDINS1vrl+pu
zMbliNrpidtCLqDXD6FfscoliY//ZWg08H0GXr5h1ZCl81BYJPStGWSvTn+tzYHx
4PD3a7fAXwKBgBeThhB7DGPbM6Vr8h4/hawHRewjdzxskdNPga2XXGxYuEaMWvhu
8Wqw+e2RgTMQhWx5J0g+XuCwU2zlsWH0pV25hDGJ4xmsglrfgYbKdbljwMDRCQBF
NcZQ/5lWpubuwXQnjtTBH5x9DydtfOBU5+BSTvoVw+167CX1/3rTV8ktAoGBAKGn
DcX9i9lcVm93a6qP6Cy9U2bLe9P1voIceKUV0Vd2bPIOJTF4f/ttRMUblB7phXMZ
yYNYfuXkFyghIpQDxIB1yFnJpwV4QloeVVVc/BpT5KG2Pp+xIQgSdsQ4mMGQJJo0
dH3F3DKPUCMpsspVnlMFbzZH6cHCw8vPGpXjXOtNAoGBANNvhQieQ2c4EfT86Twz
tHu/dx9TySipj7v7n9USM95fk3rTs4LkAcPs3Ka9BhhGflfLwZN9hKznaszwvIKW
l9sui7jMl8cJ4XxH95j4umsklisJAwBkp6J7OSd8eOX2F8gidKk3HdwLX/xFFx/9
Y5quoWDnJFfyYohaUAC7OAKR
-----END PRIVATE KEY-----
`
)
func startTLS(e *echo.Echo, address string) (err error) {
s := e.TLSServer
s.TLSConfig = new(tls.Config)
s.TLSConfig.Certificates = make([]tls.Certificate, 1)
s.TLSConfig.Certificates[0], err = tls.X509KeyPair([]byte(certText), []byte(keyText))
if err != nil {
return
}
s.Addr = address
if !e.DisableHTTP2 {
s.TLSConfig.NextProtos = append(s.TLSConfig.NextProtos, "h2")
}
return e.StartServer(e.TLSServer)
}