Skip to content
This repository has been archived by the owner on Mar 3, 2021. It is now read-only.

Commit

Permalink
Replace ColorId, HatId, PetId, SkinId with their respective enums
Browse files Browse the repository at this point in the history
  • Loading branch information
js6pak committed Mar 1, 2021
1 parent 80c4d89 commit c09b86c
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 79 deletions.
11 changes: 6 additions & 5 deletions src/Impostor.Api/Innersloth/Customization/HatType.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Impostor.Api.Innersloth.Customization
{
public enum HatType
public enum HatType : uint
{
NoHat = 0,
Astronaut = 1,
Expand Down Expand Up @@ -45,8 +45,8 @@ public enum HatType
ThirdEyeHat = 40,
ToiletPaperHat = 41,
Toppat = 42,
Fedora = 43,
Goggles2 = 44,
BlackFedora = 43,
SkiGoggles = 44,
Headphones = 45,
MaskHat = 46,
PaperMask = 47,
Expand All @@ -58,7 +58,7 @@ public enum HatType
Cheese = 53,
Cherry = 54,
Egg = 55,
Fedora2 = 56,
GreenFedora = 56,
Flamingo = 57,
FlowerPin = 58,
Helmet = 59,
Expand Down Expand Up @@ -95,6 +95,7 @@ public enum HatType
MiniCrewmate = 90,
NinjaMask = 91,
RamHorns = 92,
Snowman2 = 93,
SnowCrewmate = 93,
GeoffHat = 94,
}
}
2 changes: 1 addition & 1 deletion src/Impostor.Api/Innersloth/Customization/PetType.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Impostor.Api.Innersloth.Customization
{
public enum PetType
public enum PetType : uint
{
NoPet = 0,
Alien = 1,
Expand Down
2 changes: 1 addition & 1 deletion src/Impostor.Api/Innersloth/Customization/SkinType.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Impostor.Api.Innersloth.Customization
{
public enum SkinType : byte
public enum SkinType : uint
{
None = 0,
Astro = 1,
Expand Down
24 changes: 4 additions & 20 deletions src/Impostor.Api/Net/Inner/Objects/IInnerPlayerControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,48 +41,32 @@ public interface IInnerPlayerControl : IInnerNetObject
/// Sets the color of the current <see cref="IInnerPlayerControl"/>.
/// Visible to all players.
/// </summary>
/// <param name="colorId">A color for the player.</param>
/// <returns>Task that must be awaited.</returns>
ValueTask SetColorAsync(byte colorId);

/// <param name="colorType">A color for the player.</param>
/// <inheritdoc cref="SetColorAsync(byte)" />
/// <returns>Task that must be awaited.</returns>
ValueTask SetColorAsync(ColorType colorType);

/// <summary>
/// Sets the hat of the current <see cref="IInnerPlayerControl"/>.
/// Visible to all players.
/// </summary>
/// <param name="hatId">An hat for the player.</param>
/// <returns>Task that must be awaited.</returns>
ValueTask SetHatAsync(uint hatId);

/// <param name="hatType">An hat for the player.</param>
/// <inheritdoc cref="SetHatAsync(uint)" />
/// <returns>Task that must be awaited.</returns>
ValueTask SetHatAsync(HatType hatType);

/// <summary>
/// Sets the pet of the current <see cref="IInnerPlayerControl"/>.
/// Visible to all players.
/// </summary>
/// <param name="petId">A pet for the player.</param>
/// <returns>Task that must be awaited.</returns>
ValueTask SetPetAsync(uint petId);

/// <param name="petType">A pet for the player.</param>
/// <inheritdoc cref="SetPetAsync(uint)" />
/// <returns>Task that must be awaited.</returns>
ValueTask SetPetAsync(PetType petType);

/// <summary>
/// Sets the skin of the current <see cref="IInnerPlayerControl"/>.
/// Visible to all players.
/// </summary>
/// <param name="skinId">A skin for the player.</param>
/// <returns>Task that must be awaited.</returns>
ValueTask SetSkinAsync(uint skinId);

/// <param name="skinType">A skin for the player.</param>
/// <inheritdoc cref="SetSkinAsync(uint)" />
/// <returns>Task that must be awaited.</returns>
ValueTask SetSkinAsync(SkinType skinType);

/// <summary>
Expand Down
9 changes: 5 additions & 4 deletions src/Impostor.Api/Net/Inner/Objects/IInnerPlayerInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using Impostor.Api.Innersloth;
using Impostor.Api.Innersloth.Customization;

namespace Impostor.Api.Net.Inner.Objects
{
Expand All @@ -14,22 +15,22 @@ public interface IInnerPlayerInfo
/// <summary>
/// Gets the color of the player.
/// </summary>
byte ColorId { get; }
ColorType Color { get; }

/// <summary>
/// Gets the hat of the player.
/// </summary>
uint HatId { get; }
HatType Hat { get; }

/// <summary>
/// Gets the pet of the player.
/// </summary>
uint PetId { get; }
PetType Pet { get; }

/// <summary>
/// Gets the skin of the player.
/// </summary>
uint SkinId { get; }
SkinType Skin { get; }

/// <summary>
/// Gets a value indicating whether the player is an impostor.
Expand Down
44 changes: 12 additions & 32 deletions src/Impostor.Server/Net/Inner/Objects/InnerPlayerControl.Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,62 +26,42 @@ public async ValueTask SetNameAsync(string name)
await _game.FinishRpcAsync(writer);
}

public async ValueTask SetColorAsync(byte colorId)
public async ValueTask SetColorAsync(ColorType color)
{
PlayerInfo.ColorId = colorId;
PlayerInfo.Color = color;

using var writer = _game.StartRpc(NetId, RpcCalls.SetColor);
writer.Write(colorId);
Rpc08SetColor.Serialize(writer, color);
await _game.FinishRpcAsync(writer);
}

public ValueTask SetColorAsync(ColorType colorType)
public async ValueTask SetHatAsync(HatType hat)
{
return SetColorAsync((byte)colorType);
}

public async ValueTask SetHatAsync(uint hatId)
{
PlayerInfo.HatId = hatId;
PlayerInfo.Hat = hat;

using var writer = _game.StartRpc(NetId, RpcCalls.SetHat);
writer.WritePacked(hatId);
Rpc09SetHat.Serialize(writer, hat);
await _game.FinishRpcAsync(writer);
}

public ValueTask SetHatAsync(HatType hatType)
{
return SetHatAsync((uint)hatType);
}

public async ValueTask SetPetAsync(uint petId)
public async ValueTask SetPetAsync(PetType pet)
{
PlayerInfo.PetId = petId;
PlayerInfo.Pet = pet;

using var writer = _game.StartRpc(NetId, RpcCalls.SetPet);
writer.WritePacked(petId);
Rpc17SetPet.Serialize(writer, pet);
await _game.FinishRpcAsync(writer);
}

public ValueTask SetPetAsync(PetType petType)
public async ValueTask SetSkinAsync(SkinType skin)
{
return SetPetAsync((uint)petType);
}

public async ValueTask SetSkinAsync(uint skinId)
{
PlayerInfo.SkinId = skinId;
PlayerInfo.Skin = skin;

using var writer = _game.StartRpc(NetId, RpcCalls.SetSkin);
writer.WritePacked(skinId);
Rpc10SetSkin.Serialize(writer, skin);
await _game.FinishRpcAsync(writer);
}

public ValueTask SetSkinAsync(SkinType skinType)
{
return SetSkinAsync((uint)skinType);
}

public async ValueTask SendChatAsync(string text)
{
using var writer = _game.StartRpc(NetId, RpcCalls.SendChat);
Expand Down
18 changes: 10 additions & 8 deletions src/Impostor.Server/Net/Inner/Objects/InnerPlayerControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,11 @@ private async ValueTask<bool> HandleSetName(ClientPlayer sender, string name)
return true;
}

private static readonly byte ColorsCount = (byte)Enum.GetValues<ColorType>().Length;

private async ValueTask<bool> HandleCheckColor(ClientPlayer sender, ColorType color)
{
if (color > (ColorType)Enum.GetValues<ColorType>().Length)
if ((byte)color > ColorsCount)
{
if (await sender.Client.ReportCheatAsync(RpcCalls.CheckColor, "Client sent invalid color"))
{
Expand All @@ -415,7 +417,7 @@ private async ValueTask<bool> HandleSetColor(ClientPlayer sender, ColorType colo

if (sender.IsOwner(this))
{
if (_game.Players.Any(x => x.Character != null && x.Character != this && x.Character.PlayerInfo.ColorId == (byte)color))
if (_game.Players.Any(x => x.Character != null && x.Character != this && x.Character.PlayerInfo.Color == color))
{
if (await sender.Client.ReportCheatAsync(RpcCalls.SetColor, "Client sent a color that is already used"))
{
Expand All @@ -433,9 +435,9 @@ private async ValueTask<bool> HandleSetColor(ClientPlayer sender, ColorType colo

var expected = RequestedColorId.Dequeue();

while (_game.Players.Any(x => x.Character != null && x.Character != this && (ColorType)x.Character.PlayerInfo.ColorId == expected))
while (_game.Players.Any(x => x.Character != null && x.Character != this && x.Character.PlayerInfo.Color == expected))
{
expected = (ColorType)(((byte)expected + 1) % Enum.GetValues<ColorType>().Length);
expected = (ColorType)(((byte)expected + 1) % ColorsCount);
}

if (color != expected)
Expand All @@ -446,7 +448,7 @@ private async ValueTask<bool> HandleSetColor(ClientPlayer sender, ColorType colo
}
}

PlayerInfo.ColorId = (byte)color;
PlayerInfo.Color = color;

return true;
}
Expand All @@ -458,7 +460,7 @@ private async ValueTask<bool> HandleSetHat(ClientPlayer sender, HatType hat)
return false;
}

PlayerInfo.HatId = (byte)hat;
PlayerInfo.Hat = hat;

return true;
}
Expand All @@ -470,7 +472,7 @@ private async ValueTask<bool> HandleSetSkin(ClientPlayer sender, SkinType skin)
return false;
}

PlayerInfo.SkinId = (byte)skin;
PlayerInfo.Skin = skin;

return true;
}
Expand Down Expand Up @@ -533,7 +535,7 @@ private async ValueTask<bool> HandleSetPet(ClientPlayer sender, PetType pet)
return false;
}

PlayerInfo.PetId = (byte)pet;
PlayerInfo.Pet = pet;

return true;
}
Expand Down
17 changes: 9 additions & 8 deletions src/Impostor.Server/Net/Inner/Objects/InnerPlayerInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using Impostor.Api.Games;
using Impostor.Api.Innersloth;
using Impostor.Api.Innersloth.Customization;
using Impostor.Api.Net.Messages;

namespace Impostor.Server.Net.Inner.Objects
Expand All @@ -19,13 +20,13 @@ public InnerPlayerInfo(byte playerId)

public string PlayerName { get; internal set; }

public byte ColorId { get; internal set; }
public ColorType Color { get; internal set; }

public uint HatId { get; internal set; }
public HatType Hat { get; internal set; }

public uint PetId { get; internal set; }
public PetType Pet { get; internal set; }

public uint SkinId { get; internal set; }
public SkinType Skin { get; internal set; }

public bool Disconnected { get; internal set; }

Expand Down Expand Up @@ -57,10 +58,10 @@ public void Serialize(IMessageWriter writer)
public void Deserialize(IMessageReader reader)
{
PlayerName = reader.ReadString();
ColorId = reader.ReadByte();
HatId = reader.ReadPackedUInt32();
PetId = reader.ReadPackedUInt32();
SkinId = reader.ReadPackedUInt32();
Color = (ColorType)reader.ReadByte();
Hat = (HatType)reader.ReadPackedUInt32();
Pet = (PetType)reader.ReadPackedUInt32();
Skin = (SkinType)reader.ReadPackedUInt32();
var flag = reader.ReadByte();
Disconnected = (flag & 1) > 0;
IsImpostor = (flag & 2) > 0;
Expand Down

0 comments on commit c09b86c

Please sign in to comment.