JisolGame/JNFrame2/Assets/Scripts/Game/Data/GDataBaseSystem.cs
PC-20230316NUNE\Administrator 77b305be7a 提交
2024-08-31 21:05:29 +08:00

151 lines
4.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using AppGame;
using AppGame.Systems.CServer;
using DotRecast.Core.Collections;
using Google.Protobuf;
using JNGame.Math;
using JNGame.Sync.Entity;
using JNGame.Sync.State.Tile.Entity;
using JNGame.Sync.System;
using JNGame.Sync.System.Data;
using Newtonsoft.Json;
using Plugins.JNGame.Network.Action;
using Plugins.JNGame.Util;
namespace Game.JNGState.Logic.Data
{
[Serializable]
public class GDataValue
{
public DValuePosition Position = null;
}
public class GDataBase<Self,T,N> : ISTileData where Self : GDataBase<Self,T,N> where T : GDataValue,new() where N : JNTileEntity
{
public readonly T Value = new ();
public N Node => Entity as N;
public GDataBase()
{
}
public override void BindEntity(JNTileEntity entity)
{
base.BindEntity(entity);
Value.Position = new DValuePosition()
{
x = Node.Position.x.rawValue,
y = Node.Position.y.rawValue,
z = Node.Position.z.rawValue,
};
}
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()
{
// return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(Value));
return ToUtil.ObjectToBytes(Value);
}
public sealed override byte[] GetByteDifference(ISData diffValue = null)
{
var diff = GetDifference(diffValue);
if (diff is not null)
{
// return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(diff));
return ToUtil.ObjectToBytes(diff);
}
else
{
return Array.Empty<byte>();
}
}
public virtual T GetDifference(ISData diffValue = null)
{
var diff = diffValue as Self;
if (diff is null || IsEquals(diffValue)) return null;
var value = new T();
if (diff.Value.Position is not null) value.Position = diff.Value.Position;
return value;
}
public sealed override void UByte(byte[] bytes)
{
if (bytes.Length == 0) return;
// var value = JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(bytes));
var value = ToUtil.BytesToObject<T>(bytes);
UData(value);
}
/// <summary>
/// 更新数据
/// </summary>
public virtual void UData(T data)
{
if (data.Position is not null) Value.Position = data.Position;
}
public override LVector3 GetDataPosition()
{
return Value.Position.ToLVector3();
}
}
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()
{
protected GDataBaseSystem(SStateDataEnum type) : base(type)
{
}
public override void OnSendAllData(Dictionary<ulong, byte[]> bytes)
{
JNStateItemData player = new JNStateItemData(); //玩家状态数据
player.NetID = NetID;
foreach (var byteItem in bytes)
{
var data = new JNStateData()
{
Data = ByteString.CopyFrom(byteItem.Value)
};
player.Messages.Add(byteItem.Key,data);
}
//给玩家发送状态信息
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);
}
}
}