Example #1
0
        ///<summary>
        /// Chưa xử lý trường hợp có catapult
        ///</summary>
        ///<remarks>
        /// This is a test.
        ///</remarks>
        public override MovingCommand Effect(ISession session)
        {


            #region Data Declaration


            double luck, ratio;
            int spearLostInAttackSide;
            int swordLostInAttackSide;
            int axeLostInAttackSide;
            int scoutLostInAttackSide;
            int lightCavalryLostInAttackSide;
            int heavyCavalryLostInAttackSide;
            int ramLostInAttackSide;
            int catapultLostInAttackSide;
            int nobleLostInAttackSide;

            int spearLostInDefenseSide;
            int swordLostInDefenseSide;
            int axeLostInDefenseSide;
            int scoutLostInDefenseSide;
            int lightCavalryLostInDefenseSide;
            int heavyCavalryLostInDefenseSide;
            int ramLostInDefenseSide;
            int catapultLostInDefenseSide;
            int nobleLostInDefenseSide;

            Configuration config = Configuration.TribalWarsConfiguration;
            double spearDamage = config.GetNumericConfigurationItem("Unit.spear_damage").Value;
            double swordDamage = config.GetNumericConfigurationItem("Unit.sword_damage").Value;
            double axeDamage = config.GetNumericConfigurationItem("Unit.axe_damage").Value;
            double lightCavalryDamage = config.GetNumericConfigurationItem("Unit.light_cavalry_damage").Value;
            double heavyCavalryDamage = config.GetNumericConfigurationItem("Unit.heavy_cavalry_damage").Value;
            double scoutDamage = config.GetNumericConfigurationItem("Unit.scout_damage").Value;
            double ramDamage = config.GetNumericConfigurationItem("Unit.ram_damage").Value;
            double catapultDamage = config.GetNumericConfigurationItem("Unit.catapult_damage").Value;
            double nobleDamage = config.GetNumericConfigurationItem("Unit.noble_damage").Value;

            double spearInfantryDefense = config.GetNumericConfigurationItem("Unit.spear_infantry_defense").Value;
            double swordInfantryDefense = config.GetNumericConfigurationItem("Unit.sword_infantry_defense").Value;
            double axeInfantryDefense = config.GetNumericConfigurationItem("Unit.axe_infantry_defense").Value;
            double lightCavalryInfantryDefense = config.GetNumericConfigurationItem("Unit.light_cavalry_infantry_defense").Value;
            double heavyCavalryInfantryDefense = config.GetNumericConfigurationItem("Unit.heavy_cavalry_infantry_defense").Value;
            double scoutInfantryDefense = config.GetNumericConfigurationItem("Unit.scout_infantry_defense").Value;
            double ramInfantryDefense = config.GetNumericConfigurationItem("Unit.ram_infantry_defense").Value;
            double catapultInfantryDefense = config.GetNumericConfigurationItem("Unit.catapult_infantry_defense").Value;
            double nobleInfantryDefense = config.GetNumericConfigurationItem("Unit.noble_infantry_defense").Value;

            double spearCavalryDefense = config.GetNumericConfigurationItem("Unit.spear_cavalry_defense").Value;
            double swordCavalryDefense = config.GetNumericConfigurationItem("Unit.sword_cavalry_defense").Value;
            double axeCavalryDefense = config.GetNumericConfigurationItem("Unit.axe_cavalry_defense").Value;
            double lightCavalryCavalryDefense = config.GetNumericConfigurationItem("Unit.light_cavalry_cavalry_defense").Value;
            double heavyCavalryCavalryDefense = config.GetNumericConfigurationItem("Unit.heavy_cavalry_cavalry_defense").Value;
            double scoutCavalryDefense = config.GetNumericConfigurationItem("Unit.scout_cavalry_defense").Value;
            double ramCavalryDefense = config.GetNumericConfigurationItem("Unit.ram_cavalry_defense").Value;
            double catapultCavalryDefense = config.GetNumericConfigurationItem("Unit.catapult_cavalry_defense").Value;
            double nobleCavalryDefense = config.GetNumericConfigurationItem("Unit.noble_cavalry_defense").Value;

            double spearHaul = config.GetNumericConfigurationItem("Unit.spear_can_haul").Value;
            double swordHaul = config.GetNumericConfigurationItem("Unit.sword_can_haul").Value;
            double axeHaul = config.GetNumericConfigurationItem("Unit.axe_can_haul").Value;
            double lightCavalryHaul = config.GetNumericConfigurationItem("Unit.light_cavalry_can_haul").Value;
            double heavyCavalryHaul = config.GetNumericConfigurationItem("Unit.heavy_cavalry_can_haul").Value;

            double catapultBuldingDamage = config.GetNumericConfigurationItem("Unit.catapult_damage_building").Value;

            Return returnTroop = null;
            AttackReport attackReport;
            DefenseReport defenseReport;

            IList<Station> stations;
            //IList<Village> villages;
            #endregion


            #region Report
            attackReport = new AttackReport();
            attackReport.Time = this.LandingTime;
            attackReport.Owner = this.FromVillage.Player;
            attackReport.Title = string.Format("{0} tấn công {1} ({2}|{3})", this.FromVillage.Player.Username, this.ToVillage.Name, this.ToVillage.X.ToString("000"), this.ToVillage.Y.ToString("000"));
            defenseReport = new DefenseReport();
            defenseReport.Time = this.LandingTime;
            defenseReport.Owner = this.ToVillage.Player;
            defenseReport.Title = attackReport.Title;

            attackReport.FromVillage = this.FromVillage;
            attackReport.FromPlayer = this.FromVillage.Player;
            attackReport.ToVillage = this.ToVillage;
            attackReport.ToPlayer = this.ToVillage.Player;
            defenseReport.FromVillage = this.FromVillage;
            defenseReport.FromPlayer = this.FromVillage.Player;
            defenseReport.ToVillage = this.ToVillage;
            defenseReport.ToPlayer = this.ToVillage.Player;

            attackReport.SpearSent = this.Spear;
            attackReport.SwordSent = this.Sword;
            attackReport.AxeSent = this.Axe;
            attackReport.ScoutSent = this.Scout;
            attackReport.LightCavalrySent = this.LightCavalry;
            attackReport.HeavyCavalrySent = this.HeavyCavalry;
            attackReport.RamSent = this.Ram;
            attackReport.CatapultSent = this.Catapult;
            attackReport.NobleSent = this.Noble;
            defenseReport.SpearSent = this.Spear;
            defenseReport.SwordSent = this.Sword;
            defenseReport.AxeSent = this.Axe;
            defenseReport.ScoutSent = this.Scout;
            defenseReport.LightCavalrySent = this.LightCavalry;
            defenseReport.HeavyCavalrySent = this.HeavyCavalry;
            defenseReport.RamSent = this.Ram;
            defenseReport.CatapultSent = this.Catapult;
            defenseReport.NobleSent = this.Noble;

            attackReport.SpearDefense = this.ToVillage.VillageTroopData.SpearInVillage;
            attackReport.SwordDefense = this.ToVillage.VillageTroopData.SwordInVillage;
            attackReport.AxeDefense = this.ToVillage.VillageTroopData.AxeInVillage;
            attackReport.ScoutDefense = this.ToVillage.VillageTroopData.ScoutInVillage;
            attackReport.LightCavalryDefense = this.ToVillage.VillageTroopData.LightCavalryInVillage;
            attackReport.HeavyCavalryDefense = this.ToVillage.VillageTroopData.HeavyCavalryInVillage;
            attackReport.RamDefense = this.ToVillage.VillageTroopData.RamInVillage;
            attackReport.CatapultDefense = this.ToVillage.VillageTroopData.CatapultInVillage;
            attackReport.NobleDefense = this.ToVillage.VillageTroopData.NobleInVillage;
            defenseReport.SpearDefense = this.ToVillage.VillageTroopData.SpearInVillage;
            defenseReport.SwordDefense = this.ToVillage.VillageTroopData.SwordInVillage;
            defenseReport.AxeDefense = this.ToVillage.VillageTroopData.AxeInVillage;
            defenseReport.ScoutDefense = this.ToVillage.VillageTroopData.ScoutInVillage;
            defenseReport.LightCavalryDefense = this.ToVillage.VillageTroopData.LightCavalryInVillage;
            defenseReport.HeavyCavalryDefense = this.ToVillage.VillageTroopData.HeavyCavalryInVillage;
            defenseReport.RamDefense = this.ToVillage.VillageTroopData.RamInVillage;
            defenseReport.CatapultDefense = this.ToVillage.VillageTroopData.CatapultInVillage;
            defenseReport.NobleDefense = this.ToVillage.VillageTroopData.NobleInVillage;

            attackReport.LoyalBefore = (int)this.ToVillage.Loyal;
            attackReport.Building = this.Building;
            attackReport.BuildingBefore = this.ToVillage[this.Building];
            attackReport.WallBefore = this.ToVillage[BuildingType.Wall];
            defenseReport.LoyalBefore = (int)this.ToVillage.Loyal;
            defenseReport.Building = this.Building;
            defenseReport.BuildingBefore = this.ToVillage[this.Building];
            defenseReport.WallBefore = this.ToVillage[BuildingType.Wall];

            #endregion
            int attackBonus = this.FromVillage.VillageResearchData.Attack;
            int defenseBonus = this.ToVillage.VillageResearchData.Defense;
            int attackLevel = this.FromVillage.VillageResearchData.Attack + ((this.Hero==null)?0:this.Hero.Attack);
            int defenseLevel = this.ToVillage.VillageResearchData.Defense +((this.ToVillage.MainHero==null)?0:this.ToVillage.MainHero.Defense);

            Random r = new Random();
            luck = 0.3 * (2 * r.NextDouble() - 1);
            double infantryAttack = this.Spear * spearDamage + this.Sword * swordDamage + this.Axe * axeDamage + this.Noble * nobleDamage;
            infantryAttack += infantryAttack * Research.AttackValuesDictionary[attackLevel];
            double cavalryAttack = this.LightCavalry * lightCavalryDamage + this.HeavyCavalry * heavyCavalryDamage;
            cavalryAttack += cavalryAttack * Research.AttackValuesDictionary[attackLevel];
            double totalAttack = infantryAttack + cavalryAttack;
            double scoutAttack = this.Scout * scoutDamage;
            scoutAttack += scoutAttack * Research.AttackValuesDictionary[attackLevel];
            double ramAttack = this.Ram * ramDamage;
            ramAttack += ramAttack * Research.AttackValuesDictionary[attackLevel];

            double pInfantry = (double)infantryAttack / (double)(totalAttack + 1);
            double pCavalry = (double)cavalryAttack / (double)(totalAttack + 1);
            

            double infantryDefense = this.ToVillage.VillageTroopData.SpearInVillage * spearInfantryDefense + this.ToVillage.VillageTroopData.SwordInVillage * swordInfantryDefense + this.ToVillage.VillageTroopData.AxeInVillage * axeInfantryDefense + this.ToVillage.VillageTroopData.LightCavalryInVillage * lightCavalryInfantryDefense + this.ToVillage.VillageTroopData.HeavyCavalryInVillage * heavyCavalryInfantryDefense + this.ToVillage.VillageTroopData.NobleInVillage * nobleInfantryDefense;
            double cavalryDefense = this.ToVillage.VillageTroopData.SpearInVillage * spearCavalryDefense + this.ToVillage.VillageTroopData.SwordInVillage * swordCavalryDefense + this.ToVillage.VillageTroopData.AxeInVillage * axeCavalryDefense + this.ToVillage.VillageTroopData.LightCavalryInVillage * lightCavalryCavalryDefense + this.ToVillage.VillageTroopData.HeavyCavalryInVillage * heavyCavalryCavalryDefense + this.ToVillage.VillageTroopData.NobleInVillage * nobleCavalryDefense;
            double scoutDefense = this.ToVillage[TroopType.Scout] * scoutCavalryDefense;
            scoutDefense += scoutDefense * Research.DefenseValuesDictionary[defenseLevel];
            double wallDefense = 0;
            for (int i = 1; i <= this.ToVillage.VillageBuildingData.Wall; i++)
                wallDefense += Build.GetPrice(BuildingType.Wall, i, 1).BuildTime;
            wallDefense /= 75000;
            
            wallDefense += wallDefense * Research.DefenseValuesDictionary[defenseLevel];
            double totalDefense = (long)(infantryDefense * pInfantry + cavalryDefense * pCavalry) + this.ToVillage.VillageBuildingData.BasicDefense + 1;
            totalDefense += totalDefense * this.ToVillage.VillageBuildingData.Fortified;
            totalDefense += totalDefense * Research.DefenseValuesDictionary[defenseLevel];
            
            double luckHeroes = luck;
            if (this.Hero != null)
                luckHeroes += 0.001 * this.Hero.Luck;
            if (this.ToVillage.MainHero != null)
                luckHeroes -= 0.001 * this.ToVillage.MainHero.Luck;

            double experienceAttackingHero = totalDefense;
            double experienceDefendingHero = totalAttack;

            scoutAttack += (long)(scoutAttack * luckHeroes);
            totalAttack += (long)(totalAttack * luckHeroes);
            ramAttack += (long)(ramAttack * luckHeroes);
            if (totalAttack > totalDefense)
            {
                totalAttack = ((totalAttack - totalDefense) / (totalAttack) + 1) * totalAttack;
                ramAttack = ((totalAttack - totalDefense) / (totalAttack) + 1) * ramAttack;
            }
            else
            {
                totalDefense = ((totalDefense - totalAttack) / (totalDefense) + 1) * totalDefense;
                ramAttack = ((totalDefense - totalAttack) / (totalDefense) + 1) * ramAttack;
            }
            //totalAttack = (totalAttack / totalDefense) * totalAttack;

            if (this.Ram > 0)
            {
                bool change = false;
                double damaged = wallDefense - ramAttack;
                double hp = 0;
                for (int i = 1; i <= this.ToVillage[BuildingType.Wall]; i++)
                {
                    hp += Build.GetPrice(BuildingType.Wall, i, 1).BuildTime / 75000;
                    if (hp > damaged)
                    {
                        if (this.ToVillage.VillageBuildingData.Wall != (i - 1))
                        {
                            this.ToVillage.UpgradeBuilding(BuildingType.Wall, i - 1);
                            //this.ToVillage.VillageBuildingData.Wall = i - 1;
                            change = true;
                        }
                        break;
                    }
                    //if (this.)
                }

                defenseReport.WallAfter = this.ToVillage[BuildingType.Wall];
                attackReport.WallAfter = this.ToVillage[BuildingType.Wall];

                if (change)
                {
                    
                    totalAttack = infantryAttack + cavalryAttack;
                    totalAttack += totalAttack * Research.AttackValuesDictionary[attackLevel];
                    totalDefense = (long)(infantryDefense * pInfantry + cavalryDefense * pCavalry) + this.ToVillage.VillageBuildingData.BasicDefense + 1;
                    totalDefense += totalDefense * this.ToVillage.VillageBuildingData.Fortified;
                    totalDefense += totalDefense * Research.DefenseValuesDictionary[defenseLevel];

                    if (totalAttack > totalDefense)
                        totalAttack = ((totalAttack - totalDefense) / (totalAttack) + 1) * totalAttack;
                    else
                        totalDefense = ((totalDefense - totalAttack) / (totalDefense) + 1) * totalDefense;
                }

            }

            bool successAttack = (totalAttack > totalDefense);

            if (this.Catapult > 0)
            {
                double catapultAttack = this.Catapult * catapultBuldingDamage;
                catapultAttack += catapultAttack * Research.AttackValuesDictionary[attackLevel];

                double buildingDefense = 0;
                for (int i = 1; i <= this.ToVillage[this.Building]; i++)
                    buildingDefense += Build.GetPrice(this.Building, i, 1).BuildTime;
                buildingDefense /= 75000;

                buildingDefense += buildingDefense * Research.DefenseValuesDictionary[defenseLevel];

                catapultAttack += (long)(catapultAttack * luckHeroes);

                if (totalAttack > totalDefense)
                    catapultAttack = ((totalAttack - totalDefense) / (totalAttack) + 1) * catapultAttack;
                else
                    catapultAttack = ((totalDefense - totalAttack) / (totalDefense) + 1) * catapultAttack;

                //bool change = false;
                double damaged = buildingDefense - catapultAttack;
                double hp = 0;
                for (int i = 1; i <= this.ToVillage[this.Building]; i++)
                {
                    hp += Build.GetPrice(this.Building, i, 1).BuildTime / 75000;
                    if (hp > damaged)
                    {
                        if (this.ToVillage[this.Building] != (i - 1))
                            this.ToVillage.UpgradeBuilding(this.Building, i - 1);

                        break;
                    }
                    //if (this.)
                }

                defenseReport.BuildingAfter = this.ToVillage[this.Building];
                attackReport.BuildingAfter = this.ToVillage[this.Building];
            }

            attackReport.Luck = luck;
            attackReport.SuccessAttack = successAttack;
            defenseReport.Luck = luck;
            defenseReport.SuccessAttack = successAttack;

            if (scoutAttack > scoutDefense)
                scoutLostInAttackSide = this.Scout - (int)Math.Round(this.Scout * ((scoutAttack - scoutDefense) / scoutAttack));
            else if (scoutAttack < scoutDefense)
                scoutLostInAttackSide =(int)Math.Round(this.Scout * ((scoutDefense - scoutAttack) / scoutDefense));
            else
                scoutLostInAttackSide = (int)Math.Round(0.5 * this.Scout);

            attackReport.AttackingHero = this.Hero;
            attackReport.DefendingHero = this.ToVillage.MainHero;
            attackReport.LevelBefore = (this.Hero != null) ? this.Hero.Level : 0;
            defenseReport.AttackingHero = this.Hero;
            defenseReport.DefendingHero = this.ToVillage.MainHero;
            defenseReport.LevelBefore = (this.ToVillage.MainHero == null) ? 0 : this.ToVillage.MainHero.Level;

            if (successAttack) // quân tấn công thắng
            {
                #region Quân tấn công thắng
                ratio = 1 - ((double)totalDefense / (double)totalAttack);

                spearLostInAttackSide = (int)Math.Round(this.Spear * (1 - ratio));
                swordLostInAttackSide = (int)Math.Round(this.Sword * (1 - ratio));
                axeLostInAttackSide = (int)Math.Round(this.Axe * (1 - ratio));
                lightCavalryLostInAttackSide = (int)Math.Round(this.LightCavalry * (1 - ratio));
                heavyCavalryLostInAttackSide = (int)Math.Round(this.HeavyCavalry * (1 - ratio));
                ramLostInAttackSide = (int)Math.Round(this.Ram * (1 - ratio));
                catapultLostInAttackSide = (int)Math.Round(this.Catapult * (1 - ratio));
                nobleLostInAttackSide = (int)Math.Round(this.Noble * (1 - ratio));
                

                spearLostInDefenseSide = this.ToVillage.VillageTroopData.SpearInVillage;
                swordLostInDefenseSide = this.ToVillage.VillageTroopData.SwordInVillage;
                axeLostInDefenseSide = this.ToVillage.VillageTroopData.AxeInVillage;
                scoutLostInDefenseSide = this.ToVillage.VillageTroopData.ScoutInVillage;
                lightCavalryLostInDefenseSide = this.ToVillage.VillageTroopData.LightCavalryInVillage;
                heavyCavalryLostInDefenseSide = this.ToVillage.VillageTroopData.HeavyCavalryInVillage;
                ramLostInDefenseSide = this.ToVillage.VillageTroopData.RamInVillage;
                catapultLostInDefenseSide = this.ToVillage.VillageTroopData.CatapultInVillage;
                scoutLostInDefenseSide = this.ToVillage.VillageTroopData.ScoutInVillage;
                nobleLostInDefenseSide = this.ToVillage.VillageTroopData.NobleInVillage;

                this.FromVillage.VillageTroopData.SpearOfVillage -= spearLostInAttackSide;
                this.FromVillage.VillageTroopData.SwordOfVillage -= swordLostInAttackSide;
                this.FromVillage.VillageTroopData.AxeOfVillage -= axeLostInAttackSide;
                this.FromVillage.VillageTroopData.ScoutOfVillage -= scoutLostInAttackSide;
                this.FromVillage.VillageTroopData.LightCavalryOfVillage -= lightCavalryLostInAttackSide;
                this.FromVillage.VillageTroopData.HeavyCavalryOfVillage -= heavyCavalryLostInAttackSide;
                this.FromVillage.VillageTroopData.RamOfVillage -= ramLostInAttackSide;
                this.FromVillage.VillageTroopData.CatapultOfVillage -= catapultLostInAttackSide;
                this.FromVillage.VillageTroopData.NobleOfVillage -= nobleLostInAttackSide;
                
                this.ToVillage.VillageTroopData.SpearOfVillage -= this.ToVillage.VillageTroopData.Spear;
                this.ToVillage.VillageTroopData.SwordOfVillage -= this.ToVillage.VillageTroopData.Sword;
                this.ToVillage.VillageTroopData.AxeOfVillage -= this.ToVillage.VillageTroopData.Axe;
                this.ToVillage.VillageTroopData.ScoutOfVillage -= this.ToVillage.VillageTroopData.Scout;
                this.ToVillage.VillageTroopData.LightCavalryOfVillage -= this.ToVillage.VillageTroopData.LightCavalry;
                this.ToVillage.VillageTroopData.HeavyCavalryOfVillage -= this.ToVillage.VillageTroopData.HeavyCavalry;
                this.ToVillage.VillageTroopData.RamOfVillage -= this.ToVillage.VillageTroopData.Ram;
                this.ToVillage.VillageTroopData.CatapultOfVillage -= this.ToVillage.VillageTroopData.Catapult;
                this.ToVillage.VillageTroopData.NobleOfVillage -= this.ToVillage.VillageTroopData.Noble;

                this.ToVillage.VillageTroopData.SpearInVillage = this.ToVillage.VillageTroopData.Spear = 0;
                this.ToVillage.VillageTroopData.SwordInVillage = this.ToVillage.VillageTroopData.Sword = 0;
                this.ToVillage.VillageTroopData.AxeInVillage = this.ToVillage.VillageTroopData.Axe = 0;
                this.ToVillage.VillageTroopData.ScoutInVillage = this.ToVillage.VillageTroopData.Scout = 0;
                this.ToVillage.VillageTroopData.LightCavalryInVillage = this.ToVillage.VillageTroopData.LightCavalry = 0;
                this.ToVillage.VillageTroopData.HeavyCavalryInVillage = this.ToVillage.VillageTroopData.HeavyCavalry = 0;
                this.ToVillage.VillageTroopData.RamInVillage = this.ToVillage.VillageTroopData.Ram = 0;
                this.ToVillage.VillageTroopData.CatapultInVillage = this.ToVillage.VillageTroopData.Catapult = 0;
                this.ToVillage.VillageTroopData.NobleInVillage = this.ToVillage.VillageTroopData.Noble = 0;

                foreach (Station station in this.ToVillage.StationsAtMe) //xoá tất cả station đang đóng ở thành phố đó
                {
                    station.AtVillage.VillageCommonMethods.UpdateVillage(this.LandingTime, session, false);
                    DefenseOtherReport defenseOtherReport = new DefenseOtherReport();
                    defenseOtherReport.Owner = station.FromVillage.Player;
                    defenseOtherReport.Time = this.LandingTime;
                    defenseOtherReport.Unread = true;
                    defenseOtherReport.Title = String.Format("Quân hỗ trợ của bạn từ {0} ở {1} bị tấn công", station.FromVillage.Name, station.AtVillage.Name);
                    defenseOtherReport.FromVillage = station.FromVillage;
                    defenseOtherReport.FromPlayer = this.FromVillage.Player;
                    defenseOtherReport.ToVillage = this.ToVillage;
                    defenseOtherReport.ToPlayer = this.ToVillage.Player;

                    defenseOtherReport.SpearDefense = station.Spear;
                    defenseOtherReport.SwordDefense = station.Sword;
                    defenseOtherReport.AxeDefense = station.Axe;
                    defenseOtherReport.ScoutDefense = station.Scout;
                    defenseOtherReport.LightCavalryDefense = station.LightCavalry;
                    defenseOtherReport.HeavyCavalryDefense = station.HeavyCavalry;
                    defenseOtherReport.RamDefense = station.Ram;
                    defenseOtherReport.CatapultDefense = station.Catapult;
                    defenseOtherReport.NobleDefense = station.Noble;

                    defenseOtherReport.SpearDead = station.Spear;
                    defenseOtherReport.SwordDead = station.Sword;
                    defenseOtherReport.AxeDead = station.Axe;
                    defenseOtherReport.ScoutDead = station.Scout;
                    defenseOtherReport.LightCavalryDead = station.LightCavalry;
                    defenseOtherReport.HeavyCavalryDead = station.HeavyCavalry;
                    defenseOtherReport.RamDead = station.Ram;
                    defenseOtherReport.CatapultDead = station.Catapult;
                    defenseOtherReport.NobleDead = station.Noble;

                    station.FromVillage.VillageTroopData.SpearOfVillage -= station.Spear;
                    station.FromVillage.VillageTroopData.SwordOfVillage -= station.Sword;
                    station.FromVillage.VillageTroopData.AxeOfVillage -= station.Axe;
                    station.FromVillage.VillageTroopData.ScoutOfVillage -= station.Scout;
                    station.FromVillage.VillageTroopData.LightCavalryOfVillage -= station.LightCavalry;
                    station.FromVillage.VillageTroopData.HeavyCavalryOfVillage -= station.HeavyCavalry;
                    station.FromVillage.VillageTroopData.RamOfVillage -= station.Ram;
                    station.FromVillage.VillageTroopData.CatapultOfVillage -= station.Catapult;
                    station.FromVillage.VillageTroopData.NobleOfVillage -= station.Noble;

                    station.FromVillage.StationsFromMe.Remove(station);

                    session.Update(station.FromVillage);
                    session.Save(defenseOtherReport);
                }
                this.ToVillage.StationsAtMe.Clear();
                //session.Delete("from Station station where station.AtVillage = :village", this.ToVillage, NHibernate.NHibernateUtil.Entity(typeof(Village)));
                IQuery queryDeleteStationAtMe = session.CreateQuery("delete from Station station where station.AtVillage = :village");
                queryDeleteStationAtMe.SetEntity("village", this.ToVillage);
                queryDeleteStationAtMe.ExecuteUpdate();
                if (this.Noble > 0)
                    this.ToVillage.Loyal -= (r.Next(15) + 20);

                if (this.ToVillage.Loyal <= 0)
                {
                    this.ToVillage.Player.Point -= this.ToVillage.Points;
                    this.FromVillage.Player.Point += this.ToVillage.Points;
                    this.ToVillage.Player = this.FromVillage.Player;
                    this.ToVillage.Loyal = 25;

                    stations = (from station in session.Linq<Station>()
                                where station.FromVillage == this.ToVillage
                                select station).ToList<Station>();

                    foreach (Station station in this.ToVillage.StationsFromMe)
                    {
                        station.AtVillage.VillageCommonMethods.UpdateVillage(this.LandingTime, session, false);

                        station.FromVillage.VillageTroopData.SpearOfVillage -= station.Spear;
                        station.FromVillage.VillageTroopData.SwordOfVillage -= station.Sword;
                        station.FromVillage.VillageTroopData.AxeOfVillage -= station.Axe;
                        station.FromVillage.VillageTroopData.ScoutOfVillage -= station.Scout;
                        station.FromVillage.VillageTroopData.LightCavalryOfVillage -= station.LightCavalry;
                        station.FromVillage.VillageTroopData.HeavyCavalryOfVillage -= station.HeavyCavalry;
                        station.FromVillage.VillageTroopData.RamOfVillage -= station.Ram;
                        station.FromVillage.VillageTroopData.CatapultOfVillage -= station.Catapult;
                        station.FromVillage.VillageTroopData.NobleOfVillage -= station.Noble;

                        station.AtVillage.StationsAtMe.Remove(station);
                        session.Update(station.FromVillage);

                    }
                    this.ToVillage.StationsFromMe.Clear();
                    IQuery queryDeleteStationFromMe = session.CreateQuery("delete from Station station where station.FromVillage = :village");
                    queryDeleteStationFromMe.SetEntity("village", this.ToVillage);
                    queryDeleteStationFromMe.ExecuteUpdate();

                    Station newStation = new Station();
                    newStation.AtVillage = this.ToVillage;
                    newStation.FromVillage = this.FromVillage;
                    newStation.Spear = this.Spear;
                    newStation.Sword = this.Sword;
                    newStation.Axe = this.Sword;
                    newStation.Scout = this.Scout;
                    newStation.LightCavalry = this.LightCavalry;
                    newStation.HeavyCavalry = this.HeavyCavalry;
                    newStation.Ram = this.Ram;
                    newStation.Catapult = this.Catapult;
                    newStation.Noble = this.Noble;

                    this.ToVillage.VillageTroopData.SpearInVillage = this.Spear - spearLostInAttackSide;
                    this.ToVillage.VillageTroopData.SwordInVillage = this.Sword - swordLostInAttackSide;
                    this.ToVillage.VillageTroopData.AxeInVillage = this.Axe - axeLostInAttackSide;
                    this.ToVillage.VillageTroopData.ScoutInVillage = this.Scout - scoutLostInAttackSide;
                    this.ToVillage.VillageTroopData.LightCavalryInVillage = this.LightCavalry - lightCavalryLostInAttackSide;
                    this.ToVillage.VillageTroopData.HeavyCavalryInVillage = this.HeavyCavalry - heavyCavalryLostInAttackSide;
                    this.ToVillage.VillageTroopData.RamInVillage = this.Ram - ramLostInAttackSide;
                    this.ToVillage.VillageTroopData.CatapultInVillage = this.Catapult - catapultLostInAttackSide;
                    this.ToVillage.VillageTroopData.NobleInVillage = this.Noble - nobleLostInAttackSide;

                    this.ToVillage.StationsAtMe.Add(newStation);
                    this.FromVillage.StationsFromMe.Add(newStation);
                    session.Save(newStation);


                }
                else
                {
                    #region Quân thắng trận trở về

                    returnTroop = new Return();
                    returnTroop.FromVillage = this.ToVillage;
                    returnTroop.ToVillage = this.FromVillage;
                    returnTroop.Spear = this.Spear - spearLostInAttackSide;
                    returnTroop.Sword = this.Sword - swordLostInAttackSide;
                    returnTroop.Axe = this.Axe - axeLostInAttackSide;
                    returnTroop.Scout = this.Scout - scoutLostInAttackSide;
                    returnTroop.LightCavalry = this.LightCavalry - lightCavalryLostInAttackSide;
                    returnTroop.HeavyCavalry = this.HeavyCavalry - heavyCavalryLostInAttackSide;
                    returnTroop.Ram = this.Ram - ramLostInAttackSide;
                    returnTroop.Catapult = this.Catapult - catapultLostInAttackSide;
                    returnTroop.Noble = this.Noble - nobleLostInAttackSide;


                    if (Utilities.IsGreaterThenZero(returnTroop.Spear, 
                                                    returnTroop.Sword,
                                                    returnTroop.Axe,
                                                    returnTroop.Scout,
                                                    returnTroop.LightCavalry,
                                                    returnTroop.HeavyCavalry,
                                                    returnTroop.Ram,
                                                    returnTroop.Catapult,
                                                    returnTroop.Noble))
                    {
                        returnTroop.StartingTime = this.LandingTime;
                        returnTroop.LandingTime = this.LandingTime + (this.LandingTime - this.StartingTime);

                        int intCanHaul = (int)(this.Spear * spearHaul + this.Sword * swordHaul + this.Axe * axeHaul + this.LightCavalry * lightCavalryHaul + this.HeavyCavalry * heavyCavalryHaul);
                        int intTotalResource = this.ToVillage.VillageResourceData.Iron + this.ToVillage.VillageResourceData.Clay + this.ToVillage.VillageResourceData.Wood;

                        if (intTotalResource <= intCanHaul)
                        {
                            returnTroop.Clay = this.ToVillage.VillageResourceData.Clay;
                            returnTroop.Wood = this.ToVillage.VillageResourceData.Wood;
                            returnTroop.Iron = this.ToVillage.VillageResourceData.Iron;

                            this.ToVillage.VillageResourceData.Clay = this.ToVillage.VillageResourceData.Wood = this.ToVillage.VillageResourceData.Iron = 0;
                        }
                        else
                        {
                            returnTroop.Clay = (int)((double)this.ToVillage.VillageResourceData.Clay * (double)intCanHaul / (double)intTotalResource);
                            returnTroop.Wood = (int)((double)this.ToVillage.VillageResourceData.Wood * (double)intCanHaul / (double)intTotalResource);
                            returnTroop.Iron = (int)((double)this.ToVillage.VillageResourceData.Iron * (double)intCanHaul / (double)intTotalResource);

                            this.ToVillage.VillageResourceData.Clay -= returnTroop.Clay;
                            this.ToVillage.VillageResourceData.Wood -= returnTroop.Wood;
                            this.ToVillage.VillageResourceData.Iron -= returnTroop.Iron;
                        }
                        returnTroop.Hero = this.Hero;
                        if (this.Hero != null)
                            this.Hero.InMovingCommand = returnTroop;
                        this.ToVillage.MovingCommandsFromMe.Add(returnTroop);
                        this.FromVillage.MovingCommandsToMe.Add(returnTroop);
                        session.Save(returnTroop);
                    }

                    attackReport.Wood = returnTroop.Wood;
                    attackReport.Clay = returnTroop.Clay;
                    attackReport.Iron = returnTroop.Iron;
                    defenseReport.Wood = returnTroop.Wood;
                    defenseReport.Clay = returnTroop.Clay;
                    defenseReport.Iron = returnTroop.Iron;

                    #endregion
                }
                #endregion

                if (this.Hero != null)
                    this.Hero.LevelUp(this.Hero.Experience + experienceAttackingHero);
                attackReport.LevelAfter = (this.Hero != null) ? this.Hero.Level : 0;
                defenseReport.LevelAfter = defenseReport.LevelBefore;
                if (this.ToVillage.MainHero != null)
                {
                    this.ToVillage.MainHero.IsDead = true;
                    session.Update(this.ToVillage.MainHero);
                }
                
                this.ToVillage.MainHero = null;
                
            }
            else // quân tấn công thua
            {
                #region Quân tấn công thua
                if (totalAttack == 0)
                    totalAttack = 1;
                ratio = (double)totalAttack / (double)totalDefense;

                spearLostInAttackSide = this.Spear;
                swordLostInAttackSide = this.Sword;
                axeLostInAttackSide = this.Axe;
                lightCavalryLostInAttackSide = this.LightCavalry;
                //scoutLostInAttackSide = this.Scout;
                heavyCavalryLostInAttackSide = this.HeavyCavalry;
                ramLostInAttackSide = this.Ram;
                catapultLostInAttackSide = this.Catapult;
                nobleLostInAttackSide = this.Noble;

                spearLostInDefenseSide = (int)(this.ToVillage.VillageTroopData.SpearInVillage * ratio);
                swordLostInDefenseSide = (int)(this.ToVillage.VillageTroopData.SwordInVillage * ratio);
                axeLostInDefenseSide = (int)(this.ToVillage.VillageTroopData.AxeInVillage * ratio);
                scoutLostInDefenseSide = (int)(this.ToVillage.VillageTroopData.ScoutInVillage * ratio);
                lightCavalryLostInDefenseSide = (int)(this.ToVillage.VillageTroopData.LightCavalryInVillage * ratio);
                heavyCavalryLostInDefenseSide = (int)(this.ToVillage.VillageTroopData.HeavyCavalryInVillage * ratio);
                ramLostInDefenseSide = (int)(this.ToVillage.VillageTroopData.RamInVillage * ratio);
                catapultLostInDefenseSide = (int)(this.ToVillage.VillageTroopData.CatapultInVillage * ratio);
                nobleLostInDefenseSide = (int)(this.ToVillage.VillageTroopData.NobleInVillage * ratio);

                this.FromVillage.VillageTroopData.SpearOfVillage -= this.Spear;
                this.FromVillage.VillageTroopData.SwordOfVillage -= this.Sword;
                this.FromVillage.VillageTroopData.AxeOfVillage -= this.Axe;
                this.FromVillage.VillageTroopData.LightCavalryOfVillage -= this.LightCavalry;
                this.FromVillage.VillageTroopData.ScoutOfVillage -= scoutLostInAttackSide;
                this.FromVillage.VillageTroopData.HeavyCavalryOfVillage -= this.HeavyCavalry;
                this.FromVillage.VillageTroopData.RamOfVillage -= this.Ram;
                this.FromVillage.VillageTroopData.CatapultOfVillage -= this.Catapult;
                this.FromVillage.VillageTroopData.NobleOfVillage -= this.Noble;

                this.ToVillage.VillageTroopData.SpearInVillage -= spearLostInDefenseSide;
                this.ToVillage.VillageTroopData.SwordInVillage -= swordLostInDefenseSide;
                this.ToVillage.VillageTroopData.AxeInVillage -= axeLostInDefenseSide;
                this.ToVillage.VillageTroopData.ScoutInVillage -= scoutLostInDefenseSide;
                this.ToVillage.VillageTroopData.LightCavalryInVillage -= lightCavalryLostInDefenseSide;
                this.ToVillage.VillageTroopData.HeavyCavalryInVillage -= heavyCavalryLostInDefenseSide;
                this.ToVillage.VillageTroopData.RamInVillage -= ramLostInDefenseSide;
                this.ToVillage.VillageTroopData.CatapultInVillage -= catapultLostInDefenseSide;
                this.ToVillage.VillageTroopData.NobleInVillage -= nobleLostInDefenseSide;

                int spearOfVillageLost = (int)Math.Round(this.ToVillage.VillageTroopData.Spear * ratio);
                int swordOfVillageLost = (int)Math.Round(this.ToVillage.VillageTroopData.Sword * ratio);
                int axeOfVillageLost = (int)Math.Round(this.ToVillage.VillageTroopData.Axe * ratio);
                int scoutOfVillageLost = (int)Math.Round(this.ToVillage.VillageTroopData.Scout * ratio);
                int lightCavalryOfVillageLost = (int)Math.Round(this.ToVillage.VillageTroopData.LightCavalry * ratio);
                int heavyCavalryOfVillageLost = (int)Math.Round(this.ToVillage.VillageTroopData.HeavyCavalry * ratio);
                int ramOfVillageLost = (int)Math.Round(this.ToVillage.VillageTroopData.Ram * ratio);
                int catapultOfVillageLost = (int)Math.Round(this.ToVillage.VillageTroopData.Catapult * ratio);
                int nobleOfVillageLost = (int)Math.Round(this.ToVillage.VillageTroopData.Noble * ratio);

                this.ToVillage.VillageTroopData.Spear -= spearOfVillageLost;
                this.ToVillage.VillageTroopData.Sword -= swordOfVillageLost;
                this.ToVillage.VillageTroopData.Axe -= axeOfVillageLost;
                this.ToVillage.VillageTroopData.LightCavalry -= lightCavalryOfVillageLost;
                this.ToVillage.VillageTroopData.Scout -= scoutOfVillageLost;
                this.ToVillage.VillageTroopData.HeavyCavalry -= heavyCavalryOfVillageLost;
                this.ToVillage.VillageTroopData.Ram -= ramOfVillageLost;
                this.ToVillage.VillageTroopData.Catapult -= catapultOfVillageLost;
                this.ToVillage.VillageTroopData.Noble -= nobleOfVillageLost;

                this.ToVillage.VillageTroopData.SpearOfVillage -= spearOfVillageLost;
                this.ToVillage.VillageTroopData.SwordOfVillage -= swordOfVillageLost;
                this.ToVillage.VillageTroopData.AxeOfVillage -= axeOfVillageLost;
                this.ToVillage.VillageTroopData.LightCavalryOfVillage -= lightCavalryOfVillageLost;
                this.ToVillage.VillageTroopData.ScoutOfVillage -= scoutOfVillageLost;
                this.ToVillage.VillageTroopData.HeavyCavalryOfVillage -= heavyCavalryOfVillageLost;
                this.ToVillage.VillageTroopData.RamOfVillage -= ramOfVillageLost;
                this.ToVillage.VillageTroopData.CatapultOfVillage -= catapultOfVillageLost;
                this.ToVillage.VillageTroopData.NobleOfVillage -= nobleOfVillageLost;

                if (scoutLostInAttackSide < this.Scout)
                {
                    returnTroop = new Return();
                    returnTroop.FromVillage = this.ToVillage;
                    returnTroop.ToVillage = this.FromVillage;
                    returnTroop.Scout = this.Scout - scoutLostInAttackSide;
                    returnTroop.StartingTime = this.LandingTime;
                    returnTroop.LandingTime = this.LandingTime + (this.LandingTime - this.StartingTime);

                    this.ToVillage.MovingCommandsFromMe.Add(returnTroop);
                    this.FromVillage.MovingCommandsToMe.Add(returnTroop);
                    session.Save(returnTroop);
                }

                foreach (Station station in this.ToVillage.StationsAtMe)
                {
                    DefenseOtherReport defenseOtherReport = null;
                    if (station.FromVillage.Player != this.ToVillage.Player)
                    {
                        defenseOtherReport = new DefenseOtherReport();
                        defenseOtherReport.SpearDefense = station.Spear;
                        defenseOtherReport.SwordDefense = station.Sword;
                        defenseOtherReport.AxeDefense = station.Axe;
                        defenseOtherReport.ScoutDefense = station.Scout;
                        defenseOtherReport.LightCavalryDefense = station.LightCavalry;
                        defenseOtherReport.HeavyCavalryDefense = station.HeavyCavalry;
                        defenseOtherReport.RamDefense = station.Ram;
                        defenseOtherReport.CatapultDefense = station.Catapult;
                        defenseOtherReport.NobleDefense = station.Noble;
                    }

                    int spearDead = (int)Math.Round(station.Spear * (1 - ratio));
                    int swordDead = (int)Math.Round(station.Sword * (1 - ratio));
                    int axeDead = (int)Math.Round(station.Axe * (1 - ratio));
                    int scoutDead = (int)Math.Round(station.Scout * (1 - ratio));
                    int lightCavalryDead = (int)Math.Round(station.LightCavalry * (1 - ratio));
                    int heavyCavalryDead = (int)Math.Round(station.HeavyCavalry * (1 - ratio));
                    int ramDead = (int)Math.Round(station.Ram * (1 - ratio));
                    int catapultDead = (int)Math.Round(station.Catapult * (1 - ratio));
                    int nobleDead = (int)Math.Round(station.Noble * (1 - ratio));

                    station.Spear -= spearDead;
                    station.Sword -= swordDead;
                    station.Axe -= axeDead;
                    station.LightCavalry -= scoutDead;
                    station.Scout -= lightCavalryDead;
                    station.HeavyCavalry -= heavyCavalryDead;
                    station.Ram -= ramDead;
                    station.Catapult -= catapultDead;
                    station.Noble -= nobleDead;

                    if (Utilities.IsGreaterThenZero(station.Spear,
                                                        station.Sword,
                                                        station.Axe,
                                                        station.Scout,
                                                        station.LightCavalry,
                                                        station.HeavyCavalry,
                                                        station.Ram,
                                                        station.Catapult,
                                                        station.Noble))
                        session.Update(station);
                    else
                    {
                        station.AtVillage.StationsAtMe.Remove(station);
                        station.FromVillage.StationsFromMe.Remove(station);
                        session.Delete(station);
                        session.Update(station.FromVillage);
                    }

                    #region tạo report
                    if (station.FromVillage.Player != this.ToVillage.Player)
                    {
                        defenseOtherReport.Time = this.LandingTime;
                        defenseOtherReport.Owner = station.FromVillage.Player;
                        defenseOtherReport.Title = "Quân phòng thủ của bạn ở " + this.ToVillage.Name + "(" + this.ToVillage.X.ToString() + "|" + this.ToVillage.Y.ToString() + ") bị tấn công";

                        defenseOtherReport.FromPlayer = station.FromVillage.Player;
                        defenseOtherReport.FromVillage = station.FromVillage;
                        defenseOtherReport.ToPlayer = station.AtVillage.Player;
                        defenseOtherReport.ToVillage = station.AtVillage;

                        defenseOtherReport.SpearDefense = station.Spear;
                        defenseOtherReport.SwordDefense = station.Sword;
                        defenseOtherReport.AxeDefense = station.Axe;
                        defenseOtherReport.ScoutDefense = station.Scout;
                        defenseOtherReport.LightCavalryDefense = station.LightCavalry;
                        defenseOtherReport.HeavyCavalryDefense = station.HeavyCavalry;
                        defenseOtherReport.RamDefense = station.Ram;
                        defenseOtherReport.CatapultDefense = station.Catapult;
                        defenseOtherReport.NobleDefense = station.Noble;

                        defenseOtherReport.SpearDead = spearDead;
                        defenseOtherReport.SwordDead = swordDead;
                        defenseOtherReport.AxeDead = axeDead;
                        defenseOtherReport.ScoutDead = scoutDead;
                        defenseOtherReport.LightCavalryDead = lightCavalryDead;
                        defenseOtherReport.HeavyCavalryDead = heavyCavalryDead;
                        defenseOtherReport.RamDead = ramDead;
                        defenseOtherReport.CatapultDead = catapultDead;
                        defenseOtherReport.NobleDead = nobleDead;

                        session.Save(defenseOtherReport);
                    }
                    #endregion



                }
                #endregion

                if (this.ToVillage.MainHero != null)
                    this.ToVillage.MainHero.LevelUp(this.ToVillage.MainHero.Experience + experienceDefendingHero);
                if (this.Hero != null)
                {
                    this.Hero.IsDead = true;
                    session.Update(this.Hero);
                }

                attackReport.LevelAfter = attackReport.LevelBefore;
                defenseReport.LevelAfter = (this.ToVillage.MainHero == null) ? 0 : this.ToVillage.MainHero.Level;
            }

            if (returnTroop != null)
            {
                if (returnTroop.Scout > 0)
                {
                    attackReport.ShowTroop = true;
                    attackReport.ShowBuilding = true;
                    attackReport.ShowResource = true;
                }
                else
                {
                    attackReport.ShowTroop = true;
                    attackReport.ShowBuilding = true;
                }
            }
            else
            {
                if (totalAttack / totalDefense > 0.5)
                    attackReport.ShowTroop = true;
            }

            attackReport.Headquarter = this.ToVillage[BuildingType.Headquarter];
            attackReport.Barracks = this.ToVillage[BuildingType.Barracks];
            attackReport.Stable = this.ToVillage[BuildingType.Stable];
            attackReport.Workshop = this.ToVillage[BuildingType.Workshop];
            attackReport.Smithy = this.ToVillage[BuildingType.Smithy];
            attackReport.Market = this.ToVillage[BuildingType.Market];
            attackReport.Rally = this.ToVillage[BuildingType.Rally];
            attackReport.Academy = this.ToVillage[BuildingType.Academy];
            attackReport.Farm = this.ToVillage[BuildingType.Farm];
            attackReport.Warehouse = this.ToVillage[BuildingType.Warehouse];
            attackReport.IronMine = this.ToVillage[BuildingType.IronMine];
            attackReport.ClayPit = this.ToVillage[BuildingType.ClayPit];
            attackReport.TimberCamp = this.ToVillage[BuildingType.TimberCamp];
            attackReport.Wall = this.ToVillage[BuildingType.Wall];

            attackReport.SpearAttackDead = spearLostInAttackSide;
            attackReport.SwordAttackDead = swordLostInAttackSide;
            attackReport.AxeAttackDead = axeLostInAttackSide;
            attackReport.ScoutAttackDead = scoutLostInAttackSide;
            attackReport.LightCavalryAttackDead = lightCavalryLostInAttackSide;
            attackReport.HeavyCavalryAttackDead = heavyCavalryLostInAttackSide;
            attackReport.RamAttackDead = ramLostInAttackSide;
            attackReport.CatapultAttackDead = catapultLostInAttackSide;
            attackReport.NobleAttackDead = nobleLostInAttackSide;
            attackReport.SpearDefenseDead = spearLostInDefenseSide;
            attackReport.SwordDefenseDead = swordLostInDefenseSide;
            attackReport.AxeDefenseDead = axeLostInDefenseSide;
            attackReport.ScoutDefenseDead = scoutLostInDefenseSide = 0;
            attackReport.LightCavalryDefenseDead = lightCavalryLostInDefenseSide;
            attackReport.HeavyCavalryDefenseDead = heavyCavalryLostInDefenseSide;
            attackReport.RamDefenseDead = ramLostInDefenseSide;
            attackReport.CatapultDefenseDead = catapultLostInDefenseSide;
            attackReport.NobleDefenseDead = nobleLostInDefenseSide;

            defenseReport.SpearAttackDead = spearLostInAttackSide;
            defenseReport.SwordAttackDead = swordLostInAttackSide;
            defenseReport.AxeAttackDead = axeLostInAttackSide;
            defenseReport.ScoutAttackDead = scoutLostInAttackSide;
            defenseReport.LightCavalryAttackDead = lightCavalryLostInAttackSide;
            defenseReport.HeavyCavalryAttackDead = heavyCavalryLostInAttackSide;
            defenseReport.RamAttackDead = ramLostInAttackSide;
            defenseReport.CatapultAttackDead = catapultLostInAttackSide;
            defenseReport.NobleAttackDead = nobleLostInAttackSide;
            defenseReport.SpearDefenseDead = spearLostInDefenseSide;
            defenseReport.SwordDefenseDead = swordLostInDefenseSide;
            defenseReport.AxeDefenseDead = axeLostInDefenseSide;
            defenseReport.ScoutDefenseDead = scoutLostInDefenseSide;
            defenseReport.LightCavalryDefenseDead = lightCavalryLostInDefenseSide;
            defenseReport.HeavyCavalryDefenseDead = heavyCavalryLostInDefenseSide;
            defenseReport.RamDefenseDead = ramLostInDefenseSide;
            defenseReport.CatapultDefenseDead = catapultLostInDefenseSide;
            defenseReport.NobleDefenseDead = nobleLostInDefenseSide;

            attackReport.LoyalAfter = (int)this.ToVillage.Loyal;
            attackReport.BuildingAfter = this.ToVillage[this.Building];
            defenseReport.LoyalAfter = (int)this.ToVillage.Loyal;
            defenseReport.BuildingAfter = this.ToVillage[this.Building];

            this.FromVillage.LastUpdate = this.ToVillage.LastUpdate = this.LandingTime;

            session.Update(this.ToVillage.Player);
            session.Update(this.FromVillage.Player);

            session.Save(attackReport);
            session.Save(defenseReport);

            this.ToVillage.MovingCommandsToMe.Remove(this);
            this.FromVillage.MovingCommandsFromMe.Remove(this);
            session.Delete(this);

            return returnTroop;
        }
Example #2
0
        //chưa xét trường hợp phá tường, phá nhà
        public override void effect(ISession session)
        {
            AttackReport report = new AttackReport();
            report.Time = this.LandingTime;
            report.Title = this.From.Owner.Username + " tấn công " + this.To.Name + "(" + this.To.X.ToString() + "|" + this.To.Y.ToString() + ")";
            report.From = this.From;
            report.To = this.To;
            
            report.SpearSent = this.Spear;
            report.SwordSent = this.Sword;
            report.AxeSent = this.Axe;
            report.ScoutSent = this.Scout;
            report.LightSent = this.Light;
            report.HeavySent = this.Heavy;
            report.RamSent = this.Ram;
            report.CatapultSent = this.Catapult;
            report.NobleSent = this.Noble;

            report.SpearDefense = this.To.TotalSpear;
            report.SwordDefense = this.To.TotalSword;
            report.AxeDefense = this.To.TotalAxe;
            report.ScoutDefense = this.To.TotalScout;
            report.LightDefense = this.To.TotalLight;
            report.HeavyDefense = this.To.TotalHeavy;
            report.RamDefense = this.To.TotalRam;
            report.CatapultDefense = this.To.TotalCatapult;
            report.NobleDefense = this.To.TotalNoble;

            report.LoyalAfter = this.To.Loyal;
            report.Building = this.Building;
            report.BuildingAfter = this.To[this.Building];
            
            

            Random r = new Random();
            double luck = r.NextDouble();
            int infantryAttack = this.Spear * 10 + this.Sword * 25 + this.Axe * 40 + this.Noble * 30;
            int cavalryAttack = this.Light * 130 + this.Heavy * 150;
            int totalAttack = infantryAttack + cavalryAttack;

            double pInfantry = (double)infantryAttack / (double)totalAttack;
            double pCavalry = (double)cavalryAttack / (double)totalAttack;

            int infantryDefense = this.To.TotalSpear * 15 + this.To.TotalSword * 50 + this.To.TotalAxe * 10 + this.To.TotalLight * 30 + this.To.TotalHeavy * 200 + this.To.TotalNoble * 100;
            int cavalryDefense = this.To.TotalSpear * 45 + this.To.TotalSword * 15 + this.To.TotalAxe * 5 + this.To.TotalLight * 40 + this.To.TotalHeavy * 80 + this.To.TotalNoble * 50;
            int totalDefense = (int)(infantryDefense * pInfantry + cavalryDefense * pCavalry) + 100;

            totalAttack = (totalAttack / totalDefense) * totalAttack;
            totalAttack += (int)(totalAttack * luck);

            double ratio;
            Player owner = this.To.Owner;
            if (totalAttack > totalDefense)
            {
                ratio = 1 - ((double)totalDefense / (double)totalAttack);

                this.Spear = (int)Math.Round(this.Spear*ratio);
                this.Sword = (int)Math.Round(this.Sword * ratio);
                this.Axe = (int)Math.Round(this.Axe * ratio);
                this.Light = (int)Math.Round(this.Light * ratio);
                this.Scout = (int)Math.Round(this.Scout * ratio);
                this.Heavy = (int)Math.Round(this.Heavy * ratio);
                this.Ram = (int)Math.Round(this.Ram * ratio);
                this.Catapult = (int)Math.Round(this.Catapult * ratio);
                this.Noble = (int)Math.Round(this.Noble * ratio);
                this.To.StationedTroops.Clear();

                this.To.Spear = 0;
                this.To.Sword = 0;
                this.To.Axe = 0;
                this.To.Scout = 0;
                this.To.Light = 0;
                this.To.Heavy = 0;
                this.To.Ram = 0;
                this.To.Catapult = 0;
                this.To.Noble = 0;

                if (this.Noble > 0)
                    this.To.Loyal -= (r.Next(15) + 20);
                if (this.To.Loyal <= 0)
                {

                    this.To.Owner = this.From.Owner;
                    this.To.Loyal = 25;
                    foreach (Stationed station in this.To.TroopsOutside)
                        session.Delete(station);
                    this.To.TroopsOutside.Clear();


                    foreach (Stationed station in this.To.StationedTroops)
                    {
                        #region tạo report
                        if (station.FromVillage.Owner != this.To.Owner)
                        {
                            DefenseOtherReport defenseOtherReport = new DefenseOtherReport();
                            defenseOtherReport.Time = this.LandingTime;
                            defenseOtherReport.Owner = station.FromVillage.Owner;
                            defenseOtherReport.Title = "Quân phòng thủ của bạn ở " + this.To.Name + "(" + this.To.X.ToString() + "|" + this.To.Y.ToString() + ") bị tấn công";

                            session.Save(defenseOtherReport);
                        }
                        #endregion


                        session.Delete(station);
                    }

                    Stationed newStation = new Stationed();
                    newStation.AtVillage = this.To;
                    newStation.FromVillage = this.From;
                    newStation.Spear = this.Spear;
                    newStation.Sword = this.Sword;
                    newStation.Axe = this.Sword;
                    newStation.Scout = this.Scout;
                    newStation.Light = this.Light;
                    newStation.Heavy = this.Heavy;
                    newStation.Ram = this.Ram;
                    newStation.Catapult = this.Catapult;
                    newStation.Noble = this.Noble;

                    session.Save(newStation);

                }
                else
                {
                    #region Quân thắng trận trở về

                    Return returnTroop = new Return();
                    returnTroop.Spear = this.Spear;
                    returnTroop.Sword = this.Sword;
                    returnTroop.Axe = this.Axe;
                    returnTroop.Scout = this.Scout;
                    returnTroop.Light = this.Light;
                    returnTroop.Heavy = this.Heavy;
                    returnTroop.Ram = this.Ram;
                    returnTroop.Catapult = this.Catapult;
                    returnTroop.Noble = this.Noble;

                    int intCanHaul = this.Spear * 25 + this.Sword * 15 + this.Axe * 10 + this.Light * 80 + this.Heavy;
                    int intTotalResource = this.To.Iron + this.To.Clay + this.To.Wood;

                    if (intTotalResource <= intCanHaul)
                    {
                        returnTroop.Clay = this.To.Clay;
                        returnTroop.Wood = this.To.Wood;
                        returnTroop.Iron = this.To.Iron;

                        this.To.Clay = this.To.Iron = this.To.Wood = 0;
                    }
                    else
                    {
                        returnTroop.Clay = (int)((double)this.To.Clay * (double)intCanHaul / (double)intTotalResource);
                        returnTroop.Wood = (int)((double)this.To.Wood * (double)intCanHaul / (double)intTotalResource);
                        returnTroop.Iron = (int)((double)this.To.Iron * (double)intCanHaul / (double)intTotalResource);

                        this.To.Clay -= returnTroop.Clay;
                        this.To.Wood -= returnTroop.Wood;
                        this.To.Iron -= returnTroop.Iron;
                    }

                    session.Save(returnTroop);
                    #endregion
                }

            }
            else
            {
                if (totalAttack == 0)
                    totalAttack = 1;
                ratio = 1 - ((double)totalAttack / (double)totalDefense);

                this.To.Spear = (int)Math.Round(this.To.Spear*ratio);
                this.To.Sword = (int)Math.Round(this.To.Sword * ratio);
                this.To.Axe = (int)Math.Round(this.To.Axe * ratio);
                this.To.Light = (int)Math.Round(this.To.Light * ratio);
                this.To.Scout = (int)Math.Round(this.To.Scout * ratio);
                this.To.Heavy = (int)Math.Round(this.To.Heavy * ratio);
                this.To.Ram = (int)Math.Round(this.To.Ram * ratio);
                this.To.Catapult = (int)Math.Round(this.To.Catapult * ratio);
                this.To.Noble = (int)Math.Round(this.To.Noble * ratio);

                foreach (Stationed station in this.To.StationedTroops)
                {

                    station.Spear = (int)Math.Round(station.Spear * ratio);
                    station.Sword = (int)Math.Round(station.Sword * ratio);
                    station.Axe = (int)Math.Round(station.Axe * ratio);
                    station.Light = (int)Math.Round(station.Light * ratio);
                    station.Scout = (int)Math.Round(station.Scout * ratio);
                    station.Heavy = (int)Math.Round(station.Heavy * ratio);
                    station.Ram = (int)Math.Round(station.Ram * ratio);
                    station.Catapult = (int)Math.Round(station.Catapult * ratio);
                    station.Noble = (int)Math.Round(station.Noble * ratio);

                    #region tạo report
                    if (station.FromVillage.Owner != this.To.Owner)
                    {
                        DefenseOtherReport defenseOtherReport = new DefenseOtherReport();
                        defenseOtherReport.Time = this.LandingTime;
                        defenseOtherReport.Owner = station.FromVillage.Owner;
                        defenseOtherReport.Title = "Quân phòng thủ của bạn ở " + this.To.Name + "(" + this.To.X.ToString() + "|" + this.To.Y.ToString() + ") bị tấn công";

                        session.Save(defenseOtherReport);
                    }
                    #endregion

                    

                    session.Update(station);
                    
                }

            }

            


            report.Owner = this.From.Owner;
            session.Save(report);
            report.Owner = owner;
            session.Save(report);
        }