Esempio n. 1
0
        /// <summary>
        /// Attacks the province.
        /// </summary>
        /// <param name="factionId">Faction identifier.</param>
        /// <param name="provinceId">Province identifier.</param>
        public BattleResult AttackProvince(string factionId, string provinceId)
        {
            Province targetProvince = world.GetProvinces().FirstOrDefault(r => r.Id == provinceId);

            if (string.IsNullOrWhiteSpace(provinceId) ||
                targetProvince.Locked ||
                !world.FactionBordersProvince(factionId, provinceId))
            {
                throw new InvalidTargetProvinceException(provinceId);
            }

            Faction attackerFaction = world.GetFactions().FirstOrDefault(f => f.Id == factionId);
            Faction defenderFaction = world.GetFactions().FirstOrDefault(f => f.Id == targetProvince.FactionId);

            if (defenderFaction.Id == attackerFaction.Id ||
                defenderFaction.Id == GameDefines.GAIA_FACTION)
            {
                throw new InvalidTargetProvinceException(provinceId);
            }

            while (world.GetFactionTroopsAmount(attackerFaction.Id) > 0 &&
                   world.GetFactionTroopsAmount(defenderFaction.Id) > 0)
            {
                Army attackerArmy = world.GetFactionArmies(attackerFaction.Id)
                                    .Where(a => a.Size > 0)
                                    .GetRandomElement();
                Army defenderArmy = world.GetFactionArmies(defenderFaction.Id)
                                    .Where(a => a.Size > 0)
                                    .GetRandomElement();

                Unit attackerUnit = world.GetUnits().FirstOrDefault(u => u.Id == attackerArmy.UnitId);
                Unit defenderUnit = world.GetUnits().FirstOrDefault(u => u.Id == defenderArmy.UnitId);

                // TODO: Attack and Defence bonuses

                int attackerTroopsLeft =
                    (attackerUnit.Health * attackerArmy.Size - defenderUnit.Power * defenderArmy.Size) /
                    attackerUnit.Health;

                int defenderTroopsLeft =
                    (defenderUnit.Health * defenderArmy.Size - attackerUnit.Power * attackerArmy.Size) /
                    defenderUnit.Health;

                attackerArmy.Size = Math.Max(0, attackerTroopsLeft);
                defenderArmy.Size = Math.Max(0, defenderTroopsLeft);
            }

            // TODO: In the GameDomainService I should change the realations based on wether the
            // province was sovereign or not

            if (world.GetFactionTroopsAmount(attackerFaction.Id) >
                world.GetFactionTroopsAmount(defenderFaction.Id))
            {
                world.TransferProvince(provinceId, factionId);
                targetProvince.Locked = true;

                return(BattleResult.Victory);
            }

            return(BattleResult.Defeat);
        }