Skip to content

Commit

Permalink
Fixes #115 and #118 - Proper SetInfected packet body
Browse files Browse the repository at this point in the history
  • Loading branch information
AeonLucid committed Nov 12, 2020
1 parent 03b0dd4 commit 2ea270d
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 25 deletions.
12 changes: 10 additions & 2 deletions src/Impostor.Api/Games/IGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Impostor.Api.Innersloth;
using Impostor.Api.Net;
using Impostor.Api.Net.Inner;
using Impostor.Api.Net.Inner.Objects;
using Impostor.Api.Net.Messages;

namespace Impostor.Api.Games
Expand Down Expand Up @@ -49,9 +50,16 @@ public interface IGame
/// Syncs the internal <see cref="GameOptionsData"/> to all players.
/// Necessary to do if you modified it, otherwise it won't be used.
/// </summary>
/// <returns></returns>
/// <returns>A <see cref="ValueTask"/> representing the asynchronous operation.</returns>
ValueTask SyncSettingsAsync();

/// <summary>
/// Sets the specified list as Impostor on all connected players.
/// </summary>
/// <param name="players">List of players to be Impostor.</param>
/// <returns>A <see cref="ValueTask"/> representing the asynchronous operation.</returns>
ValueTask SetInfectedAsync(IEnumerable<IInnerPlayerControl> players);

/// <summary>
/// Send the message to all players.
/// </summary>
Expand All @@ -77,4 +85,4 @@ public interface IGame
/// <returns>A <see cref="ValueTask"/> representing the asynchronous operation.</returns>
ValueTask SendToAsync(IMessageWriter writer, int id);
}
}
}
4 changes: 2 additions & 2 deletions src/Impostor.Api/Net/IClientPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface IClientPlayer
/// </summary>
LimboStates Limbo { get; set; }

IInnerPlayerControl Character { get; }
IInnerPlayerControl? Character { get; }

public bool IsHost { get; }

Expand All @@ -40,4 +40,4 @@ public interface IClientPlayer

ValueTask BanAsync();
}
}
}
12 changes: 5 additions & 7 deletions src/Impostor.Api/Net/Inner/Objects/IInnerPlayerControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ namespace Impostor.Api.Net.Inner.Objects
{
public interface IInnerPlayerControl : IInnerNetObject
{
/// <summary>
/// Gets the <see cref="PlayerId"/> assigned by the client of the host of the game.
/// </summary>
byte PlayerId { get; }

/// <summary>
/// Gets the <see cref="IInnerPlayerPhysics"/> of the <see cref="IInnerPlayerControl"/>.
/// Contains vent logic.
Expand Down Expand Up @@ -100,13 +105,6 @@ public interface IInnerPlayerControl : IInnerNetObject
/// <returns>Task that must be awaited.</returns>
ValueTask SendChatToPlayerAsync(string text, IInnerPlayerControl? player = null);

/// <summary>
/// Sets the current to infected (impostor) <see cref="IInnerPlayerControl"/>.
/// Visible to all players.
/// </summary>
/// <returns>Task that must be awaited.</returns>
ValueTask SetInfectedAsync();

/// <summary>
/// Sets the current to be murdered <see cref="IInnerPlayerControl"/>.
/// Visible to all players.
Expand Down
3 changes: 1 addition & 2 deletions src/Impostor.Server/Net/Inner/Objects/InnerGameData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public InnerGameData(ILogger<InnerGameData> logger, Game game, IServiceProvider
return _allPlayers.TryGetValue(id, out var player) ? player : null;
}

public override ValueTask HandleRpc(ClientPlayer sender, ClientPlayer? target, RpcCalls call,
IMessageReader reader)
public override ValueTask HandleRpc(ClientPlayer sender, ClientPlayer? target, RpcCalls call, IMessageReader reader)
{
switch (call)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,5 @@ public async ValueTask SetMurderedAsync()
writer.Write((byte)NetId);
await _game.FinishRpcAsync(writer);
}

public async ValueTask SetInfectedAsync()
{
var writer = _game.StartRpc(NetId, RpcCalls.SetInfected);
writer.Write((byte)NetId);
await _game.FinishRpcAsync(writer);
}
}
}
4 changes: 2 additions & 2 deletions src/Impostor.Server/Net/State/ClientPlayer.Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ internal partial class ClientPlayer
IGame IClientPlayer.Game => Game;

/// <inheritdoc />
IInnerPlayerControl IClientPlayer.Character => Character;
IInnerPlayerControl? IClientPlayer.Character => Character;
}
}
}
2 changes: 1 addition & 1 deletion src/Impostor.Server/Net/State/ClientPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ClientPlayer(ILogger<ClientPlayer> logger, ClientBase client, Game game)
/// <inheritdoc />
public LimboStates Limbo { get; set; }

public InnerPlayerControl Character { get; internal set; }
public InnerPlayerControl? Character { get; internal set; }

public bool IsHost => Game?.Host == this;

Expand Down
32 changes: 30 additions & 2 deletions src/Impostor.Server/Net/State/Game.Api.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.IO;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Impostor.Api;
using Impostor.Api.Games;
using Impostor.Api.Innersloth;
using Impostor.Api.Net;
using Impostor.Api.Net.Inner;
using Impostor.Api.Net.Inner.Objects;
using Impostor.Server.Net.Inner;

namespace Impostor.Server.Net.State
Expand All @@ -22,6 +25,11 @@ public void BanIp(IPAddress ipAddress)

public async ValueTask SyncSettingsAsync()
{
if (Host.Character == null)
{
throw new ImpostorException("Attempted to set infected when the host was not spawned.");
}

using (var writer = StartRpc(Host.Character.NetId, RpcCalls.SyncSettings))
{
// Someone will probably forget to do this, so we include it here.
Expand All @@ -38,5 +46,25 @@ await using (var writerBin = new BinaryWriter(memory))
await FinishRpcAsync(writer);
}
}

public async ValueTask SetInfectedAsync(IEnumerable<IInnerPlayerControl> players)
{
if (Host.Character == null)
{
throw new ImpostorException("Attempted to set infected when the host was not spawned.");
}

using (var writer = StartRpc(Host.Character.NetId, RpcCalls.SetInfected))
{
writer.Write((byte)Host.Character.NetId);

foreach (var player in players)
{
writer.Write((byte)player.PlayerId);
}

await FinishRpcAsync(writer);
}
}
}
}
}

0 comments on commit 2ea270d

Please sign in to comment.