Skip to content

Commit 60a6d45

Browse files
committed
AssignedRolesEventArgs
1 parent 89fc0e1 commit 60a6d45

3 files changed

Lines changed: 106 additions & 13 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using LabExtended.API;
2+
3+
using PlayerRoles;
4+
5+
namespace LabExtended.Events.Round
6+
{
7+
/// <summary>
8+
/// Gets called after the server starts assigns roles when the round starts.
9+
/// </summary>
10+
public class AssignedRolesEventArgs : EventArgs
11+
{
12+
/// <summary>
13+
/// A dictionary of players and their decided roles.
14+
/// </summary>
15+
public IReadOnlyDictionary<ExPlayer, RoleTypeId> Roles { get; }
16+
17+
/// <summary>
18+
/// Creates a new <see cref="AssignedRolesEventArgs"/> instance.
19+
/// </summary>
20+
/// <param name="roles">The assigned roles.</param>
21+
public AssignedRolesEventArgs(Dictionary<ExPlayer, RoleTypeId> roles)
22+
=> Roles = roles;
23+
}
24+
}

LabExtended/Events/Round/AssigningRolesEventArgs.cs

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,73 @@ public class AssigningRolesEventArgs : BooleanEventArgs
1212
/// <summary>
1313
/// A dictionary of players and their decided roles.
1414
/// </summary>
15-
public Dictionary<ExPlayer?, RoleTypeId> Roles { get; }
15+
public Dictionary<ExPlayer, RoleTypeId> Roles { get; }
1616

1717
/// <summary>
1818
/// Creates a new <see cref="AssigningRolesEventArgs"/> instance.
1919
/// </summary>
20-
/// <param name="roles"></param>
21-
public AssigningRolesEventArgs(Dictionary<ExPlayer?, RoleTypeId> roles)
20+
/// <param name="roles">The assigned roles.</param>
21+
public AssigningRolesEventArgs(Dictionary<ExPlayer, RoleTypeId> roles)
2222
=> Roles = roles;
23+
24+
/// <summary>
25+
/// Sets the role for the specified player if the player exists in the collection.
26+
/// </summary>
27+
/// <param name="player">The player whose role is to be set. Cannot be null.</param>
28+
/// <param name="role">The role to assign to the specified player.</param>
29+
/// <exception cref="ArgumentNullException">Thrown if <paramref name="player"/> is null.</exception>
30+
public void Set(ExPlayer player, RoleTypeId role)
31+
{
32+
if (player is null)
33+
throw new ArgumentNullException(nameof(player));
34+
35+
if (!Roles.ContainsKey(player))
36+
return;
37+
38+
Roles[player] = role;
39+
}
40+
41+
/// <summary>
42+
/// Sets the role for all players by applying the specified selector function to each player.
43+
/// </summary>
44+
/// <param name="selector">A function that takes an ExPlayer and returns the RoleTypeId to assign to that player.</param>
45+
/// <exception cref="ArgumentNullException">Thrown if selector is null.</exception>
46+
public void SetAll(Func<ExPlayer, RoleTypeId> selector)
47+
{
48+
if (selector is null)
49+
throw new ArgumentNullException(nameof(selector));
50+
51+
foreach (var player in ExPlayer.Players)
52+
{
53+
if (!Roles.ContainsKey(player))
54+
continue;
55+
56+
Roles[player] = selector(player);
57+
}
58+
}
59+
60+
/// <summary>
61+
/// Sets the specified role for all players that match the given predicate.
62+
/// </summary>
63+
/// <param name="predicate">A predicate used to determine which players should have their role set. The method applies the role to each
64+
/// player for whom this predicate returns <see langword="true"/>.</param>
65+
/// <param name="role">The role to assign to each player that matches the predicate.</param>
66+
/// <exception cref="ArgumentNullException">Thrown if <paramref name="predicate"/> is <see langword="null"/>.</exception>
67+
public void SetWhere(Predicate<ExPlayer> predicate, RoleTypeId role)
68+
{
69+
if (predicate is null)
70+
throw new ArgumentNullException(nameof(predicate));
71+
72+
foreach (var player in ExPlayer.Players)
73+
{
74+
if (!Roles.ContainsKey(player))
75+
continue;
76+
77+
if (!predicate(player))
78+
continue;
79+
80+
Roles[player] = role;
81+
}
82+
}
2383
}
2484
}

LabExtended/Patches/Events/Round/AssigningRolesPatch.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
using PlayerRoles.RoleAssign;
66
using PlayerRoles;
77

8-
using LabExtended.Core.Pooling.Pools;
9-
108
using LabExtended.API;
9+
1110
using LabExtended.Events;
11+
1212
using LabExtended.Utilities;
1313
using LabExtended.Utilities.RoleSelection;
1414

1515
namespace LabExtended.Patches.Events.Round;
1616

1717
/// <summary>
18-
/// Implements the <see cref="ExRoundEvents.AssigningRoles"/> event.
18+
/// Implements the <see cref="ExRoundEvents.AssigningRoles"/> and <see cref="ExRoundEvents.AssignedRoles"/> events.
1919
/// </summary>
2020
public static class AssigningRolesPatch
2121
{
@@ -29,7 +29,7 @@ public static class AssigningRolesPatch
2929
/// </summary>
3030
public static readonly List<ExPlayer> Players = new();
3131

32-
public static FastEvent<Action> OnPlayersSpawned { get; } =
32+
private static FastEvent<Action> OnPlayersSpawned { get; } =
3333
FastEvents.DefineEvent<Action>(typeof(RoleAssigner), nameof(RoleAssigner.OnPlayersSpawned));
3434

3535
[HarmonyPatch(typeof(RoleAssigner), nameof(RoleAssigner.OnRoundStarted))]
@@ -62,14 +62,23 @@ private static bool Prefix()
6262

6363
foreach (var pair in Roles)
6464
{
65-
pair.Key.Role.RoundStartRole = pair.Value;
66-
pair.Key.Role.Set(pair.Value, RoleChangeReason.RoundStart, RoleSpawnFlags.All);
67-
68-
if (pair.Key.Role.IsAlive)
69-
RoleAssigner.AlreadySpawnedPlayers.Add(pair.Key.UserId);
65+
if (pair.Value != RoleTypeId.None)
66+
{
67+
pair.Key.Role.RoundStartRole = pair.Value;
68+
pair.Key.Role.Set(pair.Value, RoleChangeReason.RoundStart, RoleSpawnFlags.All);
69+
70+
if (pair.Key.Role.IsAlive)
71+
RoleAssigner.AlreadySpawnedPlayers.Add(pair.Key.UserId);
72+
}
7073
}
7174

72-
OnPlayersSpawned.InvokeEvent(null, Array.Empty<object>());
75+
OnPlayersSpawned.InvokeEvent(null!, Array.Empty<object>());
76+
77+
ExRoundEvents.OnAssignedRoles(new(Roles));
78+
79+
Roles.Clear();
80+
Players.Clear();
81+
7382
return false;
7483
}
7584
}

0 commit comments

Comments
 (0)