set selection

This commit is contained in:
Settel 2021-08-30 10:55:07 +02:00
parent adfc3c0a1a
commit 597aa65834
7 changed files with 72 additions and 4 deletions

View File

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

View File

@ -22,7 +22,7 @@ func NewGameFromFile(id, fileName string) (*Game, error) {
id: id, id: id,
name: gmJson.Name, name: gmJson.Name,
eng: engine.NewEngine(), eng: engine.NewEngine(),
players: make(map[string]playerInfo), players: make(map[string]playerInfo, 0),
state: STATE_IDLE, state: STATE_IDLE,
quotes: make(map[string]*quote.Quote, 0), quotes: make(map[string]*quote.Quote, 0),
} }

View File

@ -17,10 +17,18 @@ func (gm *Game) runRound() {
return return
} }
gm.setupRound()
gm.selectQuote() gm.selectQuote()
gm.notifyClients() 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() { func (gm *Game) selectQuote() {
gm.mu.Lock() gm.mu.Lock()
defer gm.mu.Unlock() defer gm.mu.Unlock()
@ -34,7 +42,7 @@ func (gm *Game) selectQuote() {
{id: "b6b3cd30-1d52-4e62-a0bd-bf3847c5c396", name: "Konfuzius"}, {id: "b6b3cd30-1d52-4e62-a0bd-bf3847c5c396", name: "Konfuzius"},
{id: "c3fc2091-ae0e-433d-b4c3-215a5b57b2b7", name: "George W. Bush jun."}, {id: "c3fc2091-ae0e-433d-b4c3-215a5b57b2b7", name: "George W. Bush jun."},
{id: "49514b62-96cf-4ee0-a59a-154c23b7df53", name: "Plato"}, {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: "dc20b5f1-23bc-4d80-ab2e-f3caa005064a", name: "Neil Armstrong"},
{id: "2b9fbc3c-589f-4176-8708-cc8239e19a4f", name: "Max Planck"}, {id: "2b9fbc3c-589f-4176-8708-cc8239e19a4f", name: "Max Planck"},
} }

View File

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

View File

@ -15,4 +15,6 @@ func (gm *Game) ResetGame() {
fmt.Println(err) fmt.Println(err)
return return
} }
gm.notifyClients()
} }

View File

@ -31,8 +31,9 @@ type Source struct {
} }
type Round struct { type Round struct {
quoteId string quoteId string
sources []Source selection map[string]string
sources []Source
} }
type Game struct { type Game struct {

View File

@ -25,6 +25,7 @@ func main() {
mux.PrivateHandleFunc("/api/sync", app.SyncHandler) mux.PrivateHandleFunc("/api/sync", app.SyncHandler)
mux.PrivateHandleFunc("/api/startGame", app.StartGame) mux.PrivateHandleFunc("/api/startGame", app.StartGame)
mux.PrivateHandleFunc("/api/resetGame", app.ResetGame) mux.PrivateHandleFunc("/api/resetGame", app.ResetGame)
mux.PrivateHandleFunc("/api/saveSelection", app.SaveSelection)
// default handler // default handler
fsHandler := http.FileServer(http.Dir("../../client/dist/")) fsHandler := http.FileServer(http.Dir("../../client/dist/"))