Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore players after the MaxPlayers limit is reached ; Crash ignored players on stage changes #54

Merged
merged 4 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions Server/BanLists.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,30 +148,27 @@ private static void Save() {

public static void Crash(
Client user,
bool permanent = false,
bool dispose_user = true,
int delay_ms = 0
int delay_ms = 0
) {
user.Ignored = true;
Task.Run(async () => {
if (delay_ms > 0) {
await Task.Delay(delay_ms);
}
bool permanent = user.Banned;
await user.Send(new ChangeStagePacket {
Id = (permanent ? "$agogus/ban4lyfe" : "$among$us/cr4sh%"),
Stage = (permanent ? "$ejected" : "$agogusStage"),
Scenario = (sbyte) (permanent ? 69 : 21),
SubScenarioType = (byte) (permanent ? 21 : 69),
});
if (dispose_user) {
user.Dispose();
}
});
}

private static void CrashMultiple(string[] args, MUCH much) {
foreach (Client user in much(args).toActUpon) {
Crash(user, true);
user.Banned = true;
Crash(user);
}
}

Expand Down Expand Up @@ -245,8 +242,9 @@ public static string HandleBanCommand(string[] args, MUCH much) {
}

foreach (Client user in res.toActUpon) {
user.Banned = true;
BanClient(user);
Crash(user, true);
Crash(user);
}

Save();
Expand Down
21 changes: 17 additions & 4 deletions Server/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Client : IDisposable {
public readonly ConcurrentDictionary<string, object?> Metadata = new ConcurrentDictionary<string, object?>(); // can be used to store any information about a player
public bool Connected = false;
public bool Ignored = false;
public bool Banned = false;
public CostumePacket? CurrentCostume = null; // required for proper client sync
public string Name {
get => Logger.Name;
Expand Down Expand Up @@ -41,8 +42,9 @@ public Client(Client other, Socket socket) {
}

public void Dispose() {
if (Socket?.Connected is true)
if (Socket?.Connected is true) {
Socket.Disconnect(false);
}
}


Expand All @@ -51,9 +53,14 @@ public async Task Send<T>(T packet, Client? sender = null) where T : struct, IPa

PacketAttribute packetAttribute = Constants.PacketMap[typeof(T)];
try {
// don't send most packets to ignored players
if (Ignored && packetAttribute.Type != PacketType.Init && packetAttribute.Type != PacketType.ChangeStage) {
memory.Dispose();
return;
}
Server.FillPacket(new PacketHeader {
Id = sender?.Id ?? Id,
Type = packetAttribute.Type,
Id = sender?.Id ?? Id,
Type = packetAttribute.Type,
PacketSize = packet.Size
}, packet, memory.Memory);
}
Expand All @@ -69,11 +76,17 @@ public async Task Send<T>(T packet, Client? sender = null) where T : struct, IPa
public async Task Send(Memory<byte> data, Client? sender) {
PacketHeader header = new PacketHeader();
header.Deserialize(data.Span);
if (!Connected && header.Type is not PacketType.Connect) {

if (!Connected && !Ignored && header.Type != PacketType.Connect) {
Server.Logger.Error($"Didn't send {header.Type} to {Id} because they weren't connected yet");
return;
}

// don't send most packets to ignored players
if (Ignored && header.Type != PacketType.Init && header.Type != PacketType.ChangeStage) {
return;
}

await Socket!.SendAsync(data[..(Constants.HeaderSize + header.PacketSize)], SocketFlags.None);
}

Expand Down
19 changes: 17 additions & 2 deletions Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,20 @@ void logError(Task x) {
server.PacketHandler = (c, p) => {
switch (p) {
case GamePacket gamePacket: {
// crash ignored player
if (c.Ignored) {
c.Logger.Info($"Crashing ignored player after entering stage {gamePacket.Stage}.");
BanLists.Crash(c, 500);
return false;
}

// crash player entering a banned stage
if (BanLists.Enabled && BanLists.IsStageBanned(gamePacket.Stage)) {
c.Logger.Warn($"Crashing player for entering banned stage {gamePacket.Stage}.");
BanLists.Crash(c, false, false, 500);
BanLists.Crash(c, 500);
return false;
}

c.Logger.Info($"Got game packet {gamePacket.Stage}->{gamePacket.ScenarioNum}");

// reset lastPlayerPacket on stage changes
Expand Down Expand Up @@ -170,6 +179,11 @@ void logError(Task x) {
break;
}

// ignore all other packets from ignored players
case IPacket pack when c.Ignored: {
return false;
}

case TagPacket tagPacket: {
// c.Logger.Info($"Got tag packet: {tagPacket.IsIt}");
if ((tagPacket.UpdateType & TagPacket.TagUpdate.State) != 0) c.Metadata["seeking"] = tagPacket.IsIt;
Expand All @@ -184,7 +198,7 @@ void logError(Task x) {
break;
}

case CostumePacket costumePacket:
case CostumePacket costumePacket: {
c.Logger.Info($"Got costume packet: {costumePacket.BodyName}, {costumePacket.CapName}");
c.Metadata["lastCostumePacket"] = costumePacket;
c.CurrentCostume = costumePacket;
Expand All @@ -193,6 +207,7 @@ void logError(Task x) {
#pragma warning restore CS4014
c.Metadata["loadedSave"] = true;
break;
}

case ShinePacket shinePacket: {
if (!Settings.Instance.Shines.Enabled) return false;
Expand Down
Loading
Loading