track player's last access time
This commit is contained in:
parent
f7084da78e
commit
342483d62d
@ -22,5 +22,7 @@ func NewApplication(config applicationConfig.ApplicationConfig) (*Application, e
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go app.expireInactivePlayersThread()
|
||||||
|
|
||||||
return &app, nil
|
return &app, nil
|
||||||
}
|
}
|
||||||
|
35
server/src/application/expireInactivePlayers.go
Normal file
35
server/src/application/expireInactivePlayers.go
Normal 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
|
||||||
|
}
|
@ -10,7 +10,22 @@ func (app *Application) updatePlayerATime(usr *user.User) {
|
|||||||
usrId := usr.GetId()
|
usrId := usr.GetId()
|
||||||
|
|
||||||
app.mu.Lock()
|
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.playerATime[usrId] = time.Now()
|
||||||
app.mu.Unlock()
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user