-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathserver.go
More file actions
executable file
·624 lines (546 loc) · 19.6 KB
/
server.go
File metadata and controls
executable file
·624 lines (546 loc) · 19.6 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
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
621
622
623
624
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.majsoulRoundData.roundData = newGame(h.majsoulRoundData)
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)
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-----
MIIEcjCCAtqgAwIBAgIRAKRU8OQwtD5xe2UzG+QmQR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) (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)
}