From 597aa65834e919f48e0e33a2db169a34f70fd2b8 Mon Sep 17 00:00:00 2001 From: Settel Date: Mon, 30 Aug 2021 10:55:07 +0200 Subject: [PATCH] set selection --- server/src/application/saveSelection.go | 34 +++++++++++++++++++++++++ server/src/game/game.go | 2 +- server/src/game/runRound.go | 10 +++++++- server/src/game/saveSelection.go | 22 ++++++++++++++++ server/src/game/startGame.go | 2 ++ server/src/game/struct.go | 5 ++-- server/src/knyt.go | 1 + 7 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 server/src/application/saveSelection.go create mode 100644 server/src/game/saveSelection.go diff --git a/server/src/application/saveSelection.go b/server/src/application/saveSelection.go new file mode 100644 index 0000000..17a04f8 --- /dev/null +++ b/server/src/application/saveSelection.go @@ -0,0 +1,34 @@ +package application + +import ( + "fmt" + "net/http" + "sirlab.de/go/knyt/user" +) + +func (app *Application) SaveSelection(usr *user.User, w http.ResponseWriter, r *http.Request) { + gameRef := r.URL.Query().Get("g") + gm, err := app.GetGameById(gameRef) + if err != nil { + w.WriteHeader(http.StatusNotFound) + fmt.Fprintf(w, "game not found") + return + } + + if usr.GetGameId() != gameRef || !usr.IsPlayer() { + w.WriteHeader(http.StatusForbidden) + fmt.Fprintf(w, "forbidden") + return + } + + selection := r.URL.Query().Get("selection") + + if err := gm.SaveSelection(usr, selection); err != nil { + w.WriteHeader(http.StatusForbidden) + fmt.Fprintf(w, "forbidden") + return + } + + fmt.Fprintf(w, "ok") + return +} diff --git a/server/src/game/game.go b/server/src/game/game.go index d2fdc5a..a9906af 100644 --- a/server/src/game/game.go +++ b/server/src/game/game.go @@ -22,7 +22,7 @@ func NewGameFromFile(id, fileName string) (*Game, error) { id: id, name: gmJson.Name, eng: engine.NewEngine(), - players: make(map[string]playerInfo), + players: make(map[string]playerInfo, 0), state: STATE_IDLE, quotes: make(map[string]*quote.Quote, 0), } diff --git a/server/src/game/runRound.go b/server/src/game/runRound.go index e51bd2f..b8f310a 100644 --- a/server/src/game/runRound.go +++ b/server/src/game/runRound.go @@ -17,10 +17,18 @@ func (gm *Game) runRound() { return } + gm.setupRound() gm.selectQuote() gm.notifyClients() } +func (gm *Game) setupRound() { + gm.mu.Lock() + defer gm.mu.Unlock() + + gm.round.selection = make(map[string]string, 0) +} + func (gm *Game) selectQuote() { gm.mu.Lock() defer gm.mu.Unlock() @@ -34,7 +42,7 @@ func (gm *Game) selectQuote() { {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: "Chrisoph Kolumbus"}, + {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/saveSelection.go b/server/src/game/saveSelection.go new file mode 100644 index 0000000..2c3d538 --- /dev/null +++ b/server/src/game/saveSelection.go @@ -0,0 +1,22 @@ +package game + +import ( + "fmt" + "sirlab.de/go/knyt/user" +) + +func (gm *Game) SaveSelection(usr *user.User, selection string) error { + gm.mu.Lock() + defer gm.mu.Unlock() + + id := usr.GetId() + if gm.players[id].id != id { + return fmt.Errorf("invalid user") + } + + gm.round.selection[id] = selection + fmt.Printf("selections: %d\n", len(gm.round.selection)) + + fmt.Printf("SaveSelection: \"%s\"\n", selection) + return nil +} diff --git a/server/src/game/startGame.go b/server/src/game/startGame.go index b886642..9b74659 100644 --- a/server/src/game/startGame.go +++ b/server/src/game/startGame.go @@ -15,4 +15,6 @@ func (gm *Game) ResetGame() { fmt.Println(err) return } + + gm.notifyClients() } diff --git a/server/src/game/struct.go b/server/src/game/struct.go index c28a11f..6ef1797 100644 --- a/server/src/game/struct.go +++ b/server/src/game/struct.go @@ -31,8 +31,9 @@ type Source struct { } type Round struct { - quoteId string - sources []Source + quoteId string + selection map[string]string + sources []Source } type Game struct { diff --git a/server/src/knyt.go b/server/src/knyt.go index 773a5ab..4e53fff 100644 --- a/server/src/knyt.go +++ b/server/src/knyt.go @@ -25,6 +25,7 @@ func main() { mux.PrivateHandleFunc("/api/sync", app.SyncHandler) mux.PrivateHandleFunc("/api/startGame", app.StartGame) mux.PrivateHandleFunc("/api/resetGame", app.ResetGame) + mux.PrivateHandleFunc("/api/saveSelection", app.SaveSelection) // default handler fsHandler := http.FileServer(http.Dir("../../client/dist/"))