Skip to content

Commit

Permalink
room stream mutex current player
Browse files Browse the repository at this point in the history
  • Loading branch information
ferhatbostanci committed Jun 10, 2021
1 parent 8af64c4 commit 1a0a5e0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
11 changes: 6 additions & 5 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (

type MultiplayerHandler struct {
pb.UnimplementedMultiplayerServiceServer
rooms map[string]map[string]*pb.Player
roomsMapMutex sync.RWMutex
jwtManager *jwt.Manager
mongoDB *mongo.Client
cfg *config.Config
rooms map[string]map[string]*pb.Player
roomsMapMutex sync.RWMutex
currentPlayersMutex sync.RWMutex
jwtManager *jwt.Manager
mongoDB *mongo.Client
cfg *config.Config
}

type AuthHandler struct {
Expand Down
2 changes: 2 additions & 0 deletions handler/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ func (h *MultiplayerHandler) RoomStream(stream pb.MultiplayerService_RoomStreamS

currentPlayers := make([]*pb.Player, 0, len(h.rooms[request.GetRoomId()]))
if len(h.rooms[request.GetRoomId()]) > 2 {
h.currentPlayersMutex.Lock()
for _, player := range h.rooms[request.GetRoomId()] {
currentPlayers = append(currentPlayers, player)
}
h.currentPlayersMutex.Unlock()
}

err = stream.Send(&pb.RoomStreamResponse{Players: currentPlayers})
Expand Down

0 comments on commit 1a0a5e0

Please sign in to comment.