From c4d72786258410a046a88bcc93b6dc94fafc5bf0 Mon Sep 17 00:00:00 2001 From: Settel Date: Sun, 5 Sep 2021 01:18:46 +0200 Subject: [PATCH] refactoring --- server/src/game/populateGetPlayers.go | 25 +++++++++ server/src/game/populateGetRoundInfo.go | 51 ++++++++++++++++++ server/src/game/populateSyncDataCb.go | 70 +------------------------ 3 files changed, 78 insertions(+), 68 deletions(-) create mode 100644 server/src/game/populateGetPlayers.go create mode 100644 server/src/game/populateGetRoundInfo.go diff --git a/server/src/game/populateGetPlayers.go b/server/src/game/populateGetPlayers.go new file mode 100644 index 0000000..b6c42dd --- /dev/null +++ b/server/src/game/populateGetPlayers.go @@ -0,0 +1,25 @@ +package game + +import ( + "sirlab.de/go/knyt/syncdata" + "sort" +) + +func (gm *Game) populateGetPlayers() []syncdata.PlayerInfo { + players := make([]syncdata.PlayerInfo, 0) + for _, p := range gm.players { + if p.isPlaying { + players = append(players, syncdata.PlayerInfo{ + Id: p.id, + Name: p.name, + IsIdle: p.isIdle, + }) + } + } + + sort.Slice(players, func(a, b int) bool { + return players[a].Name < players[b].Name + }) + + return players +} diff --git a/server/src/game/populateGetRoundInfo.go b/server/src/game/populateGetRoundInfo.go new file mode 100644 index 0000000..d69412b --- /dev/null +++ b/server/src/game/populateGetRoundInfo.go @@ -0,0 +1,51 @@ +package game + +import ( + "fmt" + "sirlab.de/go/knyt/syncdata" +) + +func (gm *Game) populateGetRoundInfo() *syncdata.RoundInfo { + if gm.state != STATE_PLAY { + return nil + } + + quote := gm.quotes[gm.round.quoteId] + if quote == nil { + fmt.Printf("fatal error: quote id \"%s\" not found\n", gm.round.quoteId) + return nil + } + + sources := make([]syncdata.SourceInfo, 0) + for _, source := range gm.round.sources { + sources = append(sources, syncdata.SourceInfo{ + Id: source.id, + Name: source.name, + }) + } + + roundInfo := syncdata.RoundInfo{ + Quote: quote.GetQuote(), + Sources: sources, + Selections: gm.getSelections(), + } + + return &roundInfo +} + +func (gm *Game) getSelections() map[string]bool { + selections := make(map[string]bool, 0) + for id, playerInfo := range gm.players { + if !playerInfo.isPlaying { + continue + } + + if len(gm.round.selections[id]) > 0 { + selections[id] = true + } else if !playerInfo.isIdle { + selections[id] = false + } + } + + return selections +} diff --git a/server/src/game/populateSyncDataCb.go b/server/src/game/populateSyncDataCb.go index 1fe04d1..9355fac 100644 --- a/server/src/game/populateSyncDataCb.go +++ b/server/src/game/populateSyncDataCb.go @@ -1,9 +1,7 @@ package game import ( - "fmt" "sirlab.de/go/knyt/syncdata" - "sort" ) func (gm *Game) populateSyncDataCb(syncData *syncdata.SyncData) { @@ -18,71 +16,7 @@ func (gm *Game) populateGameInfo() syncdata.GameInfo { GameId: gm.id, State: gm.state, Phase: gm.phase, - Players: gm.getPlayers(), - Round: gm.getRoundInfo(), + Players: gm.populateGetPlayers(), + Round: gm.populateGetRoundInfo(), } } - -func (gm *Game) getPlayers() []syncdata.PlayerInfo { - players := make([]syncdata.PlayerInfo, 0) - for _, p := range gm.players { - if p.isPlaying { - players = append(players, syncdata.PlayerInfo{ - Id: p.id, - Name: p.name, - IsIdle: p.isIdle, - }) - } - } - - sort.Slice(players, func(a, b int) bool { - return players[a].Name < players[b].Name - }) - - return players -} - -func (gm *Game) getRoundInfo() *syncdata.RoundInfo { - if gm.state != STATE_PLAY { - return nil - } - - quote := gm.quotes[gm.round.quoteId] - if quote == nil { - fmt.Printf("fatal error: quote id \"%s\" not found\n", gm.round.quoteId) - return nil - } - - sources := make([]syncdata.SourceInfo, 0) - for _, source := range gm.round.sources { - sources = append(sources, syncdata.SourceInfo{ - Id: source.id, - Name: source.name, - }) - } - - roundInfo := syncdata.RoundInfo{ - Quote: quote.GetQuote(), - Sources: sources, - Selections: gm.getSelections(), - } - - return &roundInfo -} - -func (gm *Game) getSelections() map[string]bool { - selections := make(map[string]bool, 0) - for id, playerInfo := range gm.players { - if !playerInfo.isPlaying { - continue - } - - if len(gm.round.selections[id]) > 0 { - selections[id] = true - } else if !playerInfo.isIdle { - selections[id] = false - } - } - - return selections -}