mirror of
https://github.com/Impostor/Impostor.git
synced 2026-10-20 01:46:11 +00:00
Add spawn and leave timeout
This commit is contained in:
1 parent
fcd54e0c63
commit
67e302798f
10 files changed
+107
-16
No files matched your search
@@ -395,8 +395,6 @@ namespace Impostor.Api.Innersloth.Net.Objects
|
||||
{
|
||||
PlayerInfo.IsDead = true;
|
||||
PlayerInfo.LastDeathReason = reason;
|
||||
|
||||
Console.WriteLine($"{PlayerInfo.PlayerName,-15} died {reason}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,5 +30,12 @@ namespace Impostor.Api.Net
|
||||
/// <param name="writer">The message.</param>
|
||||
/// <returns></returns>
|
||||
ValueTask SendAsync(IMessageWriter writer);
|
||||
|
||||
/// <summary>
|
||||
/// Disconnects the client and invokes the disconnect handler.
|
||||
/// </summary>
|
||||
/// <param name="reason">A reason.</param>
|
||||
/// <returns></returns>
|
||||
ValueTask DisconnectAsync(string reason);
|
||||
}
|
||||
}
|
||||
@@ -13,22 +13,31 @@
|
||||
/// </returns>
|
||||
public static float Clamp(float value, float min, float max)
|
||||
{
|
||||
if ((double) value < (double) min)
|
||||
if (value < (double)min)
|
||||
{
|
||||
value = min;
|
||||
else if ((double) value > (double) max)
|
||||
}
|
||||
else if (value > (double)max)
|
||||
{
|
||||
value = max;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>Clamps value between 0 and 1 and returns value.</para>
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="value">Value.</param>
|
||||
/// <returns>Clamped value.</returns>
|
||||
public static float Clamp01(float value)
|
||||
{
|
||||
if ((double) value < 0.0)
|
||||
if (value < 0.0)
|
||||
{
|
||||
return 0.0f;
|
||||
return (double) value > 1.0 ? 1f : value;
|
||||
}
|
||||
|
||||
return (double)value > 1.0 ? 1f : value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -40,6 +49,6 @@
|
||||
/// <returns>
|
||||
/// <para>The interpolated float result between the two float values.</para>
|
||||
/// </returns>
|
||||
public static float Lerp(float a, float b, float t) => a + (b - a) * Mathf.Clamp01(t);
|
||||
public static float Lerp(float a, float b, float t) => a + ((b - a) * Clamp01(t));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Impostor.Server
|
||||
{
|
||||
internal static class Constants
|
||||
{
|
||||
public const int SpawnTimeout = 2500;
|
||||
public const int ConnectionTimeout = 2500;
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,16 @@ namespace Impostor.Server.Net.Hazel
|
||||
return InnerConnection.SendAsync(writer);
|
||||
}
|
||||
|
||||
public ValueTask DisconnectAsync(string reason)
|
||||
{
|
||||
return InnerConnection.Disconnect(reason);
|
||||
}
|
||||
|
||||
public void DisposeInnerConnection()
|
||||
{
|
||||
InnerConnection.Dispose();
|
||||
}
|
||||
|
||||
private async ValueTask ConnectionOnDisconnected(DisconnectedEventArgs e)
|
||||
{
|
||||
if (Client != null)
|
||||
|
||||
@@ -1,14 +1,24 @@
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Impostor.Api.Innersloth.Net;
|
||||
using Impostor.Api.Innersloth.Net.Objects;
|
||||
using Impostor.Api.Net;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Impostor.Server.Net.State
|
||||
{
|
||||
internal partial class ClientPlayer : IClientPlayer
|
||||
{
|
||||
public ClientPlayer(ClientBase client, Game game)
|
||||
private readonly ILogger<ClientPlayer> _logger;
|
||||
|
||||
private Timer _spawnTimeout;
|
||||
|
||||
public ClientPlayer(ILogger<ClientPlayer> logger, ClientBase client, Game game)
|
||||
{
|
||||
_logger = logger;
|
||||
_spawnTimeout = new Timer(RunSpawnTimeout, null, -1, -1);
|
||||
|
||||
Game = game;
|
||||
Client = client;
|
||||
Limbo = LimboStates.PreSpawn;
|
||||
@@ -27,6 +37,17 @@ namespace Impostor.Server.Net.State
|
||||
|
||||
public string Scene { get; internal set; }
|
||||
|
||||
public void InitializeSpawnTimeout()
|
||||
{
|
||||
_spawnTimeout.Change(Constants.SpawnTimeout, -1);
|
||||
}
|
||||
|
||||
public void DisableSpawnTimeout()
|
||||
{
|
||||
_spawnTimeout.Dispose();
|
||||
_spawnTimeout = null;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsOwner(InnerNetObject netObject)
|
||||
{
|
||||
@@ -44,5 +65,26 @@ namespace Impostor.Server.Net.State
|
||||
{
|
||||
return Game.HandleKickPlayer(Client.Id, true);
|
||||
}
|
||||
|
||||
private async void RunSpawnTimeout(object state)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Character == null)
|
||||
{
|
||||
_logger.LogInformation("{0} - Player {1} spawn timed out, kicking.", Game.Code, Client.Id);
|
||||
|
||||
await KickAsync();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Exception caught while kicking player for spawn timeout.");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await _spawnTimeout.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -81,6 +81,7 @@ namespace Impostor.Server.Net.State
|
||||
if (TryGetPlayer(control.OwnerId, out var player))
|
||||
{
|
||||
player.Character = control;
|
||||
player.DisableSpawnTimeout();
|
||||
}
|
||||
|
||||
// Hook up InnerPlayerControl <-> InnerPlayerControl.PlayerInfo.
|
||||
|
||||
@@ -6,6 +6,7 @@ using Impostor.Api.Innersloth.Data;
|
||||
using Impostor.Api.Net;
|
||||
using Impostor.Api.Net.Messages;
|
||||
using Impostor.Hazel;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Impostor.Server.Net.State
|
||||
@@ -79,7 +80,7 @@ namespace Impostor.Server.Net.State
|
||||
|
||||
if (player == null || player.Game != this)
|
||||
{
|
||||
var clientPlayer = new ClientPlayer(client, this);
|
||||
var clientPlayer = new ClientPlayer(_serviceProvider.GetRequiredService<ILogger<ClientPlayer>>(), client, this);
|
||||
|
||||
if (!_clientManager.Validate(client))
|
||||
{
|
||||
|
||||
@@ -6,6 +6,7 @@ using Impostor.Api.Innersloth.Data;
|
||||
using Impostor.Api.Net;
|
||||
using Impostor.Api.Net.Messages;
|
||||
using Impostor.Hazel;
|
||||
using Impostor.Server.Net.Hazel;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Impostor.Server.Net.State
|
||||
@@ -27,6 +28,8 @@ namespace Impostor.Server.Net.State
|
||||
await InitGameDataAsync(player);
|
||||
}
|
||||
|
||||
player.InitializeSpawnTimeout();
|
||||
|
||||
await _eventManager.CallAsync(new PlayerJoinedGameEvent(this, player));
|
||||
}
|
||||
|
||||
@@ -73,6 +76,18 @@ namespace Impostor.Server.Net.State
|
||||
|
||||
await _eventManager.CallAsync(new PlayerLeftGameEvent(this, player, isBan));
|
||||
|
||||
// Player can refuse to be kicked and keep the connection open, check for this.
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(Constants.ConnectionTimeout);
|
||||
|
||||
if (player.Client.Connection.IsConnected)
|
||||
{
|
||||
_logger.LogInformation("{0} - Player {1} ({2}) kept connection open after leaving, disposing.", Code, player.Client.Name, playerId);
|
||||
((HazelConnection) player.Client.Connection).DisposeInnerConnection();
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -42,11 +42,11 @@ namespace Impostor.Tools.ServerReplay
|
||||
|
||||
private static async Task Main(string[] args)
|
||||
{
|
||||
// Log.Logger = new LoggerConfiguration()
|
||||
// // .MinimumLevel.Verbose()
|
||||
// .MinimumLevel.Debug()
|
||||
// .WriteTo.Console()
|
||||
// .CreateLogger();
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
// .MinimumLevel.Verbose()
|
||||
.MinimumLevel.Debug()
|
||||
.WriteTo.Console()
|
||||
.CreateLogger();
|
||||
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
|
||||
|
||||
Reference in new issue
Block a user