diff --git a/handler/handler.go b/handler/handler.go index 96a17d8..83e3fb1 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -5,14 +5,16 @@ import ( "github.com/PulseyTeam/game-server/jwt" pb "github.com/PulseyTeam/game-server/proto" "go.mongodb.org/mongo-driver/mongo" + "sync" ) type MultiplayerHandler struct { pb.UnimplementedMultiplayerServiceServer - rooms map[string]map[string]*pb.Player - jwtManager *jwt.Manager - mongoDB *mongo.Client - cfg *config.Config + rooms map[string]map[string]*pb.Player + roomsMapMutex sync.RWMutex + jwtManager *jwt.Manager + mongoDB *mongo.Client + cfg *config.Config } type AuthHandler struct { diff --git a/handler/room.go b/handler/room.go index e33b3dd..64c161e 100644 --- a/handler/room.go +++ b/handler/room.go @@ -73,7 +73,9 @@ func (h *MultiplayerHandler) RoomStream(stream pb.MultiplayerService_RoomStreamS log.Trace().Msg(request.String()) } + h.roomsMapMutex.Lock() h.rooms[request.GetRoomId()][request.GetPlayer().GetId()] = request.GetPlayer() + h.roomsMapMutex.Unlock() currentPlayers := make([]*pb.Player, 0, len(h.rooms[request.GetRoomId()])) if len(h.rooms[request.GetRoomId()]) > 2 {