start game
This commit is contained in:
parent
46a8f8ef84
commit
d99e052234
25
server/src/application/startGame.go
Normal file
25
server/src/application/startGame.go
Normal 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()
|
||||||
|
}
|
@ -1,7 +1,6 @@
|
|||||||
package engine
|
package engine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/imkira/go-observer"
|
"github.com/imkira/go-observer"
|
||||||
"sirlab.de/go/knyt/syncdata"
|
"sirlab.de/go/knyt/syncdata"
|
||||||
"time"
|
"time"
|
||||||
@ -33,20 +32,3 @@ func (eng *Engine) Run(populateSyncDataCb PopulateSyncDataCb) {
|
|||||||
func (eng *Engine) Update() {
|
func (eng *Engine) Update() {
|
||||||
eng.notify <- true
|
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)
|
|
||||||
}
|
|
||||||
|
23
server/src/engine/publish.go
Normal file
23
server/src/engine/publish.go
Normal 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)
|
||||||
|
}
|
@ -5,16 +5,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (gm *Game) populateSyncDataCb(syncData *syncdata.SyncData) {
|
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()
|
gm.mu.Lock()
|
||||||
defer gm.mu.Unlock()
|
defer gm.mu.Unlock()
|
||||||
|
|
||||||
syncData.GameInfo.GameId = gm.id
|
|
||||||
syncData.GameInfo.State = gm.state
|
|
||||||
|
|
||||||
players := make([]syncdata.PlayerInfo, 0)
|
players := make([]syncdata.PlayerInfo, 0)
|
||||||
for _, p := range gm.players {
|
for _, p := range gm.players {
|
||||||
if p.isPlaying {
|
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,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
9
server/src/game/startGame.go
Normal file
9
server/src/game/startGame.go
Normal 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()
|
||||||
|
}
|
@ -8,6 +8,7 @@ import (
|
|||||||
const (
|
const (
|
||||||
STATE_IDLE = "idle"
|
STATE_IDLE = "idle"
|
||||||
STATE_ENTER_STATEMENTS = "enter-statements"
|
STATE_ENTER_STATEMENTS = "enter-statements"
|
||||||
|
STATE_PLAY = "play"
|
||||||
)
|
)
|
||||||
|
|
||||||
type playerInfo struct {
|
type playerInfo struct {
|
||||||
|
@ -23,6 +23,7 @@ func main() {
|
|||||||
mux.PublicHandleFunc("/api/logout", mux.Logout)
|
mux.PublicHandleFunc("/api/logout", mux.Logout)
|
||||||
mux.PrivateHandleFunc("/api/userinfo", mux.GetUserInfo)
|
mux.PrivateHandleFunc("/api/userinfo", mux.GetUserInfo)
|
||||||
mux.PrivateHandleFunc("/api/sync", app.SyncHandler)
|
mux.PrivateHandleFunc("/api/sync", app.SyncHandler)
|
||||||
|
mux.PrivateHandleFunc("/api/startGame", app.StartGame)
|
||||||
|
|
||||||
// default handler
|
// default handler
|
||||||
fsHandler := http.FileServer(http.Dir("../../client/dist/"))
|
fsHandler := http.FileServer(http.Dir("../../client/dist/"))
|
||||||
|
Loading…
Reference in New Issue
Block a user