Skip to content

Commit

Permalink
Split ShipStatus to multiple classes, implement new door types
Browse files Browse the repository at this point in the history
  • Loading branch information
js6pak committed Apr 4, 2021
1 parent 225b56e commit 13bddeb
Show file tree
Hide file tree
Showing 34 changed files with 419 additions and 190 deletions.
2 changes: 1 addition & 1 deletion src/Impostor.Api/Innersloth/Customization/ColorType.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Impostor.Api.Innersloth.Customization
{
public enum ColorType : byte
public enum ColorType
{
Red = 0,
Blue = 1,
Expand Down
1 change: 1 addition & 0 deletions src/Impostor.Api/Net/Inner/IGameNet.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Impostor.Api.Net.Inner.Objects;
using Impostor.Api.Net.Inner.Objects.ShipStatus;

namespace Impostor.Api.Net.Inner
{
Expand Down
6 changes: 5 additions & 1 deletion src/Impostor.Api/Net/Inner/IInnerNetObject.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
namespace Impostor.Api.Net.Inner
using Impostor.Api.Games;

namespace Impostor.Api.Net.Inner
{
public interface IInnerNetObject
{
public uint NetId { get; }

public int OwnerId { get; }

public IGame Game { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Impostor.Api.Net.Inner.Objects.ShipStatus
{
public interface IInnerAirshipStatus : IInnerShipStatus
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Impostor.Api.Net.Inner.Objects.ShipStatus
{
public interface IInnerMiraShipStatus : IInnerShipStatus
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Impostor.Api.Net.Inner.Objects.ShipStatus
{
public interface IInnerPolusShipStatus : IInnerShipStatus
{
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Impostor.Api.Net.Inner.Objects
namespace Impostor.Api.Net.Inner.Objects.ShipStatus
{
public interface IInnerShipStatus : IInnerNetObject
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Impostor.Api.Net.Inner.Objects.ShipStatus
{
public interface IInnerSkeldShipStatus : IInnerShipStatus
{
}
}
1 change: 0 additions & 1 deletion src/Impostor.Api/Net/Inner/RpcCalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public enum RpcCalls : byte
CloseDoorsOfType = 27,
RepairSystem = 28,
SetTasks = 29,
UpdateGameData = 30,
ClimbLadder = 31,
UsePlatform = 32,
}
Expand Down
15 changes: 15 additions & 0 deletions src/Impostor.Server/Net/Inner/InnerNetObject.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using Impostor.Api.Games;
using Impostor.Api.Net;
using Impostor.Api.Net.Inner;
using Impostor.Api.Net.Messages;
Expand All @@ -10,10 +11,19 @@ internal abstract partial class InnerNetObject : GameObject, IInnerNetObject
{
private const int HostInheritId = -2;

protected InnerNetObject(Game game)
{
Game = game;
}

public uint NetId { get; internal set; }

public int OwnerId { get; internal set; }

public Game Game { get; }

IGame IInnerNetObject.Game => Game;

public SpawnFlags SpawnFlags { get; internal set; }

public bool IsOwnedBy(IClientPlayer player)
Expand All @@ -27,5 +37,10 @@ public bool IsOwnedBy(IClientPlayer player)
public abstract ValueTask DeserializeAsync(IClientPlayer sender, IClientPlayer? target, IMessageReader reader, bool initialState);

public abstract ValueTask<bool> HandleRpcAsync(ClientPlayer sender, ClientPlayer? target, RpcCalls call, IMessageReader reader);

public virtual ValueTask OnSpawnAsync()
{
return default;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ public async ValueTask SnapToAsync(Vector2 position)
var minSid = (ushort)(_lastSequenceId + 5U);

// Snap in the server.
await SnapToAsync(_game.GetClientPlayer(OwnerId)!, position, minSid);
await SnapToAsync(Game.GetClientPlayer(OwnerId)!, position, minSid);

// Broadcast to all clients.
using (var writer = _game.StartRpc(NetId, RpcCalls.SnapTo))
using (var writer = Game.StartRpc(NetId, RpcCalls.SnapTo))
{
writer.Write(position);
writer.Write(_lastSequenceId);
await _game.FinishRpcAsync(writer);
await Game.FinishRpcAsync(writer);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,15 @@ internal partial class InnerCustomNetworkTransform : InnerNetObject
{
private readonly ILogger<InnerCustomNetworkTransform> _logger;
private readonly InnerPlayerControl _playerControl;
private readonly Game _game;
private readonly IEventManager _eventManager;
private readonly ObjectPool<PlayerMovementEvent> _pool;

private ushort _lastSequenceId;

public InnerCustomNetworkTransform(ILogger<InnerCustomNetworkTransform> logger, InnerPlayerControl playerControl, Game game, IEventManager eventManager, ObjectPool<PlayerMovementEvent> pool)
public InnerCustomNetworkTransform(Game game, ILogger<InnerCustomNetworkTransform> logger, InnerPlayerControl playerControl, IEventManager eventManager, ObjectPool<PlayerMovementEvent> pool) : base(game)
{
_logger = logger;
_playerControl = playerControl;
_game = game;
_game = game;
_eventManager = eventManager;
_pool = pool;
}
Expand Down Expand Up @@ -106,7 +103,7 @@ internal async ValueTask SetPositionAsync(IClientPlayer sender, Vector2 position
Velocity = velocity;

var playerMovementEvent = _pool.Get();
playerMovementEvent.Reset(_game, sender, _playerControl);
playerMovementEvent.Reset(Game, sender, _playerControl);
await _eventManager.CallAsync(playerMovementEvent);
_pool.Return(playerMovementEvent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ internal partial class InnerPlayerPhysics : InnerNetObject
private readonly ILogger<InnerPlayerPhysics> _logger;
private readonly InnerPlayerControl _playerControl;
private readonly IEventManager _eventManager;
private readonly Game _game;

public InnerPlayerPhysics(ILogger<InnerPlayerPhysics> logger, InnerPlayerControl playerControl, IEventManager eventManager, Game game)
public InnerPlayerPhysics(Game game, ILogger<InnerPlayerPhysics> logger, InnerPlayerControl playerControl, IEventManager eventManager) : base(game)
{
_logger = logger;
_playerControl = playerControl;
_eventManager = eventManager;
_game = game;
}

public override ValueTask<bool> SerializeAsync(IMessageWriter writer, bool initialState)
Expand All @@ -39,27 +37,39 @@ public override ValueTask DeserializeAsync(IClientPlayer sender, IClientPlayer?

public override async ValueTask<bool> HandleRpcAsync(ClientPlayer sender, ClientPlayer? target, RpcCalls call, IMessageReader reader)
{
if (!await ValidateOwnership(call, sender) || !await ValidateImpostor(call, sender, _playerControl.PlayerInfo))
if (!await ValidateOwnership(call, sender))
{
return false;
}

int ventId;

switch (call)
{
case RpcCalls.EnterVent:
Rpc19EnterVent.Deserialize(reader, out ventId);
await _eventManager.CallAsync(new PlayerVentEvent(_game, sender, _playerControl, (VentLocation)ventId, true));
break;

case RpcCalls.ExitVent:
Rpc19EnterVent.Deserialize(reader, out ventId);
await _eventManager.CallAsync(new PlayerVentEvent(_game, sender, _playerControl, (VentLocation)ventId, false));
if (!await ValidateImpostor(call, sender, _playerControl.PlayerInfo))
{
return false;
}

int ventId;

switch (call)
{
case RpcCalls.EnterVent:
Rpc19EnterVent.Deserialize(reader, out ventId);
break;
case RpcCalls.ExitVent:
Rpc20ExitVent.Deserialize(reader, out ventId);
break;
default:
throw new ArgumentOutOfRangeException(nameof(call), call, null);
}

await _eventManager.CallAsync(new PlayerVentEvent(Game, sender, _playerControl, (VentLocation)ventId, call == RpcCalls.EnterVent));
break;

case RpcCalls.ClimbLadder:
Rpc31ClimbLadder.Deserialize(reader, out byte ladderId, out byte lastClimbLadderSid);
Rpc31ClimbLadder.Deserialize(reader, out var ladderId, out var lastClimbLadderSid);
break;

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class InnerVoteBanSystem : InnerNetObject, IInnerVoteBanSystem
private readonly ILogger<InnerVoteBanSystem> _logger;
private readonly Dictionary<int, int[]> _votes;

public InnerVoteBanSystem(ILogger<InnerVoteBanSystem> logger)
public InnerVoteBanSystem(Game game, ILogger<InnerVoteBanSystem> logger) : base(game)
{
_logger = logger;
_votes = new Dictionary<int, int[]>();
Expand Down
53 changes: 21 additions & 32 deletions src/Impostor.Server/Net/Inner/Objects/InnerGameData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@ namespace Impostor.Server.Net.Inner.Objects
internal partial class InnerGameData : InnerNetObject, IInnerGameData
{
private readonly ILogger<InnerGameData> _logger;
private readonly Game _game;
private readonly ConcurrentDictionary<byte, InnerPlayerInfo> _allPlayers;

public InnerGameData(ILogger<InnerGameData> logger, Game game, IServiceProvider serviceProvider)
public InnerGameData(Game game, ILogger<InnerGameData> logger, IServiceProvider serviceProvider) : base(game)
{
_logger = logger;
_game = game;
_allPlayers = new ConcurrentDictionary<byte, InnerPlayerInfo>();

Components.Add(this);
Components.Add(ActivatorUtilities.CreateInstance<InnerVoteBanSystem>(serviceProvider));
Components.Add(ActivatorUtilities.CreateInstance<InnerVoteBanSystem>(serviceProvider, game));
}

public int PlayerCount => _allPlayers.Count;
Expand Down Expand Up @@ -76,8 +74,25 @@ public override async ValueTask DeserializeAsync(IClientPlayer sender, IClientPl
}
else
{
// It'll send an message per dirty player
// throw new NotImplementedException("This shouldn't happen, according to Among Us disassembly.");
while (reader.Position < reader.Length)
{
var inner = reader.ReadMessage();
var playerInfo = this.GetPlayerById(inner.Tag);
if (playerInfo != null)
{
playerInfo.Deserialize(inner);
}
else
{
playerInfo = new InnerPlayerInfo(inner.Tag);
playerInfo.Deserialize(inner);

if (!_allPlayers.TryAdd(playerInfo.PlayerId, playerInfo))
{
throw new ImpostorException("Failed to add player to InnerGameData.");
}
}
}
}
}

Expand All @@ -97,32 +112,6 @@ public override async ValueTask<bool> HandleRpcAsync(ClientPlayer sender, Client
break;
}

case RpcCalls.UpdateGameData:
{
while (reader.Position < reader.Length)
{
using var message = reader.ReadMessage();
var player = GetPlayerById(message.Tag);
if (player != null)
{
player.Deserialize(message);
}
else
{
var playerInfo = new InnerPlayerInfo(message.Tag);

playerInfo.Deserialize(reader);

if (!_allPlayers.TryAdd(playerInfo.PlayerId, playerInfo))
{
throw new ImpostorException("Failed to add player to InnerGameData.");
}
}
}

break;
}

default:
return await UnregisteredCall(call, sender);
}
Expand Down
7 changes: 1 addition & 6 deletions src/Impostor.Server/Net/Inner/Objects/InnerLobbyBehaviour.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Threading.Tasks;
using Impostor.Api.Games;
using Impostor.Api.Net;
using Impostor.Api.Net.Inner;
using Impostor.Api.Net.Inner.Objects;
Expand All @@ -11,12 +10,8 @@ namespace Impostor.Server.Net.Inner.Objects
{
internal class InnerLobbyBehaviour : InnerNetObject, IInnerLobbyBehaviour
{
private readonly IGame _game;

public InnerLobbyBehaviour(IGame game)
public InnerLobbyBehaviour(Game game) : base(game)
{
_game = game;

Components.Add(this);
}

Expand Down
14 changes: 5 additions & 9 deletions src/Impostor.Server/Net/Inner/Objects/InnerMeetingHud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,14 @@ internal partial class InnerMeetingHud : InnerNetObject
{
private readonly ILogger<InnerMeetingHud> _logger;
private readonly IEventManager _eventManager;
private readonly Game _game;
private readonly GameNet _gameNet;

[AllowNull]
private PlayerVoteArea[] _playerStates;

public InnerMeetingHud(ILogger<InnerMeetingHud> logger, IEventManager eventManager, Game game)
public InnerMeetingHud(Game game, ILogger<InnerMeetingHud> logger, IEventManager eventManager) : base(game)
{
_logger = logger;
_eventManager = eventManager;
_game = game;
_gameNet = game.GameNet;
_playerStates = null;

Components.Add(this);
Expand Down Expand Up @@ -132,7 +128,7 @@ public override async ValueTask<bool> HandleRpcAsync(ClientPlayer sender, Client

private void PopulateButtons(byte reporter)
{
_playerStates = _gameNet.GameData!.Players
_playerStates = Game.GameNet.GameData!.Players
.Select(x =>
{
var area = new PlayerVoteArea(this, x.Key);
Expand All @@ -146,15 +142,15 @@ private async ValueTask HandleVotingComplete(ClientPlayer sender, ReadOnlyMemory
{
if (playerId != byte.MaxValue)
{
var player = _game.GameNet.GameData!.GetPlayerById(playerId);
var player = Game.GameNet.GameData!.GetPlayerById(playerId);
if (player?.Controller != null)
{
player.Controller.Die(DeathReason.Exile);
await _eventManager.CallAsync(new PlayerExileEvent(_game, sender, player.Controller));
await _eventManager.CallAsync(new PlayerExileEvent(Game, sender, player.Controller));
}
}

await _eventManager.CallAsync(new MeetingEndedEvent(_game, this));
await _eventManager.CallAsync(new MeetingEndedEvent(Game, this));
}

private async ValueTask<bool> HandleCastVote(ClientPlayer sender, ClientPlayer? target, byte playerId, sbyte suspectPlayerId)
Expand Down
Loading

0 comments on commit 13bddeb

Please sign in to comment.