only matchmake when a new player enters the queue

This commit is contained in:
stryan 2021-09-29 15:26:18 -04:00
parent deb6d24282
commit a4227c9728
2 changed files with 27 additions and 24 deletions

View File

@ -3,7 +3,6 @@ package main
import ( import (
"log" "log"
"net/http" "net/http"
"time"
"git.saintnet.tech/stryan/snengame/internal/coordinator" "git.saintnet.tech/stryan/snengame/internal/coordinator"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
@ -56,7 +55,6 @@ func serveWs(c *coordinator.Coordinator, w http.ResponseWriter, r *http.Request)
break break
} }
if resp.Result == coordinator.SessionRespLeft { if resp.Result == coordinator.SessionRespLeft {
time.Sleep(1 * time.Second) //give clients a second to respond
break break
} }
} }

View File

@ -25,8 +25,6 @@ func MatchMaker(c *Coordinator) {
MMR: 0, MMR: 0,
QueueTime: time.Now()}] = true QueueTime: time.Now()}] = true
log.Printf("Player %v has queued", p) log.Printf("Player %v has queued", p)
default:
//no new players, let's try to matchmake
if len(c.PlayerPool) < 2 { if len(c.PlayerPool) < 2 {
continue continue
} else { } else {
@ -58,36 +56,43 @@ func MatchMaker(c *Coordinator) {
} }
func QueueCleaner(c *Coordinator) { func QueueCleaner(c *Coordinator) {
ticker := time.NewTicker(5 * time.Minute)
for { for {
time.Sleep(5 * time.Minute) select {
for v, _ := range c.PlayerPool { case <-ticker.C:
if time.Now().After(v.QueueTime.Add(time.Minute * 5)) { for v, _ := range c.PlayerPool {
log.Printf("Removing player %v from pool", v.Id) if time.Now().After(v.QueueTime.Add(time.Minute * 5)) {
delete(c.PlayerPool, v) //probably should just flag log.Printf("Removing player %v from pool", v.Id)
delete(c.PlayerPool, v) //probably should just flag
}
} }
} }
} }
} }
func MatchCleaner(c *Coordinator) { func MatchCleaner(c *Coordinator) {
ticker := time.NewTicker(10 * time.Second)
for { for {
time.Sleep(10 * time.Second) select {
c.MatchLock.Lock() case <-ticker.C:
for _, v := range c.Matches { c.MatchLock.Lock()
if v.Game == nil && v.Active { for _, v := range c.Matches {
log.Println("clearing match with no game") if v.Game == nil && v.Active {
delete(c.Matches, v.ID) log.Println("clearing match with no game")
v.Watcher <- true delete(c.Matches, v.ID)
} v.Watcher <- true
if time.Now().After(v.LastMove.Add(time.Minute * 10)) { }
log.Println("clearing stale match") if time.Now().After(v.LastMove.Add(time.Minute * 10)) {
v.Game = nil log.Println("clearing stale match")
v.Active = false v.Game = nil
delete(c.Matches, v.ID) v.Active = false
v.Watcher <- true delete(c.Matches, v.ID)
v.Watcher <- true
}
} }
c.MatchLock.Unlock()
} }
c.MatchLock.Unlock()
} }
} }