Skip to content

Commit

Permalink
fix(server): better handling of logs, add missing packet handlers and…
Browse files Browse the repository at this point in the history
… actor action handlers
  • Loading branch information
DanielMcAssey committed Jan 12, 2025
1 parent 1e28117 commit 88a08c1
Show file tree
Hide file tree
Showing 21 changed files with 385 additions and 42 deletions.
67 changes: 63 additions & 4 deletions GLOKON.Baiters.Core/BaitersServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Steamworks.Data;
using System.Collections.Concurrent;
using GLOKON.Baiters.Core.Enums.Networking;
using GLOKON.Baiters.Core.Models.Game;

namespace GLOKON.Baiters.Core
{
Expand Down Expand Up @@ -169,7 +170,6 @@ public bool TryGetPlayer(ulong steamId, out Player? player)

public void KickPlayer(ulong steamId)
{
SendPacket(new("kick"), DataChannel.GameState, steamId);
SendPacket(new("peer_was_kicked")
{
["user_id"] = (long)steamId,
Expand Down Expand Up @@ -269,6 +269,65 @@ public void SendPacket(Packet packet, DataChannel channel, ulong? steamId = null
}
}

public void SetPlayerCosmetics(ulong steamId, Cosmetics cosmetics)
{
if (TryGetPlayer(steamId, out var player) && player != null)
{
player.Cosmetics = cosmetics;
var cosmeticsPkt = new Dictionary<string, object>()
{
["title"] = cosmetics.Title,
["eye"] = cosmetics.Eye,
["nose"] = cosmetics.Nose,
["mouth"] = cosmetics.Mouth,
["undershirt"] = cosmetics.Undershirt,
["overshirt"] = cosmetics.Overshirt,
["legs"] = cosmetics.Legs,
["hat"] = cosmetics.Hat,
["species"] = cosmetics.Species,
["accessory"] = cosmetics.Accessory,
["pattern"] = cosmetics.Pattern,
["primary_color"] = cosmetics.PrimaryColor,
["secondary_color"] = cosmetics.SecondaryColor,
["tail"] = cosmetics.Tail,
};

if (!string.IsNullOrWhiteSpace(cosmetics.Bobber))
{
cosmeticsPkt.Add("bobber", cosmetics.Bobber);
}

SendPacket(new("actor_action")
{
["action"] = "_update_cosmetics",
["actor_id"] = (long)steamId,
["params"] = new object[] { cosmeticsPkt, },
}, DataChannel.ActorAction);
}
}

public void SetPlayerHeldItem(ulong steamId, HeldItem? item = null)
{
if (TryGetPlayer(steamId, out var player) && player != null)
{
player.HeldItem = item;
var heldItemPkt = new Dictionary<string, object>();
if (item != null)
{
heldItemPkt.Add("id", item.Id);
heldItemPkt.Add("size", item.Size);
heldItemPkt.Add("quality", item.Quality);
}

SendPacket(new("actor_action")
{
["action"] = "_update_held_item",
["actor_id"] = (long)steamId,
["params"] = new object[] { heldItemPkt, },
}, DataChannel.ActorAction);
}
}

public bool IsAdmin(ulong steamId)
{
return options.Admins.Contains(steamId);
Expand Down Expand Up @@ -302,7 +361,7 @@ internal void JoinPlayerLobby(ulong steamId, string playerName)
internal virtual void JoinPlayer(ulong steamId, long actorId, Player player)
{
AddActor(actorId, player);
player.SetActorId(actorId);
player.ActorId = actorId;
Log.Information("[{0}] {1} joined the server", steamId, player.FisherName);
SendWebLobbyPacket(steamId);
UpdatePlayerCount();
Expand Down Expand Up @@ -349,7 +408,7 @@ internal void SendActor(long actorId, Actor actor, ulong? steamId = null)
instanceActorParams["rot"] = Vector3.Zero;
}

instanceActorParams["zone"] = "main_zone";
instanceActorParams["zone"] = actor.Zone;
instanceActorParams["zone_owner"] = -1;
instanceActorParams["actor_id"] = actorId;
instanceActorParams["creator_id"] = (long)ServerId;
Expand Down Expand Up @@ -388,7 +447,7 @@ internal void OnPlayerChat(ulong sender, string message)
OnChatMessage?.Invoke(sender, message);
}

Log.Information("[{sender}] {fisherName}: {message}", sender, fisherName, message);
Log.ForContext("Scope", "ChatLog").Information("[{sender}] {fisherName}: {message}", sender, fisherName, message);
}

protected abstract void ReceivePackets();
Expand Down
12 changes: 12 additions & 0 deletions GLOKON.Baiters.Core/Enums/Game/ItemQuality.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace GLOKON.Baiters.Core.Enums.Game
{
public enum ItemQuality
{
Normal = 0,
Shining = 1,
Glistening = 2,
Opulent = 3,
Radiant = 4,
Alpha = 5,
}
}
2 changes: 2 additions & 0 deletions GLOKON.Baiters.Core/Models/Actor/Actor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public abstract class Actor(string type, ulong ownerId = 0)

public ulong OwnerId { get; } = ownerId;

public string Zone { get; set; } = "main_zone";

public uint? DespawnTime
{
get { return _despawnTime; }
Expand Down
10 changes: 5 additions & 5 deletions GLOKON.Baiters.Core/Models/Actor/Player.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GLOKON.Baiters.Core.Constants;
using GLOKON.Baiters.Core.Models.Game;
using System.Numerics;

namespace GLOKON.Baiters.Core.Models.Actor
Expand All @@ -11,11 +12,10 @@ public sealed class Player(ulong steamId, string fisherName, bool isAdmin = fals

public bool IsAdmin { get; } = isAdmin;

public long? ActorId { get; private set; }
public long? ActorId { get; set; }

public void SetActorId(long actorId)
{
ActorId = actorId;
}
public Cosmetics? Cosmetics { get; set; }

public HeldItem? HeldItem { get; set; }
}
}
35 changes: 35 additions & 0 deletions GLOKON.Baiters.Core/Models/Game/Cosmetics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace GLOKON.Baiters.Core.Models.Game
{
public sealed class Cosmetics
{
public required string Title { get; set; }

public required string Eye { get; set; }

public required string Nose { get; set; }

public required string Mouth { get; set; }

public required string Undershirt { get; set; }

public required string Overshirt { get; set; }

public required string Legs { get; set; }

public required string Hat { get; set; }

public required string Species { get; set; }

public required string[] Accessory { get; set; }

public required string Pattern { get; set; }

public required string PrimaryColor { get; set; }

public required string SecondaryColor { get; set; }

public required string Tail { get; set; }

public string? Bobber { get; set; }
}
}
13 changes: 13 additions & 0 deletions GLOKON.Baiters.Core/Models/Game/HeldItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using GLOKON.Baiters.Core.Enums.Game;

namespace GLOKON.Baiters.Core.Models.Game
{
public sealed class HeldItem
{
public required string Id { get; set; }

public required float Size { get; set; }

public ItemQuality Quality { get; set; } = ItemQuality.Normal;
}
}
72 changes: 69 additions & 3 deletions GLOKON.Baiters.Core/Packets/Handlers/ActorActionHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GLOKON.Baiters.Core.Constants;
using GLOKON.Baiters.Core.Enums.Game;
using GLOKON.Baiters.Core.Models.Networking;
using Serilog;

Expand All @@ -8,14 +9,74 @@ internal class ActorActionHandler(BaitersServer server) : IPacketHandler
{
public void Handle(ulong sender, Packet data)
{
switch ((string)data["action"])
string action = (string)data["action"];
long actorId = (long)data["actor_id"];
Array actionParams = (Array)data["params"];

switch (action)
{
case "_update_cosmetics":
return; // TODO: Needs testing
var cosmeticsPkt = (Dictionary<string, object>)(actionParams.GetValue(0) ?? new Dictionary<string, object>());

if (server.TryGetPlayer(sender, out var playerCosmetic) && playerCosmetic != null)
{
playerCosmetic.Cosmetics = new()
{
Title = (string)cosmeticsPkt["title"],
Eye = (string)cosmeticsPkt["eye"],
Nose = (string)cosmeticsPkt["nose"],
Mouth = (string)cosmeticsPkt["mouth"],
Undershirt = (string)cosmeticsPkt["undershirt"],
Overshirt = (string)cosmeticsPkt["overshirt"],
Legs = (string)cosmeticsPkt["legs"],
Hat = (string)cosmeticsPkt["hat"],
Species = (string)cosmeticsPkt["species"],
Accessory = ((Array)cosmeticsPkt["accessory"]).OfType<string>().ToArray(),
Pattern = (string)cosmeticsPkt["pattern"],
PrimaryColor = (string)cosmeticsPkt["primary_color"],
SecondaryColor = (string)cosmeticsPkt["secondary_color"],
Tail = (string)cosmeticsPkt["tail"],
Bobber = (string?)cosmeticsPkt.GetValueOrDefault("bobber"),
};
}
break;
case "_update_held_item":
return; // TODO: Needs testing
var heldItemPkt = (Dictionary<string, object>)(actionParams.GetValue(0) ?? new Dictionary<string, object>());

if (server.TryGetPlayer(sender, out var playerHeldItem) && playerHeldItem != null)
{
playerHeldItem.HeldItem = new() {
Id = (string)heldItemPkt["id"],
Size = (float)heldItemPkt["size"],
Quality = (ItemQuality)heldItemPkt["quality"]
};
}
break;
case "_sync_sound":
case "_talk":
case "_face_emote":
case "_play_particle":
case "_play_sfx":
case "_sync_strum":
case "_sync_hammer":
case "_sync_punch":
case "queue_free":
case "_change_id":
case "_set_state":
case "_flush":
// TODO: Shall we do something with this?
break;
case "_set_zone":
// TODO: Set actor zone
break;
case "_sync_create_bubble":
string chatMssage = (string)(((Array)data["params"]).GetValue(0) ?? -1);
string chatMssage = (string)(actionParams.GetValue(0) ?? -1);
server.OnPlayerChat(sender, chatMssage);
break;
case "_wipe_actor":
long wipeActorId = (long)(((Array)data["params"]).GetValue(0) ?? -1);
long wipeActorId = (long)(actionParams.GetValue(0) ?? -1);
if (server.TryGetActor(wipeActorId, out var actor) && actor != null)
{
if (ActorType.ServerOnly.Contains(actor.Type))
Expand All @@ -27,6 +88,11 @@ public void Handle(ulong sender, Packet data)
server.RemoveActor(wipeActorId);
}
break;


default:
Log.Verbose("Unknown actor action {0}", action);
break;
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions GLOKON.Baiters.Core/Packets/Handlers/LetterReceivedHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using GLOKON.Baiters.Core.Models.Networking;

namespace GLOKON.Baiters.Core.Packets.Handlers
{
internal class LetterReceivedHandler(BaitersServer server) : IPacketHandler

Check warning on line 5 in GLOKON.Baiters.Core/Packets/Handlers/LetterReceivedHandler.cs

View workflow job for this annotation

GitHub Actions / build_and_release

Parameter 'server' is unread.

Check warning on line 5 in GLOKON.Baiters.Core/Packets/Handlers/LetterReceivedHandler.cs

View workflow job for this annotation

GitHub Actions / Build Server (windows-latest, win-x64) / build-server

Parameter 'server' is unread.
{
public void Handle(ulong sender, Packet packet)
{
// TODO: Do we need to do anything?
}
}
}
12 changes: 12 additions & 0 deletions GLOKON.Baiters.Core/Packets/Handlers/LetterWasAcceptedHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using GLOKON.Baiters.Core.Models.Networking;

namespace GLOKON.Baiters.Core.Packets.Handlers
{
internal class LetterWasAcceptedHandler(BaitersServer server) : IPacketHandler

Check warning on line 5 in GLOKON.Baiters.Core/Packets/Handlers/LetterWasAcceptedHandler.cs

View workflow job for this annotation

GitHub Actions / build_and_release

Parameter 'server' is unread.

Check warning on line 5 in GLOKON.Baiters.Core/Packets/Handlers/LetterWasAcceptedHandler.cs

View workflow job for this annotation

GitHub Actions / Build Server (windows-latest, win-x64) / build-server

Parameter 'server' is unread.
{
public void Handle(ulong sender, Packet packet)
{
// TODO: Do we need to do anything?
}
}
}
12 changes: 12 additions & 0 deletions GLOKON.Baiters.Core/Packets/Handlers/LetterWasDeniedHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using GLOKON.Baiters.Core.Models.Networking;

namespace GLOKON.Baiters.Core.Packets.Handlers
{
internal class LetterWasDeniedHandler(BaitersServer server) : IPacketHandler

Check warning on line 5 in GLOKON.Baiters.Core/Packets/Handlers/LetterWasDeniedHandler.cs

View workflow job for this annotation

GitHub Actions / Build Server (ubuntu-latest, linux-x64) / build-server

Parameter 'server' is unread.
{
public void Handle(ulong sender, Packet packet)
{
// TODO: Do we need to do anything?
}
}
}
12 changes: 12 additions & 0 deletions GLOKON.Baiters.Core/Packets/Handlers/PlayerPunchHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using GLOKON.Baiters.Core.Models.Networking;

namespace GLOKON.Baiters.Core.Packets.Handlers
{
internal class PlayerPunchHandler(BaitersServer server) : IPacketHandler

Check warning on line 5 in GLOKON.Baiters.Core/Packets/Handlers/PlayerPunchHandler.cs

View workflow job for this annotation

GitHub Actions / build_and_release

Parameter 'server' is unread.

Check warning on line 5 in GLOKON.Baiters.Core/Packets/Handlers/PlayerPunchHandler.cs

View workflow job for this annotation

GitHub Actions / Build Server (windows-latest, win-x64) / build-server

Parameter 'server' is unread.

Check warning on line 5 in GLOKON.Baiters.Core/Packets/Handlers/PlayerPunchHandler.cs

View workflow job for this annotation

GitHub Actions / Build Server (ubuntu-latest, linux-x64) / build-server

Parameter 'server' is unread.
{
public void Handle(ulong sender, Packet packet)
{
// Nothing to do
}
}
}
12 changes: 12 additions & 0 deletions GLOKON.Baiters.Core/Packets/Handlers/ReceiveWebLobbyHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using GLOKON.Baiters.Core.Models.Networking;

namespace GLOKON.Baiters.Core.Packets.Handlers
{
internal class ReceiveWebLobbyHandler(BaitersServer server) : IPacketHandler

Check warning on line 5 in GLOKON.Baiters.Core/Packets/Handlers/ReceiveWebLobbyHandler.cs

View workflow job for this annotation

GitHub Actions / build_and_release

Parameter 'server' is unread.

Check warning on line 5 in GLOKON.Baiters.Core/Packets/Handlers/ReceiveWebLobbyHandler.cs

View workflow job for this annotation

GitHub Actions / Build Server (windows-latest, win-x64) / build-server

Parameter 'server' is unread.

Check warning on line 5 in GLOKON.Baiters.Core/Packets/Handlers/ReceiveWebLobbyHandler.cs

View workflow job for this annotation

GitHub Actions / Build Server (ubuntu-latest, linux-x64) / build-server

Parameter 'server' is unread.
{
public void Handle(ulong sender, Packet packet)
{
// Nothing to do
}
}
}
12 changes: 12 additions & 0 deletions GLOKON.Baiters.Core/Packets/Handlers/UserJoinedWebLobbyHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using GLOKON.Baiters.Core.Models.Networking;

namespace GLOKON.Baiters.Core.Packets.Handlers
{
internal class UserJoinedWebLobbyHandler(BaitersServer server) : IPacketHandler

Check warning on line 5 in GLOKON.Baiters.Core/Packets/Handlers/UserJoinedWebLobbyHandler.cs

View workflow job for this annotation

GitHub Actions / build_and_release

Parameter 'server' is unread.

Check warning on line 5 in GLOKON.Baiters.Core/Packets/Handlers/UserJoinedWebLobbyHandler.cs

View workflow job for this annotation

GitHub Actions / Build Server (windows-latest, win-x64) / build-server

Parameter 'server' is unread.

Check warning on line 5 in GLOKON.Baiters.Core/Packets/Handlers/UserJoinedWebLobbyHandler.cs

View workflow job for this annotation

GitHub Actions / Build Server (ubuntu-latest, linux-x64) / build-server

Parameter 'server' is unread.
{
public void Handle(ulong sender, Packet packet)
{
// Nothing to do
}
}
}
Loading

0 comments on commit 88a08c1

Please sign in to comment.