forked from synthparadox/EggLedger
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdata.go
More file actions
157 lines (149 loc) · 4.71 KB
/
data.go
File metadata and controls
157 lines (149 loc) · 4.71 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
package main
import (
"context"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/DavidArthurCole/EggLedger/api"
"github.com/DavidArthurCole/EggLedger/db"
"github.com/DavidArthurCole/EggLedger/ei"
)
var _dbPath string
func dataInit() {
_dbPath = filepath.Join(_internalDir, "data.db")
if err := db.InitDB(_dbPath); err != nil {
log.Fatal("dataInit() err: ", err)
}
}
func fetchFirstContactWithContext(ctx context.Context, playerId string) (*ei.EggIncFirstContactResponse, error) {
action := fmt.Sprintf("fetching backup for player %s", playerId)
wrap := func(err error) error {
return errors.Wrap(err, "error "+action)
}
payload, err := api.RequestFirstContactRawPayloadWithContext(ctx, playerId)
if err != nil {
return nil, wrap(err)
}
fc, err := api.DecodeFirstContactPayload(payload)
if err != nil {
return nil, wrap(err)
}
if err := fc.Validate(); err != nil {
return nil, errors.Wrap(wrap(err), "please double check your ID")
}
_storage.SetLastKnownGoodApiVersion(api.AppVersion)
timestamp := fc.GetBackup().GetSettings().GetLastBackupTime()
if timestamp != 0 {
if err := db.InsertBackup(ctx, playerId, timestamp, payload, 12*time.Hour); err != nil {
// Treat as non-fatal error for now.
log.Error(err)
}
} else {
log.Warnf("%s: .backup.settings.last_backup_time is 0", playerId)
}
return fc, nil
}
func fetchCompleteMissionWithContext(ctx context.Context, playerId string, missionId string, startTimestamp float64, tracker func(string, string)) (*ei.CompleteMissionResponse, error) {
action := fmt.Sprintf("fetching mission %s for player %s", missionId, playerId)
wrap := func(err error) error {
return errors.Wrap(err, "error "+action)
}
track := func(name, status string) {
if tracker != nil {
tracker(name, status)
}
}
track("Cache", "active")
resp, err := db.RetrieveCompleteMission(ctx, playerId, missionId)
if err != nil {
track("Cache", "failed")
return nil, wrap(err)
}
if resp != nil {
track("Cache", "done")
return resp, nil
}
track("Cache", "skipped")
track("Fetch", "active")
payload, err := api.RequestCompleteMissionRawPayloadWithContext(ctx, playerId, missionId)
if err != nil {
track("Fetch", "failed")
return nil, wrap(err)
}
track("Fetch", "done")
track("Decode", "active")
resp, err = api.DecodeCompleteMissionPayload(payload)
if err != nil {
track("Decode", "failed")
return nil, wrap(err)
}
if !resp.GetSuccess() {
track("Decode", "failed")
return nil, wrap(errors.New("success is false"))
}
if len(resp.GetArtifacts()) == 0 {
track("Decode", "failed")
return nil, wrap(errors.New("no artifact found in server response"))
}
track("Decode", "done")
track("Store", "active")
missionType := int32(resp.GetInfo().GetType())
_nominalShipCapacitiesOnce.Do(initNominalShipCapacities)
cols, _ := computeMissionFilterCols(startTimestamp, resp)
err = db.InsertCompleteMission(ctx, playerId, missionId, startTimestamp, payload, missionType, cols)
if err != nil {
track("Store", "failed")
return resp, err
}
dropRows := buildArtifactDropRows(resp)
if insertErr := db.InsertArtifactDrops(ctx, playerId, missionId, dropRows); insertErr != nil {
log.Errorf("failed to insert artifact drops for mission %s: %v", missionId, insertErr)
}
track("Store", "done")
return resp, nil
}
// buildArtifactDropRows converts artifacts from a CompleteMissionResponse into
// ArtifactDropRow values ready for DB insertion. DropIndex matches the 0-based
// position in the repeated field, aligning with the migration 7 UNIQUE constraint.
func buildArtifactDropRows(resp *ei.CompleteMissionResponse) []db.ArtifactDropRow {
artifacts := resp.GetArtifacts()
drops := make([]db.ArtifactDropRow, 0, len(artifacts))
for i, drop := range artifacts {
spec := drop.GetSpec()
name := ei.ArtifactSpec_Name_name[int32(spec.GetName())]
var specType string
switch {
case strings.Contains(name, "_FRAGMENT"):
specType = "StoneFragment"
case strings.Contains(name, "_STONE"):
specType = "Stone"
case strings.Contains(name, "GOLD_METEORITE"),
strings.Contains(name, "SOLAR_TITANIUM"),
strings.Contains(name, "TAU_CETI_GEODE"):
specType = "Ingredient"
default:
specType = "Artifact"
}
var quality float64
for _, art := range _eiAfxConfigArtis {
if art.GetSpec().GetName() == spec.GetName() &&
art.GetSpec().GetLevel() == spec.GetLevel() &&
art.GetSpec().GetRarity() == spec.GetRarity() {
quality = art.GetBaseQuality()
break
}
}
drops = append(drops, db.ArtifactDropRow{
DropIndex: int32(i),
ArtifactId: int32(spec.GetName()),
SpecType: specType,
Level: int32(spec.GetLevel()),
Rarity: int32(spec.GetRarity()),
Quality: quality,
})
}
return drops
}