コード例 #1
0
        public JsonResult Fight(int battleID, int weaponQuality, bool isAttacker)
        {
            try
            {
                var citizen = SessionHelper.CurrentEntity.Citizen;
                var battle  = battleRepository.GetById(battleID);

                if (battle == null)
                {
                    return(JsonError("Battle does not exist!"));
                }

                if (citizen == null)
                {
                    return(JsonError("You are not a citizen!"));
                }

                if (citizen.HitPoints < 30)
                {
                    return(JsonError("You must have more than 30 HP to fight!"));
                }



                if (weaponQuality > 0)
                {
                    if (citizen.Entity.GetEquipmentItem(ProductTypeEnum.Weapon, weaponQuality, productRepository).Amount == 0)
                    {
                        return(JsonError("You do not have such weapon in inventory!"));
                    }
                }

                if (battle.War.IsTrainingWar == false && battle.GetTimeLeft(GameHelper.CurrentDay).TotalSeconds < 0)
                {
                    return(JsonError("You cannot take part in the fight!"));
                }

                WarSideEnum warSide = WarSideEnum.Attacker;
                if (isAttacker == false)
                {
                    warSide = WarSideEnum.Defender;
                }

                if (warService.CanFightAs(battle, battle.War, citizen, warSide) == false)
                {
                    return(JsonError("You cannot fight in that battle!"));
                }

                var damage = battleService.CalculateDamage(citizen, battle, weaponQuality, isAttacker);
                battleService.ParticipateInBattle(citizen, battle, isAttacker, weaponQuality);

                return(Json(new JsonFightModel(damage, citizen.HitPoints)));
            }
            catch (Exception e)
            {
                return(JsonDebugError(e));
            }
        }