run multiple games in parallel
This commit is contained in:
parent
105a063acf
commit
0b8cbd154b
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"players": [
|
||||||
|
]
|
||||||
|
}
|
5
server/data/users/985466.json
Normal file
5
server/data/users/985466.json
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"name": "Player #3",
|
||||||
|
"role": "player",
|
||||||
|
"game": "26be8ee5-090d-48f7-845c-f829e668a7d2"
|
||||||
|
}
|
14
server/src/application/getGame.go
Normal file
14
server/src/application/getGame.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package application
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sirlab.de/go/knyt/game"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (app Application) GetGameById(id string) (*game.Game, error) {
|
||||||
|
gm := app.games[id]
|
||||||
|
if gm == nil {
|
||||||
|
return nil, fmt.Errorf("unknown id")
|
||||||
|
}
|
||||||
|
return gm, nil
|
||||||
|
}
|
19
server/src/application/syncHandler.go
Normal file
19
server/src/application/syncHandler.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package application
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (app *Application) SyncHandler(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
|
||||||
|
}
|
||||||
|
|
||||||
|
eng := gm.GetEngine()
|
||||||
|
eng.SyncHandler(w, r)
|
||||||
|
}
|
@ -3,6 +3,7 @@ package engine
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/imkira/go-observer"
|
"github.com/imkira/go-observer"
|
||||||
|
"math/rand"
|
||||||
"sirlab.de/go/knyt/syncdata"
|
"sirlab.de/go/knyt/syncdata"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -16,10 +17,14 @@ func NewEngine(id string) *Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) Run() {
|
func (engine *Engine) Run() {
|
||||||
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
for {
|
for {
|
||||||
value := engine.obs.Value().(syncdata.SyncData)
|
value := engine.obs.Value().(syncdata.SyncData)
|
||||||
fmt.Printf("sleep: %d\n", value.VersionRef)
|
fmt.Printf("game %s: %d\n", engine.id, value.VersionRef)
|
||||||
time.Sleep(2 * time.Second)
|
wait := int(1 + r.Float32()*2)
|
||||||
|
for i := 0; i < wait; i++ {
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
value.VersionRef++
|
value.VersionRef++
|
||||||
engine.obs.Update(value)
|
engine.obs.Update(value)
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (eng *Engine) GetHttpHandler() HandleFunc {
|
func (eng *Engine) SyncHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
versionRef, err := strconv.Atoi(r.URL.Query().Get("v"))
|
versionRef, err := strconv.Atoi(r.URL.Query().Get("v"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
versionRef = -1
|
versionRef = -1
|
||||||
@ -33,4 +32,3 @@ func (eng *Engine) GetHttpHandler() HandleFunc {
|
|||||||
w.Header().Add("Content-Type", "application/json")
|
w.Header().Add("Content-Type", "application/json")
|
||||||
fmt.Fprintf(w, string(jsonString))
|
fmt.Fprintf(w, string(jsonString))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
@ -19,6 +19,7 @@ func NewGameFromFile(id, fileName string) (*Game, error) {
|
|||||||
} else {
|
} else {
|
||||||
gm.id = id
|
gm.id = id
|
||||||
gm.eng = engine.NewEngine(id)
|
gm.eng = engine.NewEngine(id)
|
||||||
|
go gm.eng.Run()
|
||||||
return &gm, nil
|
return &gm, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -26,3 +27,7 @@ func NewGameFromFile(id, fileName string) (*Game, error) {
|
|||||||
func (gm *Game) GetId() string {
|
func (gm *Game) GetId() string {
|
||||||
return gm.id
|
return gm.id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (gm *Game) GetEngine() *engine.Engine {
|
||||||
|
return gm.eng
|
||||||
|
}
|
||||||
|
@ -22,6 +22,7 @@ func main() {
|
|||||||
mux.PublicHandleFunc("/api/login", mux.Login)
|
mux.PublicHandleFunc("/api/login", mux.Login)
|
||||||
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)
|
||||||
|
|
||||||
// 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