protected override void PreLoad(Callback <uint, Neighborhood> appender) { using (var db = DAFactory.Get()) { var nhoods = db.Neighborhoods.All(ShardId); var midnight = LotVisitUtils.Midnight(); //gets this morning's midnight (basically current date, with day reset) var activityBeginning = midnight - new TimeSpan(7, 0, 0, 0); var visits = db.LotVisits.StreamBetweenPlusNhood(ShardId, activityBeginning, midnight).ToList(); var enumerator = visits.GetEnumerator(); var nhoodHours = new Dictionary <uint, double>(); while (enumerator.MoveNext()) { var visit = enumerator.Current; var span = LotVisitUtils.CalculateDateOverlap(activityBeginning, midnight, visit.time_created, visit.time_closed.Value); if (nhoodHours.ContainsKey(visit.neighborhood_id)) { nhoodHours[visit.neighborhood_id] += span.TotalMinutes; } else { nhoodHours.Add(visit.neighborhood_id, span.TotalMinutes); } } var order = nhoodHours.OrderByDescending(x => x.Value).ToList(); WorstRating = nhoods.Count; foreach (var item in nhoods) { var lots = db.Lots.GetLocationsInNhood((uint)item.neighborhood_id); var avatars = db.Avatars.GetLivingInNhood((uint)item.neighborhood_id); var townHall = db.Lots.Get(item.town_hall_id ?? 0)?.location ?? 0; var cycle = (item.election_cycle_id == null) ? null : db.Elections.GetCycle(item.election_cycle_id.Value); var converted = HydrateOne(item, avatars, lots, townHall, cycle, visits, order); var intId = (uint)item.neighborhood_id; appender(intId, converted); } // var neighObj = nhoods.Select(x => { var loc = MapCoordinates.Unpack(x.location); var result = new CityNeighbourhood() { Name = x.name, Color = new Color(x.color), Description = x.description, GUID = x.guid, Location = new Point(loc.X, loc.Y), ID = x.neighborhood_id }; return(result); }).ToList(); Lots.CityRepresentation.City_NeighJSON = JsonConvert.SerializeObject(neighObj); } }
private DbNeighborhood ImportToReal(CityNeighbourhood nhood) { return(new DbNeighborhood() { name = nhood.Name, description = nhood.Description ?? "", shard_id = Options.ShardId, location = (uint)(MapCoordinates.Pack((ushort)nhood.Location.X, (ushort)nhood.Location.Y)), color = (nhood.Color ?? Color.White).PackedValue, guid = nhood.GUID }); }
public void RandomData() { var random = new Random(); for (int i = 0; i < 100; i++) { Data.Add(new CityNeighbourhood() { Name = "Rand" + i, Location = new Point(random.Next(512), random.Next(512)) } ); } CityNeighbourhood.Init(Data); }
public override void Update(UpdateState state) { mp = state.MouseState.Position.ToVector2(); var md = state.MouseState.LeftButton == ButtonState.Pressed; CtrlDown = state.CtrlDown; ShiftDown = state.ShiftDown; if (md != MouseWasDown) { if (md) { if (ShiftDown) { var neigh = new CityNeighbourhood() { Location = new Point(256, 256), Name = "Neigh" + EditTarget.Count }; EditTarget.Add(neigh); Selected = neigh; } else { var closest = EditTarget.OrderBy(x => (City.Get2DFromTile(x.Location.X, x.Location.Y) - state.MouseState.Position.ToVector2()).Length()).FirstOrDefault(); if (closest != null && (City.Get2DFromTile(closest.Location.X, closest.Location.Y) - state.MouseState.Position.ToVector2()).Length() < 16) { if (CtrlDown) { EditTarget.Remove(closest); City.NeighGeom.Generate(GameFacade.GraphicsDevice); } else { Selected = closest; } } } } else { Selected = null; } MouseWasDown = md; } if (state.NewKeys.Contains(Keys.R)) { var proj = City.EstTileAtPosWithScroll(state.MouseState.Position.ToVector2(), null); var near = City.NeighGeom.NhoodNearest(proj); if (near != -1) { var nhood = City.NeighGeom.Data[near]; UIAlert.Prompt(new UIAlertOptions() { Message = "Rename this neighbourhood to what?", TextEntry = true }, (result, alert) => { if (result) { nhood.Name = alert.ResponseText; } }); } } if (state.NewKeys.Contains(Keys.C)) { var proj = City.EstTileAtPosWithScroll(state.MouseState.Position.ToVector2(), null); var near = City.NeighGeom.NhoodNearest(proj); if (near != -1) { var nhood = City.NeighGeom.Data[near]; UIAlert.Prompt(new UIAlertOptions() { Message = "Change this neighbourhood colour to what?", GenericAddition = new UIColorPicker() }, (result, alert) => { if (result) { var col = int.Parse(alert.ResponseText); nhood.Color = new Color(col >> 16, (col >> 8) & 0xFF, col & 0xFF); } }); } } if (state.NewKeys.Contains(Keys.F10)) { if (state.ShiftDown) { using (var file = System.IO.File.Open("Content/edit_neigh.json", System.IO.FileMode.Create, System.IO.FileAccess.Write)) using (var writer = new System.IO.StreamWriter(file)) writer.Write(Newtonsoft.Json.JsonConvert.SerializeObject(EditTarget, Newtonsoft.Json.Formatting.Indented)); } else { using (var file = System.IO.File.Open("Content/edit_neigh.json", System.IO.FileMode.Open, System.IO.FileAccess.Read)) using (var reader = new System.IO.StreamReader(file)) { EditTarget = JsonConvert.DeserializeObject <List <CityNeighbourhood> >(reader.ReadToEnd()); CityNeighbourhood.Init(EditTarget); City.NeighGeom.Data = EditTarget; City.NeighGeom.Generate(GameFacade.GraphicsDevice); } } } }