Skip to content

Commit 12e723a

Browse files
fix: correct method behavior and value properties
1 parent 49a518d commit 12e723a

14 files changed

Lines changed: 130 additions & 67 deletions

File tree

Code/ArgumentSystem/Arguments/DoorsArgument.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88

99
namespace SER.Code.ArgumentSystem.Arguments;
1010

11+
/// <summary>
12+
/// This truly amazing argument has some major issues.
13+
/// Pretty much everything that opens is a door, like elevator doors or SCP-914 doors.
14+
/// This is technically correct but practically no one wants to close 914 doors on `CloseDoor LightContainment`
15+
///
16+
/// </summary>
1117
public class DoorsArgument(string name) : EnumHandlingArgument(name)
1218
{
1319
public override string InputDescription =>
@@ -21,9 +27,17 @@ public class DoorsArgument(string name) : EnumHandlingArgument(name)
2127
[UsedImplicitly]
2228
public DynamicTryGet<Door[]> GetConvertSolution(BaseToken token)
2329
{
30+
var doorPool = new List<Door>(Door.List.Count);
31+
foreach (var door in Door.List)
32+
{
33+
if (door is ElevatorDoor) continue;
34+
if (door is NonInteractableDoor) continue; // im not sure if this is the right call
35+
doorPool.Add(door);
36+
}
37+
2438
if (token is SymbolToken { IsJoker: true } or AllToken)
2539
{
26-
return new(() => Door.List.Where(d => d is not ElevatorDoor).ToArray());
40+
return new(doorPool.ToArray);
2741
}
2842

2943
return ValueOrEnumResolver<Door[]>(token, value =>
@@ -47,27 +61,20 @@ public DynamicTryGet<Door[]> GetConvertSolution(BaseToken token)
4761
}, [
4862
new EnumHandler<DoorName, Door[]>(name => new(() =>
4963
{
50-
return Door.List
51-
.Where(door => door.DoorName == name
52-
&& door is not ElevatorDoor)
53-
.Distinct()
54-
.ToArray();
64+
return doorPool
65+
.Where(door => door.DoorName == name)
66+
.ToArray();
5567
})),
5668
new EnumHandler<FacilityZone, Door[]>(zone => new(() =>
5769
{
58-
return Door.List
59-
.Where(door => door.Zone == zone
60-
&& door is not ElevatorDoor)
61-
.Distinct()
70+
return doorPool
71+
.Where(door => door.Zone == zone)
6272
.ToArray();
6373
})),
6474
new EnumHandler<RoomName, Door[]>(name => new(() =>
6575
{
66-
return Door.List
67-
.Where(d =>
68-
d.Rooms.Any(r => r.Name == name)
69-
&& d is not ElevatorDoor)
70-
.Distinct()
76+
return doorPool
77+
.Where(d => d.Rooms.Any(r => r.Name == name))
7178
.ToArray();
7279
}))
7380
]);

Code/MethodSystem/Methods/AudioMethods/Audio_StopMethod.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace SER.Code.MethodSystem.Methods.AudioMethods;
1010
// ReSharper disable once InconsistentNaming
1111
public class Audio_StopMethod : SynchronousMethod, ICanError
1212
{
13-
public override string Description => "Plays a loaded audio clip from a created speaker.";
13+
public override string Description => "Stops all audio clips playing through a speaker.";
1414

1515
public string[] ErrorReasons =>
1616
[
@@ -31,4 +31,4 @@ public override void Execute()
3131

3232
audioPlayer.RemoveAllClips();
3333
}
34-
}
34+
}

Code/MethodSystem/Methods/DamageRuleMethods/AddDamageRuleMethod.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ public class AddDamageRuleMethod : SynchronousMethod
1212

1313
public override Argument[] ExpectedArguments { get; } =
1414
[
15-
new OptionsArgument("mode", "attacker", "reciever")
15+
new OptionsArgument("mode", "attacker", "receiver", "reciever")
1616
{
1717
Description = "Indicates whether the damage rule will be applied on all damage dealt by the attacker " +
18-
"or on all damage received by the reciever."
18+
"or on all damage received by the receiver."
1919
},
2020
new PlayersArgument("players affected"),
2121
new FloatArgument("multiplier", preferPercent: true),
@@ -60,11 +60,12 @@ public override void Execute()
6060
case "attacker":
6161
DamageRuleHandler.AttackerRules.Add(damageRule);
6262
break;
63-
case "reciever":
64-
DamageRuleHandler.RecieverRules.Add(damageRule);
63+
case "receiver":
64+
case "reciever": // Preserve the existing misspelled option for compatibility.
65+
DamageRuleHandler.ReceiverRules.Add(damageRule);
6566
break;
6667
default:
6768
throw new ArgumentException("Invalid mode");
6869
}
6970
}
70-
}
71+
}

Code/MethodSystem/Methods/DamageRuleMethods/DamageRuleHandler.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ public struct DamageRule
1515
}
1616

1717
public static readonly List<DamageRule> AttackerRules = [];
18-
public static readonly List<DamageRule> RecieverRules = [];
18+
public static readonly List<DamageRule> ReceiverRules = [];
1919

2020
public static void ResetAll()
2121
{
2222
AttackerRules.Clear();
23-
RecieverRules.Clear();
23+
ReceiverRules.Clear();
2424
}
2525

2626
public static void RemoveRule(string? id)
@@ -32,16 +32,16 @@ public static void RemoveRule(string? id)
3232
}
3333

3434
AttackerRules.RemoveAll(rule => rule.Id == id);
35-
RecieverRules.RemoveAll(rule => rule.Id == id);
35+
ReceiverRules.RemoveAll(rule => rule.Id == id);
3636
}
3737

3838
public override void OnPlayerHurting(PlayerHurtingEventArgs ev)
3939
{
4040
if (ev.DamageHandler is not StandardDamageHandler handler) return;
4141

42-
if (ev.Player is { } reciever)
42+
if (ev.Player is { } receiver)
4343
{
44-
Apply(RecieverRules, reciever);
44+
Apply(ReceiverRules, receiver);
4545
}
4646

4747
if (ev.Attacker is { } attacker)
@@ -62,4 +62,4 @@ void Apply(List<DamageRule> rules, Player player)
6262
}
6363
}
6464
}
65-
}
65+
}

Code/MethodSystem/Methods/HTTPMethods/JSON_AddMethod.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,19 @@ public override void Execute()
3838
if (value is TextValue textValue)
3939
{
4040
jsonToAddValueTo[key] = textValue.Value;
41-
return;
4241
}
43-
44-
try
45-
{
46-
jsonToAddValueTo[key] = JToken.FromObject(value.Value);
47-
}
48-
catch
42+
else
4943
{
50-
throw new ScriptRuntimeError(this, ErrorReasons[0]);
44+
try
45+
{
46+
jsonToAddValueTo[key] = JToken.FromObject(value.Value);
47+
}
48+
catch
49+
{
50+
throw new ScriptRuntimeError(this, ErrorReasons[0]);
51+
}
5152
}
53+
54+
ReturnValue = jsonToAddValueTo;
5255
}
53-
}
56+
}

Code/MethodSystem/Methods/ItemMethods/AdvGiveItemMethod.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public class AdvGiveItemMethod : ReferenceReturningMethod<Item?>, IAdditionalDes
2323

2424
public override void Execute()
2525
{
26-
ReturnValue = Args.GetPlayer("player to give item").AddItem(Args.GetEnum<ItemType>("item type to add"));
26+
ReturnValue = Args
27+
.GetPlayer("player to give item")
28+
.AddItem(Args.GetEnum<ItemType>("item type to add"));
2729
}
2830
}
Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using LabApi.Features.Wrappers;
22
using SER.Code.ArgumentSystem.Arguments;
33
using SER.Code.ArgumentSystem.BaseArguments;
4+
using SER.Code.Extensions;
45
using SER.Code.MethodSystem.BaseMethods.Synchronous;
56

67
namespace SER.Code.MethodSystem.Methods.ItemMethods;
@@ -10,24 +11,36 @@ public class ForceEquipMethod : SynchronousMethod
1011
{
1112
public override string Description => "Forces players to equip a provided item.";
1213

13-
public override Argument[] ExpectedArguments =>
14-
[
15-
new PlayersArgument("players"),
16-
new EnumArgument<ItemType>("item type")
17-
{ DefaultValue = new(ItemType.None, "Un-equip held item.") }
18-
];
14+
public override Argument[] ExpectedArguments { get; } =
15+
[
16+
new PlayersArgument("players"),
17+
new EnumArgument<ItemType>("item type")
18+
{
19+
DefaultValue = new(ItemType.None, "Un-equip held item.")
20+
}
21+
];
1922

2023
public override void Execute()
2124
{
2225
var players = Args.GetPlayers("players");
2326
var itemType = Args.GetEnum<ItemType>("item type");
24-
25-
players.ForEach(plr =>
27+
28+
if (itemType == ItemType.None)
29+
{
30+
foreach (var plr in players) plr.CurrentItem = null;
31+
return;
32+
}
33+
34+
foreach (var plr in players)
2635
{
27-
var item = itemType != ItemType.None
28-
? Item.Get(plr.Inventory.UserInventory.Items.FirstOrDefault(x => x.Value.ItemTypeId == itemType).Value)
29-
: null;
30-
plr.CurrentItem = item;
31-
});
36+
var item = Item.Get(
37+
plr.Inventory.UserInventory.Items
38+
.FirstOrDefault(x => x.Value.ItemTypeId == itemType)
39+
.Value
40+
.MaybeNull()
41+
);
42+
43+
if (item is not null) plr.CurrentItem = item;
44+
}
3245
}
33-
}
46+
}

Code/MethodSystem/Methods/ItemMethods/GiveItemMethod.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
using LabApi.Features.Wrappers;
2-
using SER.Code.ArgumentSystem.Arguments;
1+
using SER.Code.ArgumentSystem.Arguments;
32
using SER.Code.ArgumentSystem.BaseArguments;
43
using SER.Code.MethodSystem.BaseMethods.Synchronous;
54
using SER.Code.MethodSystem.Structures;
65

76
namespace SER.Code.MethodSystem.Methods.ItemMethods;
87

98
[UsedImplicitly]
10-
public class GiveItemMethod : ReferenceReturningMethod<Item>, IEssential
9+
public class GiveItemMethod : SynchronousMethod, IEssential
1110
{
1211
public override string Description => "Gives an item to players.";
1312

Code/MethodSystem/Methods/NumberMethods/ChanceMethod.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class ChanceMethod : ReturningMethod<BoolValue>, IAdditionalDescription
2020

2121
public override void Execute()
2222
{
23-
ReturnValue = Args.GetFloat("chance") >= UnityEngine.Random.Range(0f, 1f);
23+
var chance = Args.GetFloat("chance");
24+
ReturnValue = chance >= 1f || chance > 0f && UnityEngine.Random.value < chance;
2425
}
25-
}
26+
}

Code/MethodSystem/Methods/NumberMethods/RandomMethod.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,20 @@
1010
namespace SER.Code.MethodSystem.Methods.NumberMethods;
1111

1212
[UsedImplicitly]
13-
public class RandomMethod : ReturningMethod<NumberValue>, IAdditionalDescription
13+
public class RandomMethod : ReturningMethod<NumberValue>, IAdditionalDescription, ICanError
1414
{
1515
public override string Description =>
1616
"Returns a randomly generated number.";
1717

1818
public string AdditionalDescription =>
1919
"'startingNum' argument MUST be smaller than 'endingNum' argument.";
2020

21+
public string[] ErrorReasons =>
22+
[
23+
"The starting number must not be greater than the ending number.",
24+
"The requested integer range does not contain an integer."
25+
];
26+
2127
public override Argument[] ExpectedArguments { get; } =
2228
[
2329
new FloatArgument("startingNum"),
@@ -39,13 +45,22 @@ public override void Execute()
3945
var endingNum = Args.GetFloat("endingNum");
4046
var type = Args.GetOption("numberType");
4147

42-
var val = Random.Range(startingNum, endingNum);
48+
if (startingNum > endingNum)
49+
throw new SER.Code.Exceptions.ScriptRuntimeError(this, ErrorReasons[0]);
50+
4351
if (type == "int")
4452
{
45-
val = Mathf.RoundToInt(val);
53+
var firstInteger = Mathf.CeilToInt(startingNum);
54+
var lastInteger = Mathf.FloorToInt(endingNum);
55+
if (firstInteger > lastInteger)
56+
throw new SER.Code.Exceptions.ScriptRuntimeError(this, ErrorReasons[1]);
57+
58+
ReturnValue = Random.Range(firstInteger, lastInteger + 1);
59+
return;
4660
}
47-
61+
62+
var val = Random.Range(startingNum, endingNum);
4863
Log.D("random number returns " + val);
4964
ReturnValue = new NumberValue((decimal)val);
5065
}
51-
}
66+
}

0 commit comments

Comments
 (0)