From e5d13e1ff022164ac3a08181ea70ae17176260e5 Mon Sep 17 00:00:00 2001 From: Settel Date: Mon, 6 Sep 2021 23:18:34 +0200 Subject: [PATCH] refactoring --- server/src/application/addUsersToGames.go | 11 ++++++++ server/src/application/application.go | 5 ++-- server/src/game/player.go | 19 +++++++++++++ server/src/game/revealSource.go | 5 ++-- server/src/game/runRound.go | 33 ---------------------- server/src/game/setupRound.go | 34 +++++++++++++++++++++++ server/src/quote/quote.go | 10 +++---- server/src/quote/struct.go | 10 +++---- 8 files changed, 79 insertions(+), 48 deletions(-) create mode 100644 server/src/application/addUsersToGames.go create mode 100644 server/src/game/setupRound.go diff --git a/server/src/application/addUsersToGames.go b/server/src/application/addUsersToGames.go new file mode 100644 index 0000000..2dc71ef --- /dev/null +++ b/server/src/application/addUsersToGames.go @@ -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) + } + } +} diff --git a/server/src/application/application.go b/server/src/application/application.go index 3c34942..bb4ebd8 100644 --- a/server/src/application/application.go +++ b/server/src/application/application.go @@ -8,7 +8,7 @@ import ( ) func NewApplication(config applicationConfig.ApplicationConfig) (*Application, error) { - app := Application{ + app := &Application{ config: config, users: make(map[string]*user.User), games: make(map[string]*game.Game), @@ -21,8 +21,9 @@ func NewApplication(config applicationConfig.ApplicationConfig) (*Application, e if err := app.loadGames(); err != nil { return nil, err } + app.addUsersToGames() go app.expireInactivePlayersThread() - return &app, nil + return app, nil } diff --git a/server/src/game/player.go b/server/src/game/player.go index 48cefff..b0167b4 100644 --- a/server/src/game/player.go +++ b/server/src/game/player.go @@ -47,3 +47,22 @@ func (gm *Game) EventPlayerLeaves(usr *user.User) { 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, + } +} diff --git a/server/src/game/revealSource.go b/server/src/game/revealSource.go index c8e8b42..827440d 100644 --- a/server/src/game/revealSource.go +++ b/server/src/game/revealSource.go @@ -14,9 +14,8 @@ func (gm *Game) revealSource() { for _, source := range gm.round.sources { gm.round.revelation.sources[source.id] = false } - gm.round.revelation.sources["f17065b7-88e7-4777-bc48-15770a5b7c83"] = true - // quote := gm.quotes[gm.round.quoteId] - // gm.round.revelation.sources[quote.GetSourceId()] = true + quote := gm.quotes[gm.round.quoteId] + gm.round.revelation.sources[quote.GetSourceId()] = true gm.mu.Unlock() gm.notifyClients() diff --git a/server/src/game/runRound.go b/server/src/game/runRound.go index 8922261..91319e7 100644 --- a/server/src/game/runRound.go +++ b/server/src/game/runRound.go @@ -18,38 +18,5 @@ func (gm *Game) runRound() { } gm.setupRound() - gm.selectQuote() 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"}, - } -} diff --git a/server/src/game/setupRound.go b/server/src/game/setupRound.go new file mode 100644 index 0000000..1317860 --- /dev/null +++ b/server/src/game/setupRound.go @@ -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", + } +} diff --git a/server/src/quote/quote.go b/server/src/quote/quote.go index ae2e493..d8bf225 100644 --- a/server/src/quote/quote.go +++ b/server/src/quote/quote.go @@ -21,9 +21,9 @@ func NewQuoteFromFile(fileName string) (*Quote, error) { _, fileNameShort := path.Split(fileName) id := strings.TrimSuffix(fileNameShort, ".json") qu := &Quote{ - id: id, - source: quJson.Source, - quote: quJson.Quote, + id: id, + sourceId: quJson.SourceId, + quote: quJson.Quote, } return qu, nil @@ -34,8 +34,8 @@ func (qu *Quote) GetId() string { return qu.id } -func (qu *Quote) GetSource() string { - return qu.source +func (qu *Quote) GetSourceId() string { + return qu.sourceId } func (qu *Quote) GetQuote() string { diff --git a/server/src/quote/struct.go b/server/src/quote/struct.go index ca45891..89e5fb6 100644 --- a/server/src/quote/struct.go +++ b/server/src/quote/struct.go @@ -1,12 +1,12 @@ package quote type Quote struct { - id string - source string - quote string + id string + sourceId string + quote string } type QuoteJson struct { - Quote string `json:"quote"` - Source string `json:"source"` + Quote string `json:"quote"` + SourceId string `json:"source"` }