예제 #1
0
        public static Tuple <PopulationTransport, CelestialObject> CreatePopulationTransport(string playerName, string password, int fromBodyID, int toBodyID, int count)
        {
            using (var t = new TransactionScope())
            {
                var db     = new DbDataContext();
                var player = db.GetPlayer(playerName, password);
                var from   = player.CelestialObjects.Single(x => x.CelestialObjectID == fromBodyID);
                var target = db.CelestialObjects.Single(x => x.CelestialObjectID == toBodyID);

                if (from.Population < count)
                {
                    throw new ApplicationException("There are not enough people on this planet");
                }
                from.Population -= count;
                from.UpdateIncomeInfo();
                var transport = new PopulationTransport()
                {
                    Count = count, Player = player, Transit = new Transit()
                };
                player.PopulationTransports.Add(transport);

                var transit = transport.Transit;

                transit.SetTransit(target, FleetBaseWarp, db.GetConfig().CombatTurn + 1, db.GetConfig());                  // will start next turn


                db.MarkDirty();
                db.SubmitChanges();
                t.Complete();
                return(new Tuple <PopulationTransport, CelestialObject>(transport, from));
            }
        }
예제 #2
0
        public static BodyResponse SellStructure(string playerName, string password, int celestialObjectID, int structureTypeID)
        {
            using (var t = new TransactionScope()) {
                var db = new DbDataContext();
                var p  = db.GetPlayer(playerName, password);

                var planet = db.CelestialObjects.Single(x => x.CelestialObjectID == celestialObjectID && x.OwnerID == p.PlayerID);

                var ret = new BodyResponse();

                var cnt = planet.GetStructureCount(structureTypeID);
                if (cnt <= 0)
                {
                    ret.Response = BuildResponse.DoesNotExist;
                    return(ret);
                }
                else
                {
                    planet.SetStructureCount(structureTypeID, cnt - 1);
                }

                p.Metal += db.StructureTypes.Single(x => x.StructureTypeID == structureTypeID).CostMetal / 2.0;
                db.MarkDirty();
                db.SubmitChanges();
                planet.UpdateIncomeInfo();
                db.SubmitChanges();
                t.Complete();
                return(GetBodyOptions(playerName, password, celestialObjectID));
            }
        }
예제 #3
0
        public static BodyResponse SellMothershipModule(string playerName, string password, int structureType)
        {
            using (var t = new TransactionScope()) {
                var db = new DbDataContext();
                var p  = db.GetPlayer(playerName, password);

                var ret = new BodyResponse();


                var cnt = p.GetStructureCount(structureType);
                if (cnt <= 0)
                {
                    ret.Response = BuildResponse.DoesNotExist;
                    return(ret);
                }
                p.SetStructureCount(structureType, cnt - 1);

                p.Metal += db.StructureTypes.Single(x => x.StructureTypeID == structureType).CostMetal / 2.0;

                db.SubmitChanges();
                if (p.CelestialObject != null)
                {
                    p.CelestialObject.UpdateIncomeInfo();
                }
                db.MarkDirty();
                db.SubmitChanges();
                p.MothershipStructures.Load();
                ret.Player = p;
                ret.NewStructureOptions = GetMotherhipModuleBuildOption(playerName, password);
                ret.Body     = p.CelestialObject;
                ret.Response = BuildResponse.Ok;
                t.Complete();
                return(ret);
            }
        }
예제 #4
0
        public static Fleet ModifyFleet(string playerName, string password, int fleetID, IEnumerable <ShipTypeCount> fleetShips)
        {
            using (var t = new TransactionScope())
            {
                var db     = new DbDataContext();
                var player = db.GetPlayer(playerName, password);

                var fleet = player.Fleets.SingleOrDefault(x => x.FleetID == fleetID);
                if (fleet.Transit.OrbitsObjectID == null)
                {
                    throw new ApplicationException("Fleet not in orbit");
                }
                var body = fleet.Transit.CelestialObject;
                if (body.OwnerID != player.PlayerID)
                {
                    throw new ApplicationException("Not my planet");
                }

                foreach (var order in fleetShips)
                {
                    if (order.Count > 0)
                    {
                        var garrisonCount = body.GetShipCount(order.ShipTypeID);
                        if (order.Count > garrisonCount)
                        {
                            throw new ApplicationException(string.Format("Not enough ships of type {0}", order.ShipTypeID));
                        }

                        body.SetShipCount(order.ShipTypeID, garrisonCount - order.Count);
                        fleet.SetShipCount(order.ShipTypeID, order.Count);
                    }
                    else
                    {
                        var change     = order.Count;
                        var fleetCount = fleet.GetShipCount(order.ShipTypeID);
                        if (change > fleetCount)
                        {
                            throw new ApplicationException(string.Format("Not enough ships of type {0} in fleet", order.ShipTypeID));
                        }

                        fleet.SetShipCount(order.ShipTypeID, fleetCount - change);
                        body.SetShipCount(order.ShipTypeID, change);
                    }
                }
                if (!fleet.FleetShips.Any())
                {
                    db.Fleets.DeleteOnSubmit(fleet);
                    fleet = null;
                }

                db.MarkDirty();
                db.SubmitChanges();
                if (fleet != null)
                {
                    fleet.FleetShips.Load();
                }
                t.Complete();
                return(fleet);
            }
        }
예제 #5
0
        public static IEnumerable <StructureOption> GetMotherhipModuleBuildOption(string playerName, string password)
        {
            var db     = new DbDataContext();
            var player = db.GetPlayer(playerName, password);

            var structureOptions = new List <StructureOption>();

            var noRoom = player.Level <= player.MothershipStructures.Sum(x => x.Count);

            foreach (var u in db.StructureTypes.Where(x => x.NeedsTechID == null || x.Tech.StarSystemTeches.Any(y => y.StarSystemID == player.SystemID)))
            {
                var option = new StructureOption()
                {
                    StructureType = u
                };

                if (noRoom)
                {
                    option.CanBuild = BuildResponse.NotEnoughRoomOrBuildpower;
                }
                else if (u.CostMetal >= player.Metal)
                {
                    option.CanBuild = BuildResponse.NotEnoughResources;
                }
                else
                {
                    option.CanBuild = BuildResponse.Ok;
                }
                structureOptions.Add(option);
            }
            return(structureOptions);
        }
예제 #6
0
        public static Fleet CreateFleet(string playerName, string password, int bodyID, IEnumerable <ShipTypeCount> fleetShips)
        {
            using (var t = new TransactionScope())
            {
                var db     = new DbDataContext();
                var player = db.GetPlayer(playerName, password);

                var body = player.CelestialObjects.Single(x => x.CelestialObjectID == bodyID);

                var fleet = new Fleet();
                fleet.Transit = new Transit();
                fleet.Transit.OrbitsObjectID = bodyID;

                foreach (var order in fleetShips)
                {
                    var garrisonCount = body.GetShipCount(order.ShipTypeID);
                    if (order.Count > garrisonCount)
                    {
                        throw new ApplicationException(string.Format("Not enough ships of type {0}", order.ShipTypeID));
                    }

                    body.SetShipCount(order.ShipTypeID, garrisonCount - order.Count);
                    fleet.SetShipCount(order.ShipTypeID, order.Count);
                }
                player.Fleets.Add(fleet);

                db.MarkDirty();
                db.SubmitChanges();
                fleet.FleetShips.Load();
                t.Complete();
                return(fleet);
            }
        }
		public static Fleet CreateFleet(string playerName, string password, int bodyID, IEnumerable<ShipTypeCount> fleetShips)
		{
			using (var t = new TransactionScope())
			{
				var db = new DbDataContext();
				var player = db.GetPlayer(playerName, password);

				var body = player.CelestialObjects.Single(x => x.CelestialObjectID == bodyID);

				var fleet = new Fleet();
				fleet.Transit = new Transit();
				fleet.Transit.OrbitsObjectID = bodyID;

				foreach (var order in fleetShips)
				{
					var garrisonCount = body.GetShipCount(order.ShipTypeID);
					if (order.Count > garrisonCount) throw new ApplicationException(string.Format("Not enough ships of type {0}", order.ShipTypeID));

					body.SetShipCount(order.ShipTypeID, garrisonCount - order.Count);
					fleet.SetShipCount(order.ShipTypeID, order.Count);
				}
				player.Fleets.Add(fleet);

				db.MarkDirty();
				db.SubmitChanges();
				fleet.FleetShips.Load();
				t.Complete();
				return fleet;
			}
		}
		public static Fleet ModifyFleet(string playerName, string password, int fleetID, IEnumerable<ShipTypeCount> fleetShips)
		{
			using (var t = new TransactionScope())
			{
				var db = new DbDataContext();
				var player = db.GetPlayer(playerName, password);

				var fleet = player.Fleets.SingleOrDefault(x => x.FleetID == fleetID);
				if (fleet.Transit.OrbitsObjectID == null) throw new ApplicationException("Fleet not in orbit");
				var body = fleet.Transit.CelestialObject;
				if (body.OwnerID != player.PlayerID) throw new ApplicationException("Not my planet");

				foreach (var order in fleetShips)
				{
					if (order.Count > 0)
					{
						var garrisonCount = body.GetShipCount(order.ShipTypeID);
						if (order.Count > garrisonCount) throw new ApplicationException(string.Format("Not enough ships of type {0}", order.ShipTypeID));

						body.SetShipCount(order.ShipTypeID, garrisonCount - order.Count);
						fleet.SetShipCount(order.ShipTypeID, order.Count);
					}
					else
					{
						var change = order.Count;
						var fleetCount = fleet.GetShipCount(order.ShipTypeID);
						if (change > fleetCount) throw new ApplicationException(string.Format("Not enough ships of type {0} in fleet", order.ShipTypeID));

						fleet.SetShipCount(order.ShipTypeID, fleetCount - change);
						body.SetShipCount(order.ShipTypeID, change);
					}
				}
				if (!fleet.FleetShips.Any())
				{
					db.Fleets.DeleteOnSubmit(fleet);
					fleet = null;
				}

				db.MarkDirty();
				db.SubmitChanges();
				if (fleet != null) fleet.FleetShips.Load();
				t.Complete();
				return fleet;
			}
		}
예제 #9
0
        public static BodyResponse BuildMothershipModule(string playerName, string password, int structureType)
        {
            using (var t = new TransactionScope()) {
                var db = new DbDataContext();
                var p  = db.GetPlayer(playerName, password);

                var ret = new BodyResponse();

                if (p.Level <= p.MothershipStructures.Sum(x => x.Count))
                {
                    ret.Response = BuildResponse.NotEnoughRoomOrBuildpower;
                    return(ret);
                }

                var structure =
                    db.StructureTypes.Single(
                        x => x.StructureTypeID == structureType && (x.NeedsTechID == null || x.Tech.StarSystemTeches.Any(y => y.StarSystemID == p.SystemID)));

                if (structure.CostMetal >= p.Metal)
                {
                    ret.Response = BuildResponse.NotEnoughResources;
                    return(ret);
                }

                p.SetStructureCount(structureType, p.GetStructureCount(structureType) + 1);

                p.Metal -= structure.CostMetal;

                db.SubmitChanges();
                if (p.CelestialObject != null)
                {
                    p.CelestialObject.UpdateIncomeInfo();
                }
                db.MarkDirty();
                db.SubmitChanges();
                p.MothershipStructures.Load();
                ret.Player = p;
                ret.NewStructureOptions = GetMotherhipModuleBuildOption(playerName, password);
                ret.Body     = p.CelestialObject;
                ret.Response = BuildResponse.Ok;
                t.Complete();
                return(ret);
            }
        }
예제 #10
0
        public static Tuple <Player, CelestialObject> OrderMothership(string playerName, string password, int toBodyID)
        {
            using (var t = new TransactionScope())
            {
                var db     = new DbDataContext();
                var player = db.GetPlayer(playerName, password);
                if (player.Transit == null)
                {
                    player.Transit = new Transit()
                    {
                        CelestialObject = player.CelestialObject
                    }
                }
                ;
                var originalBody = player.CelestialObject;
                var target       = db.CelestialObjects.Single(x => x.CelestialObjectID == toBodyID);
                var transit      = player.Transit;

                var oldOrbit  = transit.OrbitsObjectID;
                var oldTarget = transit.CelestialObjectByToObjectID;

                transit.SetTransit(target, FleetBaseWarp, db.GetConfig().CombatTurn + 1, db.GetConfig()); // will start next turn

                if (oldOrbit == null && oldTarget != null)                                                // was heading to another planet
                {
                    var ev = db.AddEvent(EventType.Fleet, "{0} changed destination from {1} to {2}, ETA {3}", transit.GetNameWithOwner(), oldTarget.GetNameWithOwner(), transit.CelestialObjectByToObjectID.GetNameWithOwner(), transit.GetEtaString(db.GetConfig().SecondsPerTurn));
                    ev.Connect(transit.Fleets);
                    ev.Connect(transit.GetOwner());
                    ev.Connect(transit.CelestialObjectByToObjectID, oldTarget);
                    ev.Connect(transit.CelestialObjectByToObjectID.Player, oldTarget.Player);
                }

                if (originalBody != null)
                {
                    originalBody.UpdateIncomeInfo();
                }
                db.MarkDirty();
                db.SubmitChanges();
                t.Complete();
                return(new Tuple <Player, CelestialObject>(player, originalBody));
            }
        }
예제 #11
0
        public static BodyResponse BuildShip(int count, string playerName, string password, int celestialObjectID, int shipType)
        {
            using (var t = new TransactionScope()) {
                if (count < 1)
                {
                    throw new ApplicationException("Invalid count");
                }
                var db = new DbDataContext();
                var p  = db.GetPlayer(playerName, password);

                var planet = p.CelestialObjects.Single(x => x.CelestialObjectID == celestialObjectID);

                var ship =
                    db.ShipTypes.Single(x => x.ShipTypeID == shipType && (x.NeedsTechID == null || x.Tech.StarSystemTeches.Any(y => y.StarSystemID == p.SystemID)));

                var ret = new BodyResponse();

                if (planet.Buildpower - planet.BuildpowerUsed < ship.MetalCost * count)
                {
                    ret.Response = BuildResponse.NotEnoughRoomOrBuildpower;
                    return(ret);
                }

                if (ship.MetalCost * count > p.Metal || ship.QuantiumCost * count > p.Quantium || ship.DarkMetalCost * count > p.DarkMatter)
                {
                    ret.Response = BuildResponse.NotEnoughResources;
                    return(ret);
                }

                planet.SetShipCount(shipType, planet.GetShipCount(shipType) + count);

                planet.BuildpowerUsed += ship.MetalCost * count;
                p.Metal      -= ship.MetalCost * count;
                p.Quantium   -= ship.QuantiumCost * count;
                p.DarkMatter -= ship.DarkMetalCost * count;
                db.MarkDirty();
                db.SubmitChanges();
                t.Complete();
                return(GetBodyOptions(playerName, password, celestialObjectID));
            }
        }
예제 #12
0
        public static BodyResponse BuildStructure(string playerName, string password, int celestialObjectID, int structureTypeID)
        {
            using (var t = new TransactionScope()) {
                var db = new DbDataContext();
                var p  = db.GetPlayer(playerName, password);

                var ret = new BodyResponse();

                var planet = db.CelestialObjects.Single(x => x.CelestialObjectID == celestialObjectID && x.OwnerID == p.PlayerID);

                if (planet.Size <= planet.CelestialObjectStructures.Sum(x => x.Count))
                {
                    ret.Response = BuildResponse.NotEnoughRoomOrBuildpower;
                    return(ret);
                }

                var structure =
                    db.StructureTypes.Single(
                        x => x.StructureTypeID == structureTypeID && (x.NeedsTechID == null || x.Tech.StarSystemTeches.Any(y => y.StarSystemID == p.SystemID)));

                if (structure.CostMetal >= p.Metal)
                {
                    ret.Response = BuildResponse.NotEnoughResources;
                    return(ret);
                }

                planet.SetStructureCount(structureTypeID, planet.GetStructureCount(structureTypeID) + 1);
                p.Metal -= structure.CostMetal;

                db.MarkDirty();
                db.SubmitChanges();
                planet.UpdateIncomeInfo();
                db.SubmitChanges();
                t.Complete();
                return(GetBodyOptions(playerName, password, celestialObjectID));
            }
        }
예제 #13
0
        public static Fleet OrderFleet(string playerName, string password, int fleetID, int toBodyID, int futureOffset)
        {
            if (futureOffset < 0)
            {
                throw new ApplicationException("Future offset cannot be smaller than 0");
            }
            using (var t = new TransactionScope())
            {
                var db     = new DbDataContext();
                var player = db.GetPlayer(playerName, password);
                var fleet  = player.Fleets.Single(x => x.FleetID == fleetID);
                var target = db.CelestialObjects.Single(x => x.CelestialObjectID == toBodyID);

                var oldOrbit  = fleet.Transit.OrbitsObjectID;
                var oldTarget = fleet.Transit.CelestialObjectByToObjectID;

                fleet.Transit.SetTransit(target, FleetBaseWarp, db.GetConfig().CombatTurn + 1 + futureOffset, db.GetConfig()); // will start next turn

                if (oldOrbit == null && oldTarget != null)                                                                     // was heading to another planet
                {
                    var transit = fleet.Transit;
                    var ev      = db.AddEvent(EventType.Fleet, "{0} changed destination from {1} to {2}, ETA {3}", transit.GetNameWithOwner(), oldTarget.GetNameWithOwner(), transit.CelestialObjectByToObjectID.GetNameWithOwner(), transit.GetEtaString(db.GetConfig().SecondsPerTurn));
                    ev.Connect(transit.Fleets);
                    ev.Connect(transit.GetOwner());
                    ev.Connect(transit.CelestialObjectByToObjectID, oldTarget);
                    ev.Connect(transit.CelestialObjectByToObjectID.Player, oldTarget.Player);
                }


                db.MarkDirty();
                db.SubmitChanges();
                fleet.FleetShips.Load();
                t.Complete();
                return(fleet);
            }
        }
예제 #14
0
		public static Fleet OrderFleet(string playerName, string password, int fleetID, int toBodyID, int futureOffset)
		{
			if (futureOffset < 0) throw new ApplicationException("Future offset cannot be smaller than 0");
			using (var t = new TransactionScope())
			{
				var db = new DbDataContext();
				var player = db.GetPlayer(playerName, password);
				var fleet = player.Fleets.Single(x => x.FleetID == fleetID);
				var target = db.CelestialObjects.Single(x => x.CelestialObjectID == toBodyID);

				var oldOrbit = fleet.Transit.OrbitsObjectID;
				var oldTarget = fleet.Transit.CelestialObjectByToObjectID;

				fleet.Transit.SetTransit(target, FleetBaseWarp, db.GetConfig().CombatTurn + 1 + futureOffset, db.GetConfig()); // will start next turn

				if (oldOrbit == null && oldTarget != null) // was heading to another planet
				{
					var transit = fleet.Transit;
					var ev = db.AddEvent(EventType.Fleet, "{0} changed destination from {1} to {2}, ETA {3}", transit.GetNameWithOwner(), oldTarget.GetNameWithOwner(), transit.CelestialObjectByToObjectID.GetNameWithOwner(), transit.GetEtaString(db.GetConfig().SecondsPerTurn));
					ev.Connect(transit.Fleets);
					ev.Connect(transit.GetOwner());
					ev.Connect(transit.CelestialObjectByToObjectID, oldTarget);
					ev.Connect(transit.CelestialObjectByToObjectID.Player, oldTarget.Player);
				}


				db.MarkDirty();
				db.SubmitChanges();
				fleet.FleetShips.Load();
				t.Complete();
				return fleet;
			}
		}
예제 #15
0
        public static BodyResponse GetBodyOptions(string playerName, string password, int bodyID)
        {
            var db     = new DbDataContext();
            var player = db.GetPlayer(playerName, password);

            if (player == null)
            {
                throw new ApplicationException("Fail login");
            }
            var body = player.CelestialObjects.Single(x => x.CelestialObjectID == bodyID);

            body.CelestialObjectStructures.Load();
            body.CelestialObjectShips.Load();

            var shipOptions = new List <ShipOption>();

            foreach (var opt in db.ShipTypes.Where(x => x.NeedsTechID == null || x.Tech.StarSystemTeches.Any(y => y.StarSystemID == player.SystemID)))
            {
                var so = new ShipOption();
                so.ShipType = opt;
                if (opt.MetalCost > player.Metal || opt.QuantiumCost > player.Quantium || opt.DarkMetalCost > player.DarkMatter)
                {
                    so.CanBuild = BuildResponse.NotEnoughResources;
                }
                if (opt.MetalCost > body.Buildpower - body.BuildpowerUsed)
                {
                    so.CanBuild = BuildResponse.NotEnoughRoomOrBuildpower;
                }
                shipOptions.Add(so);
            }

            var noRoom = body.Size <= body.CelestialObjectStructures.Sum(x => x.Count);

            var structureOptions = new List <StructureOption>();

            foreach (var u in db.StructureTypes.Where(x => x.NeedsTechID == null || x.Tech.StarSystemTeches.Any(y => y.StarSystemID == player.SystemID)))
            {
                var option = new StructureOption()
                {
                    StructureType = u
                };

                if (noRoom)
                {
                    option.CanBuild = BuildResponse.NotEnoughRoomOrBuildpower;
                }
                else if (u.CostMetal >= player.Metal)
                {
                    option.CanBuild = BuildResponse.NotEnoughResources;
                }
                else
                {
                    option.CanBuild = BuildResponse.Ok;
                }
                structureOptions.Add(option);
            }

            var ret = new BodyResponse()
            {
                Body = body, NewShipOptions = shipOptions, Player = player, Response = BuildResponse.Ok, NewStructureOptions = structureOptions
            };

            return(ret);
        }
예제 #16
0
		public static Tuple<PopulationTransport, CelestialObject> CreatePopulationTransport(string playerName, string password, int fromBodyID, int toBodyID, int count)
		{
			using (var t = new TransactionScope())
			{
				var db = new DbDataContext();
				var player = db.GetPlayer(playerName, password);
				var from = player.CelestialObjects.Single(x => x.CelestialObjectID == fromBodyID);
				var target = db.CelestialObjects.Single(x => x.CelestialObjectID == toBodyID);
				
				if (from.Population < count) throw new ApplicationException("There are not enough people on this planet");
				from.Population -= count;
				from.UpdateIncomeInfo();
				var transport = new PopulationTransport() { Count = count, Player = player, Transit = new Transit() };
				player.PopulationTransports.Add(transport);

				var transit = transport.Transit;

				transit.SetTransit(target, FleetBaseWarp, db.GetConfig().CombatTurn + 1 , db.GetConfig()); // will start next turn
        

				db.MarkDirty();
				db.SubmitChanges();
				t.Complete();
				return new Tuple<PopulationTransport, CelestialObject>(transport, from);
			}

		}
예제 #17
0
		public static Tuple<Player, CelestialObject> OrderMothership(string playerName, string password, int toBodyID)
		{
			using (var t = new TransactionScope())
			{
				var db = new DbDataContext();
				var player = db.GetPlayer(playerName, password);
				if (player.Transit == null) player.Transit = new Transit()
				                                             	{
				                                             		CelestialObject = player.CelestialObject
				                                             	};
				var originalBody = player.CelestialObject;
				var target = db.CelestialObjects.Single(x => x.CelestialObjectID == toBodyID);
				var transit = player.Transit;

				var oldOrbit = transit.OrbitsObjectID;
				var oldTarget = transit.CelestialObjectByToObjectID;

				transit.SetTransit(target, FleetBaseWarp, db.GetConfig().CombatTurn + 1, db.GetConfig()); // will start next turn

				if (oldOrbit == null && oldTarget != null) // was heading to another planet
				{
					var ev = db.AddEvent(EventType.Fleet, "{0} changed destination from {1} to {2}, ETA {3}", transit.GetNameWithOwner(), oldTarget.GetNameWithOwner(), transit.CelestialObjectByToObjectID.GetNameWithOwner(), transit.GetEtaString(db.GetConfig().SecondsPerTurn));
					ev.Connect(transit.Fleets);
					ev.Connect(transit.GetOwner());
					ev.Connect(transit.CelestialObjectByToObjectID, oldTarget);
					ev.Connect(transit.CelestialObjectByToObjectID.Player, oldTarget.Player);
				}

				if (originalBody != null) originalBody.UpdateIncomeInfo();
				db.MarkDirty();
				db.SubmitChanges();
				t.Complete();
				return new Tuple<Player, CelestialObject>(player, originalBody);
			}
		}