handle player's last access time
This commit is contained in:
parent
257c0975fb
commit
34132c7703
@ -4,6 +4,7 @@ import (
|
|||||||
"sirlab.de/go/knyt/applicationConfig"
|
"sirlab.de/go/knyt/applicationConfig"
|
||||||
"sirlab.de/go/knyt/game"
|
"sirlab.de/go/knyt/game"
|
||||||
"sirlab.de/go/knyt/user"
|
"sirlab.de/go/knyt/user"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewApplication(config applicationConfig.ApplicationConfig) (*Application, error) {
|
func NewApplication(config applicationConfig.ApplicationConfig) (*Application, error) {
|
||||||
@ -11,6 +12,7 @@ func NewApplication(config applicationConfig.ApplicationConfig) (*Application, e
|
|||||||
config: config,
|
config: config,
|
||||||
users: make(map[string]*user.User),
|
users: make(map[string]*user.User),
|
||||||
games: make(map[string]*game.Game),
|
games: make(map[string]*game.Game),
|
||||||
|
playerATime: make(map[string]time.Time),
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := app.loadUsers(); err != nil {
|
if err := app.loadUsers(); err != nil {
|
||||||
|
16
server/src/application/playerATime.go
Normal file
16
server/src/application/playerATime.go
Normal file
@ -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()
|
||||||
|
}
|
@ -5,6 +5,7 @@ import (
|
|||||||
"sirlab.de/go/knyt/game"
|
"sirlab.de/go/knyt/game"
|
||||||
"sirlab.de/go/knyt/user"
|
"sirlab.de/go/knyt/user"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Application struct {
|
type Application struct {
|
||||||
@ -12,4 +13,5 @@ type Application struct {
|
|||||||
config applicationConfig.ApplicationConfig
|
config applicationConfig.ApplicationConfig
|
||||||
users map[string]*user.User
|
users map[string]*user.User
|
||||||
games map[string]*game.Game
|
games map[string]*game.Game
|
||||||
|
playerATime map[string]time.Time
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ func (app *Application) SyncHandler(usr *user.User, w http.ResponseWriter, r *ht
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
gm.UpdatePlayerTimestamp(usr)
|
app.updatePlayerATime(usr)
|
||||||
|
|
||||||
eng := gm.GetEngine()
|
eng := gm.GetEngine()
|
||||||
eng.SyncHandler(w, r)
|
eng.SyncHandler(w, r)
|
||||||
|
@ -5,8 +5,6 @@ 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) {
|
||||||
@ -23,7 +21,6 @@ func NewGameFromFile(id, fileName string) (*Game, error) {
|
|||||||
id: id,
|
id: id,
|
||||||
name: gmJson.Name,
|
name: gmJson.Name,
|
||||||
eng: engine.NewEngine(id),
|
eng: engine.NewEngine(id),
|
||||||
playerTimestamp: make(map[string]time.Time),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
go gm.eng.Run()
|
go gm.eng.Run()
|
||||||
@ -33,36 +30,42 @@ func NewGameFromFile(id, fileName string) (*Game, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (gm *Game) GetId() string {
|
func (gm *Game) GetId() string {
|
||||||
|
gm.mu.Lock()
|
||||||
|
defer gm.mu.Unlock()
|
||||||
|
|
||||||
return gm.id
|
return gm.id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (gm *Game) GetName() string {
|
||||||
|
gm.mu.Lock()
|
||||||
|
defer gm.mu.Unlock()
|
||||||
|
|
||||||
|
return gm.name
|
||||||
|
}
|
||||||
|
|
||||||
func (gm *Game) GetEngine() *engine.Engine {
|
func (gm *Game) GetEngine() *engine.Engine {
|
||||||
|
gm.mu.Lock()
|
||||||
|
defer gm.mu.Unlock()
|
||||||
|
|
||||||
return gm.eng
|
return gm.eng
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gm *Game) UpdatePlayerTimestamp(usr *user.User) {
|
// func (gm *Game) GetActivePlayers() []string {
|
||||||
gm.mu.Lock()
|
// gm.mu.Lock()
|
||||||
defer gm.mu.Unlock()
|
// defer gm.mu.Unlock()
|
||||||
|
//
|
||||||
gm.playerTimestamp[usr.GetId()] = time.Now()
|
// players := make([]string, 0)
|
||||||
}
|
//
|
||||||
|
// now := time.Now()
|
||||||
func (gm *Game) GetActivePlayers() []string {
|
// for usrId, timestamp := range gm.playerTimestamp {
|
||||||
gm.mu.Lock()
|
// elapsed := now.Sub(timestamp)
|
||||||
defer gm.mu.Unlock()
|
//
|
||||||
|
// fmt.Printf("%s: %.0f\n", usrId, elapsed.Seconds())
|
||||||
players := make([]string, 0)
|
//
|
||||||
|
// if elapsed.Seconds() < 30.0 {
|
||||||
now := time.Now()
|
// players = append(players, usrId)
|
||||||
for usrId, timestamp := range gm.playerTimestamp {
|
// }
|
||||||
elapsed := now.Sub(timestamp)
|
// }
|
||||||
|
//
|
||||||
fmt.Printf("%s: %.0f\n", usrId, elapsed.Seconds())
|
// return players
|
||||||
|
// }
|
||||||
if elapsed.Seconds() < 30.0 {
|
|
||||||
players = append(players, usrId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return players
|
|
||||||
}
|
|
||||||
|
@ -3,7 +3,6 @@ package game
|
|||||||
import (
|
import (
|
||||||
"sirlab.de/go/knyt/engine"
|
"sirlab.de/go/knyt/engine"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Game struct {
|
type Game struct {
|
||||||
@ -11,7 +10,6 @@ type Game struct {
|
|||||||
id string
|
id string
|
||||||
name string
|
name string
|
||||||
eng *engine.Engine
|
eng *engine.Engine
|
||||||
playerTimestamp map[string]time.Time
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type GameJson struct {
|
type GameJson struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user