Example #1
0
        public async Task <dynamic> ChangeLife(ChangeLifeRequest request)
        {
            Guid gameIdGuid;

            if (!Guid.TryParse(request.GameId, out gameIdGuid))
            {
                return(InvalidGameId(request.GameId));
            }

            var game = await _context.Games
                       .Where(g => g.Id == gameIdGuid)
                       .Include(g => g.FirstPlayer)
                       .Include(g => g.SecondPlayer)
                       .FirstOrDefaultAsync();

            if (game == null)
            {
                return(GameNotFound(request.GameId));
            }

            var player = await _context.Users.FirstOrDefaultAsync(u => u.Id == request.PlayerId);

            if (player == null)
            {
                return(PlayerNotFound(request.PlayerId));
            }

            var delta = request.Increment ? 1 : -1;

            if (game.FirstPlayer == player)
            {
                game.FirstPlayerHp += delta;
            }
            else if (game.SecondPlayer == player)
            {
                game.SecondPlayerHp += delta;
            }

            await _context.SaveChangesAsync();

            return(Json(new Response <bool>
            {
                Result = true
            }));
        }
Example #2
0
        public dynamic ChangeLife(string gameId, bool increment)
        {
            var gameRequest = new ChangeLifeRequest
            {
                GameId    = gameId,
                PlayerId  = UserId,
                Increment = increment
            };
            var result = Network.MakePostRequest <bool>("games/changeLife", gameRequest);
            var failed = result.StatusCode != HttpStatusCode.OK || result.Data.Failed;

            return(Json(new
            {
                Failed = failed,
                Error = failed ? result.Content : null,
                Data = result.Data.Result
            }));
        }