track player's last access time

This commit is contained in:
Settel 2021-08-09 14:48:44 +02:00
parent f7084da78e
commit 342483d62d
3 changed files with 53 additions and 1 deletions

View File

@ -22,5 +22,7 @@ func NewApplication(config applicationConfig.ApplicationConfig) (*Application, e
return nil, err
}
go app.expireInactivePlayersThread()
return &app, nil
}

View File

@ -0,0 +1,35 @@
package application
import (
"fmt"
"time"
)
func (app *Application) expireInactivePlayersThread() {
for {
time.Sleep(5 * time.Second)
removedIds := app.expireInactivePlayers()
for _, usrId := range removedIds {
fmt.Printf("expired %s\n", usrId)
}
}
}
func (app *Application) expireInactivePlayers() []string {
now := time.Now()
removedIds := make([]string, 0)
app.mu.Lock()
defer app.mu.Unlock()
for usrId, atime := range app.playerATime {
elapsed := now.Sub(atime)
if elapsed.Seconds() > 10 {
delete(app.playerATime, usrId)
removedIds = append(removedIds, usrId)
}
}
return removedIds
}

View File

@ -10,7 +10,22 @@ func (app *Application) updatePlayerATime(usr *user.User) {
usrId := usr.GetId()
app.mu.Lock()
fmt.Printf("updatePlayerATime \"%v\"\n", app.playerATime[usrId])
prevTime := app.playerATime[usrId]
if prevTime.IsZero() {
fmt.Printf("new Player %s\n", usr.GetName())
}
app.playerATime[usrId] = time.Now()
app.mu.Unlock()
}
func (app *Application) getActivePlayerIds() []string {
app.mu.Lock()
defer app.mu.Unlock()
playerIds := make([]string, 0)
for usrId, _ := range app.playerATime {
playerIds = append(playerIds, usrId)
}
return playerIds
}