code.vegaprotocol.io/vega@v0.79.0/datanode/gateway/graphql/game_resolver.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 gql 17 18 import ( 19 "context" 20 "fmt" 21 22 v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2" 23 ) 24 25 type gameResolver VegaResolverRoot 26 27 func (g *gameResolver) Epoch(_ context.Context, obj *v2.Game) (int, error) { 28 return int(obj.Epoch), nil 29 } 30 31 func (g *gameResolver) NumberOfParticipants(_ context.Context, obj *v2.Game) (int, error) { 32 return int(obj.Participants), nil 33 } 34 35 func (g *gameResolver) Entities(_ context.Context, obj *v2.Game) ([]GameEntity, error) { 36 switch e := obj.Entities.(type) { 37 case *v2.Game_Team: 38 return resolveTeamEntities(obj.GetTeam()) 39 case *v2.Game_Individual: 40 return resolveIndividualEntities(obj.GetIndividual()) 41 default: 42 return nil, fmt.Errorf("unsupported entity type: %T", e) 43 } 44 } 45 46 func resolveTeamEntities(team *v2.TeamGameEntities) ([]GameEntity, error) { 47 entities := make([]GameEntity, 0) 48 for _, e := range team.Team { 49 entity := TeamGameEntity{ 50 Team: &TeamParticipation{ 51 TeamID: e.Team.TeamId, 52 MembersParticipating: resolveIndividuals(e.Team.MembersParticipating), 53 }, 54 Rank: int(e.Rank), 55 Volume: e.Volume, 56 RewardMetric: e.RewardMetric, 57 RewardEarned: e.RewardEarned, 58 TotalRewardsEarned: e.TotalRewardsEarned, 59 RewardEarnedQuantum: e.RewardEarnedQuantum, 60 TotalRewardsEarnedQuantum: e.TotalRewardsEarnedQuantum, 61 } 62 entities = append(entities, entity) 63 } 64 return entities, nil 65 } 66 67 func resolveIndividualEntities(individual *v2.IndividualGameEntities) ([]GameEntity, error) { 68 entities := make([]GameEntity, 0) 69 for _, e := range resolveIndividuals(individual.Individual) { 70 entities = append(entities, e) 71 } 72 return entities, nil 73 } 74 75 func resolveIndividuals(individuals []*v2.IndividualGameEntity) []*IndividualGameEntity { 76 entities := make([]*IndividualGameEntity, 0) 77 for _, e := range individuals { 78 entity := IndividualGameEntity{ 79 Individual: e.Individual, 80 Rank: int(e.Rank), 81 Volume: e.Volume, 82 RewardMetric: e.RewardMetric, 83 RewardEarned: e.RewardEarned, 84 TotalRewardsEarned: e.TotalRewardsEarned, 85 RewardEarnedQuantum: e.RewardEarnedQuantum, 86 TotalRewardsEarnedQuantum: e.TotalRewardsEarnedQuantum, 87 } 88 entities = append(entities, &entity) 89 } 90 return entities 91 }