synchronize both player sockets on game start

This commit is contained in:
Branden J Brown 2024-02-03 09:26:46 -06:00
parent 8f285a599f
commit 23b6ab5728
3 changed files with 23 additions and 2 deletions

View File

@ -58,6 +58,10 @@ func gameActor(ctx context.Context, g *game.Match, dealer, chall person, join <-
// definitely expired. // definitely expired.
ctx, stop := context.WithTimeoutCause(ctx, 4*time.Hour, errMatchExpired) ctx, stop := context.WithTimeoutCause(ctx, 4*time.Hour, errMatchExpired)
defer stop() 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 var obs []observer
actions := make(chan action, 2) actions := make(chan action, 2)
go playerActor(ctx, dealer, actions) go playerActor(ctx, dealer, actions)

View File

@ -46,6 +46,7 @@ func main() {
creds: db, creds: db,
sessions: db, sessions: db,
pp: map[player.ID]*websocket.Conn{}, pp: map[player.ID]*websocket.Conn{},
j: map[lobby.GameID]chan person{},
} }
r := chi.NewRouter() r := chi.NewRouter()

View File

@ -26,6 +26,7 @@ type Server struct {
mu sync.Mutex mu sync.Mutex
pp map[player.ID]*websocket.Conn pp map[player.ID]*websocket.Conn
j map[lobby.GameID]chan person
} }
type db interface { type db interface {
@ -214,10 +215,25 @@ func (s *Server) joinAndServe(p person) {
return return
} }
if deal { if deal {
ch := make(chan person, 2)
s.mu.Lock()
s.j[id] = ch
s.mu.Unlock()
g := game.New(p.id, chall) g := game.New(p.id, chall)
other := s.person(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, ch)
go gameActor(context.TODO(), g, p, other, nil) 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. // Reply with the game ID so they can share.
r := serve.GameStart{ r := serve.GameStart{