Skip to content

Commit e515fcb

Browse files
authored
Merge pull request #8 from UncomplicatedCustomServer/dev-labapi
Fixed and "all" logic - v1.2.0
2 parents ec54bb0 + 7412c6a commit e515fcb

11 files changed

Lines changed: 208 additions & 421 deletions

File tree

UncomplicatedCustomEscapeZones/API/Features/CustomEscapeZone.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using LabApi.Features.Wrappers;
5+
using PlayerRoles;
56
using UncomplicatedEscapeZones.Extensions;
67
using UncomplicatedEscapeZones.Interfaces;
78
using UncomplicatedEscapeZones.Managers;
@@ -50,17 +51,17 @@ public class CustomEscapeZone : ICustomEscapeZone
5051
"InternalTeam ClassD", [
5152
new Dictionary<string, string>
5253
{
53-
{ "default", "InternalRole ChaosRepressor" },
54-
{ "cuffed by InternalFaction FoundationForces", "InternalRole NtfPrivate" }
54+
{ "default", "InternalRole ChaosConscript" },
55+
{ "cuffed by InternalFaction FoundationStaff", "InternalRole NtfPrivate" }
5556
}
5657
]
5758
},
5859
{
59-
"InternalTeam Scientist", [
60+
"InternalTeam Scientists", [
6061
new Dictionary<string, string>
6162
{
6263
{ "default", "InternalRole NtfSpecialist" },
63-
{ "cuffed by InternalFaction FoundationEnemy", "InternalRole ChaosRepressor" }
64+
{ "cuffed by InternalFaction FoundationEnemy", "InternalRole ChaosConscript" }
6465
}
6566
]
6667
}

UncomplicatedCustomEscapeZones/Commands/CommandParent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected override bool ExecuteParent(ArraySegment<string> arguments, ICommandSe
3737
{
3838
// Help page
3939
response =
40-
$"\n>> UncomplicatedCustomEscapeZones v{Plugin.Instance.Version}{(VersionManager.VersionInfo?.CustomName is not null ? $" '{VersionManager.VersionInfo.CustomName}'" : string.Empty)} <<\nby {Plugin.Instance.Author}\n\nAvailable commands:";
40+
$"\n>> UncomplicatedCustomEscapeZones v{Plugin.Instance.Version} <<\nby {Plugin.Instance.Author}\n\nAvailable commands:";
4141

4242
foreach (IUCEZCommand Command in RegisteredCommands)
4343
response += $"\n• <b>ucez {Command.Name.GenerateWithBuffer(12)}</b> → {Command.Description}";

UncomplicatedCustomEscapeZones/Events/EventHandler.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using UncomplicatedEscapeZones.Interfaces;
1010
using UncomplicatedEscapeZones.Intergrations;
1111
using UncomplicatedEscapeZones.Managers;
12+
using UnityEngine;
1213

1314
namespace UncomplicatedEscapeZones.Events;
1415

@@ -58,16 +59,29 @@ public override void OnPlayerEscaping(PlayerEscapingEventArgs ev)
5859
if (role is not RoleTypeId.None)
5960
{
6061
ev.NewRole = role;
61-
ev.IsAllowed = true;
6262
if (ev.EscapeScenario == Escape.EscapeScenarioType.None)
6363
ev.EscapeScenario = Escape.EscapeScenarioType.Custom;
64+
65+
if (UCR.TryGetSummonedCustomRole(ev.Player, out _))
66+
{
67+
ev.IsAllowed = false;
68+
ev.Player.ConnectionToClient.Send(new Escape.EscapeMessage
69+
{
70+
ScenarioId = (byte) ev.EscapeScenario,
71+
EscapeTime = (ushort) Mathf.CeilToInt(ev.Player.RoleBase.ActiveTime)
72+
});
73+
ev.Player.SetRole(ev.NewRole, RoleChangeReason.Escaped);
74+
return;
75+
}
76+
77+
ev.IsAllowed = true;
6478
LogManager.Debug($"Player {ev.Player.Nickname} will respawn as {role}!");
6579
}
6680
}
6781
else
6882
{
69-
LogManager.Debug($"Trying to find CustomRole with Id {newRoleValue.Key}");
70-
if (int.TryParse(newRoleValue.Key.ToString(), out int id) && UCR.TryGetCustomRole(id, out object _))
83+
LogManager.Debug($"Trying to find CustomRole with Id {newRoleValue.Value}");
84+
if (int.TryParse(newRoleValue.Value.ToString(), out int id) && UCR.TryGetCustomRole(id, out object _))
7185
{
7286
LogManager.Debug("Role found!");
7387
ev.IsAllowed = false;
@@ -79,12 +93,10 @@ public override void OnPlayerEscaping(PlayerEscapingEventArgs ev)
7993
UCR.GiveCustomRole(id, ev.Player);
8094
LogManager.Debug(
8195
$"Successfully called method SpawnManager::SummonCustomSubclass(<...>) for player {ev.Player.Nickname}!");
96+
return;
8297
}
83-
else
84-
{
85-
LogManager.Debug(
86-
"Canceled call to method SpawnManager::SummonCustomSubclass(<...>) due to the presence of the player inside the Escape::Bucket! - Event already fired!");
87-
}
98+
LogManager.Debug(
99+
"Canceled call to method SpawnManager::SummonCustomSubclass(<...>) due to the presence of the player inside the Escape::Bucket! - Event already fired!");
88100
}
89101
}
90102
}

UncomplicatedCustomEscapeZones/Extensions/StringExtension.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
namespace UncomplicatedEscapeZones.Extensions;
1+
using System;
2+
using System.Net;
3+
using Newtonsoft.Json.Linq;
4+
5+
namespace UncomplicatedEscapeZones.Extensions;
26

37
public static class StringExtension
48
{
@@ -9,4 +13,18 @@ public static string GenerateWithBuffer(this string str, int bufferSize)
913

1014
return str;
1115
}
16+
17+
public static HttpStatusCode GetStatusCode(this string str, out string message)
18+
{
19+
JObject obj = JObject.Parse(str);
20+
21+
message = null;
22+
if (obj.TryGetValue("message", out JToken token))
23+
message = token.ToString();
24+
25+
if (obj.TryGetValue("status", out JToken status) && Enum.TryParse(status.ToString(), out HttpStatusCode statusCode))
26+
return statusCode;
27+
28+
return HttpStatusCode.Unused;
29+
}
1230
}

UncomplicatedCustomEscapeZones/Managers/EscapeManager.cs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace UncomplicatedEscapeZones.Managers;
1212

13-
public class EscapeManager
13+
public static class EscapeManager
1414
{
1515
public static KeyValuePair<bool, object?>? ParseEscapeRole(
1616
Dictionary<string, List<Dictionary<string, string>>> roleAfterEscape, Player player)
@@ -34,10 +34,12 @@ public class EscapeManager
3434
List<Dictionary<string, string>>? entries = ResolveEntries(
3535
roleAfterEscape,
3636
$"InternalTeam {playerTeamKey}",
37-
$"IT {playerRoleKey}",
37+
$"IT {playerTeamKey}",
3838
$"InternalFaction {playerFactionKey}",
3939
$"IF {playerFactionKey}",
40-
playerRoleKey);
40+
$"InternalRole {playerRoleKey}",
41+
$"IR {playerRoleKey}",
42+
"all");
4143

4244
if (UCR.TryGetSummonedCustomRole(player, out object summonedPlayer))
4345
{
@@ -48,7 +50,8 @@ public class EscapeManager
4850
List<Dictionary<string, string>>? customEntries = ResolveEntries(
4951
roleAfterEscape,
5052
$"CustomRole {customRoleId}",
51-
$"CR {customRoleId}");
53+
$"CR {customRoleId}",
54+
"all");
5255
if (customEntries is not null)
5356
{
5457
LogManager.Debug($"Found {customEntries.Count} RoleAfterEscape entries for custom role '{customRoleId}'.");
@@ -72,6 +75,7 @@ public class EscapeManager
7275
Dictionary<int, KeyValuePair<bool, object?>?> asCuffedByCustomRole = new();
7376

7477
KeyValuePair<bool, object?>? defaultValue = new KeyValuePair<bool, object?>(false, null);
78+
KeyValuePair<bool, object?>? defaultCuffedValue = new KeyValuePair<bool, object?>(false, null);
7579

7680
// Flatten and parse all condition/value pairs for this role
7781
foreach (Dictionary<string, string> dict in entries)
@@ -99,20 +103,34 @@ public class EscapeManager
99103

100104
LogManager.Debug($"Parsing escape condition: {kvp.Key} -> {kvp.Value}");
101105

102-
switch (elements[2])
106+
switch (elements[2].ToLowerInvariant())
103107
{
104-
case "InternalFaction" or "IF" when Enum.TryParse(elements[3], out Faction faction):
105-
asCuffedByInternalFaction.TryAdd(faction, data);
108+
case "internalfaction" or "if":
109+
if (Enum.TryParse(elements[3], true, out Faction faction))
110+
asCuffedByInternalFaction.TryAdd(faction, data);
111+
else
112+
LogManager.Warn($"Failed to parse faction '{elements[3]}' for escape condition '{kvp.Key}'.");
106113
break;
107-
case "InternalTeam" or "IT" when Enum.TryParse(elements[3], out Team team):
108-
asCuffedByInternalTeam.TryAdd(team, data);
114+
case "internalteam" or "it":
115+
if (Enum.TryParse(elements[3], true, out Team team))
116+
asCuffedByInternalTeam.TryAdd(team, data);
117+
else
118+
LogManager.Warn($"Failed to parse team '{elements[3]}' for escape condition '{kvp.Key}'.");
109119
break;
110-
case "InternalRole" or "IR" when Enum.TryParse(elements[3], out RoleTypeId id):
111-
asCuffedByInternalRole.TryAdd(id, data);
120+
case "internalrole" or "ir":
121+
if (Enum.TryParse(elements[3], true, out RoleTypeId id))
122+
asCuffedByInternalRole.TryAdd(id, data);
123+
else
124+
LogManager.Warn($"Failed to parse role '{elements[3]}' for escape condition '{kvp.Key}'.");
112125
break;
113-
case "CustomRole" or "CR"
114-
when int.TryParse(elements[3], out int id) && UCR.TryGetCustomRole(id, out _):
115-
asCuffedByCustomRole.TryAdd(id, data);
126+
case "customrole" or "cr":
127+
if (int.TryParse(elements[3], out int cid) && UCR.TryGetCustomRole(cid, out _))
128+
asCuffedByCustomRole.TryAdd(cid, data);
129+
else
130+
LogManager.Warn($"Failed to parse custom role id '{elements[3]}' for escape condition '{kvp.Key}'.");
131+
break;
132+
case "all":
133+
defaultCuffedValue = data;
116134
break;
117135
default:
118136
{
@@ -152,6 +170,12 @@ when int.TryParse(elements[3], out int id) && UCR.TryGetCustomRole(id, out _):
152170

153171
if (asCuffedByInternalFaction.TryGetValue(player.DisarmedBy.Faction, out KeyValuePair<bool, object?>? factionValue) && factionValue is not null)
154172
return factionValue;
173+
174+
if (defaultCuffedValue is not null)
175+
{
176+
LogManager.Debug($"Applying default 'cuffed by all' escape outcome for player {player.PlayerId}.");
177+
return defaultCuffedValue;
178+
}
155179
}
156180

157181
LogManager.Debug(

0 commit comments

Comments
 (0)