added option to fetch servers which player joined
This commit is contained in:
parent
0c3c4e0d01
commit
0c6edadd10
2
go.mod
2
go.mod
|
@ -14,7 +14,7 @@ require (
|
|||
github.com/pkg/errors v0.9.1
|
||||
github.com/segmentio/encoding v0.1.14 // indirect
|
||||
github.com/tribalwarshelp/map-generator v0.0.0-20200622153051-b5421f3c7009
|
||||
github.com/tribalwarshelp/shared v0.0.0-20200622084436-3a768c8bf574
|
||||
github.com/tribalwarshelp/shared v0.0.0-20200623082627-128bf01a6570
|
||||
github.com/vektah/gqlparser/v2 v2.0.1
|
||||
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 // indirect
|
||||
gopkg.in/yaml.v2 v2.3.0 // indirect
|
||||
|
|
6
go.sum
6
go.sum
|
@ -169,14 +169,12 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H
|
|||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo=
|
||||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs=
|
||||
github.com/tribalwarshelp/map-generator v0.0.0-20200622134219-0543938c2c8f h1:PlYOm2vuHoaWjKcncJfXW+UeILXhuGtnZv0PLzDs7VQ=
|
||||
github.com/tribalwarshelp/map-generator v0.0.0-20200622134219-0543938c2c8f/go.mod h1:4hs+2Tr5Evk4UZWdmYpcjbdqNbYI5Z1Kc+tvanA5jHY=
|
||||
github.com/tribalwarshelp/map-generator v0.0.0-20200622152458-4d96b7a7bed5 h1:7pJjXv2Se0WelDo8Ya3nwjFyUR+yijeMx0bvvVYqEmM=
|
||||
github.com/tribalwarshelp/map-generator v0.0.0-20200622152458-4d96b7a7bed5/go.mod h1:4hs+2Tr5Evk4UZWdmYpcjbdqNbYI5Z1Kc+tvanA5jHY=
|
||||
github.com/tribalwarshelp/map-generator v0.0.0-20200622153051-b5421f3c7009 h1:RBij3jdMnGcZ4dJnE4PIvNCUmty8pwXBZdlbp4q8psE=
|
||||
github.com/tribalwarshelp/map-generator v0.0.0-20200622153051-b5421f3c7009/go.mod h1:4hs+2Tr5Evk4UZWdmYpcjbdqNbYI5Z1Kc+tvanA5jHY=
|
||||
github.com/tribalwarshelp/shared v0.0.0-20200622084436-3a768c8bf574 h1:y2EoH6zRK9Uc0AeswnJRYUUIQYcSLZB5VDFuxPCKxNM=
|
||||
github.com/tribalwarshelp/shared v0.0.0-20200622084436-3a768c8bf574/go.mod h1:tf+2yTHasV6jAF3V2deZ9slNoCyBzC0fMdTjI7clf6Y=
|
||||
github.com/tribalwarshelp/shared v0.0.0-20200623082627-128bf01a6570 h1:W2cBKRRvr1QLG2GzuJFuvtVe0dblHkkb+E2SlbdJBHo=
|
||||
github.com/tribalwarshelp/shared v0.0.0-20200623082627-128bf01a6570/go.mod h1:tf+2yTHasV6jAF3V2deZ9slNoCyBzC0fMdTjI7clf6Y=
|
||||
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
|
||||
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
|
||||
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
|
||||
|
|
|
@ -12,7 +12,8 @@ import (
|
|||
)
|
||||
|
||||
type DataLoaders struct {
|
||||
LangVersionByTag LangVersionLoader
|
||||
LangVersionByTag LangVersionLoader
|
||||
PlayerServersByID PlayerServersLoader
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
|
@ -27,9 +28,9 @@ func New(cfg Config) *DataLoaders {
|
|||
LangVersionByTag: LangVersionLoader{
|
||||
wait: 2 * time.Millisecond,
|
||||
maxBatch: 0,
|
||||
fetch: func(tagsS []string) ([]*models.LangVersion, []error) {
|
||||
fetch: func(keys []string) ([]*models.LangVersion, []error) {
|
||||
tags := []models.LanguageTag{}
|
||||
for _, tag := range tagsS {
|
||||
for _, tag := range keys {
|
||||
tags = append(tags, models.LanguageTag(tag))
|
||||
}
|
||||
langVersions, _, err := cfg.LangVersionRepo.Fetch(context.Background(), &models.LangVersionFilter{
|
||||
|
@ -44,7 +45,7 @@ func New(cfg Config) *DataLoaders {
|
|||
langVersionByTag[langVersion.Tag] = langVersion
|
||||
}
|
||||
|
||||
inOrder := make([]*models.LangVersion, len(tagsS))
|
||||
inOrder := make([]*models.LangVersion, len(keys))
|
||||
for i, tag := range tags {
|
||||
inOrder[i] = langVersionByTag[tag]
|
||||
}
|
||||
|
@ -52,5 +53,20 @@ func New(cfg Config) *DataLoaders {
|
|||
return inOrder, nil
|
||||
},
|
||||
},
|
||||
PlayerServersByID: PlayerServersLoader{
|
||||
wait: 2 * time.Millisecond,
|
||||
maxBatch: 0,
|
||||
fetch: func(keys []int) ([][]string, []error) {
|
||||
playerServersByID, err := cfg.PlayerRepo.FetchPlayerServers(context.Background(), keys...)
|
||||
if err != nil {
|
||||
return nil, []error{err}
|
||||
}
|
||||
inOrder := make([][]string, len(keys))
|
||||
for i, id := range keys {
|
||||
inOrder[i] = playerServersByID[id]
|
||||
}
|
||||
return inOrder, nil
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,223 @@
|
|||
// Code generated by github.com/vektah/dataloaden, DO NOT EDIT.
|
||||
|
||||
package dataloaders
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// PlayerServersLoaderConfig captures the config to create a new PlayerServersLoader
|
||||
type PlayerServersLoaderConfig struct {
|
||||
// Fetch is a method that provides the data for the loader
|
||||
Fetch func(keys []int) ([][]string, []error)
|
||||
|
||||
// Wait is how long wait before sending a batch
|
||||
Wait time.Duration
|
||||
|
||||
// MaxBatch will limit the maximum number of keys to send in one batch, 0 = not limit
|
||||
MaxBatch int
|
||||
}
|
||||
|
||||
// NewPlayerServersLoader creates a new PlayerServersLoader given a fetch, wait, and maxBatch
|
||||
func NewPlayerServersLoader(config PlayerServersLoaderConfig) *PlayerServersLoader {
|
||||
return &PlayerServersLoader{
|
||||
fetch: config.Fetch,
|
||||
wait: config.Wait,
|
||||
maxBatch: config.MaxBatch,
|
||||
}
|
||||
}
|
||||
|
||||
// PlayerServersLoader batches and caches requests
|
||||
type PlayerServersLoader struct {
|
||||
// this method provides the data for the loader
|
||||
fetch func(keys []int) ([][]string, []error)
|
||||
|
||||
// how long to done before sending a batch
|
||||
wait time.Duration
|
||||
|
||||
// this will limit the maximum number of keys to send in one batch, 0 = no limit
|
||||
maxBatch int
|
||||
|
||||
// INTERNAL
|
||||
|
||||
// lazily created cache
|
||||
cache map[int][]string
|
||||
|
||||
// the current batch. keys will continue to be collected until timeout is hit,
|
||||
// then everything will be sent to the fetch method and out to the listeners
|
||||
batch *playerServersLoaderBatch
|
||||
|
||||
// mutex to prevent races
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
type playerServersLoaderBatch struct {
|
||||
keys []int
|
||||
data [][]string
|
||||
error []error
|
||||
closing bool
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
// Load a string by key, batching and caching will be applied automatically
|
||||
func (l *PlayerServersLoader) Load(key int) ([]string, error) {
|
||||
return l.LoadThunk(key)()
|
||||
}
|
||||
|
||||
// LoadThunk returns a function that when called will block waiting for a string.
|
||||
// This method should be used if you want one goroutine to make requests to many
|
||||
// different data loaders without blocking until the thunk is called.
|
||||
func (l *PlayerServersLoader) LoadThunk(key int) func() ([]string, error) {
|
||||
l.mu.Lock()
|
||||
if it, ok := l.cache[key]; ok {
|
||||
l.mu.Unlock()
|
||||
return func() ([]string, error) {
|
||||
return it, nil
|
||||
}
|
||||
}
|
||||
if l.batch == nil {
|
||||
l.batch = &playerServersLoaderBatch{done: make(chan struct{})}
|
||||
}
|
||||
batch := l.batch
|
||||
pos := batch.keyIndex(l, key)
|
||||
l.mu.Unlock()
|
||||
|
||||
return func() ([]string, error) {
|
||||
<-batch.done
|
||||
|
||||
var data []string
|
||||
if pos < len(batch.data) {
|
||||
data = batch.data[pos]
|
||||
}
|
||||
|
||||
var err error
|
||||
// its convenient to be able to return a single error for everything
|
||||
if len(batch.error) == 1 {
|
||||
err = batch.error[0]
|
||||
} else if batch.error != nil {
|
||||
err = batch.error[pos]
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
l.mu.Lock()
|
||||
l.unsafeSet(key, data)
|
||||
l.mu.Unlock()
|
||||
}
|
||||
|
||||
return data, err
|
||||
}
|
||||
}
|
||||
|
||||
// LoadAll fetches many keys at once. It will be broken into appropriate sized
|
||||
// sub batches depending on how the loader is configured
|
||||
func (l *PlayerServersLoader) LoadAll(keys []int) ([][]string, []error) {
|
||||
results := make([]func() ([]string, error), len(keys))
|
||||
|
||||
for i, key := range keys {
|
||||
results[i] = l.LoadThunk(key)
|
||||
}
|
||||
|
||||
strings := make([][]string, len(keys))
|
||||
errors := make([]error, len(keys))
|
||||
for i, thunk := range results {
|
||||
strings[i], errors[i] = thunk()
|
||||
}
|
||||
return strings, errors
|
||||
}
|
||||
|
||||
// LoadAllThunk returns a function that when called will block waiting for a strings.
|
||||
// This method should be used if you want one goroutine to make requests to many
|
||||
// different data loaders without blocking until the thunk is called.
|
||||
func (l *PlayerServersLoader) LoadAllThunk(keys []int) func() ([][]string, []error) {
|
||||
results := make([]func() ([]string, error), len(keys))
|
||||
for i, key := range keys {
|
||||
results[i] = l.LoadThunk(key)
|
||||
}
|
||||
return func() ([][]string, []error) {
|
||||
strings := make([][]string, len(keys))
|
||||
errors := make([]error, len(keys))
|
||||
for i, thunk := range results {
|
||||
strings[i], errors[i] = thunk()
|
||||
}
|
||||
return strings, errors
|
||||
}
|
||||
}
|
||||
|
||||
// Prime the cache with the provided key and value. If the key already exists, no change is made
|
||||
// and false is returned.
|
||||
// (To forcefully prime the cache, clear the key first with loader.clear(key).prime(key, value).)
|
||||
func (l *PlayerServersLoader) Prime(key int, value []string) bool {
|
||||
l.mu.Lock()
|
||||
var found bool
|
||||
if _, found = l.cache[key]; !found {
|
||||
// make a copy when writing to the cache, its easy to pass a pointer in from a loop var
|
||||
// and end up with the whole cache pointing to the same value.
|
||||
cpy := make([]string, len(value))
|
||||
copy(cpy, value)
|
||||
l.unsafeSet(key, cpy)
|
||||
}
|
||||
l.mu.Unlock()
|
||||
return !found
|
||||
}
|
||||
|
||||
// Clear the value at key from the cache, if it exists
|
||||
func (l *PlayerServersLoader) Clear(key int) {
|
||||
l.mu.Lock()
|
||||
delete(l.cache, key)
|
||||
l.mu.Unlock()
|
||||
}
|
||||
|
||||
func (l *PlayerServersLoader) unsafeSet(key int, value []string) {
|
||||
if l.cache == nil {
|
||||
l.cache = map[int][]string{}
|
||||
}
|
||||
l.cache[key] = value
|
||||
}
|
||||
|
||||
// keyIndex will return the location of the key in the batch, if its not found
|
||||
// it will add the key to the batch
|
||||
func (b *playerServersLoaderBatch) keyIndex(l *PlayerServersLoader, key int) int {
|
||||
for i, existingKey := range b.keys {
|
||||
if key == existingKey {
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
pos := len(b.keys)
|
||||
b.keys = append(b.keys, key)
|
||||
if pos == 0 {
|
||||
go b.startTimer(l)
|
||||
}
|
||||
|
||||
if l.maxBatch != 0 && pos >= l.maxBatch-1 {
|
||||
if !b.closing {
|
||||
b.closing = true
|
||||
l.batch = nil
|
||||
go b.end(l)
|
||||
}
|
||||
}
|
||||
|
||||
return pos
|
||||
}
|
||||
|
||||
func (b *playerServersLoaderBatch) startTimer(l *PlayerServersLoader) {
|
||||
time.Sleep(l.wait)
|
||||
l.mu.Lock()
|
||||
|
||||
// we must have hit a batch limit and are already finalizing this batch
|
||||
if b.closing {
|
||||
l.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
l.batch = nil
|
||||
l.mu.Unlock()
|
||||
|
||||
b.end(l)
|
||||
}
|
||||
|
||||
func (b *playerServersLoaderBatch) end(l *PlayerServersLoader) {
|
||||
b.data, b.error = l.fetch(b.keys)
|
||||
close(b.done)
|
||||
}
|
|
@ -43,6 +43,7 @@ type ResolverRoot interface {
|
|||
Query() QueryResolver
|
||||
Server() ServerResolver
|
||||
ServerStatsRecord() ServerStatsRecordResolver
|
||||
Tribe() TribeResolver
|
||||
TribeHistoryRecord() TribeHistoryRecordResolver
|
||||
Village() VillageResolver
|
||||
}
|
||||
|
@ -135,6 +136,7 @@ type ComplexityRoot struct {
|
|||
ScoreDef func(childComplexity int) int
|
||||
ScoreSup func(childComplexity int) int
|
||||
ScoreTotal func(childComplexity int) int
|
||||
Servers func(childComplexity int) int
|
||||
TotalVillages func(childComplexity int) int
|
||||
Tribe func(childComplexity int) int
|
||||
}
|
||||
|
@ -189,7 +191,6 @@ type ComplexityRoot struct {
|
|||
Config func(childComplexity int) int
|
||||
DataUpdatedAt func(childComplexity int) int
|
||||
HistoryUpdatedAt func(childComplexity int) int
|
||||
ID func(childComplexity int) int
|
||||
Key func(childComplexity int) int
|
||||
LangVersion func(childComplexity int) int
|
||||
NumberOfPlayers func(childComplexity int) int
|
||||
|
@ -478,7 +479,9 @@ type LiveEnnoblementResolver interface {
|
|||
OldOwner(ctx context.Context, obj *models.LiveEnnoblement) (*models.Player, error)
|
||||
}
|
||||
type PlayerResolver interface {
|
||||
JoinedAt(ctx context.Context, obj *models.Player) (*time.Time, error)
|
||||
Tribe(ctx context.Context, obj *models.Player) (*models.Tribe, error)
|
||||
Servers(ctx context.Context, obj *models.Player) ([]string, error)
|
||||
}
|
||||
type PlayerHistoryRecordResolver interface {
|
||||
Player(ctx context.Context, obj *models.PlayerHistory) (*models.Player, error)
|
||||
|
@ -513,6 +516,9 @@ type ServerResolver interface {
|
|||
type ServerStatsRecordResolver interface {
|
||||
CreatedAt(ctx context.Context, obj *models.ServerStats) (*time.Time, error)
|
||||
}
|
||||
type TribeResolver interface {
|
||||
CreatedAt(ctx context.Context, obj *models.Tribe) (*time.Time, error)
|
||||
}
|
||||
type TribeHistoryRecordResolver interface {
|
||||
Tribe(ctx context.Context, obj *models.TribeHistory) (*models.Tribe, error)
|
||||
|
||||
|
@ -971,6 +977,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in
|
|||
|
||||
return e.complexity.Player.ScoreTotal(childComplexity), true
|
||||
|
||||
case "Player.servers":
|
||||
if e.complexity.Player.Servers == nil {
|
||||
break
|
||||
}
|
||||
|
||||
return e.complexity.Player.Servers(childComplexity), true
|
||||
|
||||
case "Player.totalVillages":
|
||||
if e.complexity.Player.TotalVillages == nil {
|
||||
break
|
||||
|
@ -1319,13 +1332,6 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in
|
|||
|
||||
return e.complexity.Server.HistoryUpdatedAt(childComplexity), true
|
||||
|
||||
case "Server.id":
|
||||
if e.complexity.Server.ID == nil {
|
||||
break
|
||||
}
|
||||
|
||||
return e.complexity.Server.ID(childComplexity), true
|
||||
|
||||
case "Server.key":
|
||||
if e.complexity.Server.Key == nil {
|
||||
break
|
||||
|
@ -2895,8 +2901,9 @@ extend type Query {
|
|||
rankTotal: Int!
|
||||
scoreTotal: Int!
|
||||
dailyGrowth: Int!
|
||||
joinedAt: Time!
|
||||
joinedAt: Time! @goField(forceResolver: true)
|
||||
tribe: Tribe @goField(forceResolver: true)
|
||||
servers: [String!]! @goField(forceResolver: true)
|
||||
}
|
||||
|
||||
type PlayersList {
|
||||
|
@ -3049,7 +3056,6 @@ extend type Query {
|
|||
}
|
||||
|
||||
type Server {
|
||||
id: Int!
|
||||
key: String!
|
||||
status: ServerStatus!
|
||||
numberOfPlayers: Int!
|
||||
|
@ -3073,9 +3079,6 @@ type ServersList {
|
|||
}
|
||||
|
||||
input ServerFilter {
|
||||
id: [Int!]
|
||||
idNEQ: [Int!]
|
||||
|
||||
key: [String!]
|
||||
keyNEQ: [String!]
|
||||
keyMATCH: String
|
||||
|
@ -3298,7 +3301,7 @@ extend type Query {
|
|||
scoreDef: Int!
|
||||
rankTotal: Int!
|
||||
dominance: Float!
|
||||
createdAt: Time!
|
||||
createdAt: Time! @goField(forceResolver: true)
|
||||
scoreTotal: Int!
|
||||
}
|
||||
|
||||
|
@ -5967,13 +5970,13 @@ func (ec *executionContext) _Player_joinedAt(ctx context.Context, field graphql.
|
|||
Object: "Player",
|
||||
Field: field,
|
||||
Args: nil,
|
||||
IsMethod: false,
|
||||
IsMethod: true,
|
||||
}
|
||||
|
||||
ctx = graphql.WithFieldContext(ctx, fc)
|
||||
resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) {
|
||||
ctx = rctx // use context from middleware stack in children
|
||||
return obj.JoinedAt, nil
|
||||
return ec.resolvers.Player().JoinedAt(rctx, obj)
|
||||
})
|
||||
if err != nil {
|
||||
ec.Error(ctx, err)
|
||||
|
@ -5985,9 +5988,9 @@ func (ec *executionContext) _Player_joinedAt(ctx context.Context, field graphql.
|
|||
}
|
||||
return graphql.Null
|
||||
}
|
||||
res := resTmp.(time.Time)
|
||||
res := resTmp.(*time.Time)
|
||||
fc.Result = res
|
||||
return ec.marshalNTime2timeᚐTime(ctx, field.Selections, res)
|
||||
return ec.marshalNTime2ᚖtimeᚐTime(ctx, field.Selections, res)
|
||||
}
|
||||
|
||||
func (ec *executionContext) _Player_tribe(ctx context.Context, field graphql.CollectedField, obj *models.Player) (ret graphql.Marshaler) {
|
||||
|
@ -6021,6 +6024,40 @@ func (ec *executionContext) _Player_tribe(ctx context.Context, field graphql.Col
|
|||
return ec.marshalOTribe2ᚖgithubᚗcomᚋtribalwarshelpᚋsharedᚋmodelsᚐTribe(ctx, field.Selections, res)
|
||||
}
|
||||
|
||||
func (ec *executionContext) _Player_servers(ctx context.Context, field graphql.CollectedField, obj *models.Player) (ret graphql.Marshaler) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
ec.Error(ctx, ec.Recover(ctx, r))
|
||||
ret = graphql.Null
|
||||
}
|
||||
}()
|
||||
fc := &graphql.FieldContext{
|
||||
Object: "Player",
|
||||
Field: field,
|
||||
Args: nil,
|
||||
IsMethod: true,
|
||||
}
|
||||
|
||||
ctx = graphql.WithFieldContext(ctx, fc)
|
||||
resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) {
|
||||
ctx = rctx // use context from middleware stack in children
|
||||
return ec.resolvers.Player().Servers(rctx, obj)
|
||||
})
|
||||
if err != nil {
|
||||
ec.Error(ctx, err)
|
||||
return graphql.Null
|
||||
}
|
||||
if resTmp == nil {
|
||||
if !graphql.HasFieldError(ctx, fc) {
|
||||
ec.Errorf(ctx, "must not be null")
|
||||
}
|
||||
return graphql.Null
|
||||
}
|
||||
res := resTmp.([]string)
|
||||
fc.Result = res
|
||||
return ec.marshalNString2ᚕstringᚄ(ctx, field.Selections, res)
|
||||
}
|
||||
|
||||
func (ec *executionContext) _PlayerHistory_total(ctx context.Context, field graphql.CollectedField, obj *PlayerHistory) (ret graphql.Marshaler) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
|
@ -7287,40 +7324,6 @@ func (ec *executionContext) _Query___schema(ctx context.Context, field graphql.C
|
|||
return ec.marshalO__Schema2ᚖgithubᚗcomᚋ99designsᚋgqlgenᚋgraphqlᚋintrospectionᚐSchema(ctx, field.Selections, res)
|
||||
}
|
||||
|
||||
func (ec *executionContext) _Server_id(ctx context.Context, field graphql.CollectedField, obj *models.Server) (ret graphql.Marshaler) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
ec.Error(ctx, ec.Recover(ctx, r))
|
||||
ret = graphql.Null
|
||||
}
|
||||
}()
|
||||
fc := &graphql.FieldContext{
|
||||
Object: "Server",
|
||||
Field: field,
|
||||
Args: nil,
|
||||
IsMethod: false,
|
||||
}
|
||||
|
||||
ctx = graphql.WithFieldContext(ctx, fc)
|
||||
resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) {
|
||||
ctx = rctx // use context from middleware stack in children
|
||||
return obj.ID, nil
|
||||
})
|
||||
if err != nil {
|
||||
ec.Error(ctx, err)
|
||||
return graphql.Null
|
||||
}
|
||||
if resTmp == nil {
|
||||
if !graphql.HasFieldError(ctx, fc) {
|
||||
ec.Errorf(ctx, "must not be null")
|
||||
}
|
||||
return graphql.Null
|
||||
}
|
||||
res := resTmp.(int)
|
||||
fc.Result = res
|
||||
return ec.marshalNInt2int(ctx, field.Selections, res)
|
||||
}
|
||||
|
||||
func (ec *executionContext) _Server_key(ctx context.Context, field graphql.CollectedField, obj *models.Server) (ret graphql.Marshaler) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
|
@ -12423,13 +12426,13 @@ func (ec *executionContext) _Tribe_createdAt(ctx context.Context, field graphql.
|
|||
Object: "Tribe",
|
||||
Field: field,
|
||||
Args: nil,
|
||||
IsMethod: false,
|
||||
IsMethod: true,
|
||||
}
|
||||
|
||||
ctx = graphql.WithFieldContext(ctx, fc)
|
||||
resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) {
|
||||
ctx = rctx // use context from middleware stack in children
|
||||
return obj.CreatedAt, nil
|
||||
return ec.resolvers.Tribe().CreatedAt(rctx, obj)
|
||||
})
|
||||
if err != nil {
|
||||
ec.Error(ctx, err)
|
||||
|
@ -12441,9 +12444,9 @@ func (ec *executionContext) _Tribe_createdAt(ctx context.Context, field graphql.
|
|||
}
|
||||
return graphql.Null
|
||||
}
|
||||
res := resTmp.(time.Time)
|
||||
res := resTmp.(*time.Time)
|
||||
fc.Result = res
|
||||
return ec.marshalNTime2timeᚐTime(ctx, field.Selections, res)
|
||||
return ec.marshalNTime2ᚖtimeᚐTime(ctx, field.Selections, res)
|
||||
}
|
||||
|
||||
func (ec *executionContext) _Tribe_scoreTotal(ctx context.Context, field graphql.CollectedField, obj *models.Tribe) (ret graphql.Marshaler) {
|
||||
|
@ -15860,18 +15863,6 @@ func (ec *executionContext) unmarshalInputServerFilter(ctx context.Context, obj
|
|||
|
||||
for k, v := range asMap {
|
||||
switch k {
|
||||
case "id":
|
||||
var err error
|
||||
it.ID, err = ec.unmarshalOInt2ᚕintᚄ(ctx, v)
|
||||
if err != nil {
|
||||
return it, err
|
||||
}
|
||||
case "idNEQ":
|
||||
var err error
|
||||
it.IdNEQ, err = ec.unmarshalOInt2ᚕintᚄ(ctx, v)
|
||||
if err != nil {
|
||||
return it, err
|
||||
}
|
||||
case "key":
|
||||
var err error
|
||||
it.Key, err = ec.unmarshalOString2ᚕstringᚄ(ctx, v)
|
||||
|
@ -17253,10 +17244,19 @@ func (ec *executionContext) _Player(ctx context.Context, sel ast.SelectionSet, o
|
|||
atomic.AddUint32(&invalids, 1)
|
||||
}
|
||||
case "joinedAt":
|
||||
out.Values[i] = ec._Player_joinedAt(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
atomic.AddUint32(&invalids, 1)
|
||||
}
|
||||
field := field
|
||||
out.Concurrently(i, func() (res graphql.Marshaler) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
ec.Error(ctx, ec.Recover(ctx, r))
|
||||
}
|
||||
}()
|
||||
res = ec._Player_joinedAt(ctx, field, obj)
|
||||
if res == graphql.Null {
|
||||
atomic.AddUint32(&invalids, 1)
|
||||
}
|
||||
return res
|
||||
})
|
||||
case "tribe":
|
||||
field := field
|
||||
out.Concurrently(i, func() (res graphql.Marshaler) {
|
||||
|
@ -17268,6 +17268,20 @@ func (ec *executionContext) _Player(ctx context.Context, sel ast.SelectionSet, o
|
|||
res = ec._Player_tribe(ctx, field, obj)
|
||||
return res
|
||||
})
|
||||
case "servers":
|
||||
field := field
|
||||
out.Concurrently(i, func() (res graphql.Marshaler) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
ec.Error(ctx, ec.Recover(ctx, r))
|
||||
}
|
||||
}()
|
||||
res = ec._Player_servers(ctx, field, obj)
|
||||
if res == graphql.Null {
|
||||
atomic.AddUint32(&invalids, 1)
|
||||
}
|
||||
return res
|
||||
})
|
||||
default:
|
||||
panic("unknown field " + strconv.Quote(field.Name))
|
||||
}
|
||||
|
@ -17683,11 +17697,6 @@ func (ec *executionContext) _Server(ctx context.Context, sel ast.SelectionSet, o
|
|||
switch field.Name {
|
||||
case "__typename":
|
||||
out.Values[i] = graphql.MarshalString("Server")
|
||||
case "id":
|
||||
out.Values[i] = ec._Server_id(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
atomic.AddUint32(&invalids, 1)
|
||||
}
|
||||
case "key":
|
||||
out.Values[i] = ec._Server_key(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
|
@ -18798,87 +18807,96 @@ func (ec *executionContext) _Tribe(ctx context.Context, sel ast.SelectionSet, ob
|
|||
case "id":
|
||||
out.Values[i] = ec._Tribe_id(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
invalids++
|
||||
atomic.AddUint32(&invalids, 1)
|
||||
}
|
||||
case "name":
|
||||
out.Values[i] = ec._Tribe_name(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
invalids++
|
||||
atomic.AddUint32(&invalids, 1)
|
||||
}
|
||||
case "tag":
|
||||
out.Values[i] = ec._Tribe_tag(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
invalids++
|
||||
atomic.AddUint32(&invalids, 1)
|
||||
}
|
||||
case "totalMembers":
|
||||
out.Values[i] = ec._Tribe_totalMembers(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
invalids++
|
||||
atomic.AddUint32(&invalids, 1)
|
||||
}
|
||||
case "totalVillages":
|
||||
out.Values[i] = ec._Tribe_totalVillages(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
invalids++
|
||||
atomic.AddUint32(&invalids, 1)
|
||||
}
|
||||
case "points":
|
||||
out.Values[i] = ec._Tribe_points(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
invalids++
|
||||
atomic.AddUint32(&invalids, 1)
|
||||
}
|
||||
case "allPoints":
|
||||
out.Values[i] = ec._Tribe_allPoints(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
invalids++
|
||||
atomic.AddUint32(&invalids, 1)
|
||||
}
|
||||
case "rank":
|
||||
out.Values[i] = ec._Tribe_rank(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
invalids++
|
||||
atomic.AddUint32(&invalids, 1)
|
||||
}
|
||||
case "exist":
|
||||
out.Values[i] = ec._Tribe_exist(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
invalids++
|
||||
atomic.AddUint32(&invalids, 1)
|
||||
}
|
||||
case "rankAtt":
|
||||
out.Values[i] = ec._Tribe_rankAtt(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
invalids++
|
||||
atomic.AddUint32(&invalids, 1)
|
||||
}
|
||||
case "scoreAtt":
|
||||
out.Values[i] = ec._Tribe_scoreAtt(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
invalids++
|
||||
atomic.AddUint32(&invalids, 1)
|
||||
}
|
||||
case "rankDef":
|
||||
out.Values[i] = ec._Tribe_rankDef(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
invalids++
|
||||
atomic.AddUint32(&invalids, 1)
|
||||
}
|
||||
case "scoreDef":
|
||||
out.Values[i] = ec._Tribe_scoreDef(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
invalids++
|
||||
atomic.AddUint32(&invalids, 1)
|
||||
}
|
||||
case "rankTotal":
|
||||
out.Values[i] = ec._Tribe_rankTotal(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
invalids++
|
||||
atomic.AddUint32(&invalids, 1)
|
||||
}
|
||||
case "dominance":
|
||||
out.Values[i] = ec._Tribe_dominance(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
invalids++
|
||||
atomic.AddUint32(&invalids, 1)
|
||||
}
|
||||
case "createdAt":
|
||||
out.Values[i] = ec._Tribe_createdAt(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
invalids++
|
||||
}
|
||||
field := field
|
||||
out.Concurrently(i, func() (res graphql.Marshaler) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
ec.Error(ctx, ec.Recover(ctx, r))
|
||||
}
|
||||
}()
|
||||
res = ec._Tribe_createdAt(ctx, field, obj)
|
||||
if res == graphql.Null {
|
||||
atomic.AddUint32(&invalids, 1)
|
||||
}
|
||||
return res
|
||||
})
|
||||
case "scoreTotal":
|
||||
out.Values[i] = ec._Tribe_scoreTotal(ctx, field, obj)
|
||||
if out.Values[i] == graphql.Null {
|
||||
invalids++
|
||||
atomic.AddUint32(&invalids, 1)
|
||||
}
|
||||
default:
|
||||
panic("unknown field " + strconv.Quote(field.Name))
|
||||
|
@ -19880,6 +19898,35 @@ func (ec *executionContext) marshalNString2string(ctx context.Context, sel ast.S
|
|||
return res
|
||||
}
|
||||
|
||||
func (ec *executionContext) unmarshalNString2ᚕstringᚄ(ctx context.Context, v interface{}) ([]string, error) {
|
||||
var vSlice []interface{}
|
||||
if v != nil {
|
||||
if tmp1, ok := v.([]interface{}); ok {
|
||||
vSlice = tmp1
|
||||
} else {
|
||||
vSlice = []interface{}{v}
|
||||
}
|
||||
}
|
||||
var err error
|
||||
res := make([]string, len(vSlice))
|
||||
for i := range vSlice {
|
||||
res[i], err = ec.unmarshalNString2string(ctx, vSlice[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (ec *executionContext) marshalNString2ᚕstringᚄ(ctx context.Context, sel ast.SelectionSet, v []string) graphql.Marshaler {
|
||||
ret := make(graphql.Array, len(v))
|
||||
for i := range v {
|
||||
ret[i] = ec.marshalNString2string(ctx, sel, v[i])
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func (ec *executionContext) unmarshalNTime2timeᚐTime(ctx context.Context, v interface{}) (time.Time, error) {
|
||||
return graphql.UnmarshalTime(v)
|
||||
}
|
||||
|
|
|
@ -2,8 +2,11 @@ package resolvers
|
|||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/tribalwarshelp/api/graphql/generated"
|
||||
"github.com/tribalwarshelp/api/middleware"
|
||||
"github.com/tribalwarshelp/api/utils"
|
||||
"github.com/tribalwarshelp/shared/models"
|
||||
)
|
||||
|
||||
|
@ -15,6 +18,23 @@ func (r *playerResolver) Tribe(ctx context.Context, obj *models.Player) (*models
|
|||
return getTribe(ctx, obj.TribeID), nil
|
||||
}
|
||||
|
||||
func (r *playerResolver) Servers(ctx context.Context, obj *models.Player) ([]string, error) {
|
||||
loaders := middleware.DataLoadersFromContext(ctx)
|
||||
if loaders != nil {
|
||||
servers, err := loaders.PlayerServersByID.Load(obj.ID)
|
||||
if err == nil {
|
||||
return servers, nil
|
||||
}
|
||||
}
|
||||
return []string{}, nil
|
||||
}
|
||||
|
||||
func (r *playerResolver) JoinedAt(ctx context.Context, obj *models.Player) (*time.Time, error) {
|
||||
server, _ := getServer(ctx)
|
||||
t := formatDate(ctx, utils.LanguageTagFromServerKey(server), obj.JoinedAt)
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
func (r *queryResolver) Players(ctx context.Context, server string, filter *models.PlayerFilter) (*generated.PlayersList, error) {
|
||||
var err error
|
||||
list := &generated.PlayersList{}
|
||||
|
|
|
@ -31,6 +31,7 @@ type Resolver struct {
|
|||
func (r *Resolver) Query() generated.QueryResolver { return &queryResolver{r} }
|
||||
func (r *Resolver) Player() generated.PlayerResolver { return &playerResolver{r} }
|
||||
func (r *Resolver) Village() generated.VillageResolver { return &villageResolver{r} }
|
||||
func (r *Resolver) Tribe() generated.TribeResolver { return &tribeResolver{r} }
|
||||
func (r *Resolver) LiveEnnoblement() generated.LiveEnnoblementResolver {
|
||||
return &liveEnnoblementResolver{r}
|
||||
}
|
||||
|
@ -49,6 +50,7 @@ func (r *Resolver) TribeHistoryRecord() generated.TribeHistoryRecordResolver {
|
|||
type queryResolver struct{ *Resolver }
|
||||
type playerResolver struct{ *Resolver }
|
||||
type villageResolver struct{ *Resolver }
|
||||
type tribeResolver struct{ *Resolver }
|
||||
type liveEnnoblementResolver struct{ *Resolver }
|
||||
type ennoblementResolver struct{ *Resolver }
|
||||
type serverResolver struct{ *Resolver }
|
||||
|
|
|
@ -20,17 +20,17 @@ func (r *serverResolver) LangVersion(ctx context.Context, obj *models.Server) (*
|
|||
}
|
||||
|
||||
func (r *serverResolver) DataUpdatedAt(ctx context.Context, obj *models.Server) (*time.Time, error) {
|
||||
t := formatDate(context.Background(), obj.LangVersionTag, obj.DataUpdatedAt)
|
||||
t := formatDate(ctx, obj.LangVersionTag, obj.DataUpdatedAt)
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
func (r *serverResolver) HistoryUpdatedAt(ctx context.Context, obj *models.Server) (*time.Time, error) {
|
||||
t := formatDate(context.Background(), obj.LangVersionTag, obj.HistoryUpdatedAt)
|
||||
t := formatDate(ctx, obj.LangVersionTag, obj.HistoryUpdatedAt)
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
func (r *serverResolver) StatsUpdatedAt(ctx context.Context, obj *models.Server) (*time.Time, error) {
|
||||
t := formatDate(context.Background(), obj.LangVersionTag, obj.StatsUpdatedAt)
|
||||
t := formatDate(ctx, obj.LangVersionTag, obj.StatsUpdatedAt)
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -2,11 +2,19 @@ package resolvers
|
|||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/tribalwarshelp/api/graphql/generated"
|
||||
"github.com/tribalwarshelp/api/utils"
|
||||
"github.com/tribalwarshelp/shared/models"
|
||||
)
|
||||
|
||||
func (r *tribeResolver) CreatedAt(ctx context.Context, obj *models.Tribe) (*time.Time, error) {
|
||||
server, _ := getServer(ctx)
|
||||
t := formatDate(ctx, utils.LanguageTagFromServerKey(server), obj.CreatedAt)
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
func (r *queryResolver) Tribes(ctx context.Context, server string, filter *models.TribeFilter) (*generated.TribesList, error) {
|
||||
var err error
|
||||
list := &generated.TribesList{}
|
||||
|
|
|
@ -14,8 +14,9 @@ type Player {
|
|||
rankTotal: Int!
|
||||
scoreTotal: Int!
|
||||
dailyGrowth: Int!
|
||||
joinedAt: Time!
|
||||
joinedAt: Time! @goField(forceResolver: true)
|
||||
tribe: Tribe @goField(forceResolver: true)
|
||||
servers: [String!]! @goField(forceResolver: true)
|
||||
}
|
||||
|
||||
type PlayersList {
|
||||
|
|
|
@ -4,7 +4,6 @@ enum ServerStatus {
|
|||
}
|
||||
|
||||
type Server {
|
||||
id: Int!
|
||||
key: String!
|
||||
status: ServerStatus!
|
||||
numberOfPlayers: Int!
|
||||
|
@ -28,9 +27,6 @@ type ServersList {
|
|||
}
|
||||
|
||||
input ServerFilter {
|
||||
id: [Int!]
|
||||
idNEQ: [Int!]
|
||||
|
||||
key: [String!]
|
||||
keyNEQ: [String!]
|
||||
keyMATCH: String
|
||||
|
|
|
@ -14,7 +14,7 @@ type Tribe {
|
|||
scoreDef: Int!
|
||||
rankTotal: Int!
|
||||
dominance: Float!
|
||||
createdAt: Time!
|
||||
createdAt: Time! @goField(forceResolver: true)
|
||||
scoreTotal: Int!
|
||||
}
|
||||
|
||||
|
|
|
@ -8,4 +8,5 @@ import (
|
|||
|
||||
type Repository interface {
|
||||
Fetch(ctx context.Context, server string, filter *models.PlayerFilter) ([]*models.Player, int, error)
|
||||
FetchPlayerServers(ctx context.Context, playerID ...int) (map[int][]string, error)
|
||||
}
|
||||
|
|
|
@ -53,3 +53,27 @@ func (repo *pgRepository) Fetch(ctx context.Context, server string, f *models.Pl
|
|||
|
||||
return data, total, nil
|
||||
}
|
||||
|
||||
type fetchPlayerServersQueryResult struct {
|
||||
PlayerID int
|
||||
Servers []string `pg:",array"`
|
||||
}
|
||||
|
||||
func (repo *pgRepository) FetchPlayerServers(ctx context.Context, playerID ...int) (map[int][]string, error) {
|
||||
data := []*fetchPlayerServersQueryResult{}
|
||||
if err := repo.Model(&models.PlayerToServer{}).
|
||||
Context(ctx).
|
||||
Column("player_id").
|
||||
ColumnExpr("array_agg(server_key) as servers").
|
||||
Where("player_id IN (?)", pg.In(playerID)).
|
||||
Group("player_id").
|
||||
Select(&data); err != nil && err != pg.ErrNoRows {
|
||||
return nil, errors.Wrap(err, "Internal server error")
|
||||
}
|
||||
|
||||
m := make(map[int][]string)
|
||||
for _, res := range data {
|
||||
m[res.PlayerID] = res.Servers
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
|
Reference in New Issue