45 lines
776 B
Go
45 lines
776 B
Go
package application
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
func (app *Application) expireInactivePlayersThread() {
|
|
for {
|
|
time.Sleep(5 * time.Second)
|
|
|
|
removedIds := app.expireInactivePlayers()
|
|
for _, usrId := range removedIds {
|
|
usr, err := app.GetUserById(usrId)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
gm, err2 := app.GetGameById(usr.GetGameId())
|
|
if err2 != nil {
|
|
continue
|
|
}
|
|
|
|
gm.EventPlayerLeaves(usr)
|
|
}
|
|
}
|
|
}
|
|
|
|
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() > 5 {
|
|
delete(app.playerATime, usrId)
|
|
removedIds = append(removedIds, usrId)
|
|
}
|
|
}
|
|
|
|
return removedIds
|
|
}
|