forked from lonng/nanoserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.go
183 lines (152 loc) · 3.79 KB
/
history.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
package history
import (
"encoding/json"
"fmt"
"time"
"github.com/lonnng/nanoserver/db"
"github.com/lonnng/nanoserver/internal/protocol"
"github.com/lonnng/nanoserver/db/model"
)
type SnapShot struct {
Enter *protocol.PlayerEnterDesk `json:"enter"`
BasicInfo *protocol.DeskBasicInfo `json:"basicInfo"`
DuanPai *protocol.DuanPai `json:"duanPai"`
End *protocol.RoundOverStats `json:"end"`
// 如果在此遇到了gang操作就去GangScoreChanges中按序拿数据,
// 如果遇到了hu就去HuScoreChanges中拿数据,
// 即杠与和数据分开放
Do []*protocol.OpTypeDo `json:"do"`
GangScoreChanges []*protocol.GangPaiScoreChange `json:"gangScoreChanges"`
HuScoreChanges []*protocol.HuInfo `json:"huScoreChanges"`
}
type History struct {
mode int
beginAt int64
endAt int64
deskID int64
playerName0 string
playerName1 string
playerName2 string
playerName3 string
scoreChange0 int
scoreChange1 int
scoreChange2 int
scoreChange3 int
SnapShot
}
func New(deskID int64, mode int, name0, name1, name2, name3 string, basic *protocol.DeskBasicInfo, enter *protocol.PlayerEnterDesk, duan *protocol.DuanPai) *History {
return &History{
beginAt: time.Now().Unix(),
deskID: deskID,
mode: mode,
playerName0: name0,
playerName1: name1,
playerName2: name2,
playerName3: name3,
SnapShot: SnapShot{
BasicInfo: basic,
DuanPai: duan,
Enter: enter,
},
}
}
func (h *History) PushAction(op *protocol.OpTypeDo) {
h.Do = append(h.Do, op)
}
func (h *History) PushGangScoreChange(g *protocol.GangPaiScoreChange) error {
h.GangScoreChanges = append(h.GangScoreChanges, g)
return nil
}
func (h *History) PushHuScoreChange(hsc *protocol.HuInfo) error {
h.HuScoreChanges = append(h.HuScoreChanges, hsc)
return nil
}
func (h *History) SetEndStats(ge *protocol.RoundOverStats) error {
h.End = ge
return nil
}
func (h *History) SetScoreChangeForTurn(turn uint8, sc int) error {
switch turn {
case 0:
h.scoreChange0 = sc
case 1:
h.scoreChange1 = sc
case 2:
h.scoreChange2 = sc
case 3:
h.scoreChange3 = sc
default:
return nil
}
return nil
}
func (h *History) Save() error {
data, err := json.Marshal(&h.SnapShot)
if err != nil {
return err
}
t := &model.History{
DeskId: h.deskID,
BeginAt: h.beginAt,
Mode: h.mode,
EndAt: time.Now().Unix(),
PlayerName0: h.playerName0,
PlayerName1: h.playerName1,
PlayerName2: h.playerName2,
PlayerName3: h.playerName3,
ScoreChange0: h.scoreChange0,
ScoreChange1: h.scoreChange1,
ScoreChange2: h.scoreChange2,
ScoreChange3: h.scoreChange3,
Snapshot: string(data),
}
return db.InsertHistory(t)
}
type Record struct {
ZiMoNum int `json:"ziMo"`
HuNum int `json:"hu"`
PaoNum int `json:"pao"`
MingGangNum int `json:"mingGang"`
AnGangNum int `json:"anGang"`
TotalScore int `json:"totalScore"`
}
//场统计
type MatchStats map[int64][]*Record
//局统计
type RoundStats map[int64]*Record
func (ps MatchStats) Push(rs RoundStats) error {
if len(rs) == 0 {
return nil
}
for uid, r := range rs {
ps[uid] = append(ps[uid], r)
}
return nil
}
func (ps MatchStats) Result() map[int64]*Record {
ret := make(map[int64]*Record)
for p, records := range ps {
if _, ok := ret[p]; !ok {
ret[p] = &Record{}
}
for _, m := range records {
ret[p].AnGangNum += m.AnGangNum
ret[p].MingGangNum += m.MingGangNum
ret[p].TotalScore += m.TotalScore
ret[p].ZiMoNum += m.ZiMoNum
ret[p].HuNum += m.HuNum
ret[p].PaoNum += m.PaoNum
}
fmt.Printf("统计 玩家: %d 明杠: %d 暗杠: %d\n", p, ret[p].MingGangNum, ret[p].AnGangNum)
}
return ret
}
func (ps MatchStats) Round() int {
round := 0
for _, r := range ps {
if l := len(r); l > round {
round = l
}
}
return round
}