From 73f4f8afee44163c19070db40698452a65486985 Mon Sep 17 00:00:00 2001 From: Steve Date: Sun, 25 Jul 2021 17:39:48 -0400 Subject: [PATCH] actually accept multiple matches --- internal/coordinator/coordinator.go | 33 ++++++++++++++++++----------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/internal/coordinator/coordinator.go b/internal/coordinator/coordinator.go index bf4bae3..7fb855e 100644 --- a/internal/coordinator/coordinator.go +++ b/internal/coordinator/coordinator.go @@ -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,27 +28,33 @@ func NewCoordinator() *Coordinator { func (c *Coordinator) Start() { go func() { - m := NewSession() - var p1, p2 uuid.UUID - p1 = <-c.PlayerQueueChan - fmt.Println("p1 join") - p2 = <-c.PlayerQueueChan - fmt.Println("p2 join") - m.Active = true - m.Game = game.NewGame() - c.Matches[m.ID] = m - c.CallbackChan[p1] <- m.ID - c.CallbackChan[p2] <- m.ID + for { + m := NewSession() + var p1, p2 uuid.UUID + p1 = <-c.PlayerQueueChan + fmt.Println("p1 join") + p2 = <-c.PlayerQueueChan + 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() } }()