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