using System;
using System.Collections.Generic;
using AppGame.Sync;
using DotRecast.Core.Collections;
using JNGame.Math;
using JNGame.Sync.Entity;
using JNGame.Sync.Frame.Entity.Components;
using JNGame.Sync.State.Tile;
using JNGame.Sync.State.Tile.Entity;
using NotImplementedException = System.NotImplementedException;
namespace JNGame.Sync.System.Data
{
public interface ISTileDataSystem
{
///
/// 清除指定区域数据
///
public void ClearTileData(int index);
///
/// 获取有权限的全部字节
///
public Dictionary GetHostDataBytes(Func filter = null);
///
/// 获取有权限的全部字节 (过滤条件 : 需同步从服务器)
///
public Dictionary GetHostDataBytesFilterSlave(Func filter = null);
///
/// 获取指定区块的全部字节
///
public Dictionary GetTileDataBytes(int index,Func filter = null);
}
public abstract class ISTileData : ISStateData
{
public JNTileEntity Entity;
///
/// 绑定实体到数据
///
///
public virtual void BindEntity(JNTileEntity entity)
{
Entity = entity;
Id = entity.Id;
}
///
/// 获取数据位置(用于区块清除)
///
public abstract LVector3 GetDataPosition();
}
///
/// 支持区块的数据类
///
public abstract class STileDataSystem : SStateDataSystem,ISTileDataSystem where T : ISTileData,new() where E : JNTileEntity, new()
{
public abstract JNTileContext NodeContext { get; }
public JNSSTileServerService TileSync => Sync as JNSSTileServerService;
public bool IsMaster => TileSync is not null && TileSync.IsMaster;
public bool IsSlave => TileSync is not null && TileSync.IsSlave;
protected STileDataSystem(SStateDataEnum type) : base(type)
{
}
public override Dictionary GetLatest()
{
var nodes = new Dictionary();
E[] entities = null;
if (IsMaster)
{
entities = NodeContext.GetHostEntities();
}else if (IsSlave)
{
entities = NodeContext.GetEntities();
}
entities.ForEach(child =>
{
var entity = new T();
entity.BindEntity(child);
nodes.Add(child.Id,entity);
});
return nodes;
}
public override void OnUByteUpdate(Dictionary bytes)
{
base.OnUByteUpdate(bytes);
if (isServer)
{
OnDataSyncContext();
}
}
public override void OnSendUBytes(Dictionary bytes)
{
Dictionary all = bytes;
Dictionary master = new Dictionary();
Dictionary slave = new Dictionary();
all.ForEach(keyValue =>
{
var entity = NodeContext.Query(keyValue.Key);
//给从服务器发送数据
if (IsMaster && entity is not null && entity.IsSyncSlave)
{
slave[keyValue.Key] = keyValue.Value;
}
});
OnSendAllData(all);
OnSendMasterData(master);
OnSendSlaveData(slave);
}
///
/// 发送玩家数据
///
public abstract void OnSendAllData(Dictionary bytes);
///
/// 发送主服务器数据
///
public virtual void OnSendMasterData(Dictionary bytes){}
///
/// 发送从服务器数据
///
public virtual void OnSendSlaveData(Dictionary bytes){}
///
/// 将数据Data同步到Context
///
protected virtual void OnDataSyncContext()
{
Dictionary lIsTileData;
lock (Data)
{
lIsTileData = new Dictionary(Data);
}
NodeContext.GetEntities().ForEach(child =>
{
//如果有则删除
if (lIsTileData.Remove(child.Id,out var data))
{
//同步不属于自己的实体
if (!child.IsHost)
{
//并且同步属性到实体中
child.TileSyncData(data);
}
}
});
//将数据同步到实体中
foreach (var keyValue in lIsTileData)
{
var entity = NodeContext.TileSyncCreate(keyValue.Key);
//如果当前是从服务器则同步的实体都是 从 主服务器 同步给 从服务器
if (IsSlave) entity.IsSyncSlave = true;
entity?.TileSyncData(keyValue.Value);
//将实体绑定到数据中
keyValue.Value.BindEntity(entity);
}
}
//只更新有权限的实体
public override void Update(T data)
{
var entity = NodeContext.Query(data.Id);
if (IsMaster)
{
if (entity is null || !entity.IsHost) return;
}
base.Update(data);
}
public override void Add(T data)
{
var entity = NodeContext.Query(data.Id);
if (IsMaster)
{
if (entity is null || !entity.IsHost) return;
}
base.Add(data);
}
///
/// 判断数据是否在区块内
///
public bool IsTileInside(int tileId,T data)
{
var index = -1;
if (Sync is JNSSTileClientService clientService)
{
index = clientService.GetTileIndex(data.GetDataPosition());
}
if (Sync is JNSSTileServerService serverService)
{
index = serverService.GetTileIndex(data.GetDataPosition());
}
return index == tileId;
}
public void ClearTileData(int index)
{
lock (Data)
{
//需要删除的数据Id
var ids = new List();
Data.ForEach(child =>
{
if (IsTileInside(index,child.Value)) ids.Add(child.Key);
});
//删除数据和实体
ids.ForEach(child =>
{
//销毁实体
Data[child].Entity?.Destroy();
//销毁数据
Data.Remove(child);
});
}
}
public Dictionary GetHostDataBytesFilterSlave(Func filter = null)
{
if (filter is null) filter = entity => true;
return GetHostDataBytes(entity => entity.IsSyncSlave && filter(entity) );
}
public Dictionary GetHostDataBytes(Func filter = null)
{
if (filter is null) filter = entity => true;
var data = new Dictionary();
lock (Data)
{
Data.ForEach(child =>
{
var entity = NodeContext.Query(child.Key);
if (entity is not null && entity.IsHost && filter(entity))
{
data[child.Key] = child.Value.GetByte();
}
});
}
return data;
}
public Dictionary GetTileDataBytes(int index,Func filter = null)
{
if (filter is null) filter = entity => true;
var data = new Dictionary();
lock (Data)
{
Data.ForEach(child =>
{
var entity = NodeContext.Query(child.Key);
if (IsTileInside(index,child.Value) && filter(entity))
{
data[child.Key] = child.Value.GetByte();
}
});
}
return data;
}
}
}