/
matches.go
104 lines (92 loc) · 2.16 KB
/
matches.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
package tetrio
import (
"context"
"encoding/json"
"errors"
"net/url"
"time"
"github.com/gkaply532/tetrio/v2/types"
)
var ErrNonMultiRecord = errors.New("tetrio: not a multiplayer record")
var ErrAmbiguousRecord = errors.New("tetrio: record has no winner or loser")
type LeagueRecord struct {
ReplayID string
TS time.Time
IsForfeit bool
Winner LeaguePlayer
Loser LeaguePlayer
}
type LeaguePlayer struct {
User types.PartialUser
Wins int
Inputs int
PiecesPlaced int
Handling types.Handling
Stats types.VersusStats
RoundStats []types.VersusStats
}
func (s Session) GetMatches(ctx context.Context, userID string) ([]LeagueRecord, error) {
return do[[]LeagueRecord](ctx, s, "/streams/league_userrecent_"+url.PathEscape(userID))
}
func (g *LeagueRecord) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
return nil
}
var rec types.GameRecord
err := json.Unmarshal(data, &rec)
if err != nil {
return err
}
if !rec.IsMulti {
return ErrNonMultiRecord
}
var cs [2]types.LeagueEndCtx
err = json.Unmarshal(rec.EndContext, &cs)
if err != nil {
return err
}
if cs[0].Success == cs[1].Success {
return ErrAmbiguousRecord
}
g.ReplayID = rec.ReplayID
g.TS = rec.TS
g.IsForfeit = cs[0].Active != cs[1].Active
for i := 0; i < 2; i++ {
apms := cs[i].Points.SecondaryAvgTracking
ppss := cs[i].Points.TertiaryAvgTracking
vss := cs[i].Points.ExtraAvgTracking.AggregateStatsVSScore
roundCount := max(len(apms), len(ppss), len(vss))
rs := make([]types.VersusStats, roundCount)
for j, apm := range apms {
rs[j].APM = apm
}
for j, pps := range ppss {
rs[j].PPS = pps
}
for j, vs := range vss {
rs[j].VS = vs
}
p := LeaguePlayer{
User: types.PartialUser{
ID: cs[i].ID,
Username: cs[i].Username,
},
Handling: cs[i].Handling,
Inputs: cs[i].Inputs,
PiecesPlaced: cs[i].PiecesPlaced,
Wins: cs[i].Wins,
Stats: types.VersusStats{
APM: cs[i].Points.Secondary,
PPS: cs[i].Points.Tertiary,
VS: cs[i].Points.Extra.VS,
},
RoundStats: rs,
}
if cs[i].Success {
g.Winner = p
} else {
g.Loser = p
}
}
return nil
}