Beispiel #1
0
        public ActionResult ActivateTargetedStructure(int planetID, int structureTypeID)
        {
            //This call is never to be called before/outside of SetStructureTarget
            var db = new ZkDataContext();
            //Get the pre-set target planet ID
            PlanetStructure structure = db.PlanetStructures.FirstOrDefault(x => x.PlanetID == planetID && x.StructureTypeID == structureTypeID);
            int             targetID  = structure.TargetPlanetID ?? -1;

            if (targetID == -1)
            {
                return(Content("Structure has no target"));
            }

            if (!structure.IsActive)
            {
                return(Content(String.Format("Structure {0} is inactive", structure.StructureType.Name)));
            }

            ActionResult ret = null;

            if (structure.StructureType.EffectCreateLink == true)
            {
                ret = CreateLink(planetID, structureTypeID, targetID);
            }
            if (ret != null)
            {
                return(ret);                // exit with message if error occurs
            }
            if (structure.StructureType.EffectChangePlanetMap == true)
            {
                ret = ChangePlanetMap(planetID, structureTypeID, targetID, null);
            }
            if (ret != null)
            {
                return(ret);                // exit with message if error occurs
            }
            if (structure.StructureType.EffectPlanetBuster == true)
            {
                ret = FirePlanetBuster(planetID, structureTypeID, targetID);
            }
            if (ret != null)
            {
                return(ret);                         // exit with message if error occurs
            }
            if (structure.StructureType.IsSingleUse) // single-use structure, remove
            {
                db.PlanetStructures.DeleteOnSubmit(structure);
            }

            db.SaveChanges();
            PlanetWarsTurnHandler.SetPlanetOwners(new PlanetwarsEventCreator(), db);     //this is needed for the buster to update ownership after planet destruction

            if (ret != null)
            {
                return(ret);
            }
            return(RedirectToAction("Planet", new { id = planetID }));
        }
Beispiel #2
0
        public ActionResult DestroyStructure(int planetID, int structureTypeID)
        {
            using (var db = new ZkDataContext())
            {
                Planet planet = db.Planets.Single(p => p.PlanetID == planetID);
                if (Global.Server.GetPlanetBattles(planet).Any(x => x.IsInGame))
                {
                    return(Content("Battle in progress on the planet, cannot destroy structures"));
                }
                Account       acc           = db.Accounts.Single(x => x.AccountID == Global.AccountID);
                StructureType structureType = db.StructureTypes.SingleOrDefault(s => s.StructureTypeID == structureTypeID);
                Faction       faction       = planet.Faction;
                if (structureType == null)
                {
                    return(Content("Structure type does not exist."));
                }
                if (!structureType.IsBuildable)
                {
                    return(Content("Structure is not buildable."));
                }

                // assumes you can only build level 1 structures! if higher level structures can be built directly, we should check down the upgrade chain too

                if (!planet.PlanetStructures.Any(x => x.StructureTypeID == structureTypeID))
                {
                    return(Content("Structure or its upgrades not present"));
                }

                List <PlanetStructure> list      = planet.PlanetStructures.Where(x => x.StructureTypeID == structureTypeID).ToList();
                PlanetStructure        toDestroy = list[0];
                if (!toDestroy.IsActive)
                {
                    return(Content("Structure is currently disabled"));
                }
                var canDestroy = toDestroy.OwnerAccountID == acc.AccountID || planet.OwnerAccountID == acc.AccountID;
                if (!canDestroy)
                {
                    return(Content("Structure is not under your control."));
                }
                var refund = toDestroy.StructureType.Cost * GlobalConst.SelfDestructRefund;
                if (toDestroy.Account != null)
                {
                    toDestroy.Account.ProduceMetal(refund);
                }
                else
                {
                    faction?.ProduceMetal(refund);
                }
                db.Events.InsertOnSubmit(PlanetwarsEventCreator.CreateEvent("{0} has demolished a {1} on {2}.", acc, toDestroy.StructureType, planet));
                db.PlanetStructures.DeleteOnSubmit(toDestroy);
                db.SaveChanges();
                PlanetWarsTurnHandler.SetPlanetOwners(new PlanetwarsEventCreator(), db);
            }

            return(RedirectToAction("Planet", new { id = planetID }));
        }
Beispiel #3
0
        public ActionResult BuildStructure(int planetID, int structureTypeID)
        {
            using (var db = new ZkDataContext())
            {
                Planet planet = db.Planets.Single(p => p.PlanetID == planetID);
                if (Global.Server.GetPlanetBattles(planet).Any(x => x.IsInGame))
                {
                    return(Content("Battle in progress on the planet, cannot build structures"));
                }
                Account acc = db.Accounts.Single(x => x.AccountID == Global.AccountID);
                if (acc.FactionID != planet.OwnerFactionID)
                {
                    return(Content("Planet is not under your control."));
                }

                StructureType structureType = db.StructureTypes.SingleOrDefault(s => s.StructureTypeID == structureTypeID);
                if (structureType == null)
                {
                    return(Content("Structure type does not exist."));
                }
                if (!structureType.IsBuildable)
                {
                    return(Content("Structure is not buildable."));
                }

                if (acc.GetMetalAvailable() < structureType.Cost)
                {
                    return(Content("Insufficient metal"));
                }
                acc.SpendMetal(structureType.Cost);

                var newBuilding = new PlanetStructure
                {
                    StructureTypeID = structureTypeID,
                    StructureType   = structureType,
                    PlanetID        = planetID,
                    OwnerAccountID  = acc.AccountID,
                };
                newBuilding.ReactivateAfterBuild();

                db.PlanetStructures.InsertOnSubmit(newBuilding);
                db.SaveChanges();

                db.Events.InsertOnSubmit(PlanetwarsEventCreator.CreateEvent("{0} has built a {1} on {2} planet {3}.",
                                                                            acc,
                                                                            newBuilding.StructureType,
                                                                            planet.Faction,
                                                                            planet));
                PlanetWarsTurnHandler.SetPlanetOwners(new PlanetwarsEventCreator(), db);
            }
            return(RedirectToAction("Planet", new { id = planetID }));
        }
Beispiel #4
0
        private ActionResult FirePlanetBuster(int planetID, int structureTypeID, int targetID)
        {
            var             db        = new ZkDataContext();
            PlanetStructure structure = db.PlanetStructures.FirstOrDefault(x => x.PlanetID == planetID && x.StructureTypeID == structureTypeID);
            Planet          source    = db.Planets.FirstOrDefault(x => x.PlanetID == planetID);
            Planet          target    = db.Planets.FirstOrDefault(x => x.PlanetID == targetID);

            Account acc = db.Accounts.Single(x => x.AccountID == Global.AccountID);

            if (acc.Faction == null)
            {
                return(Content("Join some faction first"));
            }
            if (!target.CanFirePlanetBuster(acc.Faction))
            {
                return(Content("You cannot attack here"));
            }

            //Get rid of all strutures
            var structures = target.PlanetStructures.Where(x => x.StructureType.EffectIsVictoryPlanet != true && x.StructureType.OwnerChangeWinsGame != true).ToList();


            //kill all IP
            foreach (var pf in target.PlanetFactions.Where(x => x.Influence > 0))
            {
                pf.Influence = 0;
            }
            var links = db.Links.Where(x => (x.PlanetID1 == target.PlanetID || x.PlanetID2 == target.PlanetID));

            foreach (Link link in links)
            {
                db.Links.DeleteOnSubmit(link);
            }

            db.Events.InsertOnSubmit(PlanetwarsEventCreator.CreateEvent("A {4} fired from {0} {1} has destroyed {2} {3}!", source.Faction, source, target.Faction, target, structure.StructureType));
            db.SaveChanges();

            db.PlanetStructures.DeleteAllOnSubmit(structures);
            var residue = db.StructureTypes.First(x => x.Name == "Residue"); // todo not nice use constant instead

            target.PlanetStructures.Add(new PlanetStructure()
            {
                StructureType = residue, IsActive = true, ActivatedOnTurn = null
            });
            db.SaveChanges();

            return(null);
        }
Beispiel #5
0
        public ActionResult ConfiscateStructure(int planetID, int structureTypeID)
        {
            using (var db = new ZkDataContext())
            {
                Planet planet = db.Planets.Single(p => p.PlanetID == planetID);
                //if (Global.Nightwatch.GetPlanetBattles(planet).Any(x => x.IsInGame)) return Content("Battle in progress on the planet, cannot destroy structures");
                Account acc           = db.Accounts.Single(x => x.AccountID == Global.AccountID);
                bool    factionLeader = acc.HasFactionRight(x => x.RightMetalQuota > 0) && (acc.Faction == planet.Faction);
                if ((planet.OwnerAccountID != acc.AccountID) && !factionLeader)
                {
                    return(Content("Planet not yours"));
                }
                StructureType structureType = db.StructureTypes.SingleOrDefault(s => s.StructureTypeID == structureTypeID);
                if (structureType == null)
                {
                    return(Content("Structure type does not exist."));
                }
                //if (!structureType.IsBuildable) return Content("Structure is not buildable.");

                if (!planet.PlanetStructures.Any(x => x.StructureTypeID == structureTypeID))
                {
                    return(Content("Structure or its upgrades not present"));
                }
                List <PlanetStructure> list      = planet.PlanetStructures.Where(x => x.StructureTypeID == structureTypeID).ToList();
                PlanetStructure        structure = list[0];
                Account orgAc = structure.Account;
                double  cost  = (!factionLeader) ? structure.StructureType.Cost : 0;
                if (orgAc != null)
                {
                    structure.Account.SpendMetal(-cost);
                    acc.SpendMetal(cost);
                }
                structure.Account = acc;
                db.Events.InsertOnSubmit(PlanetwarsEventCreator.CreateEvent("{0} has confiscated {1} structure {2} on {3}.",
                                                                            acc,
                                                                            orgAc,
                                                                            structure.StructureType,
                                                                            planet));
                db.SaveChanges();

                return(RedirectToAction("Planet", new { id = planetID }));
            }
        }
        /// <summary>
        /// Returns a colored string that says whether the specified <see cref="PlanetStructure"/> is ACTIVE, DISABLED or POWERING
        /// </summary>
        /// <param name="s">The <see cref="PlanetStructure"/> whose status should be printed</param>
        public static MvcHtmlString PrintStructureState(this HtmlHelper helper, PlanetStructure s)
        {
            var url   = Global.UrlHelper();
            var state = "";

            if (!s.IsActive)
            {
                if (s.ActivationTurnCounter == null)
                {
                    state = "<span style='color:red'>DISABLED</span>";
                }
                if (s.ActivationTurnCounter >= 0)
                {
                    state = string.Format(" <span style='color:orange'>POWERING {0} turns left</span>", (s.TurnsToActivateOverride ?? s.StructureType.TurnsToActivate) - s.ActivationTurnCounter);
                }
            }
            else
            {
                state = "<span style='color:green'>ACTIVE</span>";
            }
            return(new MvcHtmlString(state));
        }
Beispiel #7
0
        public ActionResult CreateLink(int planetID, int structureTypeID, int targetID)
        {
            var db = new ZkDataContext();

            PlanetStructure structure = db.PlanetStructures.FirstOrDefault(x => x.PlanetID == planetID && x.StructureTypeID == structureTypeID);
            Planet          source    = db.Planets.FirstOrDefault(x => x.PlanetID == planetID);
            Planet          target    = db.Planets.FirstOrDefault(x => x.PlanetID == targetID);

            // warp jammers protect against link creation
            //var warpDefense = target.PlanetStructures.Where(x => x.StructureType.EffectBlocksJumpgate == true).ToList();
            //if (warpDefense.Count > 0) return Content("Warp jamming prevents link creation");

            if (source.GalaxyID != target.GalaxyID)
            {
                return(Content("Cannot form exo-galaxy link"));
            }

            db.Links.InsertOnSubmit(new Link {
                PlanetID1 = source.PlanetID, PlanetID2 = target.PlanetID, GalaxyID = source.GalaxyID
            });
            db.Events.InsertOnSubmit(PlanetwarsEventCreator.CreateEvent("A new link was created between {0} planet {1} and {2} planet {3} by the {4}", source.Faction, source, target.Faction, target, structure.StructureType));
            db.SaveChanges();
            return(null);
        }
Beispiel #8
0
        private ActionResult ChangePlanetMap(int planetID, int structureTypeID, int targetID, int?newMapID)
        {
            var             db        = new ZkDataContext();
            PlanetStructure structure = db.PlanetStructures.FirstOrDefault(x => x.PlanetID == planetID && x.StructureTypeID == structureTypeID);
            Planet          source    = db.Planets.FirstOrDefault(x => x.PlanetID == planetID);
            Planet          target    = db.Planets.FirstOrDefault(x => x.PlanetID == targetID);
            Galaxy          gal       = db.Galaxies.FirstOrDefault(x => x.GalaxyID == source.GalaxyID);

            if (newMapID == null)
            {
                List <Resource> mapList =
                    db.Resources.Where(
                        x =>
                        x.MapPlanetWarsIcon != null && x.Planets.Where(p => p.GalaxyID == gal.GalaxyID).Count() == 0 && x.MapSupportLevel >= MapSupportLevel.Featured &&
                        x.ResourceID != source.MapResourceID).ToList();
                if (mapList.Count > 0)
                {
                    int r = new Random().Next(mapList.Count);
                    newMapID = mapList[r].ResourceID;
                }
            }
            if (newMapID != null)
            {
                Resource newMap = db.Resources.Single(x => x.ResourceID == newMapID);
                target.Resource = newMap;
                gal.IsDirty     = true;
                string word = "";
                if (target.Faction == source.Faction)
                {
                    if (target.TeamSize < GlobalConst.PlanetWarsMaxTeamsize)
                    {
                        target.TeamSize++;
                    }
                    word = "terraformed";
                }
                else
                {
                    word = "nanodegraded";
                    if (target.TeamSize > 1)
                    {
                        target.TeamSize--;
                    }
                }

                db.Events.InsertOnSubmit(PlanetwarsEventCreator.CreateEvent("{0} {1} has been {6} by {2} from {3} {4}. New team size is {5} vs {5}",
                                                                            target.Faction,
                                                                            target,
                                                                            structure.StructureType,
                                                                            source.Faction,
                                                                            source,
                                                                            target.TeamSize,
                                                                            word));
            }
            else
            {
                return
                    (Content(string.Format("Terraform attempt on {0} {1} using {2} from {3} {4} has failed - no valid maps",
                                           target.Faction,
                                           target,
                                           structure.StructureType,
                                           source.Faction,
                                           source)));
            }
            db.SaveChanges();
            return(null);
        }
Beispiel #9
0
        public ActionResult BombPlanet(int planetID, int count, bool?useWarp)
        {
            var     db  = new ZkDataContext();
            Account acc = db.Accounts.Single(x => x.AccountID == Global.AccountID);

            if (acc.Faction == null)
            {
                return(Content("Join some faction first"));
            }
            Planet planet     = db.Planets.Single(x => x.PlanetID == planetID);
            bool   accessible = (useWarp == true) ? planet.CanBombersWarp(acc.Faction) : planet.CanBombersAttack(acc.Faction);

            if (!accessible)
            {
                return(Content("You cannot attack here"));
            }
            if (Global.Server.GetPlanetBattles(planet).Any(x => x.IsInGame))
            {
                return(Content("Battle in progress on the planet, cannot bomb planet"));
            }

            bool selfbomb = acc.FactionID == planet.OwnerFactionID;

            if (count < 0)
            {
                count = 0;
            }
            double avail = Math.Min(count, acc.GetBombersAvailable());

            if (useWarp == true)
            {
                avail = Math.Min(acc.GetWarpAvailable(), avail);
            }

            var capa = acc.GetBomberCapacity();

            if (avail > capa)
            {
                return(Content("Too many bombers - the fleet limit is " + capa));
            }

            if (avail > 0)
            {
                double defense   = planet.PlanetStructures.Where(x => x.IsActive).Sum(x => x.StructureType.EffectBomberDefense) ?? 0;
                double effective = avail;
                if (!selfbomb)
                {
                    effective = effective - defense;
                }

                if (effective <= 0)
                {
                    return(Content("Enemy defenses completely block your ships"));
                }

                acc.SpendBombers(avail);
                if (useWarp == true)
                {
                    acc.SpendWarps(avail);
                }

                var r = new Random();

                double strucKillChance = !selfbomb ? effective * GlobalConst.BomberKillStructureChance : 0;
                int    strucKillCount  = (int)Math.Floor(strucKillChance + r.NextDouble());

                double ipKillChance = effective * GlobalConst.BomberKillIpChance;
                int    ipKillCount  = (int)Math.Floor(ipKillChance + r.NextDouble());

                List <PlanetStructure> structs = planet.PlanetStructures.Where(x => x.StructureType.IsBomberDestructible).ToList();
                var bombed = new List <StructureType>();
                while (structs.Count > 0 && strucKillCount > 0)
                {
                    strucKillCount--;
                    PlanetStructure s = structs[r.Next(structs.Count)];
                    bombed.Add(s.StructureType);
                    structs.Remove(s);
                    db.PlanetStructures.DeleteOnSubmit(s);
                }

                double ipKillAmmount = ipKillCount * GlobalConst.BomberKillIpAmount;
                if (ipKillAmmount > 0)
                {
                    var influenceDecayMin = planet.PlanetStructures.Where(x => x.IsActive && x.StructureType.EffectPreventInfluenceDecayBelow != null).Select(x => x.StructureType.EffectPreventInfluenceDecayBelow).OrderByDescending(x => x).FirstOrDefault() ?? 0;


                    foreach (PlanetFaction pf in planet.PlanetFactions.Where(x => x.FactionID != acc.FactionID))
                    {
                        pf.Influence -= ipKillAmmount;
                        if (pf.Influence < 0)
                        {
                            pf.Influence = 0;
                        }

                        // prevent bombing below influence decaymin for owner - set by active structures
                        if (pf.FactionID == planet.OwnerFactionID && pf.Influence < influenceDecayMin)
                        {
                            pf.Influence = influenceDecayMin;
                        }
                    }
                }

                var args = new List <object>
                {
                    acc,
                    acc.Faction,
                    !selfbomb ? planet.Faction : null,
                    planet,
                    avail,
                    defense,
                    useWarp == true ? "They attacked by warp. " : "",
                    ipKillAmmount
                };
                args.AddRange(bombed);

                string str;
                if (selfbomb)
                {
                    str = "{0} of {1} bombed own planet {3} using {4} bombers against {5} defenses. {6}Ground armies lost {7} influence";
                }
                else
                {
                    str = "{0} of {1} bombed {2} planet {3} using {4} bombers against {5} defenses. {6}Ground armies lost {7} influence";
                }
                if (bombed.Count > 1)
                {
                    str += " and ";
                    int counter = 8;
                    foreach (var b in bombed)
                    {
                        str += "{" + counter + "}" + ", ";
                        counter++;
                    }
                    str += " were destroyed.";
                }
                else if (bombed.Count == 1)
                {
                    str += " and {8} was destroyed.";
                }
                else
                {
                    str += ".";
                }

                db.Events.InsertOnSubmit(PlanetwarsEventCreator.CreateEvent(str, args.ToArray()));
            }

            db.SaveChanges();
            PlanetWarsTurnHandler.SetPlanetOwners(new PlanetwarsEventCreator());
            return(RedirectToAction("Planet", new { id = planetID }));
        }