Skip to content

Commit 6aad8ba

Browse files
committed
LateJoinSettingRoleEventArgs & LateJoinSetRoleEventArgs
1 parent 60a6d45 commit 6aad8ba

4 files changed

Lines changed: 171 additions & 0 deletions

File tree

LabExtended/Events/ExRoundEvents.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ public static class ExRoundEvents
4040
/// </summary>
4141
public static event Action<AssigningRolesEventArgs>? AssigningRoles;
4242

43+
/// <summary>
44+
/// Gets called after player's round-start roles are assigned.
45+
/// </summary>
46+
public static event Action<AssignedRolesEventArgs>? AssignedRoles;
47+
48+
/// <summary>
49+
/// Gets called before a player's role is set by late-join.
50+
/// </summary>
51+
public static event Action<LateJoinSettingRoleEventArgs>? LateJoinSettingRole;
52+
53+
/// <summary>
54+
/// Gets called after a player's role is set by late-join.
55+
/// </summary>
56+
public static event Action<LateJoinSetRoleEventArgs>? LateJoinSetRole;
57+
4358
/// <summary>
4459
/// Invokes the <see cref="Ended"/> event.
4560
/// </summary>
@@ -76,5 +91,26 @@ public static void OnWaitingForPlayers()
7691
/// <param name="args">The event's arguments.</param>
7792
public static bool OnAssigningRoles(AssigningRolesEventArgs args)
7893
=> AssigningRoles.InvokeBooleanEvent(args);
94+
95+
/// <summary>
96+
/// Invokes the <see cref="AssignedRoles"/> event.
97+
/// </summary>
98+
/// <param name="args">The event's arguments.</param>
99+
public static void OnAssignedRoles(AssignedRolesEventArgs args)
100+
=> AssignedRoles.InvokeEvent(args);
101+
102+
/// <summary>
103+
/// Invokes the <see cref="LateJoinSettingRole"/> event.
104+
/// </summary>
105+
/// <param name="args">The event's arguments.</param>
106+
public static bool OnLateJoinSettingRole(LateJoinSettingRoleEventArgs args)
107+
=> LateJoinSettingRole.InvokeBooleanEvent(args);
108+
109+
/// <summary>
110+
/// Invokes the <see cref="LateJoinSetRole"/> event.
111+
/// </summary>
112+
/// <param name="args">The event's arguments.</param>
113+
public static void OnLateJoinSetRole(LateJoinSetRoleEventArgs args)
114+
=> LateJoinSetRole.InvokeEvent(args);
79115
}
80116
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using LabExtended.API;
2+
3+
using PlayerRoles;
4+
5+
namespace LabExtended.Events.Round
6+
{
7+
/// <summary>
8+
/// Gets called after a player's role is set by late join.
9+
/// </summary>
10+
public class LateJoinSetRoleEventArgs : EventArgs
11+
{
12+
/// <summary>
13+
/// Gets the player who joined late.
14+
/// </summary>
15+
public ExPlayer Player { get; }
16+
17+
/// <summary>
18+
/// Gets or sets the role the player will be spawned as.
19+
/// </summary>
20+
public RoleTypeId Role { get; }
21+
22+
/// <summary>
23+
/// Gets or sets the role's spawn flags.
24+
/// </summary>
25+
public RoleSpawnFlags Flags { get; }
26+
27+
/// <summary>
28+
/// Initializes a new instance of the LateJoinSetRoleEventArgs class with the specified player, role, and
29+
/// spawn flags.
30+
/// </summary>
31+
/// <param name="player">The player for whom the role is being set during a late join event. Cannot be null.</param>
32+
/// <param name="role">The role to assign to the player.</param>
33+
/// <param name="flags">The flags that control how the role is spawned for the player.</param>
34+
public LateJoinSetRoleEventArgs(ExPlayer player, RoleTypeId role, RoleSpawnFlags flags)
35+
{
36+
Player = player;
37+
Role = role;
38+
Flags = flags;
39+
}
40+
}
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using LabExtended.API;
2+
3+
using PlayerRoles;
4+
5+
namespace LabExtended.Events.Round
6+
{
7+
/// <summary>
8+
/// Gets called before a player's role is set by late join.
9+
/// </summary>
10+
public class LateJoinSettingRoleEventArgs : BooleanEventArgs
11+
{
12+
/// <summary>
13+
/// Gets the player who joined late.
14+
/// </summary>
15+
public ExPlayer Player { get; }
16+
17+
/// <summary>
18+
/// Gets or sets the role the player will be spawned as.
19+
/// </summary>
20+
public RoleTypeId Role { get; set; }
21+
22+
/// <summary>
23+
/// Gets or sets the role's spawn flags.
24+
/// </summary>
25+
public RoleSpawnFlags Flags { get; set; }
26+
27+
/// <summary>
28+
/// Initializes a new instance of the LateJoinSettingRoleEventArgs class with the specified player, role, and
29+
/// spawn flags.
30+
/// </summary>
31+
/// <param name="player">The player for whom the role is being set during a late join event. Cannot be null.</param>
32+
/// <param name="role">The role to assign to the player.</param>
33+
/// <param name="flags">The flags that control how the role is spawned for the player.</param>
34+
public LateJoinSettingRoleEventArgs(ExPlayer player, RoleTypeId role, RoleSpawnFlags flags)
35+
{
36+
Player = player;
37+
Role = role;
38+
Flags = flags;
39+
}
40+
}
41+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using CentralAuth;
2+
3+
using GameCore;
4+
5+
using HarmonyLib;
6+
7+
using LabExtended.API;
8+
9+
using LabExtended.Events;
10+
using LabExtended.Events.Round;
11+
12+
using PlayerRoles;
13+
using PlayerRoles.RoleAssign;
14+
15+
namespace LabExtended.Patches.Events.Round
16+
{
17+
/// <summary>
18+
/// Implements the <see cref="ExRoundEvents.LateJoinSettingRole"/> and <see cref="ExRoundEvents.LateJoinSetRole"/> events.
19+
/// </summary>
20+
public static class LateJoinSettingRolePatches
21+
{
22+
[HarmonyPatch(typeof(RoleAssigner), nameof(RoleAssigner.CheckLateJoin))]
23+
private static bool Prefix(ReferenceHub hub, ClientInstanceMode cim)
24+
{
25+
if (!ExPlayer.TryGet(hub, out var player)
26+
|| !RoleAssigner.CheckPlayer(hub)
27+
|| !RoleAssigner._spawned)
28+
return false;
29+
30+
var lateJoinTime = ConfigFile.ServerConfig.GetFloat("late_join_time", 0f);
31+
var lateJoinRole = RoleTypeId.None;
32+
33+
if (!RoleAssigner.AlreadySpawnedPlayers.Add(player.UserId)
34+
|| RoleAssigner.LateJoinTimer.Elapsed.TotalSeconds > lateJoinTime)
35+
lateJoinRole = RoleTypeId.Spectator;
36+
else
37+
lateJoinRole = HumanSpawner.NextHumanRoleToSpawn;
38+
39+
var settingEventArgs = new LateJoinSettingRoleEventArgs(player, lateJoinRole, RoleSpawnFlags.All);
40+
41+
if (!ExRoundEvents.OnLateJoinSettingRole(settingEventArgs)
42+
|| settingEventArgs.Role is RoleTypeId.None)
43+
return false;
44+
45+
player.Role.LateJoinRole = settingEventArgs.Role;
46+
player.Role.Set(settingEventArgs.Role, RoleChangeReason.LateJoin, settingEventArgs.Flags);
47+
48+
ExRoundEvents.OnLateJoinSetRole(new LateJoinSetRoleEventArgs(player, settingEventArgs.Role, settingEventArgs.Flags));
49+
50+
return false;
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)