Exemplo n.º 1
0
        public ApiResult JoinBattle(JObject data)
        {
            dynamic json       = data;
            int     UserKey    = (int)json.userKey;
            String  battleGUID = json.battleGUID;
            bool    joinBattle = (bool)json.joinBattle;

            try
            {
                using (SBEntities db = new SBEntities())
                {
                    int        battleKey = db.Battles.Where(w => w.BattleGUID == battleGUID).FirstOrDefault().BattleKey;
                    UserBattle ub        = db.UserBattles.Where(w => w.BattleKey == battleKey && w.UsersKey == UserKey).FirstOrDefault();

                    if (joinBattle && ub == null)
                    {
                        ub           = new UserBattle();
                        ub.BattleKey = battleKey;
                        ub.UsersKey  = UserKey;
                        db.UserBattles.Add(ub);
                        db.SaveChanges();
                        return(new ApiResult(true, string.Empty, "User added to battle " + battleGUID));
                    }
                    else if (!joinBattle && ub != null)
                    {
                        db.UserBattles.Remove(ub);
                        db.SaveChanges();
                        return(new ApiResult(true, string.Empty, "User removed from battle " + battleGUID));
                    }
                }
            }
            catch (Exception ex)
            {
                return(new ApiResult(false, string.Empty, ex.Message));
            }

            return(new ApiResult(false, string.Empty, "No changes where made."));
        }
Exemplo n.º 2
0
        public async Task <BattleResultViewModel> Attack(string attackerId, int defenderCityId)
        {
            var attackerCityId = this.userCityRepo.All().FirstOrDefault(x => x.UserId == attackerId).CityId; //// Need attacker City Id !
            var defenderId     = this.userCityRepo.All().FirstOrDefault(x => x.CityId == defenderCityId).UserId;
            // Attack priority should come as an input
            var attackPriority = new List <UnitType>()
            {
                UnitType.Artillery, UnitType.Archers, UnitType.Cavalry, UnitType.Infantry
            };

            var attackerArmy = this.GetArmy(attackerCityId, attackPriority);
            var opponentArmy = this.GetArmy(defenderCityId, attackPriority);
            var battleResult = this.SetInitialBattleResultData(attackerArmy, opponentArmy, attackerId, defenderId);
            var battleReport = new List <string>();

            var attacker = await this.userManager.FindByIdAsync(attackerId);

            var defender = await this.userManager.FindByIdAsync(defenderId);

            //// BattleLogic

            while (attackerArmy.TotalArmyCount > 0 && opponentArmy.TotalArmyCount > 0)
            {
                foreach (var unit in attackerArmy.Army.Where(x => x.Count > 0))
                {
                    var opponentDefendingUnit = SelectDefendingUnit(unit.AttackPriority, opponentArmy);
                    this.UnitAttack(unit.TotalUnitAttack, opponentDefendingUnit);
                    UpdateBattleReport(battleReport, unit, opponentDefendingUnit, attacker, defender);

                    if (attackerArmy.TotalArmyCount == 0 || opponentArmy.TotalArmyCount == 0)
                    {
                        break;
                    }

                    // Opponent turn
                    var opponentAttackUnit = opponentArmy.Army.FirstOrDefault(x => x.Count > 0 && x.HasAttacked == false);
                    if (opponentAttackUnit != null)
                    {
                        var attackerDefendingUnit = SelectDefendingUnit(opponentAttackUnit.AttackPriority, attackerArmy);
                        this.UnitAttack(opponentAttackUnit.TotalUnitAttack, attackerDefendingUnit);

                        UpdateBattleReport(battleReport, opponentAttackUnit, attackerDefendingUnit, defender, attacker);

                        opponentAttackUnit.HasAttacked = true;
                    }

                    if (attackerArmy.TotalArmyCount == 0 || opponentArmy.TotalArmyCount == 0)
                    {
                        break;
                    }
                }

                while (opponentArmy.Army.Any(x => x.HasAttacked == false && x.Count > 0))
                {
                    var opponentAttackUnit    = opponentArmy.Army.FirstOrDefault(x => x.Count > 0 && x.HasAttacked == false);
                    var attackerDefendingUnit = SelectDefendingUnit(opponentAttackUnit.AttackPriority, attackerArmy);
                    this.UnitAttack(opponentAttackUnit.TotalUnitAttack, attackerDefendingUnit);
                    UpdateBattleReport(battleReport, opponentAttackUnit, attackerDefendingUnit, defender, attacker);


                    if (attackerArmy.TotalArmyCount == 0 || opponentArmy.TotalArmyCount == 0)
                    {
                        break;
                    }
                }

                opponentArmy.Army.ForEach(x => { x.HasAttacked = false; });
            }

            UpdateBattleResult(attackerId, defenderId, attackerArmy, opponentArmy, battleResult);
            var userBattle = new UserBattle()
            {
                BattleResult = battleResult, UserId = attackerId
            };

            await this.userBattleRepo.AddAsync(userBattle);

            await this.userBattleRepo.SaveChangesAsync();

            var result = this.mapper.Map <BattleResultViewModel>(battleResult);

            result.BattleReport = battleReport;

            return(result);
        }
Exemplo n.º 3
0
        public Battle SaveBattle([FromBody] JObject data)
        {
            using (SBEntities db = new SBEntities())
            {
                dynamic json = data;
                try
                {
                    string battleGUID = (string)json["BattleGUID"];

                    // this should handle DST
                    TimeZoneInfo est        = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
                    DateTime     battleDate = TimeZoneInfo.ConvertTimeToUtc(DateTime.Parse((string)json["BattleDate"]), est);
                    Battle       b;
                    bool         newBattle = false;
                    if (!string.IsNullOrEmpty(battleGUID))
                    {
                        b = db.Battles.Where(w => w.BattleGUID == battleGUID).FirstOrDefault();
                    }
                    else
                    {
                        newBattle     = true;
                        b             = new Battle();
                        b.BattleGUID  = Guid.NewGuid().ToString();
                        b.DateCreated = DateTime.UtcNow;
                        b.Deleted     = false;
                        db.Battles.Add(b);
                    }

                    b.GameKey    = (int)json["GameKey"];
                    b.Title      = (string)json["Title"];
                    b.BattleDate = battleDate;
                    b.Details    = (string)json["Details"];
                    b.CreatorKey = (int)json["CreatorKey"];

                    db.SaveChanges();

                    // The user is added to their own battle
                    if (newBattle)
                    {
                        UserBattle ub = db.UserBattles.Where(w => w.UsersKey == b.CreatorKey && w.BattleKey == b.BattleKey).FirstOrDefault();
                        if (ub == null)
                        {
                            ub           = new UserBattle();
                            ub.UsersKey  = b.CreatorKey;
                            ub.BattleKey = b.BattleKey;
                            db.UserBattles.Add(ub);
                            db.SaveChanges();
                        }
                        try
                        {
                            User u = db.Users.Where(w => w.UserKey == b.CreatorKey).FirstOrDefault();
                            if (u != null)
                            {
                                ShackNewsHelper.SendBattleCreation(u.Username, b.BattleKey);
                            }
                        }
                        catch
                        {
                        }
                    }

                    return(b);
                }
                catch (Exception)
                {
                    // TODO: handle
                }
                return(null);
            }
        }