matchmaking cleanup

This commit is contained in:
stryan 2021-09-23 13:04:34 -04:00
parent b5f1740d22
commit 76e20ea130
3 changed files with 20 additions and 128 deletions

View File

@ -41,6 +41,8 @@ func receiveHandler(connection *websocket.Conn, output chan string) {
case coordinator.SessionRespFound: case coordinator.SessionRespFound:
matchID = resp.MatchID matchID = resp.MatchID
output <- "game found" output <- "game found"
case coordinator.SessionRespJoinError:
output <- "error joining game"
case coordinator.SessionRespPlayed: case coordinator.SessionRespPlayed:
if resp.GameResult != nil { if resp.GameResult != nil {
output <- "played succesfully" output <- "played succesfully"

View File

@ -15,6 +15,7 @@ func Serve(c *coordinator.Coordinator) {
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
serveWs(c, w, r) serveWs(c, w, r)
}) })
log.Println("starting websocket")
err := http.ListenAndServe(":7636", nil) err := http.ListenAndServe(":7636", nil)
if err != nil { if err != nil {
log.Fatal("ListenAndServe: ", err) log.Fatal("ListenAndServe: ", err)

View File

@ -1,12 +1,10 @@
package coordinator package coordinator
import ( import (
"fmt"
"log" "log"
"sync" "sync"
"time" "time"
"git.saintnet.tech/stryan/snengame/internal/game"
"github.com/google/uuid" "github.com/google/uuid"
) )
@ -27,39 +25,8 @@ func NewCoordinator() *Coordinator {
} }
func (c *Coordinator) Start() { func (c *Coordinator) Start() {
go func() { go MatchMaker(c)
for { go MatchCleaner(c)
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 MatchWatcher(m)
}
}()
go func() {
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
}
}
c.MatchLock.Unlock()
}
}()
} }
func (c *Coordinator) Coordinate(cmd *SessionCommand) *SessionCommandResult { func (c *Coordinator) Coordinate(cmd *SessionCommand) *SessionCommandResult {
@ -67,15 +34,25 @@ func (c *Coordinator) Coordinate(cmd *SessionCommand) *SessionCommandResult {
case SessionCmdQuery: case SessionCmdQuery:
c.CallbackChan[cmd.ID] = make(chan uuid.UUID) c.CallbackChan[cmd.ID] = make(chan uuid.UUID)
c.PlayerQueueChan <- cmd.ID c.PlayerQueueChan <- cmd.ID
m := <-c.CallbackChan[cmd.ID] ticker := time.NewTicker(5 * time.Minute)
return &SessionCommandResult{ select {
ID: cmd.ID, case m := <-c.CallbackChan[cmd.ID]:
MatchID: m, return &SessionCommandResult{
Result: SessionRespFound, ID: cmd.ID,
MatchID: m,
Result: SessionRespFound,
}
case <-ticker.C:
return &SessionCommandResult{
ID: cmd.ID,
MatchID: uuid.Nil,
Result: SessionRespError,
}
} }
case SessionCmdJoin: case SessionCmdJoin:
m, exists := c.Matches[cmd.MatchID] m, exists := c.Matches[cmd.MatchID]
if !exists { if !exists {
log.Printf("player %v tried to join non-existent match %v", cmd.ID, cmd.MatchID)
return &SessionCommandResult{ return &SessionCommandResult{
ID: cmd.ID, ID: cmd.ID,
MatchID: uuid.Nil, MatchID: uuid.Nil,
@ -149,91 +126,3 @@ func (c *Coordinator) Coordinate(cmd *SessionCommand) *SessionCommandResult {
Result: SessionRespError, Result: SessionRespError,
} }
} }
func MatchWatcher(m *Session) {
ticker := time.NewTicker(1 * time.Second)
old_turn := -1
var old_board *game.Board
old_sen_hand := -1
old_sco_hand := -1
old_sen_life := -1
old_sco_life := -1
sen_ready := false
sco_ready := false
for {
select {
case <-ticker.C:
if m.Active && m.Game != nil && len(m.Broadcasts) > 0 {
if m.Game.Status == game.StatusLobby && !sen_ready && m.Game.SentinalPlayer.Ready {
for _, v := range m.Broadcasts {
v <- SessionRespBroadcastSenReady
}
sen_ready = true
}
if m.Game.Status == game.StatusLobby && !sco_ready && m.Game.ScourgePlayer.Ready {
for _, v := range m.Broadcasts {
v <- SessionRespBroadcastScoReady
}
sco_ready = true
}
if m.Game.Status == game.StatusSentinalWin {
for _, v := range m.Broadcasts {
v <- SessionRespBroadcastSenWin
}
}
if m.Game.Status == game.StatusScourgeWin {
for _, v := range m.Broadcasts {
v <- SessionRespBroadcastScoWin
}
}
if m.p1 == uuid.Nil && sen_ready {
for _, v := range m.Broadcasts {
v <- SessionRespBroadcastSenLeft
}
sen_ready = false
}
if m.p2 == uuid.Nil && sco_ready {
for _, v := range m.Broadcasts {
v <- SessionRespBroadcastScoLeft
}
sco_ready = false
}
if old_board == nil || old_board.Sentinal != m.Game.GameBoard.Sentinal || old_board.Scourge != m.Game.GameBoard.Scourge || old_sen_hand != len(m.Game.SentinalPlayer.Hand) || old_sco_hand != len(m.Game.ScourgePlayer.Hand) || old_sen_life != m.Game.SentinalPlayer.Life || old_sco_life != m.Game.ScourgePlayer.Life {
if old_board == nil {
old_board = m.Game.GameBoard
} else {
old_board.Sentinal = m.Game.GameBoard.Sentinal
old_board.Scourge = m.Game.GameBoard.Scourge
}
old_sen_hand = len(m.Game.SentinalPlayer.Hand)
old_sco_hand = len(m.Game.ScourgePlayer.Hand)
old_sen_life = m.Game.SentinalPlayer.Life
old_sco_life = m.Game.ScourgePlayer.Life
for _, v := range m.Broadcasts {
v <- SessionRespBroadcastUpdate
}
}
if old_turn != m.Game.CurrentTurn {
old_turn = m.Game.CurrentTurn
if old_turn == game.SentinalID {
for k, v := range m.pMap {
if v == game.SentinalID {
m.Broadcasts[k] <- SessionRespBroadcastSenTurn
}
}
} else if old_turn == game.ScourgeID {
for k, v := range m.pMap {
if v == game.ScourgeID {
m.Broadcasts[k] <- SessionRespBroadcastScoTrun
}
}
}
}
}
case <-m.Watcher:
close(m.Watcher)
return
}
}
}