Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix zone removals crasing server #279

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lang/Jailbreak.English/SpecialDay/GunDayLocale.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;
using Jailbreak.Formatting.Base;
using Jailbreak.Formatting.Views;
using Jailbreak.Formatting.Views.SpecialDay;

namespace Jailbreak.English.SpecialDay;
Expand Down
1 change: 0 additions & 1 deletion lang/Jailbreak.English/SpecialDay/SDLocale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Jailbreak.Formatting.Core;
using Jailbreak.Formatting.Logistics;
using Jailbreak.Formatting.Objects;
using Jailbreak.Formatting.Views;
using Jailbreak.Formatting.Views.SpecialDay;

namespace Jailbreak.English.SpecialDay;
Expand Down
1 change: 0 additions & 1 deletion lang/Jailbreak.English/SpecialDay/SoloDayLocale.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Jailbreak.Formatting.Base;
using Jailbreak.Formatting.Views;
using Jailbreak.Formatting.Views.SpecialDay;
using Jailbreak.Public.Utils;

Expand Down
1 change: 0 additions & 1 deletion lang/Jailbreak.English/SpecialDay/SpeedrunDayLocale.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;
using Jailbreak.Formatting.Base;
using Jailbreak.Formatting.Views;
using Jailbreak.Formatting.Views.SpecialDay;

namespace Jailbreak.English.SpecialDay;
Expand Down
1 change: 0 additions & 1 deletion lang/Jailbreak.English/SpecialDay/TeamDayLocale.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CounterStrikeSharp.API.Modules.Utils;
using Jailbreak.Formatting.Base;
using Jailbreak.Formatting.Views;
using Jailbreak.Formatting.Views.SpecialDay;
using Jailbreak.Public.Utils;

Expand Down
30 changes: 18 additions & 12 deletions mod/Jailbreak.Debug/Subcommands/DebugZone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public override void OnCommand(CCSPlayerController? executor,
specifiedType = success;
}

string map = Server.MapName;
switch (info.GetArg(1).ToLower()) {
case "finish":
case "done":
Expand Down Expand Up @@ -76,8 +77,9 @@ public override void OnCommand(CCSPlayerController? executor,
var zoneCount = 0;
foreach (var type in Enum.GetValues<ZoneType>()) {
if (specifiedType != null && type != specifiedType) continue;
var displayZones =
zoneManager.GetZones(type).GetAwaiter().GetResult();
var displayZones = zoneManager.GetZones(map, type)
.GetAwaiter()
.GetResult();
foreach (var z in displayZones) z.Draw(plugin, type.GetColor(), 120);

zoneCount += displayZones.Count;
Expand All @@ -101,7 +103,7 @@ public override void OnCommand(CCSPlayerController? executor,
var toDelete = getUniqueZone(executor, specifiedType);
if (toDelete == null) return;
Server.NextFrameAsync(async () => {
await zoneManager.DeleteZone(toDelete.Value.Item1.Id);
await zoneManager.DeleteZone(toDelete.Value.Item1.Id, map);
Server.NextFrame(() => {
executor.PrintToChat("Deleted zone #" + toDelete.Value.Item1.Id);
});
Expand All @@ -113,9 +115,8 @@ public override void OnCommand(CCSPlayerController? executor,
if (innerPair == null) return;
var innerZone = innerPair.Value.Item1;
innerZone.AddPoint(position);
var map = Server.MapName;
Server.NextFrameAsync(async () => {
await zoneManager.DeleteZone(innerZone.Id);
await zoneManager.DeleteZone(innerZone.Id, map);
await zoneManager.PushZoneWithID(innerZone, innerPair.Value.Item2,
map);
Server.NextFrame(() => {
Expand Down Expand Up @@ -145,7 +146,9 @@ await zoneManager.PushZoneWithID(innerZone, innerPair.Value.Item2,

if (specifiedType == null) {
foreach (var type in Enum.GetValues<ZoneType>()) {
var zones = zoneManager.GetZones(type).GetAwaiter().GetResult();
var zones = zoneManager.GetZones(Server.MapName, type)
.GetAwaiter()
.GetResult();
if (!allZones.ContainsKey(type)) continue;

info.ReplyToCommand($"{type} zones: {zones.Count}");
Expand All @@ -154,7 +157,7 @@ await zoneManager.PushZoneWithID(innerZone, innerPair.Value.Item2,
return;
}

var toList = zoneManager.GetZones(specifiedType.Value)
var toList = zoneManager.GetZones(map, specifiedType.Value)
.GetAwaiter()
.GetResult();
foreach (var listZone in toList)
Expand All @@ -164,7 +167,7 @@ await zoneManager.PushZoneWithID(innerZone, innerPair.Value.Item2,
case "cleanup":
// Cleanup auto-generated zones
// Remove spawns that are inside of any DO NOT TELEPORT zones
var spawns = zoneManager.GetZones(ZoneType.SPAWN_AUTO)
var spawns = zoneManager.GetZones(map, ZoneType.SPAWN_AUTO)
.GetAwaiter()
.GetResult();
if (spawns.Count == 0) {
Expand All @@ -173,7 +176,8 @@ await zoneManager.PushZoneWithID(innerZone, innerPair.Value.Item2,
}

var doNotTeleport = zoneManager
.GetZones(ZoneTypeExtensions.DoNotTeleports().ToArray())
.GetZones(Server.MapName,
ZoneTypeExtensions.DoNotTeleports().ToArray())
.GetAwaiter()
.GetResult();

Expand All @@ -186,7 +190,8 @@ await zoneManager.PushZoneWithID(innerZone, innerPair.Value.Item2,
info.ReplyToCommand("Removing " + toRemove.Count
+ " auto-generated zones");
Server.NextFrameAsync(async () => {
foreach (var z in toRemove) await zoneManager.DeleteZone(z.Id);
foreach (var z in toRemove)
await zoneManager.DeleteZone(z.Id, map);
});
return;
}
Expand All @@ -203,14 +208,15 @@ await zoneManager.PushZoneWithID(innerZone, innerPair.Value.Item2,
attemptBeginCreation(executor, specifiedType.Value);
return;
case "set":
var zones = zoneManager.GetZones(specifiedType.Value)
var zones = zoneManager.GetZones(Server.MapName, specifiedType.Value)
.GetAwaiter()
.GetResult();

Server.NextFrameAsync(async () => {
var copy = zones.ToList();

foreach (var zone in copy) await zoneManager.DeleteZone(zone.Id);
foreach (var zone in copy)
await zoneManager.DeleteZone(zone.Id, Server.MapName);

Server.NextFrame(()
=> attemptBeginCreation(executor, specifiedType.Value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public virtual IView ArmoryReminder

override protected IZone GetZone() {
var manager = provider.GetRequiredService<IZoneManager>();
var zones = manager.GetZones(ZoneType.ARMORY).GetAwaiter().GetResult();
var zones = manager.GetZones(Server.MapName, ZoneType.ARMORY).GetAwaiter().GetResult();
if (zones.Count > 0) return new MultiZoneWrapper(zones);

var bounds = new DistanceZone(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public virtual IView CellReminder

override protected IZone GetZone() {
var manager = provider.GetRequiredService<IZoneManager>();
var zones = manager.GetZones(ZoneType.CELL).GetAwaiter().GetResult();
var zones = manager.GetZones(Server.MapName, ZoneType.CELL).GetAwaiter().GetResult();
if (zones.Count > 0) return new MultiZoneWrapper(zones);

var bounds = new DistanceZone(
Expand Down
18 changes: 13 additions & 5 deletions mod/Jailbreak.Zones/RandomZoneGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,23 @@ private float zoneScore(IList<IZone>? zones) {
}

private IList<IZone> getManualSpawnPoints() {
var zones = zoneManager.GetZones(ZoneType.SPAWN).GetAwaiter().GetResult();
var zones = zoneManager.GetZones(Server.MapName, ZoneType.SPAWN)
.GetAwaiter()
.GetResult();
return zones;
}

private IList<IZone> getAutoSpawnPoints() {
var zones = zoneManager.GetZones(ZoneType.SPAWN_AUTO)
var zones = zoneManager.GetZones(Server.MapName, ZoneType.SPAWN_AUTO)
.GetAwaiter()
.GetResult();
return zones;
}

private IZone getCells() {
var result = zoneManager.GetZones(ZoneType.CELL).GetAwaiter().GetResult();
var result = zoneManager.GetZones(Server.MapName, ZoneType.CELL)
.GetAwaiter()
.GetResult();
if (result.Count > 0) return new MultiZoneWrapper(result);

var bounds = new DistanceZone(
Expand All @@ -154,11 +158,15 @@ private IZone getCells() {
private IList<IZone> getRestrictedAreas() {
List<IZone> result = [];
foreach (var zone in ZoneTypeExtensions.DoNotTeleports()) {
var zones = zoneManager.GetZones(zone).GetAwaiter().GetResult();
var zones = zoneManager.GetZones(Server.MapName, zone)
.GetAwaiter()
.GetResult();
result.AddRange(zones);
}

var armory = zoneManager.GetZones(ZoneType.ARMORY).GetAwaiter().GetResult();
var armory = zoneManager.GetZones(Server.MapName, ZoneType.ARMORY)
.GetAwaiter()
.GetResult();
if (armory.Count == 0) {
var bounds = new DistanceZone(
Utilities
Expand Down
6 changes: 3 additions & 3 deletions mod/Jailbreak.Zones/SqlZoneManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ public async Task PushZone(IZone zone, ZoneType type, string map) {
await PushZoneWithID(zone, type, map);
}

public async Task UpdateZone(IZone zone, ZoneType type, int id) {
await DeleteZone(id, Server.MapName);
public async Task UpdateZone(IZone zone, ZoneType type, int id, string map) {
await DeleteZone(id, map);
zone.Id = id;
await PushZoneWithID(zone, type, Server.MapName);
await PushZoneWithID(zone, type, map);
}

public Task<Dictionary<ZoneType, IList<IZone>>> GetAllZones() {
Expand Down
7 changes: 5 additions & 2 deletions public/Jailbreak.Public/Mod/SpecialDay/AbstractSpecialDay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,15 @@ protected List<Vector> getRandomSpawns(bool includeSpawns = true,
var zoneManager = Provider.GetService<IZoneManager>();
if (zoneManager != null) {
if (includeAuto)
result.AddRange(zoneManager.GetZones(ZoneType.SPAWN_AUTO)
result.AddRange(zoneManager
.GetZones(Server.MapName, ZoneType.SPAWN_AUTO)
.GetAwaiter()
.GetResult()
.Select(z => z.GetCenterPoint()));

var zones = zoneManager.GetZones(ZoneType.SPAWN).GetAwaiter().GetResult();
var zones = zoneManager.GetZones(Server.MapName, ZoneType.SPAWN)
.GetAwaiter()
.GetResult();
result.AddRange(zones.Select(z => z.GetCenterPoint()));
}

Expand Down
22 changes: 16 additions & 6 deletions public/Jailbreak.Public/Mod/Zones/IZoneManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,29 @@ namespace Jailbreak.Public.Mod.Zones;

public interface IZoneManager : IPluginBehavior {
Task LoadZones(string map);
Task DeleteZone(int zoneId) { return DeleteZone(zoneId, Server.MapName); }

[Obsolete(
"This method hides asynchroneous behavior, use the async version instead")]
Task DeleteZone(int zoneId) {
Server.NextFrame(() => {
var map = Server.MapName;
Server.NextFrameAsync(async () => { await DeleteZone(zoneId, map); });
});
return Task.CompletedTask;
}

Task DeleteZone(int zoneId, string map);

Task<IList<IZone>> GetZones(string map, ZoneType type);

async Task<IList<IZone>> GetZones(params ZoneType[] type) {
async Task<IList<IZone>> GetZones(string map, params ZoneType[] type) {
List<Task<IList<IZone>>> tasks = [];
tasks.AddRange(type.Select(GetZones));
tasks.AddRange(type.Select(t => GetZones(t, map)));
return (await Task.WhenAll(tasks)).SelectMany(x => x).ToList();
}

Task<IList<IZone>> GetZones(ZoneType type) {
return GetZones(Server.MapName, type);
Task<IList<IZone>> GetZones(ZoneType type, string map) {
return GetZones(map, type);
}

Task PushZoneWithID(IZone zone, ZoneType type, string map);
Expand All @@ -27,7 +37,7 @@ Task PushZone(IZone zone, ZoneType type) {
return PushZone(zone, type, Server.MapName);
}

Task UpdateZone(IZone zone, ZoneType type, int id);
Task UpdateZone(IZone zone, ZoneType type, int id, string map);

Task<Dictionary<ZoneType, IList<IZone>>> GetAllZones();
}
2 changes: 1 addition & 1 deletion public/Jailbreak.Public/Utils/MapUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private static Vector getCtSpawn() {
}

public static bool OpenCells(IZoneManager zoneManager) {
var zones = zoneManager.GetZones(ZoneType.CELL_BUTTON)
var zones = zoneManager.GetZones(Server.MapName, ZoneType.CELL_BUTTON)
.GetAwaiter()
.GetResult();

Expand Down
Loading