Skip to content

Commit

Permalink
chore: Update SetStackStream function in GameManager
Browse files Browse the repository at this point in the history
This commit modifies the `SetStackStream` function in the `GameManager` to include an additional parameter `initMsg *pb.StackRequest`. This parameter is used to send an initialization message to the stack stream when `initMsg.DrawingCard` is false. The commit also removes the commented out code for handling card draw messages, which is marked as a TODO.
  • Loading branch information
oriventi committed Jun 5, 2024
1 parent 1588a55 commit 3b40279
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
50 changes: 26 additions & 24 deletions duo_backend/api/game_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func (gm *GameManager) onStackDisconnected(gameId int) error {
return gm.DeleteGame(gameId, true, "stack stream disconnected")
}

func (gm *GameManager) SetStackStream(gameId int, userId uuid.UUID, stream pb.DUOService_GetStackStreamServer) error {
func (gm *GameManager) SetStackStream(gameId int, userId uuid.UUID, initMsg *pb.StackRequest, stream pb.DUOService_GetStackStreamServer) error {
game, exists := gm.GetGame(gameId)
if !exists {
log.Printf("[stack stream] game does not exist")
Expand Down Expand Up @@ -399,6 +399,27 @@ func (gm *GameManager) SetStackStream(gameId int, userId uuid.UUID, stream pb.DU

log.Printf(" [stack stream]stack stream of game: %d connected", gameId)

if !initMsg.DrawingCard {
//INIT MESSAGE
game.Mu.Lock()
sendErr := game.StackStream.Send(&pb.StackState{
PlaceStack: &pb.PlaceStackState{
AmountCards: int32(len(game.CardsOnPlaceStack)),
CardIdOnTop: game.CardsOnPlaceStack[len(game.CardsOnPlaceStack)-1],
},
DrawStack: &pb.DrawStackState{
StackId: int32(gameId),
CardIds: game.CardsOnDrawStack,
},
})
if sendErr != nil {
log.Printf("[stack stream] error sending init message to stack %v: %v", userId, sendErr)
game.Mu.Unlock()
return sendErr
}
game.Mu.Unlock()
}

for {
select {
case <-stream.Context().Done():
Expand All @@ -425,29 +446,10 @@ func (gm *GameManager) SetStackStream(gameId int, userId uuid.UUID, stream pb.DU
}

log.Printf("[stack stream] Received message from stack %v: %v", userId, msg)
if !msg.DrawingCard {
//INIT MESSAGE
game.Mu.Lock()
sendErr := game.StackStream.Send(&pb.StackState{
PlaceStack: &pb.PlaceStackState{
AmountCards: int32(len(game.CardsOnPlaceStack)),
CardIdOnTop: game.CardsOnPlaceStack[len(game.CardsOnPlaceStack)-1],
},
DrawStack: &pb.DrawStackState{
StackId: int32(gameId),
CardIds: game.CardsOnDrawStack,
},
})
if sendErr != nil {
log.Printf("[stack stream] error sending init message to stack %v: %v", userId, sendErr)
game.Mu.Unlock()
return sendErr
}
game.Mu.Unlock()
} else {
//CARD DRAW MESSAGE
//TODO
}

//CARD DRAW MESSAGE
//TODO

}
}
}
Expand Down
2 changes: 1 addition & 1 deletion duo_backend/api/game_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (server *Server) GetStackStream(stream pb.DUOService_GetStackStreamServer)
}

// Add user to game
addErr := server.GameHandler.SetStackStream(int(gameId.GameID), payload.UserID, stream)
addErr := server.GameHandler.SetStackStream(int(gameId.GameID), payload.UserID, msg, stream)
if addErr != nil {
log.Printf("error adding user to game: %v", addErr)
return status.Errorf(codes.Internal, "error adding user to game")
Expand Down

0 comments on commit 3b40279

Please sign in to comment.