/* public members */ public static void CreateGame(int userId, string title, string password) { ValidateGameTitle(title); if (password != null) { ValidateGamePassword(password); } var context = new BattleGameEntities(); using (context) { var user = GetUser(userId, context); var game = new Game() { Title = title, Password = password, Status = context.Statuses.First(st => st.Value == GameStatusOpen), RedUser = user }; context.Games.Add(game); context.SaveChanges(); } }
protected static void SendMessage(string text, User toUser, Game game, UserMessagesType msgType, BattleGameEntities context) { toUser.UserMessages.Add(new UserMessage() { Game = game, MessageState = context.MessageStates.First(ms => ms.State == MessageStateUnread), UserMessagesType = msgType, Text = text }); }
private static Unit GetAttackedUnit(User owner, Game game, long toX, long toY) { var attackedUnit = game.Units.FirstOrDefault(u => u.PositionX == toX && u.PositionY == toY && u.HitPoints > 0); if (attackedUnit == null) { throw new ServerErrorException("Nothing to attack on that position", "INV_ATT_EMPTY"); } else if (attackedUnit.User == owner) { throw new ServerErrorException("Unit cannot attack allied units", "INV_ATT_ALLY"); } return attackedUnit; }
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"); } }
protected static void ValidateGameInProgressStatus(Game game, BattleGameEntities context) { if (game.Status.Value == GameStatusOpen) { throw new ServerErrorException("Game not yet full", "INV_GAME_STATE"); } else if (game.Status.Value == GameStatusFinished) { throw new ServerErrorException("Game is already finished", "INV_GAME_STATE"); } else if (game.Status.Value == GameStatusFull) { throw new ServerErrorException("Game is not started yet", "INV_GAME_STATE"); } }
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"); } }
protected static IEnumerable<UnitModel> GetUserUnits(User owner, Game game) { var unitModels = from unit in game.Units where unit.HitPoints > 0 && unit.User == owner select new UnitModel() { Id = unit.Id, Owner = (game.RedUser == unit.User) ? "red" : "blue", Type = unit.UnitType.Value, Mode = unit.Mode.Value, Attack = (int)unit.Attack, Armor = (int)unit.Armor, Range = (int)unit.Range, Speed = (int)unit.Speed, HitPoints = (int)unit.HitPoints, Position = new PositionModel() { X = unit.PositionX, Y = unit.PositionY } }; return unitModels.ToList(); }