Example #1
0
        public TreeNode AddItemToTree(ItemWithId item, TreeNode root)
        {
            var node = root.AddSubNode(item.Id);

            item.IdChanged += (sender, args) =>
            {
                node.Text = item.Id;
            };

            node.Tag = item;

            return(node);
        }
Example #2
0
        public async Task <string> MakeMove(string choice)
        {
            string userId = ReadUserToken();

            _logger.LogInformation($"{nameof(GameHub)} | {userId}: Move processing was requested");

            GameRoom currentRoom = _gameStorageService.GetRoomByPlayer(userId);

            if (currentRoom == null)
            {
                string failMessage = "You are not allowed to make moves in this game";
                _logger.LogInformation($"{nameof(GameHub)} | {userId}: Move processing was denied because user is not allowed to make moves in this game");
                await SendMessageToCaller(failMessage);

                return(GameEvents.ErrorOccured);
            }

            Series currentRound = currentRoom.Series;

            if (currentRound == null)
            {
                string failMessage = "Error occured. Try again later";
                _logger.LogInformation($"{nameof(GameHub)} | {userId}: Move processing was denied because there is no Series in GameRoom");
                await SendMessageToCaller(failMessage);

                return(GameEvents.ErrorOccured);
            }

            bool isSuccess = Enum.TryParse(choice, true, out MoveOptions playerMoveOption);

            if (!isSuccess)
            {
                string failMessage = "Invalid move option";
                _logger.LogInformation($"{nameof(GameHub)} | {userId}: Move processing was denied because invalid parameter was passed");
                await SendMessageToCaller(failMessage);

                return(GameEvents.ErrorOccured);
            }

            if (currentRound.Player(userId).SelectedOption.HasValue)
            {
                string failMessage = "You already made your move";
                _logger.LogInformation($"{nameof(GameHub)} | {userId}: Move processing was denied because user already made his move");
                await SendMessageToCaller(failMessage);

                return(GameEvents.ErrorOccured);
            }

            if (playerMoveOption == MoveOptions.Undefined)
            {
                _logger.LogInformation($"{nameof(GameHub)} | {userId}: User aborted round");

                currentRound.CreateNewRound();
                await Clients.OthersInGroup(currentRoom.RoomToken).SendAsync(GameEvents.GameEnd,
                                                                             "Round is canceled, opponent's time to make move expired");

                await Clients.Caller.SendAsync(GameEvents.GameEnd,
                                               "Round is canceled, your time to make move expired\nPress 'Enter' to continue...");

                await Clients.OthersInGroup(currentRoom.RoomToken).SendAsync(GameEvents.GameAborted);

                return(GameEvents.GameEnd);
            }
            currentRound.MakeMove(userId, playerMoveOption);

            if (currentRound.Opponent(userId).SelectedOption.HasValue&& currentRound.Player(userId).SelectedOption.HasValue)
            {
                ItemWithId <string> playerActiveToken   = (await _tokens.GetAllAsync()).FirstOrDefault(tk => tk.Item == currentRound.Player(userId).PlayerId);
                ItemWithId <string> opponentActiveToken = (await _tokens.GetAllAsync()).FirstOrDefault(tk => tk.Item == currentRound.Opponent(userId).PlayerId);

                if (playerActiveToken == null)
                {
                    _logger.LogInformation($"{nameof(GameHub)}: Unauthorized user tried to make move");
                    currentRound.CreateNewRound();
                    await Clients.Caller.SendAsync(GameEvents.GameEnd,
                                                   "Your access token is expired. You are not allowed to make moves in any game.\nTry to login again");

                    await Clients.OthersInGroup(currentRoom.RoomToken).SendAsync(GameEvents.GameEnd,
                                                                                 "Your opponent disconnected");

                    return(GameEvents.GameEnd);
                }
                if (opponentActiveToken == null)
                {
                    _logger.LogInformation($"{nameof(GameHub)}: Unauthorized user tried to make move");
                    currentRound.CreateNewRound();
                    await Clients.OthersInGroup(currentRoom.RoomToken).SendAsync(GameEvents.GameEnd,
                                                                                 "Your access token is expired. You are not allowed to make moves in any game.\nTry to login again");

                    await Clients.Caller.SendAsync(GameEvents.GameEnd,
                                                   "Your opponent disconnected");

                    return(GameEvents.GameEnd);
                }

                //var playerMoveOption = currentRound.Player(userId).SelectedOption.Value;
                var opponentMoveOption = currentRound.Opponent(userId).SelectedOption.Value;

                GameOutcome playerResult  = _gameService.GetGameResult(playerMoveOption, opponentMoveOption);
                string      playerSummary = $"Your choice: {playerMoveOption}\n" +
                                            $"Opponent's choice: {opponentMoveOption}\n" +
                                            $"Round result: {playerResult}\n";

                var isPlayersStatSaved = await _statisticsService.SaveAsync(currentRound.Player(userId).PlayerId, playerResult, playerMoveOption);

                if (!isPlayersStatSaved)
                {
                    _logger.LogInformation($"{nameof(GameHub)} | {userId}: Round statistics wasn't saved");
                }

                await Clients.Caller.SendAsync(GameEvents.GameEnd, playerSummary);


                GameOutcome opponentResult  = _gameService.GetGameResult(opponentMoveOption, playerMoveOption);
                string      opponentSummary = $"Your choice: {opponentMoveOption}\n" +
                                              $"Opponent's choice: {playerMoveOption}\n" +
                                              $"Round result: {opponentResult}\n";

                var isOpponentStatSaved = await _statisticsService.SaveAsync(currentRound.Opponent(userId).PlayerId, opponentResult, opponentMoveOption);

                if (!isOpponentStatSaved)
                {
                    _logger.LogInformation($"{nameof(GameHub)} | {userId}: Round statistics wasn't saved");
                }

                await Clients.OthersInGroup(currentRoom.RoomToken).SendAsync(GameEvents.GameEnd, opponentSummary);

                currentRound.CreateNewRound();

                _logger.LogInformation($"{nameof(GameHub)} | {userId}: Move successfully processed");
                return(GameEvents.GameEnd);
            }

            _logger.LogInformation($"{nameof(GameHub)} | {userId}: Move successfully processed");
            await SendMessageToCaller("Waiting for opponent to make move");

            return(GameEvents.WaitingForPlayerToPlay);
        }