-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
250 lines (224 loc) · 5.85 KB
/
helper.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
package controllers
import (
v1 "github.com/LilithGames/spiracle/api/v1"
)
func GetPlayerStatus(player v1.RoomIngressPlayer, base *v1.RoomIngressPlayerStatus) v1.RoomIngressPlayerStatus {
s := v1.RoomIngressPlayerStatus{
Id: player.Id,
}
if base == nil {
s.Token = player.Token
s.Status = v1.PlayerStatusPending
} else {
if player.Token != int64(0) {
s.Token = player.Token
} else {
s.Token = base.Token
}
s.Externals = base.Externals
s.Status = base.Status
s.Detail = base.Detail
s.Timestamp = base.Timestamp
s.Expire = base.Expire
}
return s
}
func GetRoomStatus(room v1.RoomIngressRoom, base *v1.RoomIngressRoomStatus) v1.RoomIngressRoomStatus {
players := make([]v1.RoomIngressPlayerStatus, len(room.Players))
dict := make(map[string]*v1.RoomIngressPlayerStatus)
if base != nil {
for i := range base.Players {
player := &base.Players[i]
dict[player.Id] = player
}
}
for i := range room.Players {
pbase, _ := dict[room.Players[i].Id]
players[i] = GetPlayerStatus(room.Players[i], pbase)
}
return v1.RoomIngressRoomStatus{
Id: room.Id,
Server: room.Server,
Upstream: room.Upstream,
Players: players,
}
}
func GetRoomStatusDict(rooms []v1.RoomIngressRoomStatus) map[RoomKey]*v1.RoomIngressRoomStatus {
dict := make(map[RoomKey]*v1.RoomIngressRoomStatus)
for i := range rooms {
room := &rooms[i]
dict[RoomKey{ServerId: room.Server, RoomId: room.Id}] = room
}
return dict
}
func GetStatus(spec *v1.RoomIngressSpec, base *v1.RoomIngressStatus) *v1.RoomIngressStatus {
rooms := make([]v1.RoomIngressRoomStatus, len(spec.Rooms))
var dict map[RoomKey]*v1.RoomIngressRoomStatus
if base != nil {
dict = GetRoomStatusDict(base.Rooms)
}
for i := range spec.Rooms {
room := &spec.Rooms[i]
key := RoomKey{ServerId: room.Server, RoomId: room.Id}
rbase := dict[key]
rooms[i] = GetRoomStatus(spec.Rooms[i], rbase)
}
return &v1.RoomIngressStatus{Rooms: rooms}
}
type DiffType string
const DiffNew = "New"
const DiffUpdated = "Updated"
const DiffDeleted = "Deleted"
const DiffUnchanged = "Unchanged"
const DiffRoomUpdated = "RoomUpdated"
type RoomKey struct {
ServerId string
RoomId string
}
type PlayerKey struct {
RoomKey
PlayerId string
}
type PlayerPos struct {
RoomIndex int
PlayerIndex int
}
type PlayerDetail struct {
Room *v1.RoomIngressRoomStatus
Player *v1.RoomIngressPlayerStatus
}
var NilPos = PlayerPos{RoomIndex: -1, PlayerIndex: -1}
type DiffResult struct {
Type DiffType
Key PlayerKey
Past PlayerPos
Current PlayerPos
}
func GetPlayerStatusDict(s *v1.RoomIngressStatus) map[PlayerKey]PlayerPos {
r := make(map[PlayerKey]PlayerPos)
for i := range s.Rooms {
room := &s.Rooms[i]
roomkey := RoomKey{ServerId: room.Server, RoomId: room.Id}
for j := range room.Players {
player := &room.Players[j]
key := PlayerKey{RoomKey: roomkey, PlayerId: player.Id}
r[key] = PlayerPos{RoomIndex: i, PlayerIndex: j}
}
}
return r
}
func UnionPlayerKeys(a map[PlayerKey]PlayerPos, b map[PlayerKey]PlayerPos) []PlayerKey {
dict := make(map[PlayerKey]struct{})
for k, _ := range a {
dict[k] = struct{}{}
}
for k, _ := range b {
dict[k] = struct{}{}
}
r := make([]PlayerKey, 0, len(dict))
for k, _ := range dict {
r = append(r, k)
}
return r
}
func GetPlayerStatusByPos(s *v1.RoomIngressStatus, pos PlayerPos) PlayerDetail {
room := &s.Rooms[pos.RoomIndex]
player := &room.Players[pos.PlayerIndex]
return PlayerDetail{Room: room, Player: player}
}
func GetPlayerStatusByKey(s *v1.RoomIngressStatus, key PlayerKey) *PlayerDetail {
for i := range s.Rooms {
room := &s.Rooms[i]
if room.Server == key.ServerId && room.Id == key.RoomId {
for j := range room.Players {
player := &room.Players[j]
if player.Id == key.PlayerId {
return &PlayerDetail{Room: room, Player: player}
}
}
}
}
return nil
}
func DiffRoomStatus(past *v1.RoomIngressStatus, curr *v1.RoomIngressStatus, opts ...DiffOption) []DiffResult {
o := getDiffOptions(opts...)
pastd := GetPlayerStatusDict(past)
currd := GetPlayerStatusDict(curr)
keys := UnionPlayerKeys(pastd, currd)
r := make([]DiffResult, 0, len(keys))
for _, key := range keys {
diff := DiffResult{
Key: key,
Current: NilPos,
Past: NilPos,
}
p, pok := pastd[key]
c, cok := currd[key]
if cok && !pok {
diff.Type = DiffNew
diff.Current = c
} else if cok && pok {
diff.Type = o.uh(past, p, curr, c)
diff.Current = c
diff.Past = p
} else if !cok && pok {
diff.Type = DiffDeleted
diff.Past = p
} else {
panic("impossible")
}
r = append(r, diff)
}
return r
}
func TokenUpdatedHandler() UpdatedHandler {
return func(past *v1.RoomIngressStatus, pp PlayerPos, curr *v1.RoomIngressStatus, cp PlayerPos) DiffType {
p := GetPlayerStatusByPos(past, pp)
c := GetPlayerStatusByPos(curr, cp)
if c.Player.Status == v1.PlayerStatusRetry {
return DiffUpdated
}
if p.Room.Upstream != c.Room.Upstream {
return DiffRoomUpdated
}
if p.Player.Token != c.Player.Token {
return DiffUpdated
}
return DiffUnchanged
}
}
func AlwaysUpdatedHandler() UpdatedHandler {
return func(past *v1.RoomIngressStatus, pp PlayerPos, curr *v1.RoomIngressStatus, cp PlayerPos) DiffType {
return DiffUpdated
}
}
type UpdatedHandler func(past *v1.RoomIngressStatus, pp PlayerPos, curr *v1.RoomIngressStatus, cp PlayerPos) DiffType
type diffOptions struct {
uh UpdatedHandler
}
type DiffOption interface {
apply(*diffOptions)
}
type funcDiffOption struct {
f func(*diffOptions)
}
func (it *funcDiffOption) apply(o *diffOptions) {
it.f(o)
}
func newFuncDiffOption(f func(*diffOptions)) DiffOption {
return &funcDiffOption{f: f}
}
func getDiffOptions(opts ...DiffOption) *diffOptions {
o := &diffOptions{
uh: TokenUpdatedHandler(),
}
for _, opt := range opts {
opt.apply(o)
}
return o
}
func DiffUpdater(uh UpdatedHandler) DiffOption {
return newFuncDiffOption(func(o *diffOptions) {
o.uh = uh
})
}