This commit is contained in:
Settel 2021-08-06 21:46:00 +02:00
parent 00b1925969
commit ce46b6f027
7 changed files with 76 additions and 4 deletions

View File

@ -0,0 +1,5 @@
{
"name": "Master",
"role": "gamemaster",
"game": "067fb1b8-8303-4faa-95d2-1832770a791c"
}

View File

@ -21,6 +21,8 @@ func (app *Application) SyncHandler(usr *user.User, w http.ResponseWriter, r *ht
return return
} }
gm.UpdatePlayerTimestamp(usr)
eng := gm.GetEngine() eng := gm.GetEngine()
eng.SyncHandler(w, r) eng.SyncHandler(w, r)
} }

View File

@ -21,7 +21,7 @@ func (engine *Engine) Run() {
for { for {
value := engine.obs.Value().(syncdata.SyncData) value := engine.obs.Value().(syncdata.SyncData)
fmt.Printf("game %s: %d\n", engine.id, value.VersionRef) fmt.Printf("game %s: %d\n", engine.id, value.VersionRef)
wait := int(1 + r.Float32()*2) wait := int(1 + r.Float32()*5)
for i := 0; i < wait; i++ { for i := 0; i < wait; i++ {
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
} }

View File

@ -5,6 +5,8 @@ import (
"fmt" "fmt"
"os" "os"
"sirlab.de/go/knyt/engine" "sirlab.de/go/knyt/engine"
"sirlab.de/go/knyt/user"
"time"
) )
func NewGameFromFile(id, fileName string) (*Game, error) { func NewGameFromFile(id, fileName string) (*Game, error) {
@ -19,7 +21,10 @@ 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)
gm.playerTimestamp = make(map[string]time.Time)
go gm.eng.Run() go gm.eng.Run()
return &gm, nil return &gm, nil
} }
} }
@ -31,3 +36,24 @@ func (gm *Game) GetId() string {
func (gm *Game) GetEngine() *engine.Engine { func (gm *Game) GetEngine() *engine.Engine {
return gm.eng return gm.eng
} }
func (gm *Game) UpdatePlayerTimestamp(usr *user.User) {
gm.playerTimestamp[usr.GetId()] = time.Now()
}
func (gm *Game) GetActivePlayers() []string {
players := make([]string, 0)
now := time.Now()
for usrId, timestamp := range gm.playerTimestamp {
elapsed := now.Sub(timestamp)
fmt.Printf("%s: %.0f\n", usrId, elapsed.Seconds())
if elapsed.Seconds() < 30.0 {
players = append(players, usrId)
}
}
return players
}

View File

@ -2,10 +2,12 @@ package game
import ( import (
"sirlab.de/go/knyt/engine" "sirlab.de/go/knyt/engine"
"time"
) )
type Game struct { type Game struct {
id string id string
Name string `json:"name"` Name string `json:"name"`
eng *engine.Engine eng *engine.Engine
playerTimestamp map[string]time.Time
} }

View File

@ -0,0 +1,36 @@
package handler
import (
"encoding/json"
"fmt"
"net/http"
"sirlab.de/go/knyt/user"
)
type gameLight struct {
Players []string `json:"players"`
}
func (authMux *AuthMux) GetGameInfo(usr *user.User, w http.ResponseWriter, r *http.Request) {
gameRef := r.URL.Query().Get("g")
gm, err := authMux.app.GetGameById(gameRef)
if err != nil {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "game not found")
return
}
if !usr.IsGamemaster() || (usr.Game != gameRef && !usr.IsAdmin()) {
w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "forbidden")
return
}
gmLight := gameLight{
Players: gm.GetActivePlayers(),
}
w.Header().Add("Content-Type", "application/json")
jsonString, _ := json.Marshal(gmLight)
fmt.Fprintf(w, string(jsonString))
}

View File

@ -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/gameinfo", mux.GetGameInfo)
mux.PrivateHandleFunc("/api/sync", app.SyncHandler) mux.PrivateHandleFunc("/api/sync", app.SyncHandler)
// default handler // default handler