Beispiel #1
0
        public WebDriverResponse Post(MoveToRequest request)
        {
            var session = _sessionRepository.GetById(request.SessionId);

            _automationService.MoveTo(session, request.Element, request.XOffset, request.YOffset);
            return(new WebDriverResponse(session)
            {
                Status = StatusCode.Success
            });
        }
Beispiel #2
0
        public dynamic MoveTo(string gameId, string cardLinkId, string where, string from)
        {
            var gameRequest = new MoveToRequest
            {
                GameId     = gameId,
                PlayerId   = UserId,
                CardLinkId = cardLinkId,
                Where      = (WhereToMove)Enum.Parse(typeof(WhereToMove), @where),
                From       = (MoveFrom)Enum.Parse(typeof(MoveFrom), from),
            };

            var result = Network.MakePostRequest <bool>("cards/moveTo", gameRequest);
            var failed = result.StatusCode != HttpStatusCode.OK || result.Data.Failed;

            return(Json(new
            {
                Failed = failed,
                Error = failed ? result.Content : null,
                Data = result.Data
            }));
        }
Beispiel #3
0
        public async Task <dynamic> MoveTo(MoveToRequest request)
        {
            Guid gameIdGuid;

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

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

            switch (request.Where)
            {
            case WhereToMove.Hand:
                gameQuery = gameQuery.Include(g => g.HandCards);
                break;

            case WhereToMove.Top:
                gameQuery = gameQuery.Include(g => g.Top);
                break;

            case WhereToMove.Bottom:
                gameQuery = gameQuery.Include(g => g.Bottom);
                break;

            case WhereToMove.Graveyard:
                gameQuery = gameQuery.Include(g => g.Grave);
                break;

            case WhereToMove.Exile:
                gameQuery = gameQuery.Include(g => g.Exile);
                break;

            case WhereToMove.Battlefield:
                gameQuery = gameQuery.Include(g => g.BattlefieldCards);
                break;
            }

            switch (request.From)
            {
            case MoveFrom.Hand:
                gameQuery = gameQuery.Include(g => g.HandCards);
                break;

            case MoveFrom.Top:
                gameQuery = gameQuery.Include(g => g.Top);
                break;

            case MoveFrom.Graveyard:
                gameQuery = gameQuery.Include(g => g.Grave);
                break;

            case MoveFrom.Exile:
                gameQuery = gameQuery.Include(g => g.Exile);
                break;

            case MoveFrom.Battlefield:
                gameQuery = gameQuery.Include(g => g.BattlefieldCards);
                break;
            }
            var game = await gameQuery.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));
            }

            if (game.FirstPlayer != player && game.SecondPlayer != player)
            {
                return(PlayerNotInGame(request.PlayerId, request.GameId));
            }

            Guid cardlinkGuid;

            if (!Guid.TryParse(request.CardLinkId, out cardlinkGuid))
            {
                return(InvalidCardLinkId(request.CardLinkId));
            }

            var cardLink = await _context.CardLinks.Include(l => l.Token).Include(l => l.Card).FirstOrDefaultAsync(l => l.Id == cardlinkGuid);

            if (cardLink == null)
            {
                return(CardNotFound(request.CardLinkId, MoveFrom.NA));
            }

            //if token moved anywhere it just disappears
            if (cardLink.Card == null && cardLink.Token != null)
            {
                var token = cardLink.Token;
                _context.CardLinks.Remove(cardLink);
                _context.Tokens.Remove(token);
                await _context.SaveChangesAsync();

                return(Json(new Response <bool>
                {
                    Result = true
                }));
            }

            int max;

            switch (request.Where)
            {
            case WhereToMove.Top:
                max = game.Top.Any() ? game.Top.Max(t => t.Number) : 0;
                game.Top.Add(new Top {
                    CardLink = cardLink, Number = ++max
                });
                break;

            case WhereToMove.Bottom:
                max = game.Bottom.Any() ? game.Bottom.Max(t => t.Number) : 0;
                game.Bottom.Add(new Bottom {
                    CardLink = cardLink, Number = ++max
                });
                break;

            case WhereToMove.Graveyard:
                max = game.Grave.Any() ? game.Grave.Max(t => t.Number) : 0;
                game.Grave.Add(new Grave {
                    CardLink = cardLink, Number = ++max
                });
                break;

            case WhereToMove.Exile:
                max = game.Exile.Any() ? game.Exile.Max(t => t.Number) : 0;
                game.Exile.Add(new Exile {
                    CardLink = cardLink, Number = ++max
                });
                break;

            case WhereToMove.Hand:
                game.HandCards.Add(new HandCard {
                    CardLink = cardLink, Player = player
                });
                _context.Games.AddOrUpdate(game);
                break;

            case WhereToMove.Battlefield:
                game.BattlefieldCards.Add(new BattlefieldCard {
                    CardLink = cardLink, Player = player
                });
                _context.Games.AddOrUpdate(game);
                break;
            }

            switch (request.From)
            {
            case MoveFrom.Hand:
                var handCard = game.HandCards.FirstOrDefault(c => c.CardLink == cardLink && c.Player == player);
                if (handCard == null)
                {
                    return(CardNotFound(request.CardLinkId, MoveFrom.Hand));
                }
                _context.HandCards.Remove(handCard);
                break;

            case MoveFrom.Top:
                var top = game.Top.FirstOrDefault(t => t.CardLink == cardLink);
                if (top == null)
                {
                    return(CardNotFound(request.CardLinkId, MoveFrom.Top));
                }
                _context.Tops.Remove(game.Top.First(t => t.CardLink == cardLink));
                break;

            case MoveFrom.Graveyard:
                var grave = game.Grave.FirstOrDefault(g => g.CardLink == cardLink);
                if (grave == null)
                {
                    return(CardNotFound(request.CardLinkId, MoveFrom.Graveyard));
                }
                _context.Graves.Remove(grave);
                break;

            case MoveFrom.Exile:
                var exile = game.Exile.FirstOrDefault(g => g.CardLink == cardLink);
                if (exile == null)
                {
                    return(CardNotFound(request.CardLinkId, MoveFrom.Exile));
                }
                _context.Exiles.Remove(exile);
                break;

            case MoveFrom.Battlefield:
                var bf = game.BattlefieldCards.FirstOrDefault(b => b.CardLink == cardLink);
                if (bf == null)
                {
                    return(CardNotFound(request.CardLinkId, MoveFrom.Battlefield));
                }
                _context.BattlefieldCards.Remove(bf);
                break;
            }

            await _context.SaveChangesAsync();

            return(Json(new Response <bool>
            {
                Result = true
            }));
        }