Skip to content

Commit

Permalink
Bugfixes + Improved Adventures Gen
Browse files Browse the repository at this point in the history
  • Loading branch information
andiricum2 committed Apr 30, 2024
1 parent 543cb70 commit 868f33c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 40 deletions.
20 changes: 18 additions & 2 deletions ProjectEarthServerAPI/Util/AdventureUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,22 @@ public static AdventureRequestResult RedeemCrystal(string playerId, PlayerAdvent
return Encounters;
}

public static LocationResponse.ActiveLocation CreateEncounterLocation(double randomLatitude, double randomLongitude, DateTime expirationTime)
public static LocationResponse.ActiveLocation GenerateEncounterLocation(double randomLatitude, double randomLongitude, DateTime expirationTime)
{

AdventureUtils adventureUtils = new AdventureUtils();

return adventureUtils.CreateEncounterLocation(randomLatitude, randomLongitude, expirationTime);
}

public LocationResponse.ActiveLocation CreateEncounterLocation(double randomLatitude, double randomLongitude, DateTime expirationTime)
{
Encounters.RemoveAll(match => match.expirationTime < DateTime.UtcNow);

string selectedAdventureIcon = AdventureIcons[random.Next(0, AdventureIcons.Length)];
Guid selectedAdventureId = Guid.NewGuid(); // Generate a new unique ID for the encounter

var newEncounterLocation = new LocationResponse.ActiveLocation
LocationResponse.ActiveLocation newEncounterLocation = new LocationResponse.ActiveLocation
{
coordinate = new Coordinate { latitude = randomLatitude, longitude = randomLongitude },
encounterMetadata = new EncounterMetadata
Expand All @@ -140,9 +148,17 @@ public static LocationResponse.ActiveLocation CreateEncounterLocation(double ran

Encounters.Add(newEncounterLocation);

StoreEncounter(newEncounterLocation);

return newEncounterLocation;
}

private void StoreEncounter(LocationResponse.ActiveLocation newEncounterLocation)
{
var storage = new LocationResponse.ActiveLocationStorage { location = newEncounterLocation };
StateSingleton.Instance.activeTappables.Add(newEncounterLocation.id, storage);
// Log.Debug($"Active tappables count: {StateSingleton.Instance.activeTappables.Count}");
}
}
}

2 changes: 1 addition & 1 deletion ProjectEarthServerAPI/Util/ConfigGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void GenerateConfigFile(string configFilePath)
{ "tappableExpirationTime", 10 }, // Minutes
{ "biomeGeneration", true }, // Custom Generation. Needs Tile Server!!
{ "publicAdventureSpawnPercentage", 5 }, // Percentage of Adventure Spawn rate
{ "publicAdventuresLimit", 2 }, // Public Adventures Limit
{ "publicAdventuresLimit", 5 }, // Public Adventures Limit
{ "webPanel", true },
{ "webPanelPassword", "password" },
{ "maxTappablesPerTile", 20 },
Expand Down
78 changes: 41 additions & 37 deletions ProjectEarthServerAPI/Util/TappableUpdates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static void RemoveExpiredTappables()
StateSingleton.Instance.activeTappables.Remove(tappableId);
}

Log.Debug($"Removed {tappablesToRemove.Count} expired tappables.");
// Log.Debug($"Removed {tappablesToRemove.Count} expired tappables.");
}

public static LocationResponse.Root GetActiveLocations(double lat, double lon, int radius = 1)
Expand All @@ -37,7 +37,11 @@ public static LocationResponse.Root GetActiveLocations(double lat, double lon, i
.Select(kvp => kvp.Value.location)
.ToList();

var globalEncounters = AdventureUtils.GetEncountersForLocation(lat, lon);
var globalEncounters = StateSingleton.Instance.activeTappables
.Where(kvp => kvp.Value.location.type == "Encounter")
.Select(kvp => kvp.Value.location)
.ToList();

globalTappables.AddRange(globalEncounters);

return new LocationResponse.Root
Expand All @@ -54,6 +58,7 @@ public static LocationResponse.Root GetActiveLocations(double lat, double lon, i
}
else
{
AdventureUtils.GetEncountersForLocation(lat, lon);
radius = StateSingleton.Instance.config.tappableSpawnRadius;
string tileId = Tile.GetTileForCoordinates(lat, lon);
string[] parts = tileId.Split('_');
Expand All @@ -69,17 +74,18 @@ public static LocationResponse.Root GetActiveLocations(double lat, double lon, i
var maxCoordinates = new Coordinate { latitude = maxTileCoordinates[1][0], longitude = maxTileCoordinates[1][1] };

var tappables = StateSingleton.Instance.activeTappables
.Where(pred =>
pred.Value.location.coordinate.latitude >= minCoordinates.latitude &&
pred.Value.location.coordinate.latitude <= maxCoordinates.latitude &&
pred.Value.location.coordinate.longitude >= minCoordinates.longitude &&
pred.Value.location.coordinate.longitude <= maxCoordinates.longitude)
.Select(pred => pred.Value.location)
.ToList();
.Where(pred => pred.Value.location.type == "Tappable")
.Select(pred => pred.Value.location)
.ToList();

var encounters = StateSingleton.Instance.activeTappables
.Where(pred => pred.Value.location.type == "Encounter")
.Select(pred => pred.Value.location)
.ToList();


// TAPPABLE GENERATION
if (StateSingleton.Instance.config.maxTappableSpawnAmount > tappables.Count)
// TAPPABLE GENERATION
if (StateSingleton.Instance.config.maxTappableSpawnAmount > tappables.Count)
{
// For each tile
for (int latLoop = tileIdLat - radius; latLoop <= tileIdLat + radius; latLoop++)
Expand Down Expand Up @@ -129,32 +135,30 @@ public static LocationResponse.Root GetActiveLocations(double lat, double lon, i
}
}

// Encounters/Adventures
var encountersInCurrentTile = AdventureUtils.GetEncountersForLocation(lat, lon)
.Where(kvp => kvp.tileId == currentTileId)
.Select(pred => pred)
.ToList();

tappables.AddRange(encountersInCurrentTile);

double randomLatitude = minCoordinates.latitude + (random.NextDouble() * (maxCoordinates.latitude - minCoordinates.latitude));
double randomLongitude = minCoordinates.longitude + (random.NextDouble() * (maxCoordinates.longitude - minCoordinates.longitude));

var encounterCount = AdventureUtils.GetEncountersForLocation(lat, lon).Count - 1;
var existingLocationsCount = AdventureUtils.ReadEncounterLocations().Count;

if (random.Next(1, 101) <= StateSingleton.Instance.config.publicAdventureSpawnPercentage)
{
if (StateSingleton.Instance.config.publicAdventuresLimit > encounterCount + existingLocationsCount)
{
DateTime expirationTime = DateTime.UtcNow.AddMinutes(30);
var newAdventures = AdventureUtils.CreateEncounterLocation(randomLatitude, randomLongitude, expirationTime);
if (newAdventures != null)
{
tappables.Add(newAdventures);
}
}
}
// Encounters/Adventures
if (StateSingleton.Instance.config.publicAdventuresLimit > encounters.Count)
{
var encountersInCurrentTile = StateSingleton.Instance.activeTappables
.Where(kvp => kvp.Value.location.tileId == currentTileId && kvp.Value.location.type == "Encounter")
.Select(kvp => kvp.Value.location)
.ToList();

tappables.AddRange(encountersInCurrentTile);

if (random.Next(1, 101) <= StateSingleton.Instance.config.publicAdventureSpawnPercentage)
{
double randomLatitude = minCoordinates.latitude + (random.NextDouble() * (maxCoordinates.latitude - minCoordinates.latitude));
double randomLongitude = minCoordinates.longitude + (random.NextDouble() * (maxCoordinates.longitude - minCoordinates.longitude));

DateTime expirationTime = DateTime.UtcNow.AddMinutes(30);

var newAdventures = AdventureUtils.GenerateEncounterLocation(randomLatitude, randomLongitude, expirationTime);
if (newAdventures != null)
{
tappables.Add(newAdventures);
}
}
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions ProjectEarthServerAPI/Views/Panel/index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
<li class="list-group-item d-flex justify-content-between align-items-center">
Live Tappable Count: <span class="badge bg-success">@ViewBag.TappableCount</span>
</li>
<li class="list-group-item d-flex justify-content-between align-items-center">
Live Adventures Count: <span class="badge bg-success">@ViewBag.AdventuresCount</span>
</li>
<li class="list-group-item d-flex justify-content-between align-items-center">
Biome Generation: <span class="badge bg-info">@ViewBag.BiomeGeneration</span>
</li>
Expand Down
6 changes: 6 additions & 0 deletions ProjectEarthServerAPI/WebControllers/panel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Http;
using ProjectEarthServerAPI.Util;
using Serilog;
using System.Linq;

namespace ProjectEarthServerAPI.Views
{
Expand Down Expand Up @@ -48,6 +49,11 @@ public IActionResult Index()
ViewBag.SpawnRadius = StateSingleton.Instance.config.tappableSpawnRadius;
ViewBag.TappableExpirationTime = StateSingleton.Instance.config.tappableExpirationTime;
ViewBag.AdventuresCountJson = AdventureUtils.ReadEncounterLocations().Count;
var adventures = StateSingleton.Instance.activeTappables
.Where(pred => pred.Value.location.type == "Encounter")
.Select(pred => pred.Value.location)
.ToList();
ViewBag.AdventuresCount = adventures.Count;
ViewBag.PublicAdventuresLimit = StateSingleton.Instance.config.publicAdventuresLimit;
ViewBag.AdventureSpawnPercentage = StateSingleton.Instance.config.publicAdventureSpawnPercentage;
ViewBag.MaxTappablesPerTile = StateSingleton.Instance.config.maxTappablesPerTile;
Expand Down

0 comments on commit 868f33c

Please sign in to comment.