This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgamesubmit.go
323 lines (309 loc) · 10.9 KB
/
gamesubmit.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
package main
import (
gamereport "autohoster-backend/gameReport"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"math/big"
"os"
"path"
"strings"
"github.com/DataDog/zstd"
"github.com/jackc/pgx/v4"
)
func submitReport(inst *instance, reportBytes []byte) {
if inst.GameId <= 0 {
inst.GameId = submitBegin(inst, reportBytes)
err := recoverSave(inst)
if err != nil {
inst.logger.Printf("Failed to save instance recovery json: %s", err.Error())
discordPostError("Failed to save instance recovery json: %s (instance %d)", err.Error(), inst.Id)
}
} else {
submitFrame(inst, reportBytes)
}
}
func submitFinalReport(inst *instance, reportBytes []byte) {
if inst.GameId <= 0 {
inst.logger.Printf("Trying to submit final report without valid game ID!")
} else {
submitEnd(inst, reportBytes)
}
}
func submitBegin(inst *instance, reportBytes []byte) int {
report := gamereport.GameReport{}
err := json.Unmarshal(reportBytes, &report)
if err != nil {
inst.logger.Printf("Failed to unmarshal game report: %s report was %q", err.Error(), string(reportBytes))
discordPostError("Failed to unmarshal game report: %s report was %q (instance %d)", err.Error(), string(reportBytes), inst.Id)
return -1
}
var gid int
ctx := context.Background()
err = dbpool.BeginFunc(ctx, func(tx pgx.Tx) error {
err := tx.QueryRow(ctx, `insert into games (version, instance,
setting_scavs, setting_alliance, setting_power, setting_base,
map_name, map_hash, mods, display_category) values ($1, $2,
$3, $4, $5, $6,
$7, $8, $9, $10) returning id`, report.Game.Version, inst.Id,
report.Game.Scavengers, report.Game.AlliancesType, report.Game.PowerType, report.Game.BaseType,
inst.Settings.MapName, inst.Settings.MapHash, inst.Settings.Mods, inst.Settings.DisplayCategory).Scan(&gid)
if err != nil {
return err
}
for _, v := range report.PlayerData {
if v.PublicKey == "" {
continue
}
PublicKeyBytes, err := base64.StdEncoding.DecodeString(v.PublicKey)
if err != nil {
return err
}
pid := -1
err = tx.QueryRow(ctx, `insert into identities (name, pkey, hash) values
($1, $2, encode(sha256($2), 'hex'))
on conflict (hash) do update set name = $1, pkey = $2 returning id;`, v.Name, PublicKeyBytes).Scan(&pid)
if err != nil {
return err
}
_, err = tx.Exec(ctx, `insert into players (game, identity, position, team, color, props) values
($1, $2, $3, $4, $5, $6)`, gid, pid, v.Position, v.Team, v.Color, v.GameReportPlayerStatistics)
if err != nil {
return err
}
}
for _, v := range inst.Settings.RatingCategories {
_, err := tx.Exec(ctx, `insert into games_rating_categories (game, category) values ($1, $2)`, gid, v)
if err != nil {
return err
}
}
return nil
})
if err != nil {
inst.logger.Printf("Failed to begin game: %s (gid %d)", err.Error(), inst.GameId)
discordPostError("Failed to begin game: %s (gid %d) (instance %d)", err.Error(), inst.GameId, inst.Id)
}
return gid
}
var (
frameErrorSuspends = map[int64]string{}
)
func submitFrame(inst *instance, reportBytes []byte) {
report := gamereport.GameReport{}
err := json.Unmarshal(reportBytes, &report)
if err != nil {
inst.logger.Printf("Failed to unmarshal game report: %s (gid %d) report was %q", err.Error(), inst.GameId, string(reportBytes))
discordPostError("Failed to unmarshal game report: %s report was %q (instance %d)", err.Error(), string(reportBytes), inst.Id)
return
}
frame := gamereport.GameReportGraphFrame{
GameTime: report.GameTime,
Kills: make([]int, len(report.PlayerData)),
Power: make([]int, len(report.PlayerData)),
Score: make([]int, len(report.PlayerData)),
Droids: make([]int, len(report.PlayerData)),
DroidsBuilt: make([]int, len(report.PlayerData)),
DroidsLost: make([]int, len(report.PlayerData)),
Hp: make([]int, len(report.PlayerData)),
Structs: make([]int, len(report.PlayerData)),
StructuresBuilt: make([]int, len(report.PlayerData)),
StructuresLost: make([]int, len(report.PlayerData)),
StructureKills: make([]int, len(report.PlayerData)),
SummExp: make([]int, len(report.PlayerData)),
OilRigs: make([]int, len(report.PlayerData)),
ResearchComplete: make([]int, len(report.PlayerData)),
RecentPowerLost: make([]int, len(report.PlayerData)),
RecentPowerWon: make([]int, len(report.PlayerData)),
RecentResearchPerformance: make([]int, len(report.PlayerData)),
RecentResearchPotential: make([]int, len(report.PlayerData)),
RecentDroidPowerLost: make([]int, len(report.PlayerData)),
RecentStructurePowerLost: make([]int, len(report.PlayerData)),
}
for i, v := range report.PlayerData {
if v.PublicKey == "" {
continue
}
frame.Kills[i] = v.Kills
frame.Power[i] = v.Power
frame.Score[i] = v.Score
frame.Droids[i] = v.Droids
frame.DroidsBuilt[i] = v.DroidsBuilt
frame.DroidsLost[i] = v.DroidsLost
frame.Hp[i] = v.Hp
frame.Structs[i] = v.Structs
frame.StructuresBuilt[i] = v.StructuresBuilt
frame.StructuresLost[i] = v.StructuresLost
frame.StructureKills[i] = v.StructureKills
frame.SummExp[i] = v.SummExp
frame.OilRigs[i] = v.OilRigs
frame.ResearchComplete[i] = v.ResearchComplete
frame.RecentPowerLost[i] = v.RecentPowerLost
frame.RecentPowerWon[i] = v.RecentPowerWon
frame.RecentResearchPerformance[i] = v.RecentResearchPerformance
frame.RecentResearchPotential[i] = v.RecentResearchPotential
frame.RecentDroidPowerLost[i] = v.RecentDroidPowerLost
frame.RecentStructurePowerLost[i] = v.RecentStructurePowerLost
}
tag, err := dbpool.Exec(context.Background(), `update games set graphs = coalesce(graphs, '[]'::json)::jsonb || $1::jsonb where id = $2`, frame, inst.GameId)
if err != nil {
inst.logger.Printf("Failed to add game frame: %s (gid %d)", err.Error(), inst.GameId)
discordPostError("Failed to add game frame: %s (gid %d) (instance %d)", err.Error(), inst.GameId, inst.Id)
}
if !tag.Update() || tag.RowsAffected() != 1 {
inst.logger.Printf("SUS tag while adding game: %s (gid %d)", tag, inst.GameId)
errMsgToSend := fmt.Sprintf("SUS tag while adding game: %s (gid %d) (instance %d)", tag, inst.GameId, inst.Id)
errMsg, ok := frameErrorSuspends[inst.Id]
if !ok {
discordPostError(errMsgToSend)
frameErrorSuspends[inst.Id] = errMsgToSend
} else {
if errMsg != errMsgToSend {
discordPostError(errMsgToSend)
frameErrorSuspends[inst.Id] = errMsgToSend
}
}
}
}
func submitEnd(inst *instance, reportBytes []byte) {
submitFrame(inst, reportBytes)
report := gamereport.GameReportExtended{}
err := json.Unmarshal(reportBytes, &report)
if err != nil {
inst.logger.Printf("Failed to unmarshal game report: %s (gid %d) report was %q", err.Error(), inst.GameId, string(reportBytes))
return
}
err = dbpool.BeginFunc(context.Background(), func(tx pgx.Tx) error {
for _, v := range report.PlayerData {
if v.PublicKey == "" {
continue
}
_, err := dbpool.Exec(context.Background(), `update players set usertype = $1, props = $2 where game = $3 and position = $4`,
v.Usertype, v.GameReportPlayerStatistics, inst.GameId, v.Position)
if err != nil {
inst.logger.Printf("Failed to finalize player at position %d: %s (gid %d)", v.Position, err.Error(), inst.GameId)
return err
}
}
_, err = dbpool.Exec(context.Background(), `update games set research_log = $1, time_ended = TO_TIMESTAMP($2::double precision / 1000), debug_triggered = $3, game_time = $4 where id = $5`,
report.ResearchComplete, report.EndDate, inst.DebugTriggered, report.GameTime, inst.GameId)
if err != nil {
inst.logger.Printf("Failed to finalize game: %s (gid %d)", err.Error(), inst.GameId)
}
return err
})
if err != nil {
inst.logger.Printf("Failed to finalize: %s (gid %d)", err.Error(), inst.GameId)
discordPostError("Failed to finalize: %s (gid %d) (instance %d)", err.Error(), inst.GameId, inst.Id)
}
}
func sendReplayToStorage(inst *instance) {
replayPath, err := findReplay(inst)
if err != nil {
inst.logger.Printf("Failed to find replay: %s", err.Error())
discordPostError("Failed to find replay: %s (instance %d)", err.Error(), inst.Id)
return
}
err = copyReplayToStorage(inst, replayPath)
if err != nil {
inst.logger.Printf("Failed to copy replay: %s", err.Error())
discordPostError("Failed to copy replay: %s (instance %d)", err.Error(), inst.Id)
}
err = saveReplayToDatabase(inst, replayPath)
if err != nil {
inst.logger.Printf("Failed to save replay to database: %s", err.Error())
discordPostError("Failed to save replay to database: %s (instance %d)", err.Error(), inst.Id)
}
}
func saveReplayToDatabase(inst *instance, p string) error {
rplBytes, err := os.ReadFile(p)
if err != nil {
return err
}
rplCompressed, err := zstd.CompressLevel(nil, rplBytes, zstd.BestCompression)
if err != nil {
return err
}
tag, err := dbpool.Exec(context.Background(), `update games set replay = $1 where id = $2`, rplCompressed, inst.GameId)
if err != nil {
return err
}
if !tag.Update() || tag.RowsAffected() != 1 {
return fmt.Errorf("sus tag: %s", tag.String())
}
return nil
}
func copyReplayToStorage(inst *instance, p string) error {
b, err := os.ReadFile(p)
if err != nil {
return err
}
storageDir := getStorageReplayDir(inst.GameId)
err = os.MkdirAll(storageDir, fs.FileMode(cfg.GetDInt(755, "dirPerms")))
if err != nil {
return err
}
storageFilePath := path.Join(storageDir, getStorageReplayFilename(inst.GameId)+".wzrp.zst")
bc, err := zstd.Compress(nil, b)
if err != nil {
return err
}
perm := fs.FileMode(cfg.GetDInt(644, "filePerms"))
return os.WriteFile(storageFilePath, bc, perm)
}
func findReplay(inst *instance) (string, error) {
replaydir := path.Join(inst.ConfDir, "replay", "multiplay")
files, err := os.ReadDir(replaydir)
if err != nil {
return "", err
}
for _, f := range files {
if strings.HasSuffix(f.Name(), ".wzrp") {
h, err := os.Open(replaydir + "/" + f.Name())
if err != nil {
return "", err
}
var header [4]byte
n, err := io.ReadFull(h, header[:])
if err != nil {
return "", err
}
h.Close()
if n == 4 && string(header[:]) == "WZrp" {
return replaydir + "/" + f.Name(), nil
}
}
}
return "", errors.New("replay not found")
}
func getStorageReplayDir(gid int) string {
ret := cfg.GetDSString("./replayStorage/", "replayStorage")
if ret == "" {
ret = "./replayStorage/"
}
if gid <= 0 {
return ret
}
num := ""
for _, v := range big.NewInt(int64(gid)).Text(32) {
num = string(v) + num
}
for _, n := range num[0 : len(num)-1] {
ret = path.Join(ret, string(n))
}
return ret
}
func getStorageReplayFilename(gid int) string {
if gid < 0 {
gid = -gid
}
num := ""
for _, v := range big.NewInt(int64(gid)).Text(32) {
num = string(v) + num
}
return string(num[len(num)-1:])
}