178 lines
5.3 KiB
C#
Raw Normal View History

2024-08-22 20:37:39 +08:00
using System;
using System.Collections.Generic;
using AppGame;
2024-08-31 15:35:12 +08:00
using AppGame.Systems.CServer;
2024-08-22 20:37:39 +08:00
using Google.Protobuf;
using JNGame.Math;
using JNGame.Sync.State.Tile.Entity;
using JNGame.Sync.System;
using JNGame.Sync.System.Data;
using Plugins.JNGame.Network.Action;
2024-08-31 15:35:12 +08:00
using Plugins.JNGame.Util;
2024-09-22 03:27:16 +08:00
using TouchSocket.Core;
2024-08-22 20:37:39 +08:00
namespace Game.JNGState.Logic.Data
{
2024-09-22 03:27:16 +08:00
public enum GDataValueCode : int
{
Position = 101
}
2024-08-22 20:37:39 +08:00
[Serializable]
public class GDataValue
{
public DValuePosition Position = null;
}
2024-09-22 03:27:16 +08:00
public abstract class IGDataBase : ISTileData
{
//计入修改
public readonly ConcurrentList<int> WriteCodes = new();
/// <summary>
/// 计入修改
/// </summary>
/// <param name="code"></param>
public void WriteUpdate(int code)
{
if (WriteCodes.Contains(code)) return;
WriteCodes.Add(code);
}
}
2024-08-22 20:37:39 +08:00
2024-09-22 03:27:16 +08:00
public class GDataBase<Self,T,N> : IGDataBase where Self : GDataBase<Self,T,N> where T : GDataValue,new() where N : JNTileEntity
2024-08-22 20:37:39 +08:00
{
public readonly T Value = new ();
public N Node => Entity as N;
public GDataBase()
{
}
2024-08-31 15:35:12 +08:00
public override void BindEntity(JNTileEntity entity)
2024-08-22 20:37:39 +08:00
{
2024-08-31 15:35:12 +08:00
base.BindEntity(entity);
2024-08-22 20:37:39 +08:00
Value.Position = new DValuePosition()
{
2024-08-31 15:35:12 +08:00
x = Node.Position.x.rawValue,
y = Node.Position.y.rawValue,
z = Node.Position.z.rawValue,
2024-08-22 20:37:39 +08:00
};
}
public override bool IsEquals(ISData data)
{
var node = data as Self;
if (node is null) return false;
return Value.Position.Equals(node.Value.Position);
}
public sealed override byte[] GetByte()
{
2024-08-31 15:35:12 +08:00
// return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(Value));
return ToUtil.ObjectToBytes(Value);
2024-08-22 20:37:39 +08:00
}
public sealed override byte[] GetByteDifference(ISData diffValue = null)
{
var diff = GetDifference(diffValue);
if (diff is not null)
{
2024-08-31 15:35:12 +08:00
// return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(diff));
return ToUtil.ObjectToBytes(diff);
2024-08-22 20:37:39 +08:00
}
else
{
return Array.Empty<byte>();
}
}
2024-09-25 23:45:48 +08:00
public T GetDifference(ISData diffValue = null)
2024-08-22 20:37:39 +08:00
{
var diff = diffValue as Self;
if (diff is null || IsEquals(diffValue)) return null;
var value = new T();
2024-09-25 23:45:48 +08:00
Difference(value,diff.Value);
2024-08-22 20:37:39 +08:00
return value;
}
2024-09-25 23:45:48 +08:00
public virtual void Difference(T value, T diff)
{
if (diff.Position is not null && !Equals(Value.Position, diff.Position)) value.Position = diff.Position;
}
2024-08-22 20:37:39 +08:00
public sealed override void UByte(byte[] bytes)
{
if (bytes.Length == 0) return;
2024-08-31 15:35:12 +08:00
// var value = JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(bytes));
var value = ToUtil.BytesToObject<T>(bytes);
2024-08-22 20:37:39 +08:00
UData(value);
}
/// <summary>
/// 更新数据
/// </summary>
public virtual void UData(T data)
{
2024-09-22 03:27:16 +08:00
if (data.Position is not null)
{
Value.Position = data.Position;
WriteUpdate((int)GDataValueCode.Position);
}
2024-08-22 20:37:39 +08:00
}
public override LVector3 GetDataPosition()
{
return Value.Position.ToLVector3();
}
}
2024-08-31 15:35:12 +08:00
public abstract class GDataBaseSystem<T,V,E> : STileDataSystem<T,E> where T : GDataBase<T,V,E>,new() where E : JNTileEntity, new() where V : GDataValue,new()
2024-08-22 20:37:39 +08:00
{
protected GDataBaseSystem(SStateDataEnum type) : base(type)
{
}
2024-08-31 15:35:12 +08:00
public override void OnSendAllData(Dictionary<ulong, byte[]> bytes)
{
JNStateItemData player = new JNStateItemData(); //玩家状态数据
player.NetID = NetID;
2024-08-22 20:37:39 +08:00
foreach (var byteItem in bytes)
{
2024-08-31 15:35:12 +08:00
var data = new JNStateData()
2024-08-22 20:37:39 +08:00
{
Data = ByteString.CopyFrom(byteItem.Value)
2024-08-31 15:35:12 +08:00
};
player.Messages.Add(byteItem.Key,data);
2024-08-22 20:37:39 +08:00
}
2024-08-31 15:35:12 +08:00
//给玩家发送状态信息
App.Server.AllSend((int)NActionEnum.NSyncStateDataUpdate,player,client => App.Server.Roles.TryGetValue(client.Id,out var role) && role == JNGClientRole.Player);
}
public override void OnSendSlaveData(Dictionary<ulong, byte[]> bytes)
{
JNStateItemData slave = new JNStateItemData(); //玩家状态数据
slave.NetID = NetID;
foreach (var byteItem in bytes)
{
var data = new JNStateData()
{
Data = ByteString.CopyFrom(byteItem.Value)
};
slave.Messages.Add(byteItem.Key,data);
}
//给从服务器发送状态信息
App.Server.AllSend((int)NActionEnum.NSyncStateDataUpdate,slave,client => App.Server.Roles.TryGetValue(client.Id,out var role) && role == JNGClientRole.SlaveServer);
2024-08-22 20:37:39 +08:00
}
}
}