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 (
"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
}
}

View File

@ -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()
}
}