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 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)); } }
public static void ProcessTurn(int count) { for (var i = 0; i < count; i++) { using (var t = new TransactionScope()) { var db = new DbDataContext(); var config = db.GetConfig(); config.CombatTurn++; FleetLogic.ProcessTransitTurn(db); foreach (var o in db.CelestialObjects) { o.UpdateIncomeInfo(); } foreach (var player in db.Players.Where(x => x.IsActive)) { if (config.CombatTurn % config.PopulationTick == 0) { player.ApplyPopulationTick(config.CombatTurn); } player.ApplyResourceTurn(config.CombatTurn); } db.MarkDirty(); config.TurnStarted = DateTime.UtcNow; // todo synchronize properly when not faking db.SubmitChanges(); t.Complete(); } } }
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); } }
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 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); } }
public static StarMap GetMapData(string playerName, string password) { var db = new DbDataContext(); var ret = new StarMap(); var lo = new DataLoadOptions(); lo.LoadWith <CelestialObject>(x => x.CelestialObjectShips); // todo stealth also in all fleet structures lo.LoadWith <CelestialObject>(x => x.CelestialObjectStructures); db.LoadOptions = lo; ret.CelestialObjects = db.CelestialObjects.ToList(); ret.Players = db.Players.Where(x => x.IsActive).ToList(); ret.StarSystems = db.StarSystems.ToList(); ret.Transits = FleetLogic.GetTransits(playerName, password); ret.Config = db.GetConfig(); ret.ObjectLinks = db.CelestialObjectLinks.ToList(); return(ret); }
public static void ProcessTurn(int count) { for (var i = 0; i < count; i++) { using (var t = new TransactionScope()) { var db = new DbDataContext(); var config = db.GetConfig(); config.CombatTurn++; FleetLogic.ProcessTransitTurn(db); foreach (var o in db.CelestialObjects) { o.UpdateIncomeInfo(); } foreach (var player in db.Players.Where(x => x.IsActive)) { if (config.CombatTurn%config.PopulationTick == 0) player.ApplyPopulationTick(config.CombatTurn); player.ApplyResourceTurn(config.CombatTurn); } db.MarkDirty(); config.TurnStarted = DateTime.UtcNow; // todo synchronize properly when not faking db.SubmitChanges(); t.Complete(); } } }
public static LoginResponse Login(string login, string password) { var db = new DbDataContext(); var hp = SpringAccount.HashPassword(password); var user = db.SpringAccounts.SingleOrDefault(x => x.Name == login); if (user == null) { return(LoginResponse.InvalidLogin); } if (user.Password != hp) { return(LoginResponse.InvalidPassword); } if (user.Players == null) { return(LoginResponse.Unregistered); } user.Players.IsActive = true; db.SubmitChanges(); return(LoginResponse.Ok); }
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)); } }
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); }
public void NewMap() { var db = new DbDataContext(); db.CelestialObjects.DeleteAllOnSubmit(db.CelestialObjects.Where(x => x.CelestialObjectType != CelestialObjectType.Star && (x.ParentCelestialObject.StarSystems == null))); db.SubmitChanges(); var neutralStars = db.CelestialObjects.Where(x => x.CelestialObjectType == CelestialObjectType.Star && x.StarSystems == null); foreach (var star in neutralStars) { var planetCount = random.Next(3, 10); for (int i =0 ; i< planetCount; i++) { int distance = 0; var ok = false; while (!ok) { distance = random.Next(5, 100); ok = true; foreach (var p in star.ChildCelestialObjects) { if (Math.Abs(p.OrbitDistance - distance) < 5) { ok = false; break; } } } // todo set proper naming (in order)! CelestialObject o = GetRandomObject(CelestialObjectType.Star); star.ChildCelestialObjects.Add(o); // orbits star o.Name = string.Format("{0} {1}", star.Name, ToRoman(i+1)); o.OrbitDistance = distance; o.OrbitSpeed = 1 / o.OrbitDistance / 100; o.OrbitSpeed = o.OrbitSpeed*(1+GaussianRandom(-30, 30)/100); if (o.CelestialObjectType != CelestialObjectType.Asteroid) { var moonCount = random.Next(3); double moonDistance = 0; for (int j = 0 ; j < moonCount;j++) { var moonStep = GaussianRandom(0.5, 2.5); CelestialObject m = GetRandomObject(o.CelestialObjectType); o.ChildCelestialObjects.Add(m); moonDistance += moonStep; m.OrbitDistance = moonDistance; m.OrbitSpeed = 1/m.OrbitDistance/300; m.OrbitSpeed = m.OrbitSpeed * (1 + GaussianRandom(-30, 30) / 100); m.Name = o.Name + (char)('A' + j); } } } db.SubmitChanges(); } }
private void button1_Click(object sender, EventArgs e) { var mg = new MapGen(); mg.NewMap(); pictureBox1.Refresh(); var db = new DbDataContext(); co = db.CelestialObjects.ToList(); }
static void Main(string[] args) { var db = new DbDataContext(); var dict = db.SpringAccounts.ToDictionary(x => x.SpringAccountID); var path = args.Length > 0 ? args[0] : @"accounts.txt"; using (var r = new StreamReader(path)) { string line; while ((line = r.ReadLine()) != null) { try { var parts = line.Split(' '); if (parts.Length < 9) { Trace.TraceError("Faulty line: ", line); continue; } var name = parts[0]; var pass = parts[1]; var flags = parts[2]; var cookie = int.Parse(parts[3]); var lastLogin = ConvertTimestamp(double.Parse(parts[4])/1000); var lastIP = parts[5]; var registered = ConvertTimestamp(double.Parse(parts[6])/1000); var country = parts[7]; var id = int.Parse(parts[8]); SpringAccount de = null; dict.TryGetValue(id, out de); SpringAccount ac = null; Console.WriteLine(string.Format("{0} {1}", id, name)); if (de == null || name != de.Name || pass != de.Password || cookie != de.UserCookie || lastIP != de.LastIP) { if (de == null) { ac = new SpringAccount(); db.SpringAccounts.InsertOnSubmit(ac); } else ac = db.SpringAccounts.SingleOrDefault(x => x.SpringAccountID == id); ac.SpringAccountID = id; ac.Name = name; ac.Flags = flags; ac.Password = pass; ac.UserCookie = cookie; ac.LastLogin = lastLogin; ac.LastIP = lastIP; ac.Created = registered; ac.LastCountry = country; Console.Write(" CHANGED!"); db.SubmitChanges(); } } catch (Exception e) { Console.WriteLine("Problem importing line: {0}: {1}", line, e); db = new DbDataContext(); } } } }
public static Invariants GetInvariants() { var db = new DbDataContext(); var ret = new Invariants(); ret.StructureTypes = db.StructureTypes.ToList(); ret.ShipTypes = db.ShipTypes.ToList(); ret.Technologies = db.Teches.ToList(); return ret; }
private void Form1_Load(object sender, EventArgs e) { var db = new DbDataContext(); co = db.CelestialObjects.ToList(); var timer = new Timer(); timer.Tick += new EventHandler(timer_Tick); timer.Interval = 100; timer.Start(); }
public static Invariants GetInvariants() { var db = new DbDataContext(); var ret = new Invariants(); ret.StructureTypes = db.StructureTypes.ToList(); ret.ShipTypes = db.ShipTypes.ToList(); ret.Technologies = db.Teches.ToList(); return(ret); }
public static IEnumerable<Transit> GetTransits(string playerName, string password) { var db = new DbDataContext(); var dl = new DataLoadOptions(); dl.LoadWith<Transit>(x => x.Fleets); dl.LoadWith<Fleet>(x => x.FleetShips); dl.LoadWith<Transit>(x => x.PopulationTransports); dl.LoadWith<Transit>(x => x.Players); db.LoadOptions = dl; return db.Transits.ToList(); }
public static RegisterResponse Register(string login, string password) { using (var t = new TransactionScope()) { var db = new DbDataContext(); var user = db.SpringAccounts.SingleOrDefault(x => x.Name == login); if (user == null) { return(RegisterResponse.NotValidSpringLogin); } if (user.Players != null) { return(RegisterResponse.AlreadyRegistered); } if (db.SpringAccounts.Any(x => x.UserCookie == user.UserCookie && x.Players != null)) { return(RegisterResponse.IsSmurf); } if (user.Password != SpringAccount.HashPassword(password)) { return(RegisterResponse.NotValidSpringPassword); } // create player and assign to smallest alliance var player = new Player() { IsActive = true, StarSystem = db.StarSystems.OrderBy(x => x.Players.Where(y => y.IsActive).Count()).First() }; user.Players = player; player.MothershipName = player.SpringAccount.Name + "'s mothership"; // create his planet var mg = new MapGen(); var hisPlanet = mg.CreateHomeworld(player.StarSystem.CelestialObject); hisPlanet.Name = player.SpringAccount.Name + "'s home"; hisPlanet.Player = player; player.StarSystem.CelestialObject.ChildCelestialObjects.Add(hisPlanet); hisPlanet.UpdateIncomeInfo(); db.SubmitChanges(); var e = db.AddEvent(EventType.Player, "{0} joined the {1}", player.Name, player.StarSystem.Name); e.Connect(player); e.Connect(hisPlanet); db.MarkDirty(); db.SubmitChanges(); t.Complete(); return(RegisterResponse.Ok); } }
public static IEnumerable <Transit> GetTransits(string playerName, string password) { var db = new DbDataContext(); var dl = new DataLoadOptions(); dl.LoadWith <Transit>(x => x.Fleets); dl.LoadWith <Fleet>(x => x.FleetShips); dl.LoadWith <Transit>(x => x.PopulationTransports); dl.LoadWith <Transit>(x => x.Players); db.LoadOptions = dl; return(db.Transits.ToList()); }
public string GetLoginHint() { var context = OperationContext.Current; var messageProperties = context.IncomingMessageProperties; var endpointProperty = messageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty; var db = new DbDataContext(); var acc = db.SpringAccounts.Where(x => x.LastIP == endpointProperty.Address).OrderByDescending(x => x.LastLogin).FirstOrDefault(); if (acc != null) return acc.Name; return null; }
public static LoginResponse Login(string login, string password) { var db = new DbDataContext(); var hp = SpringAccount.HashPassword(password); var user = db.SpringAccounts.SingleOrDefault(x => x.Name == login); if (user == null) return LoginResponse.InvalidLogin; if (user.Password != hp) return LoginResponse.InvalidPassword; if (user.Players == null) return LoginResponse.Unregistered; user.Players.IsActive = true; db.SubmitChanges(); return LoginResponse.Ok; }
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; } }
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); } }
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)); } }
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)); } }
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)); } }
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); } }
public static StarMap GetMapData(string playerName, string password) { var db = new DbDataContext(); var ret = new StarMap(); var lo = new DataLoadOptions(); lo.LoadWith<CelestialObject>(x => x.CelestialObjectShips); // todo stealth also in all fleet structures lo.LoadWith<CelestialObject>(x => x.CelestialObjectStructures); db.LoadOptions = lo; ret.CelestialObjects = db.CelestialObjects.ToList(); ret.Players = db.Players.Where(x => x.IsActive).ToList(); ret.StarSystems = db.StarSystems.ToList(); ret.Transits = FleetLogic.GetTransits(playerName, password); ret.Config = db.GetConfig(); ret.ObjectLinks = db.CelestialObjectLinks.ToList(); return ret; }
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); } }
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); } }
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); }
public static void ProcessTransitTurn(DbDataContext db) { var config = db.GetConfig(); foreach (var transit in db.Transits) { if (transit.StartBattleTurn >= config.CombatTurn && transit.EndBattleTurn >= config.CombatTurn && transit.OrbitsObjectID != null) { // log set sail var owner = transit.GetOwner(); var ev = db.AddEvent(EventType.Fleet, "{0} set sail from {1} to {2}. It will need {3} to get there", transit.GetNameWithOwner(), transit.CelestialObjectByFromObjectID.GetNameWithOwner(), transit.CelestialObjectByToObjectID.GetNameWithOwner(), transit.GetEtaString(config.SecondsPerTurn)); ev.Connect(transit.Fleets); ev.Connect(owner); if (transit.FromObjectID != null) { ev.Connect(transit.CelestialObjectByFromObjectID); ev.Connect(transit.CelestialObjectByFromObjectID.Player); } ev.Connect(transit.CelestialObjectByToObjectID); ev.Connect(transit.CelestialObjectByToObjectID.Player); // process set sail transit.OrbitsObjectID = null; if (transit.Players.Any()) // its mothership and it just left homeworld { var p = transit.Players.First(); if (p.HomeworldID != null) { var eve = db.AddEvent(EventType.Player, "{0} has lost homeworld {1}", p.Name, p.CelestialObject.GetName()); eve.Connect(p); eve.Connect(p.CelestialObject); } p.HomeworldID = null; } } else if (transit.EndBattleTurn <= config.CombatTurn && transit.OrbitsObjectID == null) { // log arrival var owner = transit.GetOwner(); var ev = db.AddEvent(EventType.Fleet, "{0} arrived to {1}", transit.GetNameWithOwner(), transit.CelestialObjectByToObjectID.GetNameWithOwner()); ev.Connect(transit.Fleets); ev.Connect(owner); ev.Connect(transit.CelestialObjectByToObjectID); ev.Connect(transit.CelestialObjectByToObjectID.Player); // process arrival transit.CelestialObject = transit.CelestialObjectByToObjectID; transit.FromObjectID = null; transit.ToObjectID = null; transit.FromX = 0; transit.ToX = 0; transit.FromY = 0; transit.ToY = 0; PopulationTransport transport = transit.PopulationTransports; if (transport != null) { if (transit.CelestialObjectByToObjectID.OwnerID == transport.OwnerID) // todo handle alliances!! { transit.CelestialObjectByToObjectID.Population += transport.Count; if (transit.CelestialObjectByToObjectID.Population > transit.CelestialObjectByToObjectID.MaxPopulation) { transit.CelestialObjectByToObjectID.Population = transit.CelestialObjectByToObjectID.MaxPopulation; } transit.PopulationTransports = null; db.Transits.DeleteOnSubmit(transit); db.PopulationTransports.DeleteOnSubmit(transport); } } if (transit.Players.Any()) { var p = transit.Players.First(); p.CelestialObject = transit.CelestialObjectByToObjectID; // set homeworld to object where we arrived var eve = db.AddEvent(EventType.Player, "{0} has new homeworld {1}", p.Name, p.CelestialObject.GetName()); eve.Connect(p); eve.Connect(p.CelestialObject); } } } }
public bool VerifySpringAccount(string login, string password) { var db = new DbDataContext(); return db.SpringAccounts.Any(x => x.Name == login && x.Password == SpringAccount.HashPassword(password)); }
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; } }
public IEnumerable<Event1> GetPlayerEvents(int playerID) { var db = new DbDataContext(); return db.Players.Single(x => x.PlayerID == playerID).PlayerEvents.Select(x => x.Event1).OrderByDescending(x => x.Time).ToList(); }
public IEnumerable<Event1> GetStarSystemEvents(int starSystemID) { var db = new DbDataContext(); return db.Events.Where(x => x.PlayerEvents.Any(y => y.Player.SystemID == starSystemID)).OrderByDescending(x => x.Time).ToList(); }
public Player GetPlayerData(string playerName) { var db = new DbDataContext(); var p = db.Players.SingleOrDefault(x => x.SpringAccount.Name == playerName); p.MothershipStructures.Load(); return p; }
public static void ProcessTransitTurn(DbDataContext db) { var config = db.GetConfig(); foreach (var transit in db.Transits) { if (transit.StartBattleTurn >= config.CombatTurn && transit.EndBattleTurn >= config.CombatTurn && transit.OrbitsObjectID != null) { // log set sail var owner = transit.GetOwner(); var ev = db.AddEvent(EventType.Fleet, "{0} set sail from {1} to {2}. It will need {3} to get there", transit.GetNameWithOwner(), transit.CelestialObjectByFromObjectID.GetNameWithOwner(), transit.CelestialObjectByToObjectID.GetNameWithOwner(), transit.GetEtaString(config.SecondsPerTurn)); ev.Connect(transit.Fleets); ev.Connect(owner); if (transit.FromObjectID != null) { ev.Connect(transit.CelestialObjectByFromObjectID); ev.Connect(transit.CelestialObjectByFromObjectID.Player); } ev.Connect(transit.CelestialObjectByToObjectID); ev.Connect(transit.CelestialObjectByToObjectID.Player); // process set sail transit.OrbitsObjectID = null; if (transit.Players.Any()) // its mothership and it just left homeworld { var p = transit.Players.First(); if (p.HomeworldID != null) { var eve = db.AddEvent(EventType.Player, "{0} has lost homeworld {1}", p.Name, p.CelestialObject.GetName()); eve.Connect(p); eve.Connect(p.CelestialObject); } p.HomeworldID = null; } } else if (transit.EndBattleTurn <= config.CombatTurn && transit.OrbitsObjectID == null) { // log arrival var owner = transit.GetOwner(); var ev = db.AddEvent(EventType.Fleet, "{0} arrived to {1}", transit.GetNameWithOwner(), transit.CelestialObjectByToObjectID.GetNameWithOwner()); ev.Connect(transit.Fleets); ev.Connect(owner); ev.Connect(transit.CelestialObjectByToObjectID); ev.Connect(transit.CelestialObjectByToObjectID.Player); // process arrival transit.CelestialObject = transit.CelestialObjectByToObjectID; transit.FromObjectID = null; transit.ToObjectID = null; transit.FromX = 0; transit.ToX = 0; transit.FromY = 0; transit.ToY = 0; PopulationTransport transport = transit.PopulationTransports; if (transport != null) { if (transit.CelestialObjectByToObjectID.OwnerID == transport.OwnerID) // todo handle alliances!! { transit.CelestialObjectByToObjectID.Population += transport.Count; if (transit.CelestialObjectByToObjectID.Population > transit.CelestialObjectByToObjectID.MaxPopulation) transit.CelestialObjectByToObjectID.Population = transit.CelestialObjectByToObjectID.MaxPopulation; transit.PopulationTransports = null; db.Transits.DeleteOnSubmit(transit); db.PopulationTransports.DeleteOnSubmit(transport); } } if (transit.Players.Any()) { var p = transit.Players.First(); p.CelestialObject = transit.CelestialObjectByToObjectID; // set homeworld to object where we arrived var eve = db.AddEvent(EventType.Player, "{0} has new homeworld {1}", p.Name, p.CelestialObject.GetName()); eve.Connect(p); eve.Connect(p.CelestialObject); } } } }
public IEnumerable<Event1> GetBodyEvents(int bodyID) { var db = new DbDataContext(); return db.CelestialObjects.Single(x => x.CelestialObjectID == bodyID).CelestialObjectEvents.Select(x => x.Event1).OrderByDescending(x => x.Time).ToList(); }
public IEnumerable<Event1> GetBattleEvents(int battleID) { var db = new DbDataContext(); return db.Battles.Single(x => x.BattleID == battleID).BattleEvents.Select(x => x.Event1).OrderByDescending(x => x.Time).ToList(); }
public static RegisterResponse Register(string login, string password) { using (var t = new TransactionScope()) { var db = new DbDataContext(); var user = db.SpringAccounts.SingleOrDefault(x => x.Name == login); if (user == null) return RegisterResponse.NotValidSpringLogin; if (user.Players != null) return RegisterResponse.AlreadyRegistered; if (db.SpringAccounts.Any(x => x.UserCookie == user.UserCookie && x.Players != null)) return RegisterResponse.IsSmurf; if (user.Password != SpringAccount.HashPassword(password)) return RegisterResponse.NotValidSpringPassword; // create player and assign to smallest alliance var player = new Player() { IsActive = true, StarSystem = db.StarSystems.OrderBy(x => x.Players.Where(y => y.IsActive).Count()).First() }; user.Players = player; player.MothershipName = player.SpringAccount.Name + "'s mothership"; // create his planet var mg = new MapGen(); var hisPlanet = mg.CreateHomeworld(player.StarSystem.CelestialObject); hisPlanet.Name = player.SpringAccount.Name + "'s home"; hisPlanet.Player = player; player.StarSystem.CelestialObject.ChildCelestialObjects.Add(hisPlanet); hisPlanet.UpdateIncomeInfo(); db.SubmitChanges(); var e = db.AddEvent(EventType.Player, "{0} joined the {1}", player.Name, player.StarSystem.Name); e.Connect(player); e.Connect(hisPlanet); db.MarkDirty(); db.SubmitChanges(); t.Complete(); return RegisterResponse.Ok; } }