From a4227c9728255d00078cbd3a4ef5bc586c1dfcc4 Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 29 Sep 2021 15:26:18 -0400 Subject: [PATCH] only matchmake when a new player enters the queue --- cmd/server/server.go | 2 -- internal/coordinator/match.go | 49 +++++++++++++++++++---------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/cmd/server/server.go b/cmd/server/server.go index 94bf2fc..d014c9b 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -3,7 +3,6 @@ package main import ( "log" "net/http" - "time" "git.saintnet.tech/stryan/snengame/internal/coordinator" "github.com/gorilla/websocket" @@ -56,7 +55,6 @@ func serveWs(c *coordinator.Coordinator, w http.ResponseWriter, r *http.Request) break } if resp.Result == coordinator.SessionRespLeft { - time.Sleep(1 * time.Second) //give clients a second to respond break } } diff --git a/internal/coordinator/match.go b/internal/coordinator/match.go index bea5882..1613cb1 100644 --- a/internal/coordinator/match.go +++ b/internal/coordinator/match.go @@ -25,8 +25,6 @@ func MatchMaker(c *Coordinator) { MMR: 0, QueueTime: time.Now()}] = true log.Printf("Player %v has queued", p) - default: - //no new players, let's try to matchmake if len(c.PlayerPool) < 2 { continue } else { @@ -58,36 +56,43 @@ func MatchMaker(c *Coordinator) { } func QueueCleaner(c *Coordinator) { + ticker := time.NewTicker(5 * time.Minute) for { - time.Sleep(5 * time.Minute) - for v, _ := range c.PlayerPool { - if time.Now().After(v.QueueTime.Add(time.Minute * 5)) { - log.Printf("Removing player %v from pool", v.Id) - delete(c.PlayerPool, v) //probably should just flag + select { + case <-ticker.C: + for v, _ := range c.PlayerPool { + if time.Now().After(v.QueueTime.Add(time.Minute * 5)) { + log.Printf("Removing player %v from pool", v.Id) + delete(c.PlayerPool, v) //probably should just flag + } } } } } func MatchCleaner(c *Coordinator) { + ticker := time.NewTicker(10 * time.Second) for { - 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) - v.Watcher <- true - } - if time.Now().After(v.LastMove.Add(time.Minute * 10)) { - log.Println("clearing stale match") - v.Game = nil - v.Active = false - delete(c.Matches, v.ID) - v.Watcher <- true + select { + case <-ticker.C: + 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) + v.Watcher <- true + } + if time.Now().After(v.LastMove.Add(time.Minute * 10)) { + log.Println("clearing stale match") + v.Game = nil + v.Active = false + delete(c.Matches, v.ID) + v.Watcher <- true + } } + + c.MatchLock.Unlock() } - c.MatchLock.Unlock() } }