Funkční komunikace mezi jedním clientem a serverem

This commit is contained in:
Perry 2025-12-19 17:54:50 +01:00
commit 3f235f6e04
44 changed files with 1030 additions and 0 deletions

17
PacketLib/EventQueue.cs Normal file
View file

@ -0,0 +1,17 @@
using LiteNetLib.Utils;
namespace PacketLib;
public struct EventQueue : INetSerializable {
public GameEvent[] Events{ get; set; }
public void Serialize(NetDataWriter writer) {
writer.PutArray(Events);
}
public void Deserialize(NetDataReader reader) {
Events = reader.GetArray<GameEvent>();
}
// public GameEvent this[int index] => Events[index];
}

29
PacketLib/GameEvent.cs Normal file
View file

@ -0,0 +1,29 @@
using System.Diagnostics.CodeAnalysis;
using LiteNetLib.Utils;
namespace PacketLib;
#nullable disable
public struct GameEvent : INetSerializable{
public static GameEvent PLAYER_JOIN(int pid) => new(){ID = 0, Args = [pid] };
public static GameEvent PLAYER_LEAVE(int pid) => new(){ID = 1, Args = [pid] };
public static GameEvent SWITCH_CAM(int pid, int camId) => new(){ID = 2, Args = [pid, camId] };
public static GameEvent TOGGLE_MONITOR(int pid, bool state) => new(){ID = 3, Args = [pid, state ? 1 : 0]};
public static GameEvent ENEMY_MOVEMENT(int enemyId, int camId) => new(){ID = -1, Args = [enemyId, camId]};
public int ID{ get; set; }
public bool Hideable => ID < 0;
public int[] Args{ get; private set; }
public void Serialize(NetDataWriter writer) {
writer.Put(ID);
writer.PutArray(Args);
}
public void Deserialize(NetDataReader reader) {
ID = reader.GetInt();
Args = reader.GetIntArray();
}
}

View file

@ -0,0 +1,5 @@
namespace PacketLib;
public class JoinAcceptPacket {
public PlayerState state { get; set; }
}

5
PacketLib/JoinPacket.cs Normal file
View file

@ -0,0 +1,5 @@
namespace PacketLib;
public class JoinPacket {
public string username { get; set; }
}

View file

@ -0,0 +1,12 @@
using LiteNetLib.Utils;
namespace PacketLib;
public static class NestedTypeManager {
public static void AutoRegister(NetPacketProcessor processor) {
processor.RegisterNestedType<PlayerState>();
processor.RegisterNestedType<GameEvent>();
processor.RegisterNestedType<EventQueue>();
processor.RegisterNestedType<PlayerCommand>();
}
}

View file

@ -0,0 +1,12 @@
using System.Xml;
using LiteNetLib.Utils;
namespace PacketLib;
public static class NetDataWriterExtensions {
public static GameEvent GetGameEvent(this NetDataReader reader) {
GameEvent gevent = new();
gevent.Deserialize(reader);
return gevent;
}
}

View file

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="LiteNetLib" Version="1.3.1" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,23 @@
using LiteNetLib.Utils;
namespace PacketLib;
public struct PlayerCommand : INetSerializable {
public static PlayerCommand SWITCH_CAM(int camId) => new(){ID = 0, Args = [camId] };
public static PlayerCommand TOGGLE_MONITOR() => new(){ID = 1, Args = []};
public int ID{ get; set; }
public bool Hideable => ID < 0;
public int[] Args{ get; private set; }
public void Serialize(NetDataWriter writer) {
writer.Put(ID);
writer.PutArray(Args);
}
public void Deserialize(NetDataReader reader) {
ID = reader.GetInt();
Args = reader.GetIntArray();
}
}

View file

@ -0,0 +1,5 @@
namespace PacketLib;
public class PlayerCommandPacket {
public PlayerCommand[] commands { get; set; }
}

29
PacketLib/PlayerState.cs Normal file
View file

@ -0,0 +1,29 @@
using LiteNetLib;
using LiteNetLib.Utils;
namespace PacketLib;
public struct PlayerState : INetSerializable {
public uint pid;
public int camera;
public bool monitorUp;
public void Serialize(NetDataWriter writer) {
writer.Put(pid);
writer.Put(camera);
writer.Put(monitorUp);
}
public void Deserialize(NetDataReader reader) {
pid = reader.GetUInt();
camera = reader.GetInt();
monitorUp = reader.GetBool();
}
}

View file

@ -0,0 +1,11 @@
namespace PacketLib;
public class UpdatePlayerPacket {
// public PlayerState stateP1{ get; set; }
// public PlayerState stateP2{ get; set; }
// TODO: implement anti-desync measures by comparing server and client states
public GameEvent[] events { get; set; }
// public EventQueue eventQueue { get; set; }
}