snengame/internal/coordinator/coordinator.go

145 lines
3.2 KiB
Go
Raw Normal View History

2021-07-22 18:19:21 -04:00
package coordinator
import (
"log"
2021-07-25 17:39:48 -04:00
"sync"
2021-07-22 18:19:21 -04:00
"time"
"git.saintnet.tech/stryan/snengame/internal/game"
2021-07-22 18:19:21 -04:00
"github.com/google/uuid"
)
type Coordinator struct {
Matches map[uuid.UUID]*Session
2021-07-25 17:39:48 -04:00
MatchLock *sync.Mutex
2021-07-22 18:19:21 -04:00
PlayerQueueChan chan uuid.UUID
PlayerPool map[MMRPlayer]bool
CallbackChan map[uuid.UUID]chan uuid.UUID
2021-07-22 18:19:21 -04:00
}
func NewCoordinator() *Coordinator {
return &Coordinator{
Matches: make(map[uuid.UUID]*Session),
2021-07-25 17:39:48 -04:00
MatchLock: &sync.Mutex{},
2021-07-22 18:19:21 -04:00
PlayerQueueChan: make(chan uuid.UUID),
PlayerPool: make(map[MMRPlayer]bool),
CallbackChan: make(map[uuid.UUID]chan uuid.UUID),
2021-07-22 18:19:21 -04:00
}
}
func (c *Coordinator) Start() {
2021-09-23 13:04:34 -04:00
go MatchMaker(c)
go MatchCleaner(c)
go QueueCleaner(c)
2021-07-22 18:19:21 -04:00
}
func (c *Coordinator) Coordinate(cmd *SessionCommand) *SessionCommandResult {
switch cmd.Command {
case SessionCmdQuery:
c.CallbackChan[cmd.ID] = make(chan uuid.UUID)
2021-07-22 18:19:21 -04:00
c.PlayerQueueChan <- cmd.ID
2021-09-23 13:04:34 -04:00
ticker := time.NewTicker(5 * time.Minute)
select {
case m := <-c.CallbackChan[cmd.ID]:
return &SessionCommandResult{
ID: cmd.ID,
MatchID: m,
Result: SessionRespFound,
}
case <-ticker.C:
return &SessionCommandResult{
ID: cmd.ID,
MatchID: uuid.Nil,
Result: SessionRespError,
}
2021-07-22 18:19:21 -04:00
}
case SessionCmdJoin:
m, exists := c.Matches[cmd.MatchID]
if !exists {
2021-09-23 13:04:34 -04:00
log.Printf("player %v tried to join non-existent match %v", cmd.ID, cmd.MatchID)
2021-07-25 16:23:39 -04:00
return &SessionCommandResult{
ID: cmd.ID,
MatchID: uuid.Nil,
Result: SessionRespJoinError,
2021-07-25 16:23:39 -04:00
}
}
resp := m.Join(cmd.ID)
if m.p1 != uuid.Nil && m.p2 != uuid.Nil {
log.Printf("Starting game for %v and %v\n", m.p1, m.p2)
m.Game = game.NewGame()
m.Active = true
}
2021-07-22 18:19:21 -04:00
return &SessionCommandResult{
ID: cmd.ID,
MatchID: m.ID,
Result: resp,
2021-07-22 18:19:21 -04:00
}
case SessionCmdLeave:
m, exists := c.Matches[cmd.MatchID]
if !exists || !m.PlayerIn(cmd.ID) {
2021-07-25 16:23:39 -04:00
return &SessionCommandResult{
ID: cmd.ID,
MatchID: uuid.Nil,
Result: SessionRespLeft,
2021-07-25 16:23:39 -04:00
}
}
m.Leave(cmd.ID)
2021-07-22 18:19:21 -04:00
return &SessionCommandResult{
ID: cmd.ID,
MatchID: uuid.Nil,
Result: SessionRespLeft,
2021-07-22 18:19:21 -04:00
}
case SessionCmdPlay:
m, exists := c.Matches[cmd.MatchID]
if !exists || !m.PlayerIn(cmd.ID) {
return &SessionCommandResult{
ID: cmd.ID,
MatchID: uuid.Nil,
Result: SessionRespLeft,
}
}
if m.Game == nil {
return &SessionCommandResult{
ID: cmd.ID,
MatchID: m.ID,
Result: SessionRespError,
}
}
resp := m.Play(cmd.ID, cmd.GameCommand)
2021-07-22 18:19:21 -04:00
return &SessionCommandResult{
ID: cmd.ID,
MatchID: m.ID,
2021-07-22 18:19:21 -04:00
Result: SessionRespPlayed,
GameResult: resp,
}
2021-07-26 13:02:00 -04:00
case SessionCmdPoll:
m, exists := c.Matches[cmd.MatchID]
if exists {
_, exists = m.Broadcasts[cmd.ID]
if !exists {
log.Printf("%v has opted in to polling", cmd.ID)
m.Broadcasts[cmd.ID] = make(chan SessionResp, 10)
}
2021-07-26 13:02:00 -04:00
select {
case res := <-m.Broadcasts[cmd.ID]:
return &SessionCommandResult{
ID: cmd.ID,
MatchID: m.ID,
Result: res,
}
default:
return &SessionCommandResult{
ID: cmd.ID,
MatchID: m.ID,
Result: SessionRespBroadcastNone,
}
}
}
2021-07-22 18:19:21 -04:00
}
return &SessionCommandResult{
ID: cmd.ID,
MatchID: uuid.Nil,
Result: SessionRespError,
2021-07-22 18:19:21 -04:00
}
}