-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock.go
378 lines (325 loc) · 8.55 KB
/
mock.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
package main
import (
"crypto/md5"
"encoding/gob"
"encoding/hex"
"fmt"
"io"
"log/slog"
"math"
"math/rand"
"os"
"path/filepath"
"slices"
"strings"
"time"
)
type Mock struct {
SelectedGame GameId // store GameId
AcceleedGame GameId // store GameId
AddedGames []GameId
User UserInfo
Games map[GameId]GameInfo
// temp
qrImgPath string
statsTime time.Time
}
func (i *Mock) init() *Mock {
fh, err := os.Open(i.storePath())
if err != nil && !os.IsNotExist(err) {
panic(err)
}
fmt.Println("store path", i.storePath())
if os.IsNotExist(err) {
i.User = UserInfo{
Name: "test",
Password: "1234567",
Phone: "13448612544",
Icon: "./assets/images/logo-universal.png",
Expire: time.Now().AddDate(1, 0, 0).Unix(),
}
i.Games = map[GameId]GameInfo{
1: {
GameId: 1,
Name: "反恐精英",
IconPath: "./assets/images/images/csgo-icon.png",
BgimgPath: "./assets/images/images/csgo-bg.png",
AdimgPath: "./assets/images/images/csgo-ad.jpg",
GameServers: []string{"美服", "欧服", "亚服"},
LastActive: time.Now().Local().AddDate(0, 0, 0).Unix(),
TotalSeconds: 5645,
TotalBytes: 145234,
},
2: {
GameId: 2,
Name: "地下城与勇士阿巴阿巴阿巴阿巴",
IconPath: "./assets/images/images/dnf-icon.png",
BgimgPath: "./assets/images/images/dnf-bg.jpg",
AdimgPath: "./assets/images/images/dnf-ad.jpg",
GameServers: []string{"台服", "北美服", "日服"},
LastActive: time.Now().Local().AddDate(-1, 0, 0).Unix(),
TotalSeconds: 13435,
TotalBytes: 6733412,
},
3: {
GameId: 3,
Name: "绝地求生",
IconPath: "./assets/images/images/pubg-icon.png",
BgimgPath: "./assets/images/images/pubg-bg.jpg",
AdimgPath: "./assets/images/images/pubg-ad.png",
GameServers: []string{"国际服", "国服", "日服"},
LastActive: time.Now().Local().AddDate(-1, -2, 0).Unix(),
TotalSeconds: 3452,
TotalBytes: 457234,
},
}
i.AddedGames = []GameId{
2, 3,
}
i.SelectedGame = i.AddedGames[0]
} else {
if err := gob.NewDecoder(fh).Decode(i); err != nil {
panic(err)
}
i.AcceleedGame = 0
}
i.qrImgPath = "./assets/images/qr-alipay-img.png"
i.statsTime = time.Time{}
return i
}
func (i *Mock) storePath() (path string) {
if p, err := os.Executable(); err != nil {
panic(err)
} else {
fh, err := os.Open(p)
if err != nil {
panic(err)
}
defer fh.Close()
h := md5.New()
io.Copy(h, fh)
path = filepath.Join(os.TempDir(), fmt.Sprintf("app-%s.bin", hex.EncodeToString(h.Sum(nil))))
return path
}
}
func (i *Mock) sync() {
path := i.storePath()
w, err := os.Create(path)
if err != nil {
panic(err)
}
defer w.Close()
if err := gob.NewEncoder(w).Encode(i); err != nil {
panic(err)
}
}
func (i *Mock) gameIdValid(id GameId) bool {
_, has := i.Games[id]
return has
}
func (i *Mock) logined() bool { return i.User.Name != "" }
func (a *Mock) GetUser() (msg Message) {
if !a.logined() {
return NotLogin.Message(nil)
} else {
return OK.Message(a.User)
}
}
func (a *Mock) RegisterOrLogin(user, pwd string) (msg Message) {
if a.logined() {
return IsLogined.Message(nil)
} else {
a.User.Name = user
a.User.Password = pwd
a.User.Phone = "1378494xxxxx"
a.User.Expire = 0
slog.Info("log", slog.String("user", user), slog.String("pwd", pwd))
a.sync()
return OK.Message(nil)
}
}
func (a *Mock) Recharge(months int, callback func(Message)) (msg Message) {
if months <= 0 {
return InvalidMonths.Message("")
} else if !a.logined() {
return NotLogin.Message("")
}
go func() {
time.Sleep(time.Second * 3)
if time.Now().UnixNano()%2 == 0 {
t := time.Now().Local().AddDate(0, months, 0)
a.User.Expire = t.Unix()
a.sync()
callback(Message{Code: OK, Msg: fmt.Sprintf("支付成功, 有效期至 %s", t.Format("2006-01-02T15:04:05"))})
} else {
callback(Message{Code: Unknown, Msg: "支付失败, xxxx"})
}
}()
return OK.Message(a.qrImgPath)
}
func (a *Mock) ListGames() (msg Message) {
if !a.logined() {
return NotLogin.Message(nil)
}
var gs = GameInfos{SelectGame: a.SelectedGame, AcceleGame: a.AcceleedGame}
for _, id := range a.AddedGames {
gs.Games = append(gs.Games, a.Games[id])
}
return OK.Message(gs)
}
func (a *Mock) GetGame(id GameId) Message {
if !a.logined() {
return NotLogin.Message(nil)
} else if !a.gameIdValid(id) {
return RequireGameId.Message(nil)
}
return OK.Message(a.Games[id])
}
func (a *Mock) SelectGame(id GameId) Message {
if !a.logined() {
return NotLogin.Message(nil)
} else if !a.gameIdValid(id) {
return RequireGameId.Message(nil)
} else if !slices.Contains(a.AddedGames, id) {
return Notfound.Message(nil)
}
a.SelectedGame = id
a.sync()
return a.ListGames()
}
func (a *Mock) GetSelectedGame() Message {
if !a.logined() {
return NotLogin.Message(nil)
}
if a.SelectedGame == 0 {
return NotSelectGame.Message(nil)
} else {
return OK.Message(a.Games[a.SelectedGame])
}
}
func (a *Mock) SearchGame(keyword string) (msg Message) {
if !a.logined() {
return NotLogin.Message(nil)
}
var list []GameInfo
for _, e := range a.Games {
if !slices.Contains(a.AddedGames, e.GameId) &&
(keyword == "" || strings.ContainsAny(e.Name, keyword)) {
list = append(list, e)
}
}
return OK.Message(list)
}
func (a *Mock) AddGame(gameId GameId) Message {
if !a.logined() {
return NotLogin.Message(nil)
} else if !a.gameIdValid(gameId) {
return RequireGameId.Message(nil)
} else if slices.Contains(a.AddedGames, gameId) {
return GameExist.Message(nil)
}
a.AddedGames = append(a.AddedGames, gameId)
a.SelectedGame = gameId
a.sync()
return a.ListGames()
}
func (a *Mock) DelGame(gameId GameId) Message {
if !a.logined() {
return NotLogin.Message(nil)
} else if !a.gameIdValid(gameId) {
return RequireGameId.Message(nil)
} else if !slices.Contains(a.AddedGames, gameId) {
return GameNotExist.Message(nil)
}
a.AddedGames = slices.DeleteFunc(a.AddedGames, func(id GameId) bool { return id == gameId })
if a.SelectedGame == gameId {
if len(a.AddedGames) > 0 {
a.SelectedGame = a.AddedGames[0]
} else {
a.SelectedGame = 0
}
}
a.sync()
return a.ListGames()
}
func (a *Mock) SetGameServer(gameServer string) Message {
if !a.logined() {
return NotLogin.Message(nil)
} else if !a.gameIdValid(a.SelectedGame) {
return RequireGameId.Message(nil)
}
g := a.Games[a.SelectedGame]
i := slices.Index(g.GameServers, gameServer)
if i == -1 {
panic(fmt.Sprintf("unknown game %s server %s", g.Name, gameServer))
} else if i != 0 {
g.GameServers[i], g.GameServers[0] = g.GameServers[0], g.GameServers[i]
a.Games[a.SelectedGame] = g
a.sync()
}
return OK.Message(nil)
}
func (a *Mock) SetRouteMode(fixRoute bool) Message {
if !a.logined() {
return NotLogin.Message(nil)
} else if !a.gameIdValid(a.SelectedGame) {
return GameExist.Message(nil)
}
g := a.Games[a.SelectedGame]
if g.FixRoute != fixRoute {
g.FixRoute = fixRoute
a.Games[a.SelectedGame] = g
a.sync()
}
return OK.Message(g)
}
func (a *Mock) StartAccele(gameId GameId) Message {
if !a.logined() {
return NotLogin.Message(nil)
} else if a.User.Expire < time.Now().Unix() {
return VIPExpired.Message(nil)
} else if !a.gameIdValid(gameId) {
return RequireGameId.Message(nil)
} else if a.AcceleedGame != 0 {
return Message{Code: Accelerating, Msg: fmt.Sprintf("%s 正在加速", a.Games[a.AcceleedGame].Name)}
}
a.AcceleedGame = gameId
info := a.Games[gameId]
slog.Info("开始加速", slog.String("name", info.Name), slog.String("server", info.GameServers[0]), slog.Bool("fix-route", info.FixRoute))
a.sync()
return a.ListGames()
}
func (a *Mock) StopAccele() Message {
if !a.logined() {
return NotLogin.Message(nil)
} else if a.AcceleedGame == 0 {
return NotAccelerated.Message(nil)
}
a.AcceleedGame = 0
a.sync()
return a.ListGames()
}
var RandNew *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
func (a *Mock) Stats() (s Stats) {
time.Sleep(time.Until(a.statsTime.Add(time.Second * 3)))
a.statsTime = time.Now()
return randStats()
}
func randStats() Stats {
var s = Stats{
Stamp: time.Now().UnixMilli(),
Seconds: 8234,
Bytes: 345254,
GatewayLoc: "北京",
ForwardLoc: "莫斯科",
ServerLoc: "圣彼得堡",
}
s.RttGateway = 30 + time.Duration(rand.Intn(30))
s.RttForward = s.RttGateway + 90
s.LossClientUplink = f(1.5 + rand.Float64()*3)
s.LossClientDownlink = f(rand.Float64())
s.LossGatewayUplink = f(rand.Float64())
s.LossGatewayDownlink = f(rand.Float64())
return s
}
func f(v float64) float64 { return math.Round(v*100) / 100 }