提交新概念 Tile从服务器

This commit is contained in:
PC-20230316NUNE\Administrator
2024-08-31 15:35:12 +08:00
parent 77db4d7d71
commit d67032e1de
1039 changed files with 57738 additions and 412 deletions

View File

@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using AppGame.Sync;
using DotRecast.Core.Collections;
using JNGame.Math;
using JNGame.Sync.Entity;
@@ -21,13 +23,17 @@ namespace JNGame.Sync.System.Data
/// <summary>
/// 获取有权限的全部字节
/// </summary>
public Dictionary<ulong, byte[]> GetHostDataBytes();
public Dictionary<ulong, byte[]> GetHostDataBytes(Func<JNTileEntity,bool> filter = null);
/// <summary>
/// 获取有权限的全部字节 (过滤条件 : 需同步从服务器)
/// </summary>
public Dictionary<ulong, byte[]> GetHostDataBytesFilterSlave(Func<JNTileEntity,bool> filter = null);
/// <summary>
/// 获取指定区块的全部字节
/// </summary>
public Dictionary<ulong, byte[]> GetTileDataBytes(int index);
public Dictionary<ulong, byte[]> GetTileDataBytes(int index,Func<JNTileEntity,bool> filter = null);
@@ -35,16 +41,17 @@ namespace JNGame.Sync.System.Data
public abstract class ISTileData : ISStateData
{
public abstract bool IsHost { get; }
public JNTileEntity Entity;
/// <summary>
/// 绑定实体到数据 (销毁数据时顺带销毁实体)
/// 绑定实体到数据
/// </summary>
/// <param name="entity"></param>
public void BindEntity(JNTileEntity entity)
public virtual void BindEntity(JNTileEntity entity)
{
Entity = entity;
Id = entity.Id;
}
/// <summary>
@@ -61,7 +68,10 @@ namespace JNGame.Sync.System.Data
{
public abstract JNTileContext<E> 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)
@@ -76,7 +86,42 @@ namespace JNGame.Sync.System.Data
OnDataSyncContext();
}
}
public override void OnSendUBytes(Dictionary<ulong, byte[]> bytes)
{
Dictionary<ulong, byte[]> all = bytes;
Dictionary<ulong, byte[]> master = new Dictionary<ulong, byte[]>();
Dictionary<ulong, byte[]> slave = new Dictionary<ulong, byte[]>();
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);
}
/// <summary>
/// 发送玩家数据
/// </summary>
public abstract void OnSendAllData(Dictionary<ulong, byte[]> bytes);
/// <summary>
/// 发送主服务器数据
/// </summary>
public virtual void OnSendMasterData(Dictionary<ulong, byte[]> bytes){}
/// <summary>
/// 发送从服务器数据
/// </summary>
public virtual void OnSendSlaveData(Dictionary<ulong, byte[]> bytes){}
/// <summary>
/// 将数据Data同步到Context
/// </summary>
@@ -118,12 +163,14 @@ namespace JNGame.Sync.System.Data
//只更新有权限的实体
public override void Update(T data)
{
if (!data.IsHost) return;
var entity = NodeContext.Query(data.Id);
if (entity is null || !entity.IsHost) return;
base.Update(data);
}
public override void Add(T data)
{
if (!data.IsHost) return;
var entity = NodeContext.Query(data.Id);
if (entity is null || !entity.IsHost) return;
base.Add(data);
}
@@ -175,16 +222,27 @@ namespace JNGame.Sync.System.Data
}
public Dictionary<ulong, byte[]> GetHostDataBytes()
public Dictionary<ulong, byte[]> GetHostDataBytesFilterSlave(Func<JNTileEntity,bool> filter = null)
{
if (filter is null) filter = entity => true;
return GetHostDataBytes(entity => entity.IsSyncSlave && filter(entity) );
}
public Dictionary<ulong, byte[]> GetHostDataBytes(Func<JNTileEntity,bool> filter = null)
{
if (filter is null) filter = entity => true;
var data = new Dictionary<ulong, byte[]>();
lock (Data)
{
Data.ForEach(child =>
{
if (child.Value.IsHost)
var entity = NodeContext.Query(child.Key);
if (entity is not null && entity.IsHost && filter(entity))
{
data[child.Key] = child.Value.GetByte();
}
@@ -195,16 +253,19 @@ namespace JNGame.Sync.System.Data
}
public Dictionary<ulong, byte[]> GetTileDataBytes(int index)
public Dictionary<ulong, byte[]> GetTileDataBytes(int index,Func<JNTileEntity,bool> filter = null)
{
if (filter is null) filter = entity => true;
var data = new Dictionary<ulong, byte[]>();
lock (Data)
{
Data.ForEach(child =>
{
if (IsTileInside(index,child.Value))
var entity = NodeContext.Query(child.Key);
if (IsTileInside(index,child.Value) && filter(entity))
{
data[child.Key] = child.Value.GetByte();
}