Skip to content

Commit

Permalink
send ingame state
Browse files Browse the repository at this point in the history
reject spawns in-game if cannot spawn
  • Loading branch information
Govorunb committed Jun 12, 2024
1 parent 200f983 commit 398da35
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 96 deletions.
17 changes: 10 additions & 7 deletions SCHIZO/SwarmControl/ControlWebSocket.Plumbing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private async Task SendThread()
{
try
{
await SendStringInternal(JsonConvert.SerializeObject(msg, Formatting.None));
await SendStringInternal(JsonConvert.SerializeObject(msg, Formatting.None), msg.MessageType != MessageType.Ping);
if (msg.MessageType != MessageType.Ping)
LOGGER.LogInfo($"Sent {msg.MessageType} ({msg.Guid})");
}
Expand Down Expand Up @@ -103,6 +103,7 @@ private async Task PingThread()
if (_sendQueue.IsEmpty)
{
SendMessage(new PingMessage());
await Task.Delay(1000);
}
if (_lastPong.AddSeconds(5) < DateTime.UtcNow)
{
Expand All @@ -112,15 +113,16 @@ private async Task PingThread()
}
}

private async Task SendStringInternal(string message, CancellationToken ct = default)
private async Task SendStringInternal(string message, bool print = true, CancellationToken ct = default)
{
if (string.IsNullOrEmpty(message))
throw new ArgumentNullException(nameof(message));

if (!CheckOpen())
throw new InvalidOperationException("Socket not connected");

LOGGER.LogDebug($"SEND:\n{message}");
if (print)
LOGGER.LogDebug($"SEND:\n{message}");
await _socket.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes(message)), WebSocketMessageType.Text, true, ct);
}

Expand All @@ -131,7 +133,8 @@ public void SendMessage(GameMessage message)
LOGGER.LogDebug($"Enqueued {message.MessageType} ({message.Guid})");
}

private async Task<string?> ReceiveStringAsync(CancellationToken ct = default)
// note: cancelling in ReceiveAsync kills the socket, there's an issue on this that's closed as "by design" Smile
private async Task<string?> ReceiveStringAsync(CancellationToken killSocketCt = default)
{
if (!CheckOpen())
return null;
Expand All @@ -144,7 +147,7 @@ public void SendMessage(GameMessage message)
int totalBytes = 0;
while (result is not { EndOfMessage: true })
{
result = await _socket.ReceiveAsync(buffer, ct);
result = await _socket.ReceiveAsync(buffer, killSocketCt);
ms.Write(buffer.Array, 0, result.Count);
totalBytes += result.Count;
}
Expand All @@ -156,10 +159,10 @@ public void SendMessage(GameMessage message)
case WebSocketMessageType.Text:
return Encoding.UTF8.GetString(ms.ToArray());
case WebSocketMessageType.Binary:
await _socket.CloseOutputAsync(WebSocketCloseStatus.InvalidMessageType, "Unexpected binary data", ct);
await _socket.CloseOutputAsync(WebSocketCloseStatus.InvalidMessageType, "Unexpected binary data", killSocketCt);
throw new InvalidDataException("Received unexpected binary data");
default:
await _socket.CloseOutputAsync(WebSocketCloseStatus.InvalidMessageType, "Invalid message type", ct);
await _socket.CloseOutputAsync(WebSocketCloseStatus.InvalidMessageType, "Invalid message type", killSocketCt);
throw new InvalidDataException("Received invalid message type");
}
}
Expand Down
12 changes: 8 additions & 4 deletions SCHIZO/SwarmControl/MessageProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ private void OnClose(WebSocketCloseStatus status, string? reason)

private void OnHelloBack(HelloBackMessage helloBack)
{
if (helloBack.Allowed) return;
if (!helloBack.Allowed)
{
LOGGER.LogError("Server rejected handshake");
_socket.Disconnect().Start();
uGUI.main.confirmation.Show("Server rejected handshake\nConnection is not possible");
return;
}

LOGGER.LogError("Server rejected handshake");
_socket.Disconnect().Start();
uGUI.main.confirmation.Show("Server rejected handshake\nConnection is not possible");
SwarmControlManager.Instance.SendIngameStateMsg();
}
private void OnConsoleInput(ConsoleInputMessage msg)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ internal record IngameStateChangedMessage : GameMessage
{
public override MessageType MessageType => MessageType.IngameStateChanged;
public bool Ingame { get; set; }
public bool Paused { get; set; }
/// <summary>
/// Forbid spawning while inside a base or seatruck (prawn suit is fine)
/// Spawning is forbidden while on land or inside a base/seatruck (prawn suit is fine)
/// </summary>
public bool Indoors { get; set; }
public bool OnLand { get; set; }
public bool CanSpawn { get; set; }
}
57 changes: 0 additions & 57 deletions SCHIZO/SwarmControl/Redeems/Neuro/RerollPlayerName.cs

This file was deleted.

21 changes: 0 additions & 21 deletions SCHIZO/SwarmControl/Redeems/Neuro/SetPlayerName.cs

This file was deleted.

9 changes: 9 additions & 0 deletions SCHIZO/SwarmControl/Redeems/Spawns/SpawnFiltered.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using System.Collections.Generic;
using SCHIZO.Commands.Base;
using SCHIZO.Commands.ConsoleCommands;
using SCHIZO.Commands.Context;
using SCHIZO.Commands.Input;
using SCHIZO.Commands.Output;

namespace SCHIZO.SwarmControl.Redeems.Spawns;

Expand All @@ -26,6 +28,13 @@ public SpawnFiltered() : base("spawn")
];
}

protected override object? ExecuteCore(CommandExecutionContext ctx)
{
if (!SwarmControlManager.Instance.CanSpawn)
return CommonResults.Error("Cannot spawn at this time.");
return base.ExecuteCore(ctx);
}

protected override Dictionary<string, object?>? GetTargetArgs(Dictionary<string, object?>? proxyArgs)
{
if (proxyArgs is null)
Expand Down
49 changes: 46 additions & 3 deletions SCHIZO/SwarmControl/SwarmControlManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Cci;
using Nautilus.Utility;
using SCHIZO.Attributes;
using SCHIZO.Commands.Attributes;
using SCHIZO.Helpers;
using SCHIZO.Resources;
using SwarmControl.Models.Game.Messages;
using TMPro;
using UnityEngine;

Expand Down Expand Up @@ -37,11 +40,28 @@ public string PrivateApiKey

private ControlWebSocket _socket;

public bool Ingame { get; private set; }
public bool CanSpawn { get; private set; }

private void Awake()
{
Instance = this;
_socket = new();
Processor = new(_socket);
SaveUtils.RegisterOnFinishLoadingEvent(OnLoad);
SaveUtils.RegisterOnQuitEvent(OnQuit);
}

private void OnLoad()
{
Ingame = true;
SendIngameStateMsg();
}

private void OnQuit()
{
Ingame = false;
SendIngameStateMsg();
}

private void OnDestroy()
Expand Down Expand Up @@ -159,15 +179,26 @@ private void Update()
action();
}

private void FixedUpdate()
{
bool canSpawn = Player.main
&& Player.main.IsUnderwaterForSwimming()
&& !Player.main.IsPilotingSeatruck()
&& !Player.main.IsInBase()
;
if (canSpawn != CanSpawn)
{
CanSpawn = canSpawn;
SendIngameStateMsg();
}
}

private int _retries;
private const int MaxRetries = int.MaxValue;
private IEnumerator AutoReconnectCoro()
{
while (_retries < MaxRetries)
{
// unfortunately the socket state stays "Open" even if the server stops replying to pings
// so detecting disconnects robustly is left as a "fun" exercise for the reader
// no you can't just ReceiveAsync with a timeout ct because cancelling it kills the socket, there's an issue on this that's closed as "by design" Smile
if (_socket.IsConnected)
{
_retries = 0;
Expand All @@ -185,4 +216,16 @@ private IEnumerator AutoReconnectCoro()
LOGGER.LogWarning("Could not reconnect to websocket");
ShowConfirmation("Lost connection to server\nReconnect in options");
}

internal void SendIngameStateMsg()
{
if (_socket.IsConnected)
{
_socket.SendMessage(new IngameStateChangedMessage()
{
Ingame = Ingame,
CanSpawn = CanSpawn,
});
}
}
}

0 comments on commit 398da35

Please sign in to comment.