From 23b6ab5728d4cd73825a0d04940ee677e76d513c Mon Sep 17 00:00:00 2001 From: Branden J Brown Date: Sat, 3 Feb 2024 09:26:46 -0600 Subject: [PATCH] synchronize both player sockets on game start --- game.go | 4 ++++ main.go | 1 + server.go | 20 ++++++++++++++++++-- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/game.go b/game.go index 889f843..e2ef2ef 100644 --- a/game.go +++ b/game.go @@ -58,6 +58,10 @@ func gameActor(ctx context.Context, g *game.Match, dealer, chall person, join <- // definitely expired. ctx, stop := context.WithTimeoutCause(ctx, 4*time.Hour, errMatchExpired) defer stop() + // Accept joins from the dealer and challenger. We don't care which is + // which, but we need to get both to synchronize the socket. + <-join + <-join var obs []observer actions := make(chan action, 2) go playerActor(ctx, dealer, actions) diff --git a/main.go b/main.go index f96575f..5feea51 100644 --- a/main.go +++ b/main.go @@ -46,6 +46,7 @@ func main() { creds: db, sessions: db, pp: map[player.ID]*websocket.Conn{}, + j: map[lobby.GameID]chan person{}, } r := chi.NewRouter() diff --git a/server.go b/server.go index 892f59b..c6ee28e 100644 --- a/server.go +++ b/server.go @@ -26,6 +26,7 @@ type Server struct { mu sync.Mutex pp map[player.ID]*websocket.Conn + j map[lobby.GameID]chan person } type db interface { @@ -214,10 +215,25 @@ func (s *Server) joinAndServe(p person) { return } if deal { + ch := make(chan person, 2) + s.mu.Lock() + s.j[id] = ch + s.mu.Unlock() g := game.New(p.id, chall) other := s.person(chall) - // TODO(zeph): save the game state s.t. we can provide a join channel - go gameActor(context.TODO(), g, p, other, nil) + go gameActor(context.TODO(), g, p, other, ch) + ch <- p + } else { + // very gross, but i just want this to exist + for { + s.mu.Lock() + ch := s.j[id] + s.mu.Unlock() + if ch != nil { + ch <- p + break + } + } } // Reply with the game ID so they can share. r := serve.GameStart{