예제 #1
0
        public async Task SelectPlay(string play)
        {
            //find the game which the player is playing
            var game = await _gameDataService.FindGameBySessionId(Context.ConnectionId);

            //update the player session with the choice the player made
            await _gameDataService.UpdatePlayForPlayer(game, Context.ConnectionId, (Play)Enum.Parse(typeof(Play), play));

            var playerSession   = game.GetPlayerSession(Context.ConnectionId);
            var opponentSession = game.GetOpponentSession(playerSession);

            if (opponentSession.Play.HasValue)
            {
                //If the opposing player has already made their play, we are ready to compute the game results and notify both players
                //Compute game result for current player
                var playerResult = _gameService.GetGameResult(
                    playerSession.Play.Value,
                    opponentSession.Play.Value).ToString();
                //Notify current player of their game result
                await Clients.Client(playerSession.Id)
                .SendAsync(GameEventNames.GameEnd, playerResult);

                //Compute game result for opposing player
                var opponentResult = _gameService.GetGameResult(
                    opponentSession.Play.Value,
                    playerSession.Play.Value).ToString();
                //Notify opposing player of their game result
                await Clients.Client(opponentSession.Id)
                .SendAsync(GameEventNames.GameEnd, opponentResult);
            }
            else
            {
                //The opposing player has not made their play yet. We notify the current player to wait for the opposing player to make a choice.
                await Clients.Caller.SendAsync(GameEventNames.WaitingForPlayerToPlay);
            }
        }