/* public methods */ public static void CreateUser(string username, string nickname, string authCode) { ValidateUsername(username); ValidateNickname(nickname); ValidateAuthCode(authCode); using (BattleGameEntities context = new BattleGameEntities()) { var usernameToLower = username.ToLower(); var nicknameToLower = nickname.ToLower(); var dbUser = context.Users.FirstOrDefault(u => u.Username == usernameToLower || u.Nickname.ToLower() == nicknameToLower); if (dbUser != null) { if (dbUser.Username.ToLower() == usernameToLower) { throw new ServerErrorException("Username already exists", "ERR_DUP_USR"); } else { throw new ServerErrorException("Nickname already exists", "ERR_DUP_NICK"); } } dbUser = new User() { Username = usernameToLower, Nickname = nickname, AuthCode = authCode }; context.Users.Add(dbUser); 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"); } }
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"); } }
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(); }