feat: player/room user data now available on joined event

This commit is contained in:
2024-05-12 10:57:46 +03:00
parent a9e6a3e853
commit 646744c9a1
26 changed files with 328 additions and 143 deletions
+52 -9
View File
@@ -4,20 +4,63 @@ namespace Ragon.Server.Data;
public class RagonData
{
private byte[] _data;
public bool IsDirty { get; set; }
public byte[] Data
private readonly Dictionary<string, byte[]> _data = new Dictionary<string, byte[]>();
public bool IsDirty { get; private set; }
public RagonData()
{
get => _data;
set
}
public void Read(RagonBuffer buffer)
{
var len = buffer.ReadUShort();
for (int i = 0; i < len; i++)
{
_data = value;
IsDirty = true;
var key = buffer.ReadString();
var valueSize = buffer.ReadUShort();
if (valueSize > 0)
{
var value = buffer.ReadBytes(valueSize);
_data[key] = value;
}
else
{
_data[key] = Array.Empty<byte>();
}
}
}
public RagonData(byte[] data)
public void Write(RagonBuffer buffer)
{
_data = data;
buffer.WriteUShort((ushort)_data.Count);
foreach (var prop in _data)
{
buffer.WriteString(prop.Key);
buffer.WriteUShort((ushort)prop.Value.Length);
buffer.WriteBytes(prop.Value);
}
var toDelete = _data
.Where(p => p.Value.Length == 0)
.Select(p => p.Key);
foreach (var prop in toDelete)
_data.Remove(prop);
IsDirty = false;
}
public void Snapshot(RagonBuffer buffer)
{
buffer.WriteUShort((ushort)_data.Count);
foreach (var prop in _data)
{
buffer.WriteString(prop.Key);
buffer.WriteUShort((ushort)prop.Value.Length);
buffer.WriteBytes(prop.Value);
Console.WriteLine($"Key: {prop.Key} Value: {prop.Value.Length}");
}
}
}