From 34132c7703494c1db2f3a33f3a21d1a5b18ba7a8 Mon Sep 17 00:00:00 2001 From: Settel Date: Mon, 9 Aug 2021 11:16:30 +0200 Subject: [PATCH] handle player's last access time --- server/src/application/application.go | 8 ++-- server/src/application/playerATime.go | 16 +++++++ server/src/application/struct.go | 10 ++-- server/src/application/syncHandler.go | 2 +- server/src/game/game.go | 67 ++++++++++++++------------- server/src/game/struct.go | 10 ++-- 6 files changed, 67 insertions(+), 46 deletions(-) create mode 100644 server/src/application/playerATime.go diff --git a/server/src/application/application.go b/server/src/application/application.go index b1e714b..afece56 100644 --- a/server/src/application/application.go +++ b/server/src/application/application.go @@ -4,13 +4,15 @@ import ( "sirlab.de/go/knyt/applicationConfig" "sirlab.de/go/knyt/game" "sirlab.de/go/knyt/user" + "time" ) func NewApplication(config applicationConfig.ApplicationConfig) (*Application, error) { app := Application{ - config: config, - users: make(map[string]*user.User), - games: make(map[string]*game.Game), + config: config, + users: make(map[string]*user.User), + games: make(map[string]*game.Game), + playerATime: make(map[string]time.Time), } if err := app.loadUsers(); err != nil { diff --git a/server/src/application/playerATime.go b/server/src/application/playerATime.go new file mode 100644 index 0000000..7ff4c5d --- /dev/null +++ b/server/src/application/playerATime.go @@ -0,0 +1,16 @@ +package application + +import ( + "fmt" + "sirlab.de/go/knyt/user" + "time" +) + +func (app *Application) updatePlayerATime(usr *user.User) { + usrId := usr.GetId() + + app.mu.Lock() + fmt.Printf("updatePlayerATime \"%v\"\n", app.playerATime[usrId]) + app.playerATime[usrId] = time.Now() + app.mu.Unlock() +} diff --git a/server/src/application/struct.go b/server/src/application/struct.go index 57804c8..5ef51d6 100644 --- a/server/src/application/struct.go +++ b/server/src/application/struct.go @@ -5,11 +5,13 @@ import ( "sirlab.de/go/knyt/game" "sirlab.de/go/knyt/user" "sync" + "time" ) type Application struct { - mu sync.Mutex - config applicationConfig.ApplicationConfig - users map[string]*user.User - games map[string]*game.Game + mu sync.Mutex + config applicationConfig.ApplicationConfig + users map[string]*user.User + games map[string]*game.Game + playerATime map[string]time.Time } diff --git a/server/src/application/syncHandler.go b/server/src/application/syncHandler.go index fe320a5..5c1f136 100644 --- a/server/src/application/syncHandler.go +++ b/server/src/application/syncHandler.go @@ -21,7 +21,7 @@ func (app *Application) SyncHandler(usr *user.User, w http.ResponseWriter, r *ht return } - gm.UpdatePlayerTimestamp(usr) + app.updatePlayerATime(usr) eng := gm.GetEngine() eng.SyncHandler(w, r) diff --git a/server/src/game/game.go b/server/src/game/game.go index f6e6b48..29b510b 100644 --- a/server/src/game/game.go +++ b/server/src/game/game.go @@ -5,8 +5,6 @@ import ( "fmt" "os" "sirlab.de/go/knyt/engine" - "sirlab.de/go/knyt/user" - "time" ) func NewGameFromFile(id, fileName string) (*Game, error) { @@ -20,10 +18,9 @@ func NewGameFromFile(id, fileName string) (*Game, error) { return nil, fmt.Errorf("%s: %v\n", fileName, err) } else { gm := Game{ - id: id, - name: gmJson.Name, - eng: engine.NewEngine(id), - playerTimestamp: make(map[string]time.Time), + id: id, + name: gmJson.Name, + eng: engine.NewEngine(id), } go gm.eng.Run() @@ -33,36 +30,42 @@ func NewGameFromFile(id, fileName string) (*Game, error) { } func (gm *Game) GetId() string { + gm.mu.Lock() + defer gm.mu.Unlock() + return gm.id } +func (gm *Game) GetName() string { + gm.mu.Lock() + defer gm.mu.Unlock() + + return gm.name +} + func (gm *Game) GetEngine() *engine.Engine { + gm.mu.Lock() + defer gm.mu.Unlock() + return gm.eng } -func (gm *Game) UpdatePlayerTimestamp(usr *user.User) { - gm.mu.Lock() - defer gm.mu.Unlock() - - gm.playerTimestamp[usr.GetId()] = time.Now() -} - -func (gm *Game) GetActivePlayers() []string { - gm.mu.Lock() - defer gm.mu.Unlock() - - 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 -} +// func (gm *Game) GetActivePlayers() []string { +// gm.mu.Lock() +// defer gm.mu.Unlock() +// +// 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 +// } diff --git a/server/src/game/struct.go b/server/src/game/struct.go index c10b15e..1234a11 100644 --- a/server/src/game/struct.go +++ b/server/src/game/struct.go @@ -3,15 +3,13 @@ package game import ( "sirlab.de/go/knyt/engine" "sync" - "time" ) type Game struct { - mu sync.Mutex - id string - name string - eng *engine.Engine - playerTimestamp map[string]time.Time + mu sync.Mutex + id string + name string + eng *engine.Engine } type GameJson struct {