private static void ValidateUnitAttackPosition(Game game, Unit unit, User owner, long toX, long toY)
 {
     if (toX < 0 || toX > BattleFieldMaxX || toY < 0 || toY > BattleFieldMaxY)
     {
         throw new ServerErrorException("Units cannot attack outside of the battle field", "INV_ATT_POS");
     }
     var diffX = Math.Abs(unit.PositionX - toX);
     var diffY = Math.Abs(unit.PositionY - toY);
     if (diffX + diffY > unit.Range)
     {
         throw new ServerErrorException("The Unit cannot attack that far", "INV_ATT_RANGE");
     }
 }
 private static Unit GetWarrior(UnitType type, int x, int y)
 {
     var warrior = new Unit()
     {
         PositionX = x,
         PositionY = y,
         HitPoints = WarriorHitPoints,
         Attack = WarriorAttack,
         Armor = WarriorArmor,
         Range = WarriorRange,
         Speed = WarriorSpeed,
         UnitType = type
     };
     return warrior;
 }
 private static Unit GetRanger(UnitType type, int x, int y)
 {
     var ranger = new Unit()
     {
         PositionX = x,
         PositionY = y,
         HitPoints = RangerHitPoints,
         Attack = RangerAttack,
         Armor = RangerArmor,
         Range = RangerRange,
         Speed = RangerSpeed,
         UnitType = type
     };
     return ranger;
 }
        private static void ValidateUserUnitInGame(User user, Unit unit, Game game)
        {
            if (game.UserInTurn != user.Id)
            {
                throw new ServerErrorException("It is not your turn", "INV_USR_TURN");
            }

            if (!user.Units.Any(u => u == unit))
            {
                throw new ServerErrorException("This is not your unit", "INV_USR_UNIT");
            }

            if (!game.Units.Any(u => u == unit))
            {
                throw new ServerErrorException("No such unit in the game", "INV_USR_GAME");
            }
        }
 private static void ValidateUnitMovePosition(Game game, Unit unit, long toX, long toY)
 {
     if (toX < 0 || toX > BattleFieldMaxX || toY < 0 || toY > BattleFieldMaxY)
     {
         throw new ServerErrorException("Units cannot go outside of the battle field", "INV_UNIT_POS");
     }
     var diffX = Math.Abs(unit.PositionX - toX);
     var diffY = Math.Abs(unit.PositionY - toY);
     if (diffX + diffY > unit.Speed)
     {
         throw new ServerErrorException("Unit cannot move to that position", "INV_UNIT_POS");
     }
     if (game.Units.Any(u => u.HitPoints > 0 && u.PositionX == toX && u.PositionY == toY))
     {
         throw new ServerErrorException("Position is already occupied", "INV_UNIT_POS");
     }
 }