start game

This commit is contained in:
Settel 2021-08-13 01:07:06 +02:00
parent 46a8f8ef84
commit d99e052234
7 changed files with 67 additions and 24 deletions

View File

@ -0,0 +1,25 @@
package application
import (
"fmt"
"net/http"
"sirlab.de/go/knyt/user"
)
func (app *Application) StartGame(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.IsGamemaster() {
w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "forbidden")
return
}
gm.StartGame()
}

View File

@ -1,7 +1,6 @@
package engine
import (
"fmt"
"github.com/imkira/go-observer"
"sirlab.de/go/knyt/syncdata"
"time"
@ -33,20 +32,3 @@ func (eng *Engine) Run(populateSyncDataCb PopulateSyncDataCb) {
func (eng *Engine) Update() {
eng.notify <- true
}
func (eng *Engine) publish(populateSyncDataCb PopulateSyncDataCb) {
eng.mu.Lock()
defer eng.mu.Unlock()
eng.versionRef++
data := syncdata.SyncData{
VersionRef: eng.versionRef,
}
if populateSyncDataCb != nil {
populateSyncDataCb(&data)
}
fmt.Printf("engine versionRef %d\n", eng.versionRef)
eng.obs.Update(data)
}

View File

@ -0,0 +1,23 @@
package engine
import (
"fmt"
"sirlab.de/go/knyt/syncdata"
)
func (eng *Engine) publish(populateSyncDataCb PopulateSyncDataCb) {
eng.mu.Lock()
defer eng.mu.Unlock()
eng.versionRef++
data := syncdata.SyncData{
VersionRef: eng.versionRef,
}
if populateSyncDataCb != nil {
populateSyncDataCb(&data)
}
fmt.Printf("engine versionRef %d\n", eng.versionRef)
eng.obs.Update(data)
}

View File

@ -5,16 +5,13 @@ import (
)
func (gm *Game) populateSyncDataCb(syncData *syncdata.SyncData) {
gm.populatePlayers(syncData)
syncData.GameInfo = gm.populateGameInfo()
}
func (gm *Game) populatePlayers(syncData *syncdata.SyncData) {
func (gm *Game) populateGameInfo() syncdata.GameInfo {
gm.mu.Lock()
defer gm.mu.Unlock()
syncData.GameInfo.GameId = gm.id
syncData.GameInfo.State = gm.state
players := make([]syncdata.PlayerInfo, 0)
for _, p := range gm.players {
if p.isPlaying {
@ -25,5 +22,10 @@ func (gm *Game) populatePlayers(syncData *syncdata.SyncData) {
})
}
}
syncData.GameInfo.Players = players
return syncdata.GameInfo{
GameId: gm.id,
State: gm.state,
Players: players,
}
}

View File

@ -0,0 +1,9 @@
package game
func (gm *Game) StartGame() {
gm.mu.Lock()
defer gm.mu.Unlock()
gm.state = STATE_PLAY
gm.eng.Update()
}

View File

@ -8,6 +8,7 @@ import (
const (
STATE_IDLE = "idle"
STATE_ENTER_STATEMENTS = "enter-statements"
STATE_PLAY = "play"
)
type playerInfo struct {

View File

@ -23,6 +23,7 @@ func main() {
mux.PublicHandleFunc("/api/logout", mux.Logout)
mux.PrivateHandleFunc("/api/userinfo", mux.GetUserInfo)
mux.PrivateHandleFunc("/api/sync", app.SyncHandler)
mux.PrivateHandleFunc("/api/startGame", app.StartGame)
// default handler
fsHandler := http.FileServer(http.Dir("../../client/dist/"))