gameinfo
This commit is contained in:
parent
00b1925969
commit
ce46b6f027
5
server/data/users/123123.json
Normal file
5
server/data/users/123123.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "Master",
|
||||
"role": "gamemaster",
|
||||
"game": "067fb1b8-8303-4faa-95d2-1832770a791c"
|
||||
}
|
@ -21,6 +21,8 @@ func (app *Application) SyncHandler(usr *user.User, w http.ResponseWriter, r *ht
|
||||
return
|
||||
}
|
||||
|
||||
gm.UpdatePlayerTimestamp(usr)
|
||||
|
||||
eng := gm.GetEngine()
|
||||
eng.SyncHandler(w, r)
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ func (engine *Engine) Run() {
|
||||
for {
|
||||
value := engine.obs.Value().(syncdata.SyncData)
|
||||
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++ {
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
|
@ -5,6 +5,8 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sirlab.de/go/knyt/engine"
|
||||
"sirlab.de/go/knyt/user"
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewGameFromFile(id, fileName string) (*Game, error) {
|
||||
@ -19,7 +21,10 @@ func NewGameFromFile(id, fileName string) (*Game, error) {
|
||||
} else {
|
||||
gm.id = id
|
||||
gm.eng = engine.NewEngine(id)
|
||||
gm.playerTimestamp = make(map[string]time.Time)
|
||||
|
||||
go gm.eng.Run()
|
||||
|
||||
return &gm, nil
|
||||
}
|
||||
}
|
||||
@ -31,3 +36,24 @@ func (gm *Game) GetId() string {
|
||||
func (gm *Game) GetEngine() *engine.Engine {
|
||||
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
|
||||
}
|
||||
|
@ -2,10 +2,12 @@ package game
|
||||
|
||||
import (
|
||||
"sirlab.de/go/knyt/engine"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Game struct {
|
||||
id string
|
||||
Name string `json:"name"`
|
||||
eng *engine.Engine
|
||||
playerTimestamp map[string]time.Time
|
||||
}
|
||||
|
36
server/src/handler/gameinfo.go
Normal file
36
server/src/handler/gameinfo.go
Normal 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))
|
||||
}
|
@ -22,6 +22,7 @@ func main() {
|
||||
mux.PublicHandleFunc("/api/login", mux.Login)
|
||||
mux.PublicHandleFunc("/api/logout", mux.Logout)
|
||||
mux.PrivateHandleFunc("/api/userinfo", mux.GetUserInfo)
|
||||
mux.PrivateHandleFunc("/api/gameinfo", mux.GetGameInfo)
|
||||
mux.PrivateHandleFunc("/api/sync", app.SyncHandler)
|
||||
|
||||
// default handler
|
||||
|
Loading…
Reference in New Issue
Block a user