clean out stale matches

This commit is contained in:
stryan 2021-09-27 16:51:40 -04:00
parent 351e5eebc6
commit f5eac37b06
2 changed files with 13 additions and 0 deletions

View File

@ -60,6 +60,13 @@ func MatchCleaner(c *Coordinator) {
delete(c.Matches, v.ID)
v.Watcher <- true
}
if time.Now().After(v.LastMove.Add(time.Minute * 10)) {
log.Println("clearing stale match")
v.Game = nil
v.Active = false
delete(c.Matches, v.ID)
v.Watcher <- true
}
}
c.MatchLock.Unlock()
}

View File

@ -2,6 +2,7 @@ package coordinator
import (
"fmt"
"time"
"git.saintnet.tech/stryan/snengame/internal/game"
"github.com/google/uuid"
@ -49,6 +50,7 @@ type Session struct {
Game *game.Game
Watcher chan bool
Broadcasts map[uuid.UUID]chan SessionResp
LastMove time.Time
}
func NewSession() *Session {
@ -61,6 +63,7 @@ func NewSession() *Session {
Game: nil,
Watcher: make(chan bool),
Broadcasts: make(map[uuid.UUID]chan SessionResp),
LastMove: time.Now(),
}
}
@ -102,6 +105,9 @@ func (s *Session) Play(id uuid.UUID, cmd *game.Command) *game.CommandResult {
if s.pMap[id] != cmd.PlayerID {
return nil
}
if cmd.Type != SessionCmdPoll {
s.LastMove = time.Now()
}
res := s.Game.Parse(cmd)
return res
}