diff --git a/server/src/application/application.go b/server/src/application/application.go index afece56..3c34942 100644 --- a/server/src/application/application.go +++ b/server/src/application/application.go @@ -22,5 +22,7 @@ func NewApplication(config applicationConfig.ApplicationConfig) (*Application, e return nil, err } + go app.expireInactivePlayersThread() + return &app, nil } diff --git a/server/src/application/expireInactivePlayers.go b/server/src/application/expireInactivePlayers.go new file mode 100644 index 0000000..973fb02 --- /dev/null +++ b/server/src/application/expireInactivePlayers.go @@ -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 +} diff --git a/server/src/application/playerATime.go b/server/src/application/playerATime.go index 7ff4c5d..be9cfc9 100644 --- a/server/src/application/playerATime.go +++ b/server/src/application/playerATime.go @@ -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 +}