public static HerosLocations GenInitialLocation(my_appContext _context, int heroID, int locationID = 1) { var locSk = _context.LocationsDb.FirstOrDefault(e => e.LocationIdentifier == locationID); if (locSk == null) { throw new Exception("Starting in unknown location."); } string Description = ""; int LocationType = locSk.LocationGlobalType; if (LocationType != 2) { LocationDescription description = JsonConvert.DeserializeObject <LocationDescription>(locSk.Sketch); Description = JsonConvert.SerializeObject(description.InitializeLocation()); } else { InstanceDescription description = JsonConvert.DeserializeObject <InstanceDescription>(locSk.Sketch); Description = JsonConvert.SerializeObject(description.InitializeLocation()); } return(new HerosLocations() { Description = Description, HeroId = heroID, LocationIdentifier = locationID, LocationId = 1000 * heroID + locationID, }); }
public static AstarResult DistanceToMove(InstanceDescription description, InstanceState state, int LocalTarget) { LocationResult <InstanceNodeResult> res = description.GenLocalForm(state); Graph <Node> graph = new Graph <Node>(res.Nodes, res.Edges, Node.HeuristicDistance); return(graph.Astar(res.CurrentLocation, LocalTarget)); }
public static LocationResult <InstanceNodeResult> InstanceClearCurrent(my_appContext _context, Heros hero) { var location = _context.HerosLocations.FirstOrDefault(e => (e.HeroId == hero.HeroId) && (e.LocationIdentifier == hero.CurrentLocation)); if (location == null) { throw new Exception("Location is not available."); } var descr = _context.LocationsDb.FirstOrDefault(e => e.LocationIdentifier == location.LocationIdentifier); if (descr == null) { throw new Exception("LocationData is not available."); } int LocationType = descr.LocationGlobalType; if (LocationType != 2) { throw new OperationException("locationErr", "Fight outside the location"); } else { InstanceDescription description = JsonConvert.DeserializeObject <InstanceDescription>(descr.Sketch); InstanceState state = JsonConvert.DeserializeObject <InstanceState>(location.Description); description.LocationGlobalType = descr.LocationGlobalType; if (hero.Hp > 0) { state.IsCleared[state.CurrentLocation] = true; location.Description = JsonConvert.SerializeObject(state); } return(description.GenLocalForm(state)); } }
public static GeneralStatus GetHeroGeneralStatus(my_appContext _context, Heros hero, DateTime now) { object statusData = null; var location = _context.HerosLocations.FirstOrDefault(e => (e.HeroId == hero.HeroId) && (e.LocationIdentifier == hero.CurrentLocation)); if (location == null) { throw new Exception("Location is not available."); } var descr = _context.LocationsDb.FirstOrDefault(e => e.LocationIdentifier == location.LocationIdentifier); if (descr == null) { throw new Exception("LocationData is not available."); } // TODO Location Type object locationResult = new LocationResult <object>(); int LocationType = descr.LocationGlobalType; if (LocationType != 2) { LocationDescription description = JsonConvert.DeserializeObject <LocationDescription>(descr.Sketch); LocationState state = JsonConvert.DeserializeObject <LocationState>(location.Description); description.LocationGlobalType = descr.LocationGlobalType; if (hero.Status == 1) { Traveling travel = _context.Traveling.FirstOrDefault(e => e.HeroId == hero.HeroId); if (travel == null) { throw new Exception("Traveling hero without travel in DB."); } if (travel.HasEnded(now)) { state = description.MoveTo(travel.UpdatedLocationID(), state); hero.Status = 0; location.Description = JsonConvert.SerializeObject(state); _context.Traveling.Remove(travel); try { _context.SaveChanges(); } catch (DbUpdateException) { throw new Exception("Failed to remove travel."); } } else { statusData = travel.GenTravelResult(now); } } locationResult = description.GenLocalForm(state); } else { InstanceDescription description = JsonConvert.DeserializeObject <InstanceDescription>(descr.Sketch); InstanceState state = JsonConvert.DeserializeObject <InstanceState>(location.Description); description.LocationGlobalType = descr.LocationGlobalType; if (hero.Status == 1) { Traveling travel = _context.Traveling.FirstOrDefault(e => e.HeroId == hero.HeroId); if (travel == null) { throw new Exception("Traveling hero without travel in DB."); } if (travel.HasEnded(now)) { state = description.MoveTo(travel.UpdatedLocationID(), state); hero.Status = 0; location.Description = JsonConvert.SerializeObject(state); _context.Traveling.Remove(travel); try { _context.SaveChanges(); } catch (DbUpdateException) { throw new Exception("Failed to remove travel."); } } else { statusData = travel.GenTravelResult(now); } } locationResult = description.GenLocalForm(state); } if (hero.Status == 2) { Healing heal = _context.Healing.FirstOrDefault(e => e.HeroId == hero.HeroId); if (heal == null) { throw new Exception("Healing hero without heal in DB."); } if (heal.HasEnded(now)) { int newHP = heal.FinalHealth(now); hero.Hp = newHP; HeroCalculator.CheckHeroHP(hero, _context); _context.Healing.Remove(heal); hero.Status = 0; try { _context.SaveChanges(); } catch (DbUpdateException) { throw new Exception("Failed to remove healing."); } } else { statusData = heal.GenHealingResult(now); } } if (hero.Status == 3) { Fighting fight = _context.Fighting.FirstOrDefault(e => e.HeroId == hero.HeroId); if (fight == null) { throw new Exception("Healing hero without heal in DB."); } statusData = fight.GenResult(_context, hero); } return(new GeneralStatus() { HeroStatus = hero.Status, Location = locationResult, StatusData = statusData, }); }