forked from fanaticscripter/EggLedger
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata.go
82 lines (75 loc) · 2.26 KB
/
data.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
package main
import (
"context"
"fmt"
"path/filepath"
"time"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/fanaticscripter/EggLedger/api"
"github.com/fanaticscripter/EggLedger/db"
"github.com/fanaticscripter/EggLedger/ei"
)
var _dbPath string
func dataInit() {
_dbPath = filepath.Join(_internalDir, "data.db")
if err := db.InitDB(_dbPath); err != nil {
log.Fatal(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")
}
timestamp := fc.GetBackup().GetSettings().GetLastBackupTime()
if timestamp != 0 {
if err := db.InsertBackup(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) (*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)
}
resp, err := db.RetrieveCompleteMission(playerId, missionId)
if err != nil {
return nil, wrap(err)
}
if resp != nil {
return resp, nil
}
payload, err := api.RequestCompleteMissionRawPayloadWithContext(ctx, playerId, missionId)
if err != nil {
return nil, wrap(err)
}
resp, err = api.DecodeCompleteMissionPayload(payload)
if err != nil {
return nil, wrap(err)
}
if !resp.GetSuccess() {
return nil, wrap(errors.New("success is false"))
}
if len(resp.GetArtifacts()) == 0 {
return nil, wrap(errors.New("no artifact found in server response"))
}
err = db.InsertCompleteMission(playerId, missionId, startTimestamp, payload)
return resp, err
}