-
Notifications
You must be signed in to change notification settings - Fork 19
/
games.go
363 lines (316 loc) · 13.4 KB
/
games.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package sqlstore
import (
"context"
"encoding/hex"
"fmt"
"sort"
"strings"
"time"
"code.vegaprotocol.io/vega/datanode/entities"
"code.vegaprotocol.io/vega/libs/num"
v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
"code.vegaprotocol.io/vega/protos/vega"
"github.com/georgysavva/scany/pgxscan"
"github.com/shopspring/decimal"
)
type Games struct {
*ConnectionSource
}
var gameOrdering = TableOrdering{
ColumnOrdering{Name: "epoch_id", Sorting: ASC},
ColumnOrdering{Name: "game_id", Sorting: DESC},
}
func NewGames(connectionSource *ConnectionSource) *Games {
return &Games{
ConnectionSource: connectionSource,
}
}
type GameReward struct {
PartyID entities.PartyID
AssetID entities.AssetID
MarketID entities.MarketID
EpochID int64
Amount decimal.Decimal
QuantumAmount decimal.Decimal
PercentOfTotal float64
RewardType string
Timestamp time.Time
TxHash entities.TxHash
VegaTime time.Time
SeqNum uint64
LockedUntilEpochID int64
GameID []byte
DispatchStrategy vega.DispatchStrategy
TeamID entities.TeamID
MemberRank *int64
TeamRank *int64
TotalRewards num.Decimal
TotalRewardsQuantum num.Decimal
TeamTotalRewards *num.Decimal
TeamTotalRewardsQuantum *num.Decimal
EntityScope string
}
func (g *Games) ListGames(ctx context.Context, gameID *string, entityScope *vega.EntityScope, epochFrom, epochTo *uint64,
teamID *entities.TeamID, partyID *entities.PartyID, pagination entities.CursorPagination,
) ([]entities.Game, entities.PageInfo, error) {
var pageInfo entities.PageInfo
var gameRewards []GameReward
// because we have to build the games data from the rewards data, paging backwards adds more complexity
// therefore we aren't going to support it for now as the games data API is high priority
if pagination.HasBackward() {
return nil, pageInfo, fmt.Errorf("backward pagination is not currently supported")
}
query, args, err := g.buildGamesQuery(gameID, entityScope, epochFrom, epochTo, teamID, partyID, pagination)
if err != nil {
return nil, pageInfo, err
}
if err := pgxscan.Select(ctx, g.Connection, &gameRewards, query, args...); err != nil {
return nil, pageInfo, err
}
games, err := parseGameRewards(gameRewards)
if err != nil {
return nil, pageInfo, err
}
games, pageInfo = entities.PageEntities[*v2.GameEdge](games, pagination)
return games, pageInfo, nil
}
func (g *Games) parseEpochs(from, to *uint64) (uint64, uint64) {
var eFrom, eTo uint64
if from != nil || to != nil {
// no more than 30 epochs for performance sake
if from != nil && to == nil {
eFrom, eTo = *from, *from+30-1
} else if from == nil && to != nil {
eTo, eFrom = *to, *to-30+1
} else if from != nil && to != nil {
eFrom, eTo = *from, *to
if eTo-eFrom > 30 {
eFrom = eTo - 30 + 1
}
}
}
return eFrom, eTo
}
func (g *Games) buildPagingQuery(selectTable string, gameID *string, entityScope *vega.EntityScope, epochFrom, epochTo uint64,
teamID *entities.TeamID, partyID *entities.PartyID, pagination entities.CursorPagination,
) (string, []interface{}, error) {
selectQuery := fmt.Sprintf(`select distinct game_id, epoch_id from %s`, selectTable)
var where []string
var args []interface{}
if epochFrom > 0 && epochTo > 0 {
where = append(where, fmt.Sprintf("epoch_id >= %s AND epoch_id <= %s",
nextBindVar(&args, epochFrom), nextBindVar(&args, epochTo)))
}
if gameID != nil {
where = append(where, fmt.Sprintf("game_id = %s", nextBindVar(&args, entities.GameID(*gameID))))
}
if entityScope != nil {
where = append(where, fmt.Sprintf("entity_scope = %s", nextBindVar(&args, entityScope.String())))
// only add the teams filter if the entity scope is teams or not specified
if *entityScope == vega.EntityScope_ENTITY_SCOPE_TEAMS && teamID != nil {
where = append(where, fmt.Sprintf("team_id = %s", nextBindVar(&args, teamID)))
}
} else if entityScope == nil && teamID != nil {
where = append(where, fmt.Sprintf("team_id = %s", nextBindVar(&args, teamID)))
}
// We should be able to filter by party regardless of the entity scope
if partyID != nil {
where = append(where, fmt.Sprintf("party_id = %s", nextBindVar(&args, partyID)))
}
whereClause := ""
if len(where) > 0 {
whereClause = "WHERE " + strings.Join(where, " AND ")
}
query := fmt.Sprintf("%s %s", selectQuery, whereClause)
return PaginateQuery[entities.GameCursor](query, args, gameOrdering, pagination)
}
func (g *Games) buildGamesQuery(gameID *string, entityScope *vega.EntityScope, epochFrom, epochTo *uint64,
teamID *entities.TeamID, partyID *entities.PartyID, pagination entities.CursorPagination,
) (string, []interface{}, error) {
// Games are intrinsically created by a recurring transfer with a game ID
// Rewards are paid out to participants of a game and the game ID is recorded on the reward
// We need to query the rewards and build the games data from that.
// If we page on the reward data, we may not have a complete data set for the game. Therefore we need to only page/filter on the distinct game IDs per epoch
// and then use that data to query the corresponding rewards data we need for the API to return.
eFrom, eTo := g.parseEpochs(epochFrom, epochTo)
// The select table query determines if we should just be querying the games data for the most current epoch or all epochs
selectTable := g.getSelectTable(eFrom, eTo)
// The page query determines which games/epochs should be included in the result set for pagination
// For example, if we have 100 games, and we want to page on the first 10, we would need to know which games to include rewards for
// The number of rewards we may get back in order to build the data will be much more than just 10 records.
pageQuery, args, err := g.buildPagingQuery(selectTable, gameID, entityScope, eFrom, eTo, teamID, partyID, pagination)
if err != nil {
return "", nil, err
}
query := fmt.Sprintf("select s.* from %s s join (%s) as p on s.game_id = p.game_id and s.epoch_id = p.epoch_id order by s.epoch_id desc, s.game_id", selectTable, pageQuery)
return query, args, nil
}
func (g *Games) getSelectTable(from, to uint64) string {
if from == 0 && to == 0 {
return `game_stats_current`
}
return `game_stats`
}
func parseGameRewards(rewards []GameReward) ([]entities.Game, error) {
if len(rewards) <= 0 {
return []entities.Game{}, nil
}
type gameKey struct {
EpochID uint64
GameID entities.GameID
}
games := make(map[gameKey]entities.Game)
lastGameID := entities.GameID("")
lastEpochID := int64(0)
participants := uint64(0)
gameIndividuals := make(map[gameKey][]entities.GameEntity)
teamMembers := make(map[gameKey]map[entities.TeamID][]*entities.IndividualGameEntity)
teamRanks := make(map[gameKey]map[entities.TeamID]uint64)
var game entities.Game
var gk gameKey
// first go through all the rewards and build the participation stats
// if the reward is for a team participant, i.e. there is a team ID then the participant will be added to the teamMembers map
// otherwise we add it to the gameIndividuals map
for i := range rewards {
gID := hex.EncodeToString(rewards[i].GameID)
currentGameID := entities.GameID(gID)
currentEpochID := rewards[i].EpochID
gk = gameKey{
EpochID: uint64(currentEpochID),
GameID: currentGameID,
}
if currentGameID != lastGameID || currentEpochID != lastEpochID {
// add the game to the map of games
lastKey := gameKey{
EpochID: uint64(lastEpochID),
GameID: lastGameID,
}
if lastGameID != "" && lastEpochID != 0 {
game.Participants = participants
games[lastKey] = game
}
game = entities.Game{
ID: currentGameID,
Epoch: uint64(currentEpochID),
Participants: participants,
Entities: []entities.GameEntity{},
RewardAssetID: rewards[i].AssetID,
}
lastGameID = currentGameID
lastEpochID = currentEpochID
participants = 0
games[gk] = game
}
rewardEarned, _ := num.UintFromDecimal(rewards[i].Amount)
totalRewardsEarned, _ := num.UintFromDecimal(rewards[i].TotalRewards)
rewardEarnedQuantum, _ := num.UintFromDecimal(rewards[i].QuantumAmount)
totalRewardsEarnedQuantum, _ := num.UintFromDecimal(rewards[i].TotalRewardsQuantum)
var rank uint64
if rewards[i].MemberRank != nil {
rank = uint64(*rewards[i].MemberRank)
}
individual := entities.IndividualGameEntity{
Individual: rewards[i].PartyID.String(),
Rank: rank,
Volume: num.DecimalZero(),
RewardMetric: rewards[i].DispatchStrategy.Metric,
RewardEarned: rewardEarned,
TotalRewardsEarned: totalRewardsEarned,
RewardEarnedQuantum: rewardEarnedQuantum,
TotalRewardsEarnedQuantum: totalRewardsEarnedQuantum,
}
if rewards[i].TeamID != "" {
currentTeamID := rewards[i].TeamID
if teamMembers[gk] == nil {
teamMembers[gk] = make(map[entities.TeamID][]*entities.IndividualGameEntity)
}
teamMembers[gk][currentTeamID] = append(teamMembers[gk][currentTeamID], &individual)
if rewards[i].TeamRank == nil {
return nil, fmt.Errorf("team rank is nil for team %s", currentTeamID)
}
if teamRanks[gk] == nil {
teamRanks[gk] = make(map[entities.TeamID]uint64)
}
teamRanks[gk][currentTeamID] = uint64(*rewards[i].TeamRank)
} else {
gameIndividuals[gk] = append(gameIndividuals[gk], &individual)
}
participants++
}
game.Participants = participants
games[gk] = game
results := make([]entities.Game, 0, len(games))
// now that we have the participation involvement, we can use that to build the game entities for each game.
for key, game := range games {
if teamMembers[key] != nil {
for teamID, individuals := range teamMembers[key] {
sort.Slice(individuals, func(i, j int) bool {
return individuals[i].Rank < individuals[j].Rank || (individuals[i].Rank == individuals[j].Rank && individuals[i].Individual < individuals[j].Individual)
})
team := entities.TeamGameParticipation{
TeamID: teamID,
MembersParticipating: individuals,
}
teamVolume := num.DecimalZero()
teamRewardEarned := num.NewUint(0)
teamTotalRewardsEarned := num.NewUint(0)
teamRewardEarnedQuantum := num.NewUint(0)
teamTotalRewardsEarnedQuantum := num.NewUint(0)
rewardMetric := vega.DispatchMetric_DISPATCH_METRIC_UNSPECIFIED
for _, individual := range individuals {
if rewardMetric == vega.DispatchMetric_DISPATCH_METRIC_UNSPECIFIED {
rewardMetric = individual.RewardMetric
}
teamVolume = teamVolume.Add(individual.Volume)
teamRewardEarned = teamRewardEarned.Add(teamRewardEarned, individual.RewardEarned)
teamTotalRewardsEarned = teamTotalRewardsEarned.Add(teamTotalRewardsEarned, individual.TotalRewardsEarned)
teamRewardEarnedQuantum = teamRewardEarnedQuantum.Add(teamRewardEarnedQuantum, individual.RewardEarnedQuantum)
teamTotalRewardsEarnedQuantum = teamTotalRewardsEarnedQuantum.Add(teamTotalRewardsEarnedQuantum, individual.TotalRewardsEarnedQuantum)
}
game.Entities = append(game.Entities, &entities.TeamGameEntity{
Team: team,
Rank: teamRanks[key][teamID],
Volume: teamVolume,
RewardMetric: rewardMetric,
RewardEarned: teamRewardEarned,
TotalRewardsEarned: teamTotalRewardsEarned,
RewardEarnedQuantum: teamRewardEarnedQuantum,
TotalRewardsEarnedQuantum: teamTotalRewardsEarnedQuantum,
})
}
sort.Slice(game.Entities, func(i, j int) bool {
return game.Entities[i].(*entities.TeamGameEntity).Rank < game.Entities[j].(*entities.TeamGameEntity).Rank ||
(game.Entities[i].(*entities.TeamGameEntity).Rank == game.Entities[j].(*entities.TeamGameEntity).Rank &&
game.Entities[i].(*entities.TeamGameEntity).Team.TeamID < game.Entities[j].(*entities.TeamGameEntity).Team.TeamID)
})
}
if gameIndividuals[key] != nil {
game.Entities = append(game.Entities, gameIndividuals[key]...)
sort.Slice(game.Entities, func(i, j int) bool {
return game.Entities[i].(*entities.IndividualGameEntity).Rank < game.Entities[j].(*entities.IndividualGameEntity).Rank ||
(game.Entities[i].(*entities.IndividualGameEntity).Rank == game.Entities[j].(*entities.IndividualGameEntity).Rank &&
game.Entities[i].(*entities.IndividualGameEntity).Individual < game.Entities[j].(*entities.IndividualGameEntity).Individual)
})
}
results = append(results, game)
}
sort.Slice(results, func(i, j int) bool {
return results[i].Epoch > results[j].Epoch ||
(results[i].Epoch == results[j].Epoch && results[i].ID < results[j].ID)
})
return results, nil
}