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"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Coordinator struct {
|
2021-07-25 17:22:57 -04:00
|
|
|
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
|
2021-07-25 17:22:57 -04:00
|
|
|
CallbackChan map[uuid.UUID]chan uuid.UUID
|
2021-07-22 18:19:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewCoordinator() *Coordinator {
|
|
|
|
return &Coordinator{
|
2021-07-25 17:22:57 -04:00
|
|
|
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),
|
2021-07-25 17:22:57 -04:00
|
|
|
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)
|
2021-07-22 18:19:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Coordinator) Coordinate(cmd *SessionCommand) *SessionCommandResult {
|
|
|
|
switch cmd.Command {
|
|
|
|
case SessionCmdQuery:
|
2021-07-25 17:22:57 -04:00
|
|
|
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:
|
2021-07-25 17:22:57 -04:00
|
|
|
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{
|
2021-07-25 17:22:57 -04:00
|
|
|
ID: cmd.ID,
|
|
|
|
MatchID: uuid.Nil,
|
|
|
|
Result: SessionRespJoinError,
|
2021-07-25 16:23:39 -04:00
|
|
|
}
|
|
|
|
}
|
2021-07-25 17:22:57 -04:00
|
|
|
resp := m.Join(cmd.ID)
|
2021-07-22 18:19:21 -04:00
|
|
|
return &SessionCommandResult{
|
2021-07-25 17:22:57 -04:00
|
|
|
ID: cmd.ID,
|
|
|
|
MatchID: m.ID,
|
|
|
|
Result: resp,
|
2021-07-22 18:19:21 -04:00
|
|
|
}
|
|
|
|
case SessionCmdLeave:
|
2021-07-25 17:22:57 -04:00
|
|
|
m, exists := c.Matches[cmd.MatchID]
|
|
|
|
if !exists || !m.PlayerIn(cmd.ID) {
|
2021-07-25 16:23:39 -04:00
|
|
|
return &SessionCommandResult{
|
2021-07-25 17:22:57 -04:00
|
|
|
ID: cmd.ID,
|
|
|
|
MatchID: uuid.Nil,
|
|
|
|
Result: SessionRespLeft,
|
2021-07-25 16:23:39 -04:00
|
|
|
}
|
|
|
|
}
|
2021-07-25 17:22:57 -04:00
|
|
|
m.Leave(cmd.ID)
|
2021-07-22 18:19:21 -04:00
|
|
|
return &SessionCommandResult{
|
2021-07-25 17:22:57 -04:00
|
|
|
ID: cmd.ID,
|
|
|
|
MatchID: uuid.Nil,
|
|
|
|
Result: SessionRespLeft,
|
2021-07-22 18:19:21 -04:00
|
|
|
}
|
|
|
|
case SessionCmdPlay:
|
2021-07-25 17:22:57 -04:00
|
|
|
m, exists := c.Matches[cmd.MatchID]
|
|
|
|
if !exists || !m.PlayerIn(cmd.ID) {
|
|
|
|
return &SessionCommandResult{
|
|
|
|
ID: cmd.ID,
|
|
|
|
MatchID: uuid.Nil,
|
|
|
|
Result: SessionRespLeft,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resp := m.Play(cmd.ID, cmd.GameCommand)
|
2021-07-22 18:19:21 -04:00
|
|
|
return &SessionCommandResult{
|
|
|
|
ID: cmd.ID,
|
2021-07-25 17:22:57 -04:00
|
|
|
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 {
|
2021-07-29 17:40:11 -04:00
|
|
|
_, 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{
|
2021-07-25 17:22:57 -04:00
|
|
|
ID: cmd.ID,
|
|
|
|
MatchID: uuid.Nil,
|
|
|
|
Result: SessionRespError,
|
2021-07-22 18:19:21 -04:00
|
|
|
}
|
|
|
|
}
|