code.vegaprotocol.io/vega@v0.79.0/core/events/game_scores.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package events 17 18 import ( 19 "context" 20 "time" 21 22 "code.vegaprotocol.io/vega/core/types" 23 eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1" 24 ) 25 26 type GameScores struct { 27 *Base 28 pb eventspb.GameScores 29 } 30 31 func (gs *GameScores) GameScoreEvent() eventspb.GameScores { 32 return gs.pb 33 } 34 35 func NewPartyGameScoresEvent(ctx context.Context, epoch int64, gameID string, time time.Time, partyScores []*types.PartyContributionScore) *GameScores { 36 ps := make([]*eventspb.GamePartyScore, 0, len(partyScores)) 37 for _, partyScore := range partyScores { 38 var ov, sb, tfp string 39 if partyScore.OpenVolume != nil { 40 ov = partyScore.OpenVolume.String() 41 } 42 if partyScore.StakingBalance != nil { 43 sb = partyScore.StakingBalance.String() 44 } 45 if partyScore.TotalFeesPaid != nil { 46 tfp = partyScore.TotalFeesPaid.String() 47 } 48 49 ps = append(ps, &eventspb.GamePartyScore{ 50 GameId: gameID, 51 Epoch: epoch, 52 Time: time.UnixNano(), 53 Party: partyScore.Party, 54 Score: partyScore.Score.String(), 55 IsEligible: partyScore.IsEligible, 56 OpenVolume: ov, 57 StakingBalance: sb, 58 TotalFeesPaid: tfp, 59 }) 60 } 61 62 return &GameScores{ 63 Base: newBase(ctx, GameScoresEvent), 64 pb: eventspb.GameScores{ 65 PartyScores: ps, 66 }, 67 } 68 } 69 70 func NewTeamGameScoresEvent(ctx context.Context, epoch int64, gameID string, time time.Time, teamScores []*types.PartyContributionScore, teamPartyScores map[string][]*types.PartyContributionScore) *GameScores { 71 ts := make([]*eventspb.GameTeamScore, 0, len(teamScores)) 72 ps := []*eventspb.GamePartyScore{} 73 for _, teamScore := range teamScores { 74 team := &eventspb.GameTeamScore{ 75 GameId: gameID, 76 Time: time.UnixNano(), 77 Epoch: epoch, 78 TeamId: teamScore.Party, 79 Score: teamScore.Score.String(), 80 } 81 for _, partyScore := range teamPartyScores[teamScore.Party] { 82 var rank *uint64 83 if partyScore.RankingIndex >= 0 { 84 r := uint64(partyScore.RankingIndex) 85 rank = &r 86 } 87 var ov, sb, tfp string 88 if partyScore.OpenVolume != nil { 89 ov = partyScore.OpenVolume.String() 90 } 91 if partyScore.StakingBalance != nil { 92 sb = partyScore.StakingBalance.String() 93 } 94 if partyScore.TotalFeesPaid != nil { 95 tfp = partyScore.TotalFeesPaid.String() 96 } 97 ps = append(ps, &eventspb.GamePartyScore{ 98 GameId: gameID, 99 TeamId: &team.TeamId, 100 Time: time.UnixNano(), 101 Epoch: epoch, 102 Party: partyScore.Party, 103 Score: partyScore.Score.String(), 104 IsEligible: partyScore.IsEligible, 105 OpenVolume: ov, 106 StakingBalance: sb, 107 TotalFeesPaid: tfp, 108 Rank: rank, 109 }) 110 } 111 ts = append(ts, team) 112 } 113 114 return &GameScores{ 115 Base: newBase(ctx, GameScoresEvent), 116 pb: eventspb.GameScores{ 117 PartyScores: ps, 118 TeamScores: ts, 119 }, 120 } 121 } 122 123 func (gs *GameScores) Proto() eventspb.GameScores { 124 return gs.pb 125 } 126 127 func (gs *GameScores) StreamMessage() *eventspb.BusEvent { 128 busEvent := newBusEventFromBase(gs.Base) 129 cpy := gs.pb 130 busEvent.Event = &eventspb.BusEvent_GameScores{ 131 GameScores: &cpy, 132 } 133 134 return busEvent 135 } 136 137 func GameScoresEventFromStream(ctx context.Context, be *eventspb.BusEvent) *GameScores { 138 m := be.GetGameScores() 139 return &GameScores{ 140 Base: newBaseFromBusEvent(ctx, GameScoresEvent, be), 141 pb: *m, 142 } 143 }