refactoring

This commit is contained in:
Settel 2021-09-05 01:18:46 +02:00
parent c67b395e5f
commit c4d7278625
3 changed files with 78 additions and 68 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -1,9 +1,7 @@
package game package game
import ( import (
"fmt"
"sirlab.de/go/knyt/syncdata" "sirlab.de/go/knyt/syncdata"
"sort"
) )
func (gm *Game) populateSyncDataCb(syncData *syncdata.SyncData) { func (gm *Game) populateSyncDataCb(syncData *syncdata.SyncData) {
@ -18,71 +16,7 @@ func (gm *Game) populateGameInfo() syncdata.GameInfo {
GameId: gm.id, GameId: gm.id,
State: gm.state, State: gm.state,
Phase: gm.phase, Phase: gm.phase,
Players: gm.getPlayers(), Players: gm.populateGetPlayers(),
Round: gm.getRoundInfo(), 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
}