Exemple #1
0
        public TronLobby JoinLobby(JoinLobbyRequest request)
        {
            TronLobby tronLobby;

            lock (GameLobbiesLock)
            {
                var index = GameLobbies.FindIndex(lobby => lobby.Board.Cols == request.PlayerBoard.Cols &&
                                                  lobby.Board.Rows == request.PlayerBoard.Rows &&
                                                  lobby.Players.All(p => p.Key != request.Player.Key));

                if (index > -1)
                {
                    var lobby = GameLobbies[index];
                    lobby.Players.Add(request.Player);
                    lobby.IsReady = lobby.Players.Count == 2;

                    if (lobby.IsReady)
                    {
                        GameLobbies.RemoveAt(index);
                    }

                    tronLobby = lobby.ToTronLobby();
                }
                else
                {
                    var lobby = new MatchmakingLobby
                    {
                        Players = new List <TronPlayer>
                        {
                            request.Player
                        },
                        Board = request.PlayerBoard
                    };

                    GameLobbies.Add(lobby);

                    tronLobby = lobby.ToTronLobby();
                }
            }

            return(tronLobby);
        }
Exemple #2
0
        public async Task FindGame(FindGameDto dto)
        {
            if (dto == null || string.IsNullOrWhiteSpace(dto.PlayerName) || dto.PlayerBoard == null ||
                dto.PlayerBoard.Rows <= 0 || dto.PlayerBoard.Cols <= 0)
            {
                throw new ArgumentException(nameof(FindGameDto));
            }

            var request = new JoinLobbyRequest
            {
                PlayerBoard = dto.PlayerBoard.ToModel(),
                Player      = new TronPlayer(dto.PlayerName, Context.ConnectionId)
            };

            var gameLobby = _playersMatchmakingService.JoinLobby(request);

            if (gameLobby.IsReady)
            {
                var game = await CreateNewGame(gameLobby);

                await StartGame(game);
            }
        }