mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-20 01:46:15 +00:00
71 lines
2.0 KiB
C#
71 lines
2.0 KiB
C#
using System.Collections.Generic;
|
|
using Hazel;
|
|
using InnerNet;
|
|
using MiraAPI.GameEnd;
|
|
using Reactor.Networking.Attributes;
|
|
using Reactor.Networking.Rpc;
|
|
using Reactor.Utilities;
|
|
|
|
namespace MiraAPI.Networking;
|
|
|
|
/// <summary>
|
|
/// Custom Game Over RPC.
|
|
/// </summary>
|
|
/// <inheritdoc />
|
|
[RegisterCustomRpc((uint)MiraRpc.CustomGameOver)]
|
|
public class CustomGameOverRpc(MiraApiPlugin plugin, uint id) : PlayerCustomRpc<MiraApiPlugin, GameOverData>(plugin, id)
|
|
{
|
|
/// <inheritdoc />
|
|
public override RpcLocalHandling LocalHandling => RpcLocalHandling.After;
|
|
|
|
/// <inheritdoc />
|
|
public override void Write(MessageWriter writer, GameOverData data)
|
|
{
|
|
writer.WritePacked(data.Reason);
|
|
writer.WritePacked(data.Winners.Count);
|
|
foreach (var player in data.Winners)
|
|
{
|
|
writer.WriteNetObject(player);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override GameOverData Read(MessageReader reader)
|
|
{
|
|
var reason = reader.ReadPackedInt32();
|
|
var count = reader.ReadPackedInt32();
|
|
var data = new List<NetworkedPlayerInfo>(count);
|
|
for (var i = 0; i < count; i++)
|
|
{
|
|
data.Add(reader.ReadNetObject<NetworkedPlayerInfo>());
|
|
}
|
|
|
|
return new GameOverData(reason, data);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override void Handle(PlayerControl innerNetObject, GameOverData data)
|
|
{
|
|
if (GameOverManager.TryGetGameOver(data.Reason, out var gameOver))
|
|
{
|
|
if (!gameOver.VerifyCondition(innerNetObject, [.. data.Winners]))
|
|
{
|
|
Logger<MiraApiPlugin>.Info($"Game over condition not met for {gameOver.GetType().Name}");
|
|
return;
|
|
}
|
|
|
|
CustomGameOver.Instance = gameOver;
|
|
|
|
if (AmongUsClient.Instance.AmHost)
|
|
{
|
|
GameManager.Instance.RpcEndGame(gameOver, false);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
CustomGameOver.Instance = null;
|
|
Logger<MiraApiPlugin>.Error($"Unknown game over reason: {data.Reason}");
|
|
}
|
|
}
|
|
}
|