refactoring

This commit is contained in:
Settel 2021-09-06 23:18:34 +02:00
parent 38810210ba
commit e5d13e1ff0
8 changed files with 79 additions and 48 deletions

View File

@ -0,0 +1,11 @@
package application
func (app *Application) addUsersToGames() {
for _, usr := range app.users {
if gm, err := app.GetGameById(usr.GetGameId()); err != nil {
continue
} else {
gm.AddPlayer(usr)
}
}
}

View File

@ -8,7 +8,7 @@ import (
) )
func NewApplication(config applicationConfig.ApplicationConfig) (*Application, error) { func NewApplication(config applicationConfig.ApplicationConfig) (*Application, error) {
app := Application{ app := &Application{
config: config, config: config,
users: make(map[string]*user.User), users: make(map[string]*user.User),
games: make(map[string]*game.Game), games: make(map[string]*game.Game),
@ -21,8 +21,9 @@ func NewApplication(config applicationConfig.ApplicationConfig) (*Application, e
if err := app.loadGames(); err != nil { if err := app.loadGames(); err != nil {
return nil, err return nil, err
} }
app.addUsersToGames()
go app.expireInactivePlayersThread() go app.expireInactivePlayersThread()
return &app, nil return app, nil
} }

View File

@ -47,3 +47,22 @@ func (gm *Game) EventPlayerLeaves(usr *user.User) {
gm.eng.Update() gm.eng.Update()
} }
func (gm *Game) AddPlayer(usr *user.User) {
usrId := usr.GetId()
gm.mu.Lock()
defer gm.mu.Unlock()
player := gm.players[usrId]
if player.id == usrId {
return
}
gm.players[usrId] = playerInfo{
id: usrId,
name: usr.GetName(),
isPlaying: false,
isIdle: true,
}
}

View File

@ -14,9 +14,8 @@ func (gm *Game) revealSource() {
for _, source := range gm.round.sources { for _, source := range gm.round.sources {
gm.round.revelation.sources[source.id] = false gm.round.revelation.sources[source.id] = false
} }
gm.round.revelation.sources["f17065b7-88e7-4777-bc48-15770a5b7c83"] = true quote := gm.quotes[gm.round.quoteId]
// quote := gm.quotes[gm.round.quoteId] gm.round.revelation.sources[quote.GetSourceId()] = true
// gm.round.revelation.sources[quote.GetSourceId()] = true
gm.mu.Unlock() gm.mu.Unlock()
gm.notifyClients() gm.notifyClients()

View File

@ -18,38 +18,5 @@ func (gm *Game) runRound() {
} }
gm.setupRound() gm.setupRound()
gm.selectQuote()
gm.notifyClients() gm.notifyClients()
} }
func (gm *Game) setupRound() {
gm.mu.Lock()
defer gm.mu.Unlock()
gm.round = Round{
selections: make(map[string]string, 0),
revelation: Revelation{
votes: make(map[string][]string, 0),
sources: make(map[string]bool, 0),
},
}
}
func (gm *Game) selectQuote() {
gm.mu.Lock()
defer gm.mu.Unlock()
gm.round.quoteId = "455df6bc-070d-4728-83ab-481ceafa8590"
gm.round.sources = []Source{
{id: "cbc34770-3686-45ee-93bd-c5521e276e21", name: "Herbert Grönemeyer"},
{id: "f0422e15-59c7-480b-adea-9e54f8471f02", name: "Frank Sinatra"},
{id: "22915112-836e-4a11-b66c-a38a0e0f87b2", name: "Gaius Julius Cäsar"},
{id: "f17065b7-88e7-4777-bc48-15770a5b7c83", name: "Thomas Alva Edison"},
{id: "b6b3cd30-1d52-4e62-a0bd-bf3847c5c396", name: "Konfuzius"},
{id: "c3fc2091-ae0e-433d-b4c3-215a5b57b2b7", name: "George W. Bush jun."},
{id: "49514b62-96cf-4ee0-a59a-154c23b7df53", name: "Plato"},
{id: "7545ee0f-0447-4c15-adc2-87d85304aeea", name: "Christoph Kolumbus"},
{id: "dc20b5f1-23bc-4d80-ab2e-f3caa005064a", name: "Neil Armstrong"},
{id: "2b9fbc3c-589f-4176-8708-cc8239e19a4f", name: "Max Planck"},
}
}

View File

@ -0,0 +1,34 @@
package game
func (gm *Game) setupRound() {
gm.round = newRound()
gm.selectQuote()
}
func newRound() Round {
return Round{
selections: make(map[string]string, 0),
revelation: Revelation{
votes: make(map[string][]string, 0),
sources: make(map[string]bool, 0),
},
}
}
func (gm *Game) selectQuote() {
gm.mu.Lock()
defer gm.mu.Unlock()
quote := gm.quotes["455df6bc-070d-4728-83ab-481ceafa8590"]
gm.round.quoteId = quote.GetId()
gm.round.sources = []Source{
gm.getSourceById(quote.GetSourceId()),
}
}
func (gm *Game) getSourceById(id string) Source {
return Source{
id: id,
name: "Herbert",
}
}

View File

@ -22,7 +22,7 @@ func NewQuoteFromFile(fileName string) (*Quote, error) {
id := strings.TrimSuffix(fileNameShort, ".json") id := strings.TrimSuffix(fileNameShort, ".json")
qu := &Quote{ qu := &Quote{
id: id, id: id,
source: quJson.Source, sourceId: quJson.SourceId,
quote: quJson.Quote, quote: quJson.Quote,
} }
@ -34,8 +34,8 @@ func (qu *Quote) GetId() string {
return qu.id return qu.id
} }
func (qu *Quote) GetSource() string { func (qu *Quote) GetSourceId() string {
return qu.source return qu.sourceId
} }
func (qu *Quote) GetQuote() string { func (qu *Quote) GetQuote() string {

View File

@ -2,11 +2,11 @@ package quote
type Quote struct { type Quote struct {
id string id string
source string sourceId string
quote string quote string
} }
type QuoteJson struct { type QuoteJson struct {
Quote string `json:"quote"` Quote string `json:"quote"`
Source string `json:"source"` SourceId string `json:"source"`
} }