コード例 #1
0
        public virtual Attack CreateAttack(ISession session,
                                    int x,
                                    int y,
                                    int spear,
                                    int sword,
                                    int axe,
                                    int scout,
                                    int light,
                                    int heavy,
                                    int ram,
                                    int catapult,
                                    Hero hero,
                                    BuildingType building)
        {
            if (x == this.Village.X && y == this.Village.Y)
                throw new Exception("Nhập toạ độ");

            if ((spear + sword + axe + scout + light + heavy + ram + catapult) == 0)
                throw new Exception("Nhập một loại quân");

            if ((spear > this.Village.VillageTroopData.Spear) ||
            (sword > this.Village.VillageTroopData.Sword) ||
            (axe > this.Village.VillageTroopData.Axe) ||
            (scout > this.Village.VillageTroopData.Scout) ||
            (light > this.Village.VillageTroopData.LightCavalry) ||
            (heavy > this.Village.VillageTroopData.HeavyCavalry) ||
            (ram > this.Village.VillageTroopData.Ram) ||
            (catapult > this.Village.VillageTroopData.Catapult))
                throw new Exception("Không đủ quân");

            Village toVillage = Village.GetVillageByCoordinate(x, y, session);
            if (toVillage == null)
                throw new Exception("Toạ độ không tồn tại");

            if (hero != null)
            {
                if ((from h in this.Village.Heroes
                     where h == hero
                     select h).Count<Hero>() == 0)
                    throw new TribalWarsException("Không tồn tại hero trong thành phố");
                else if (this.Village.MainHero == hero)
                    throw new TribalWarsException("Không thể đưa chủ thành đi tấn công");
            }

            Attack attack = new Attack();
            attack.ToVillage = toVillage;
            attack.FromVillage = this.Village;

            TroopType type = TroopType.Spear;
            if (scout > 0)
                type = TroopType.Scout;
            if (light > 0)
                type = TroopType.Light;
            if (heavy > 0)
                type = TroopType.Heavy;
            if (spear > 0)
                type = TroopType.Spear;
            if (axe > 0)
                type = TroopType.Axe;
            if (sword > 0)
                type = TroopType.Sword;
            if (ram > 0)
                type = TroopType.Ram;
            if (catapult > 0)
                type = TroopType.Catapult;
            attack.Hero = hero;

            attack.Building = building;
            attack.Spear = spear;
            attack.Sword = sword;
            attack.Axe = axe;
            attack.Scout = scout;
            attack.LightCavalry = light;
            attack.HeavyCavalry = heavy;
            attack.Ram = ram;
            attack.Catapult = catapult;
            attack.StartingTime = DateTime.Now;
            attack.LandingTime = Map.LandingTime(type, attack.FromVillage.X, attack.FromVillage.Y, attack.ToVillage.X, attack.ToVillage.Y, attack.StartingTime, Research.SpeedValuesDictionary[this.Village[ResearchType.Speed]]);
            return attack;
        }
コード例 #2
0
ファイル: Attack.cs プロジェクト: DF-thangld/web_game
        public static Attack CreateAttack(  ISession session,
                                            Village from,
                                            int x,
                                            int y,
                                            int spear,
                                            int sword,
                                            int axe,
                                            int scout,
                                            int light,
                                            int heavy,
                                            int ram,
                                            int catapult,
                                            int noble,
                                            BuildingType building)
        {
            int intTo = Village.CheckVillage(x, y, session);
            
            if (intTo < 0)
                throw new Exception("Toạ độ không tồn tại");

            if ((spear + sword + axe + scout + light + heavy + ram + catapult + noble) == 0)
                throw new Exception("Nhập một loại quân");

            from.Update(DateTime.Now, session);
            if ((spear > from.Spear) ||
            (sword > from.Sword) ||
            (axe > from.Axe) ||
            (scout > from.Scout) ||
            (light > from.Light) ||
            (heavy > from.Heavy) ||
            (ram > from.Ram) ||
            (catapult > from.Catapult) ||
            (noble > from.Noble))
                throw new Exception("Không đủ quân");

            Attack attack = new Attack();
            
            TroopType type = TroopType.Spear;
            if (scout > 0)
                type = TroopType.Scout;
            if (light > 0)
                type = TroopType.Light;
            if (heavy > 0)
                type = TroopType.Heavy;
            if (spear > 0)
                type = TroopType.Spear;
            if (axe > 0)
                type = TroopType.Axe;
            if (sword > 0)
                type = TroopType.Sword;
            if (ram > 0)
                type = TroopType.Ram;
            if (catapult > 0)
                type = TroopType.Catapult;
            if (noble > 0)
                type = TroopType.Nobleman;

            attack.From = from;
            attack.StartTime = DateTime.Now;
            attack.To = session.Load<Village>(intTo);
            attack.Spear = spear;
            attack.Sword = sword;
            attack.Axe = axe;
            attack.Scout = scout;
            attack.Light = light;
            attack.Heavy = heavy;
            attack.Ram = ram;
            attack.Catapult = catapult;
            attack.Noble = noble;
            attack.LandingTime = Map.LandingTime(type, attack.From.X, attack.From.Y, attack.To.X, attack.To.Y, attack.StartTime);
            attack.Type = MoveType.Attack;
            attack.Building = building;

            from.Spear -= spear;
            from.Sword -= sword;
            from.Axe -= axe;
            from.Scout -= scout;
            from.Light -= light;
            from.Heavy -= heavy;
            from.Ram -= ram;
            from.Catapult -= catapult;
            from.Noble -= noble;

            session.Save(attack);
            session.Update(from);
            return attack;
        }