set selection
This commit is contained in:
parent
adfc3c0a1a
commit
597aa65834
34
server/src/application/saveSelection.go
Normal file
34
server/src/application/saveSelection.go
Normal 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
|
||||
}
|
@ -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),
|
||||
}
|
||||
|
@ -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"},
|
||||
}
|
||||
|
22
server/src/game/saveSelection.go
Normal file
22
server/src/game/saveSelection.go
Normal 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
|
||||
}
|
@ -15,4 +15,6 @@ func (gm *Game) ResetGame() {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
gm.notifyClients()
|
||||
}
|
||||
|
@ -32,6 +32,7 @@ type Source struct {
|
||||
|
||||
type Round struct {
|
||||
quoteId string
|
||||
selection map[string]string
|
||||
sources []Source
|
||||
}
|
||||
|
||||
|
@ -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/"))
|
||||
|
Loading…
Reference in New Issue
Block a user