actually accept multiple matches

This commit is contained in:
stryan 2021-07-25 17:39:48 -04:00
parent c5e8f2d404
commit 73f4f8afee

View File

@ -3,6 +3,7 @@ package coordinator
import ( import (
"fmt" "fmt"
"log" "log"
"sync"
"time" "time"
"git.saintnet.tech/stryan/snengame/internal/game" "git.saintnet.tech/stryan/snengame/internal/game"
@ -11,6 +12,7 @@ import (
type Coordinator struct { type Coordinator struct {
Matches map[uuid.UUID]*Session Matches map[uuid.UUID]*Session
MatchLock *sync.Mutex
PlayerQueueChan chan uuid.UUID PlayerQueueChan chan uuid.UUID
CallbackChan map[uuid.UUID]chan uuid.UUID CallbackChan map[uuid.UUID]chan uuid.UUID
} }
@ -18,6 +20,7 @@ type Coordinator struct {
func NewCoordinator() *Coordinator { func NewCoordinator() *Coordinator {
return &Coordinator{ return &Coordinator{
Matches: make(map[uuid.UUID]*Session), Matches: make(map[uuid.UUID]*Session),
MatchLock: &sync.Mutex{},
PlayerQueueChan: make(chan uuid.UUID), PlayerQueueChan: make(chan uuid.UUID),
CallbackChan: make(map[uuid.UUID]chan uuid.UUID), CallbackChan: make(map[uuid.UUID]chan uuid.UUID),
} }
@ -25,27 +28,33 @@ func NewCoordinator() *Coordinator {
func (c *Coordinator) Start() { func (c *Coordinator) Start() {
go func() { go func() {
m := NewSession() for {
var p1, p2 uuid.UUID m := NewSession()
p1 = <-c.PlayerQueueChan var p1, p2 uuid.UUID
fmt.Println("p1 join") p1 = <-c.PlayerQueueChan
p2 = <-c.PlayerQueueChan fmt.Println("p1 join")
fmt.Println("p2 join") p2 = <-c.PlayerQueueChan
m.Active = true fmt.Println("p2 join")
m.Game = game.NewGame() m.Active = true
c.Matches[m.ID] = m m.Game = game.NewGame()
c.CallbackChan[p1] <- m.ID c.MatchLock.Lock()
c.CallbackChan[p2] <- m.ID c.Matches[m.ID] = m
c.MatchLock.Unlock()
c.CallbackChan[p1] <- m.ID
c.CallbackChan[p2] <- m.ID
}
}() }()
go func() { go func() {
for { for {
time.Sleep(10) time.Sleep(10 * time.Second)
c.MatchLock.Lock()
for _, v := range c.Matches { for _, v := range c.Matches {
if v.Game == nil && v.Active { if v.Game == nil && v.Active {
log.Println("clearing match with no game") log.Println("clearing match with no game")
delete(c.Matches, v.ID) delete(c.Matches, v.ID)
} }
} }
c.MatchLock.Unlock()
} }
}() }()