/// <summary> /// Checks whether this ship has arrived at its destination and updates status variables, if it has. /// Basically this is the "time has passed, check if you are there yet" event. /// </summary> public void UpdateTravelStatus() { //If the launch date is null, the ship is currently docked - nothing to do if (LaunchDate == null) { return; } //Otherwise the ship is on its way else { //Calculate the distance already covered var currentStar = db.Stars.Single(i => i.Id == CurrentStarId); var targetStar = db.Stars.Single(i => i.Id == TargetStarId); double distanceBetweenStars = DistanceCalculator.GetDistance(currentStar.X.Value, currentStar.Y.Value, targetStar.X.Value, targetStar.Y.Value); double distanceTraveled = SpeedInLightYearsPerMinute * (TimeGetter.GetLocalTime() - LaunchDate.Value).TotalMinutes; //If the ship is still en route, nothing to do if (distanceTraveled < distanceBetweenStars) { return; } //The ship has arrived - update status else { CurrentStarId = TargetStarId; LaunchDate = null; TargetStarId = null; db.SubmitChanges(); } } }
protected void CreatePilotButton_Click(object sender, EventArgs e) { if (!User.Identity.IsAuthenticated) { Response.Redirect("/AccountLogin.aspx?ReturnUrl=" + Request.Url.PathAndQuery); } AndromedaDataContext db = new AndromedaDataContext(); if (db.Players.Any(i => i.PlayerName == User.Identity.Name)) { Response.Redirect("Map.aspx"); //Avoid duplicates } Random rnd = new Random((int)DateTime.Now.Ticks); //Create the player var player = new Player() { FirstShipGuid = Guid.NewGuid(), PlayerMoney = 5000, PlayerName = User.Identity.Name }; db.Players.InsertOnSubmit(player); //Create the user's spaceship. //The page will now reload and the UI will automatically update to display the Guid and //make the template available for download. db.Spaceships.InsertOnSubmit( new Spaceship() { CannonCount = 0, Created = TimeGetter.GetLocalTime(), CurrentStarId = rnd.Next(0, db.Stars.Count()), Deleted = false, DriveCount = 0, LastRaided = TimeGetter.GetLocalTime(), ModificationCount = 0, Player = player, PlayerGuid = player.FirstShipGuid, SensorCount = 0, ShieldCount = 0, ShipModel = 0, TransponderCode = Guid.NewGuid() }); db.SubmitChanges(); Response.Redirect("RegisterComplete.aspx"); }
protected void TeleportPilotButton_Click(object sender, EventArgs e) { AndromedaDataContext db = new AndromedaDataContext(); if (!User.Identity.IsAuthenticated) { return; //Avoid unauthenticated users } if (!db.Players.Any(i => i.PlayerName == User.Identity.Name)) { return; //There must be a player } #region Log update bool alreadyLogged = false; PilotWarp logEntry = new PilotWarp() { Player = User.Identity.Name, Timestamp = DateTime.Now }; #endregion Random rnd = new Random((int)DateTime.Now.Ticks); try { var player = db.Players.Single(i => i.PlayerName == User.Identity.Name); #region Log update logEntry.Guid = player.FirstShipGuid.Value; #endregion foreach (var spaceship in player.Spaceships) { if (spaceship.DebugTimestamp != null && spaceship.DebugTimestamp.Value.Add(TimeSpan.FromHours(48)) > TimeGetter.GetLocalTime()) { throw new Exception("Az űrhajó még nem teleportálható!"); } else { var stars = db.Stars.Where(i => i.Id != spaceship.CurrentStarId).ToList(); var newStar = stars[rnd.Next(0, stars.Count)]; spaceship.CurrentStarId = newStar.Id; spaceship.LaunchDate = null; spaceship.TargetStarId = null; spaceship.DebugTimestamp = TimeGetter.GetLocalTime(); db.SubmitChanges(); } } Response.Redirect("Map.aspx"); //Make the browser throw away the submitted POST so that a F5 does not repeat the Reset Pilot command } catch (Exception ex) { #region Log update logEntry.Error = ex.ToString(); Logger.Log(logEntry); alreadyLogged = true; //throw ex; WARNING THIS IS NOT USUALLY RESET #endregion } finally { #region Log update if (!alreadyLogged) { logEntry.Error = ""; Logger.Log(logEntry); } #endregion } }
protected void ResetPilotButton_Click(object sender, EventArgs e) { AndromedaDataContext db = new AndromedaDataContext(); if (!User.Identity.IsAuthenticated) { return; //Avoid unauthenticated users } if (!db.Players.Any(i => i.PlayerName == User.Identity.Name)) { return; //There must be an old ship } #region Log update bool alreadyLogged = false; PilotReset logEntry = new PilotReset() { Player = User.Identity.Name, Timestamp = DateTime.Now }; #endregion Random rnd = new Random((int)DateTime.Now.Ticks); try { //Get player var player = db.Players.Single(i => i.PlayerName == User.Identity.Name); #region Log update logEntry.Guid = player.FirstShipGuid.Value; #endregion //Set player player.PlayerMoney = 5000; //Take old ships out of commission by taking it away from the player and assigning it a new GUID //Since all other resources reference the ship by ID (not GUID), all other resources will be decommissioned as well foreach (var oldSpaceship in player.Spaceships) { oldSpaceship.PlayerGuid = Guid.NewGuid(); //Guid changed to make the ship un-controllable oldSpaceship.Deleted = true; //This will hide the ship from the map } player.Spaceships.Clear(); //Create the user's new spaceship with the old GUID. //The page will now reload and the UI will automatically update to display the Guid and //make the template available for download. db.Spaceships.InsertOnSubmit( new Spaceship() { PlayerGuid = player.FirstShipGuid, DriveCount = 0, SensorCount = 0, CurrentStarId = rnd.Next(0, db.Stars.Count()), Deleted = false, ModificationCount = 0, ShipModel = 0, CannonCount = 0, ShieldCount = 0, LastRaided = TimeGetter.GetLocalTime(), Created = TimeGetter.GetLocalTime(), TransponderCode = Guid.NewGuid(), Player = player, }); db.SubmitChanges(); Response.Redirect("Map.aspx"); //Make the browser throw away the submitted POST so that a F5 does not repeat the Reset Pilot command } catch (Exception ex) { #region Log update logEntry.Error = ex.ToString(); Logger.Log(logEntry); alreadyLogged = true; throw ex; #endregion } finally { #region Log update if (!alreadyLogged) { logEntry.Error = ""; Logger.Log(logEntry); } #endregion } }
/// <summary> /// If enough time has elapsed since the last increase, increases the stock count for all commodities. /// </summary> public static void IncreaseCommodityQuantities() { #region Log update bool shouldBeLogged = false; bool alreadyLogged = false; StockIncrease logEntry = new StockIncrease() { Guid = Guid.Empty, Player = string.Empty, Timestamp = TimeGetter.GetLocalTime() }; #endregion int exceptionCount = 0; try { if (IncreaseCommodityQuantities_LastRunTime + IncreaseCommodityQuantities_MinimumInterval <= TimeGetter.GetLocalTime()) { #region Log update shouldBeLogged = true; #endregion IncreaseCommodityQuantities_LastRunTime = TimeGetter.GetLocalTime(); var commodityAtStarsIds = db.CommodityAtStars.ToList().Select(i => new { i.Id }); //Increase all commodities //All done in separate data contexts to help successfully continue after an optimistic concurrency exception foreach (var commodityAtStarId in commodityAtStarsIds) { try { using (var db2 = DataContextFactory.GetAndromedaDataContext()) { var commodityAtStar = db2.CommodityAtStars.Single(i => i.Id == commodityAtStarId.Id); if (commodityAtStar.Stock.Value + commodityAtStar.ProductionRate.Value > commodityAtStar.MaxCapacity) { commodityAtStar.Stock = commodityAtStar.MaxCapacity; } else { commodityAtStar.Stock += commodityAtStar.ProductionRate.Value; } db2.SubmitChanges(); } } catch { exceptionCount++; } } } } catch (Exception ex) { #region Log update if (shouldBeLogged) { logEntry.Error = ex.ToString(); Logger.Log(logEntry); alreadyLogged = true; } throw ex; #endregion } finally { #region Log update if (shouldBeLogged && !alreadyLogged) { logEntry.Error = exceptionCount.ToString() + " exceptions"; Logger.Log(logEntry); } #endregion } }