public async Task <IActionResult> GetGameState(string gameId)
        {
            ServiceEventSource.Current.ServiceMessage(context, $"GetGameState({gameId})");
            var chessClient = proxyFactory.CreateServiceProxy <IChessFabrickStatefulService>(chessStatefulUri, ChessFabrickUtils.GuidPartitionKey(gameId));

            try
            {
                var board = await chessClient.ActiveGameStateAsync(gameId);

                return(Ok(board));
            }
            catch (Exception ex)
            {
                try
                {
                    var board = await chessClient.NewGameStateAsync(gameId);

                    return(Ok(board));
                }
                catch (Exception ex1)
                {
                    try
                    {
                        var board = await chessClient.CompletedGameStateAsync(gameId);

                        return(Ok(board));
                    }
                    catch (Exception ex2)
                    {
                        return(StatusCode(500, ex2.Message));
                    }
                }
            }
        }
Example #2
0
        public async Task PerformMove()
        {
            ActorEventSource.Current.ActorMessage(this, $"PerformMove({gameId})");
            await Task.Delay(rand.Next(800, 1600));

            var chessClient = proxyFactory.CreateServiceProxy <IChessFabrickStatefulService>(chessStatefulUri, ChessFabrickUtils.GuidPartitionKey(gameId));
            var game        = await chessClient.ActiveGameStateAsync(gameId);

            while (true)
            {
                try
                {
                    var board = new Board();
                    board.PerformMoves(game.GameInfo.MoveHistory);
                    var move = GetRandomMove(board);
                    await chessClient.MovePieceAsync(gameId, ChessFabrickUtils.BOT_NAME, move.Item1, move.Item2);

                    return;
                }
                catch (Exception ex)
                {
                    ActorEventSource.Current.ActorMessage(this, $"PerformMoveException({gameId}): \n{ex}");
                }
            }
        }
        public async Task <IActionResult> PostAddBot(string gameId)
        {
            ServiceEventSource.Current.ServiceMessage(context, $"PostAddBot({gameId}): {User.Identity.Name}");
            try
            {
                var chessClient = proxyFactory.CreateServiceProxy <IChessFabrickStatefulService>(chessStatefulUri, ChessFabrickUtils.GuidPartitionKey(gameId));
                var game        = await chessClient.AddBot(gameId);

                return(Ok(game));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));
            }
        }
        public async Task <IActionResult> PostBotGame()
        {
            ServiceEventSource.Current.ServiceMessage(context, $"PostBotGame(): {User.Identity.Name}");
            try
            {
                var gameId      = $"{ChessFabrickUtils.BOT_NAME}-{Guid.NewGuid()}";
                var chessClient = proxyFactory.CreateServiceProxy <IChessFabrickStatefulService>(chessStatefulUri, ChessFabrickUtils.GuidPartitionKey(gameId));
                var newGame     = await chessClient.NewGameAsync(gameId, ChessFabrickUtils.BOT_NAME, PieceColor.White);

                var startedGame = await chessClient.AddBot(gameId);

                return(Ok(startedGame));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));
            }
        }
        public async Task <IActionResult> PostNewGame([FromBody] NewGameModel model)
        {
            ServiceEventSource.Current.ServiceMessage(context, $"PostNewGame({model.PlayerColor}): {User.Identity.Name}");
            try
            {
                var gameId      = $"{User.Identity.Name}-{Guid.NewGuid()}";
                var chessClient = proxyFactory.CreateServiceProxy <IChessFabrickStatefulService>(chessStatefulUri, ChessFabrickUtils.GuidPartitionKey(gameId));
                var game        = await chessClient.NewGameAsync(gameId, User.Identity.Name, model.PlayerColor);

                return(Ok(game));
            } catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));
            }
        }
Example #6
0
        public async Task <List <string> > GetPieceMoves(string gameId, string field)
        {
            ServiceEventSource.Current.ServiceMessage(serviceContext, $"GetPieceMoves({gameId}, {field}): {Context.User.Identity.Name}");
            try
            {
                var chessClient = proxyFactory.CreateServiceProxy <IChessFabrickStatefulService>(chessStatefulUri, ChessFabrickUtils.GuidPartitionKey(gameId));
                var moves       = await chessClient.ListPieceMovesAsync(gameId, field);

                return(moves);
            } catch (Exception ex)
            {
                throw new HubException(ex.Message);
            }
        }
Example #7
0
        public async Task <ChessGameState> SpectateGame(string gameId)
        {
            ServiceEventSource.Current.ServiceMessage(serviceContext, $"SpectateGame({gameId}): {Context.User.Identity.Name}");
            try
            {
                var chessClient = proxyFactory.CreateServiceProxy <IChessFabrickStatefulService>(chessStatefulUri, ChessFabrickUtils.GuidPartitionKey(gameId));
                var board       = await chessClient.ActiveGameStateAsync(gameId);

                await Groups.AddToGroupAsync(Context.ConnectionId, ChessFabrickUtils.GameGroupName(gameId));

                return(board);
            }
            catch (Exception ex)
            {
                throw new HubException(ex.Message);
            }
        }
Example #8
0
        public async Task <ChessGameState> JoinGame(string gameId)
        {
            ServiceEventSource.Current.ServiceMessage(serviceContext, $"JoinGame({gameId}): {Context.User.Identity.Name}");
            try
            {
                var chessClient = proxyFactory.CreateServiceProxy <IChessFabrickStatefulService>(chessStatefulUri, ChessFabrickUtils.GuidPartitionKey(gameId));
                var board       = await chessClient.ActiveGameStateAsync(gameId);

                if (board.GameInfo.White.Name != Context.User.Identity.Name && board.GameInfo.Black.Name != Context.User.Identity.Name)
                {
                    throw new ArgumentException("Player not in the game.");
                }
                Clients.User(board.GameInfo.White.Name == Context.User.Identity.Name ? board.GameInfo.Black.Name : board.GameInfo.White.Name)
                .SendAsync("OnPlayerJoined", board);
                return(board);
            }
            catch (Exception ex)
            {
                throw new HubException(ex.Message);
            }
        }
Example #9
0
        public async Task <ChessGameState> MovePiece(string gameId, string from, string to)
        {
            try
            {
                ServiceEventSource.Current.ServiceMessage(serviceContext, $"MovePiece({gameId}, {from}, {to}): {Context.User.Identity.Name}");
                var chessClient = proxyFactory.CreateServiceProxy <IChessFabrickStatefulService>(chessStatefulUri, ChessFabrickUtils.GuidPartitionKey(gameId));
                var board       = await chessClient.MovePieceAsync(gameId, Context.User.Identity.Name, from, to);

                return(board);
            }
            catch (Exception ex)
            {
                throw new HubException(ex.Message);
            }
        }