GameMode/Rules/ResetGame.cs

A game Component that listens for state enter and update events to reset player pawns once when the state updates. On entering the state it clears a flag; on the first update (host only asserted) it iterates active player states and respawns players at a random team spawn if they have a requested character definition.

using KOTH.UI;
using Sandbox.Diagnostics;
using Sandbox.Events;

namespace KOTH;

public sealed class ResetGame : Component,
	IGameEventHandler<EnterStateEvent>,
	IGameEventHandler<UpdateStateEvent>
{
	bool HasReset = false;

	public void OnGameEvent(EnterStateEvent eventArgs)
	{
		HasReset = false;
	}

	void IGameEventHandler<UpdateStateEvent>.OnGameEvent(UpdateStateEvent eventArgs)
	{
		Assert.True(Networking.IsHost);

		if (HasReset)
		{
			return;
		}

		HasReset = true;

		foreach (var PlayerState in GameNetworkManager.PlayerStates)
		{
			if (!PlayerState.IsValid() || PlayerState.RequestedCharacterDefinition == null)
			{
				continue;
			}

			var SpawnPoint = GameUtils.GetRandomTeamSpawn(PlayerState.Team);
			PlayerState.SpawnPlayerPawn(SpawnPoint);
		}
	}
}