This commit is contained in:
PC-20230316NUNE\Administrator
2024-09-29 20:30:29 +08:00
parent c5700ce655
commit a16d51d033
151 changed files with 69 additions and 63 deletions

View File

@@ -0,0 +1,123 @@
using System;
using Cysharp.Threading.Tasks;
using DotRecast.Core.Collections;
using Game.Input;
using Game.JNGFrame.Logic;
using Game.JNGFrame.Logic.Entity;
using Game.JNGFrame.View;
using Game.JNGState.Logic.Data;
using Game.Logic.System.Logic;
using Game.Logic.System.Usual;
using JNGame.Sync.Entity;
using JNGame.Sync.Frame;
using JNGame.Sync.System;
using JNGame.Sync.System.Data;
using Plugins.JNGame.Network.Action;
using UnityEngine;
namespace AppGame.Sync
{
/// <summary>
/// 帧同步游戏
/// </summary>
public class JNGFrameSystem : JNSyncFrameService
{
public override SLogicSystem[] NewLogicSystems()
{
return new SLogicSystem[]
{
//基础数据
new DInputSystem(), //游戏输入
new DDataSystem(), //游戏数据
//帧同步逻辑层
new DMapSystem(), //游戏地图
new DWorldSystem(), //游戏逻辑
};
}
public override SDataSystemBase[] NewDataSystems()
{
return new SDataSystemBase[] {
new EDNodeDataSystem(SStateDataEnum.ServerClient), //游戏数据
};
}
public override SViewSystem[] NewViewSystems()
{
return new SViewSystem[]
{
//视图层
new DViewSystem(), //游戏视图
};
}
public override bool IsStartGame => true;
public override JNContexts CreateContexts()
{
return new EDContexts();
}
protected override void OnRunSimulate()
{
if (!(NFrameQueue.TryDequeue(out var frame))) return;
//插入当前输入
frame.Messages.ForEach(child =>
{
GetSystem<DInputSystem>().Enqueue(child);
});
Simulate();
}
/// <summary>
/// 获取输入
/// </summary>
/// <returns></returns>
protected override JNFrameInputs GetInputs()
{
return GetSystem<DInputSystem>().Dequeue();
}
/// <summary>
/// 发送输入
/// </summary>
/// <param name="inputs"></param>
protected override void OnSendInput(JNFrameInputs inputs)
{
//发送帧数据给服务端
App.Business.Send((int)NActionEnum.NSyncFrameInput,inputs);
}
/// <summary>
/// 追帧
/// </summary>
/// <param name="start"></param>
/// <param name="end"></param>
/// <returns></returns>
protected override async UniTask<JNFrameInfos> OnServerData(int start, int end)
{
Debug.Log($"OnServerData - {start}");
try
{
var data = (await App.API.GetByte($"/sync/frame?start={start}"));
if (data is { Length: > 0 })
{
JNFrameInfos info = JNFrameInfos.Parser.ParseFrom(data);
Debug.Log($"OnServerData - {start} {end} 结束");
return info;
}
}
catch(Exception e)
{
// ignored
Debug.LogError(e.Message);
}
return new JNFrameInfos();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e596ac8b6f4144ba89fdd7ca984dd7f1
timeCreated: 1712663552

View File

@@ -0,0 +1,108 @@
using System.Collections.Generic;
using Game.Input;
using Game.JNGFrame.Logic;
using Game.JNGFrame.Logic.Entity;
using Game.JNGFrame.View;
using Game.JNGState.Logic.Data;
using Game.Logic.System;
using Game.Logic.System.Logic;
using Game.Logic.System.Usual;
using JNGame.Sync.Entity;
using JNGame.Sync.State;
using JNGame.Sync.System;
using JNGame.Sync.System.Data;
using Plugins.JNGame.Network.Action;
namespace AppGame.Sync
{
/// <summary>
/// 状态同步[服务器]
/// </summary>
public class JNGStateServerSystem : JNSStateServerService
{
protected List<JNFrameInput> Inputs = new();
public override SLogicSystem[] NewLogicSystems()
{
return new SLogicSystem[]
{
//基础数据
new DInputSystem(), //游戏输入
new DDataSystem(), //游戏数据
//逻辑层
new DMapSystem(), //游戏地图
new DWorldSystem(), //游戏逻辑
new DPlayerSystem(), //玩家逻辑
new DBossSystem(), //Boss逻辑
};
}
public override SDataSystemBase[] NewDataSystems()
{
return new SDataSystemBase[] {
new EDNodeDataSystem(SStateDataEnum.ServerClient), //游戏数据
new EDPlayerDataSystem(SStateDataEnum.ServerClient), //游戏数据
};
}
public override SViewSystem[] NewViewSystems()
{
return new SViewSystem[]
{
//视图层
new DViewSystem(), //游戏视图
};
}
public override JNContexts CreateContexts()
{
return new EDContexts();
}
protected override void OnRunSimulate()
{
//插入未处理输入
foreach (var input in Inputs)
{
GetSystem<DInputSystem>().Enqueue(input);
}
Inputs.Clear();
base.OnRunSimulate();
//发送输入
OnSendInput();
}
/// <summary>
/// 发送输入 (正常服务器是不需要发送输入的 这里用于测试)
/// </summary>
protected void OnSendInput()
{
var inputs = GetSystem<DInputSystem>().Dequeue();
if (inputs.Inputs.Count > 0)
{
//发送帧数据给服务端
App.Business.Send((int)NActionEnum.NSyncFrameInput,inputs);
}
}
/// <summary>
/// 添加输入
/// </summary>
public void AddInput(JNFrameInfo info)
{
foreach (var input in info.Messages)
{
Inputs.Add(input);
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0dbc5b160b164d41bb2138f2ed76ebb3
timeCreated: 1721811852

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 75baa1cf11cc44c2bb1e10fb46f74e50
timeCreated: 1722493271

View File

@@ -0,0 +1,241 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using AppGame.Systems;
using DotRecast.Core.Collections;
using Game.Input;
using Game.JNGFrame.View;
using Game.JNGState.Logic.Data;
using JNGame.Math;
using JNGame.Sync.State.Tile;
using JNGame.Sync.System;
using JNGame.Sync.System.Data;
using JNGame.Util;
using Plugins.JNGame.Network.Action;
using UnityEngine;
namespace AppGame.Sync
{
public class JNGTileClientSystem : JNSSTileClientService
{
//区块Socket
public Dictionary<int, JNGClient> Sockets = new ();
//玩家位置 和 区块
public LVector3? PlayerPos;
public int? PlayerTile;
public override void Initialize()
{
base.Initialize();
//默认玩家位置
SetPlayerPosition(LVector3.Zero);
//定时更新Socket
Timers.Instance.SetInterval(1f, UpdateTileSocket);
}
protected override int[][] Tiles => new[]
{
new[] { 1, 2, 3 },
new[] { 4, 5, 6 },
new[] { 7, 8, 9 },
};
protected override int TileSize => 100;
public override SLogicSystem[] NewLogicSystems()
{
return new SLogicSystem[]
{
//基础数据
new DInputSystem(), //游戏输入
};
}
public override SDataSystemBase[] NewDataSystems()
{
return new SDataSystemBase[] {
new EDNodeDataSystem(SStateDataEnum.Client), //游戏数据
new EDPlayerDataSystem(SStateDataEnum.Client), //游戏数据
new EDBossDataSystem(SStateDataEnum.Client), //游戏数据
};
}
public override SViewSystem[] NewViewSystems()
{
return new SViewSystem[]
{
//视图层
new DViewSystem(), //游戏视图
};
}
protected override void OnRunSimulate()
{
//更新玩家位置
UpdatePlayerPosition();
base.OnRunSimulate();
//发送输入
OnSendInput();
}
/// <summary>
/// 发送输入 (正常服务器是不需要发送输入的 这里用于测试)
/// </summary>
private void OnSendInput()
{
var inputs = GetSystem<DInputSystem>().Dequeue();
if (inputs.Inputs.Count > 0)
{
//发送帧数据给服务端
JNStateTileInputs tileInputs = new JNStateTileInputs()
{
TId = PlayerTile ?? (Sockets.Keys.Count > 0 ? Sockets.Keys.Last() : 0),
Message = inputs
};
App.Client.Send((int)NActionEnum.NSyncTileInput,tileInputs);
}
}
/// <summary>
/// 设置玩家位置
/// </summary>
public void SetPlayerPosition(LVector3 pos)
{
PlayerPos = pos;
int index = GetTileIndex(pos);
if (PlayerTile != index)
{
PlayerTile = index;
App.Event.Dispatch(GEvent.GSwPlayerTile);
}
}
/// <summary>
/// 更新玩家位置
/// </summary>
public void UpdatePlayerPosition()
{
if (PlayerPos is null) return;
List<int> ids = GetTileGridIndex(PlayerPos.Value);
ids.ForEach(AddTileShow);
TileShow.ToArray().ForEach(id =>
{
if (!(ids.Contains(id)))
{
RemoveTileShow(id);
}
});
}
/// <summary>
/// 更新区块Socket
/// </summary>
public void UpdateTileSocket()
{
TileShow.ForEach(index =>
{
if (!IsTileConnect(index))
{
AddSocket(index);
}
});
var keysToRemove = Sockets.Keys.Where(key => !TileShow.Contains(key)).ToList();
foreach (var key in keysToRemove)
{
RemoveSocket(key);
}
}
/// <summary>
/// 判断是否当前区块是否连接
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public bool IsTileConnect(int index)
{
return Sockets.ContainsKey(index);
}
protected async Task AddSocket(int index,TileServerInfo info = null)
{
if (IsTileConnect(index)) return;
var client = new JNGClient();
Sockets.Add(index,client);
//获取连接
if (info is null)
{
var message = (await App.GAPI.NSyncTileServer(index));
info = message.data;
}
if (info is not null)
{
client.SetPoint($"{info.ip}:{info.port}");
client.SetTileServer(info.server);
if (IsTileConnect(index))
{
Debug.Log($"[{index}] 连接 Socket");
App.Client.AddClient(client);
}
}
else
{
Sockets.Remove(index);
}
}
public async Task SwSocket(int index,TileServerInfo info)
{
RemoveSocket(index);
await AddSocket(index, info);
}
protected void RemoveSocket(int index)
{
if (Sockets.TryGetValue(index,out var client))
{
Debug.Log($"[{index}] 卸载 Socket");
App.Client.RemoveClient(client);
}
Sockets.Remove(index);
//并且释放数据
GetSystems<ISTileDataSystem>().ForEach(data =>
{
data.ClearTileData(index);
});
}
public void RemoveSocket(string server)
{
Sockets.ToArray().ForEach(tile =>
{
if (tile.Value.TileServer != server) return;
tile.Value.OnClose();
Sockets.Remove(tile.Key);
//并且释放数据
GetSystems<ISTileDataSystem>().ForEach(data =>
{
data.ClearTileData(tile.Key);
});
});
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a158f1d590a642988611d229ddaa6a1c
timeCreated: 1722493289

View File

@@ -0,0 +1,292 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AppGame.Systems;
using AppGame.Systems.CServer;
using Cysharp.Threading.Tasks;
using DotRecast.Core.Collections;
using Game.Input;
using Game.JNGFrame.Logic.Entity;
using Game.JNGFrame.View;
using Game.JNGState.Logic.Data;
using Game.Logic.System.Logic;
using Game.Logic.System.Usual;
using JNGame.Sync.State.Tile;
using JNGame.Sync.State.Tile.Entity;
using JNGame.Sync.System;
using JNGame.Sync.System.Data;
using JNGame.Util;
using Plugins.JNGame.Network.Action;
using UnityEngine;
namespace AppGame.Sync
{
/// <summary>
/// 瓦片状态同步[服务器]
/// </summary>
public class JNGTileServerSystem : JNSSTileServerService
{
protected List<JNFrameInput> Inputs = new();
//区块Socket
public Dictionary<int, JNGTileClient> Sockets = new ();
//是否开始前尝试连接周围区块去恢复历史数据
public bool isRecover = true;
public override TileMasterSlaveEnum MSRole => TileMasterSlaveEnum.Master;
/// <summary>
/// 初始化服务器
/// </summary>
/// <returns></returns>
protected override async Task OnInit()
{
RandomSize = (await App.GAPI.NSyncTileRandomId).data;
await base.OnInit();
if (isRecover)
{
List<int> tileIds = GetTileGridIndex(TID);
foreach (var tileId in tileIds)
{
var client = await AddSocket(tileId);
if (client is null) continue;
//连接到附近区块 开始 恢复数据
Debug.Log($"[JNGTileServerSystem] 连接到附近区块{tileId} 开始 恢复数据");
var tileInfo = await client.NSyncTileGetTileInfo(TID);
if (tileInfo is not null)
{
Debug.Log("[JNGTileServerSystem] 获取到恢复数据成功 正在恢复数据");
var message = new Dictionary<ulong, byte[]>();
tileInfo.Data.Data.ForEach(frame =>
{
message.Clear();
foreach (var data in frame.Messages)
{
message.Add(data.Key,data.Value.Data.ToByteArray());
}
GetSystems<ISStateDataSystem>().ForEach(child =>
{
if (child.NetID != frame.NetID) return;
child.OnInsertUBytes(message,true);
});
});
}
else
{
Debug.Log("[JNGTileServerSystem] 获取到恢复数据失败");
}
}
Debug.Log("[JNGTileServerSystem] 恢复数据结束");
}
//添加Tile服务器
App.Business.Send((int)NActionEnum.NAddTileServer,new JNAddTileServer()
{
Tile = TID,
Ip = "127.0.0.1",
Port = App.Server.Port,
Master = true
});
//定时更新Socket
Timers.Instance.SetInterval(1f, UpdateTileSocket);
}
public override SLogicSystem[] NewLogicSystems()
{
return new SLogicSystem[]
{
//基础数据
new DInputSystem(), //游戏输入
new DDataSystem(), //游戏数据
//逻辑层
new DMapSystem(), //游戏地图
new DWorldSystem(), //游戏逻辑
new DPlayerSystem(), //玩家逻辑
new DBossSystem(), //Boss逻辑
};
}
public override SDataSystemBase[] NewDataSystems()
{
return new SDataSystemBase[] {
new EDNodeDataSystem(SStateDataEnum.Server), //游戏数据
new EDPlayerDataSystem(SStateDataEnum.Server), //游戏数据
new EDBossDataSystem(SStateDataEnum.Server), //游戏数据
};
}
#if UNITY_EDITOR
/// <summary>
/// 编辑器显示视图层
/// </summary>
/// <returns></returns>
public override SViewSystem[] NewViewSystems()
{
return new SViewSystem[]
{
//视图层
new DViewSystem(), //游戏视图
};
}
#endif
protected override JNTileContexts CreateTileContexts()
{
return new EDContexts();
}
protected override int[][] Tiles => new[]
{
new[] { 1, 2, 3 },
new[] { 4, 5, 6 },
new[] { 7, 8, 9 },
};
protected override int TileSize => 100;
protected override async UniTask<int> FetchTileId()
{
// await UniTask.NextFrame();
// return TileId++;
var message = await App.GAPI.NSyncTileId;
return message.data;
}
protected override void OnRunSimulate()
{
//插入未处理输入
lock (Inputs)
{
foreach (var input in Inputs)
{
GetSystem<DInputSystem>().Enqueue(input);
}
Inputs.Clear();
}
base.OnRunSimulate();
}
/// <summary>
/// 添加输入
/// </summary>
public void AddInput(JNStateTileInputs info)
{
lock (Inputs)
{
info.Message.Inputs.ForEach(child =>
{
Inputs.Add(child);
});
}
}
/// <summary>
/// 更新区块Socket
/// </summary>
public void UpdateTileSocket()
{
//获取周围TileId
List<int> grid = GetTileGridIndex(TID);
grid.Remove(TID);
grid.ForEach(index =>
{
if (!IsTileConnect(index))
{
AddSocket(index);
}
});
}
/// <summary>
/// 判断是否当前区块是否连接
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public bool IsTileConnect(int index)
{
return Sockets.ContainsKey(index);
}
/// <summary>
/// 获取当前连接的区块列表
/// </summary>
/// <returns></returns>
public override int[] GetLinkTiles()
{
return Sockets.Keys.Where(key => Sockets[key].IsOpen).ToArray();
}
protected async Task<JNGTileClient> AddSocket(int index)
{
if (IsTileConnect(index)) return null;
var client = new JNGTileClient();
Sockets.Add(index,client);
//获取连接
var message = (await App.GAPI.NSyncTileServer(index));
TileServerInfo info = message.data;
if (info is not null)
{
Debug.Log($"[{index}] 连接 Socket");
client.SetRole(JNGClientRole.Player);
client.SetPoint($"{info.ip}:{info.port}");
client.SetTileServer(info.server);
await client.OnInit();
return client;
}
else
{
Sockets.Remove(index);
}
return null;
}
/// <summary>
/// 销毁Socket
/// </summary>
/// <param name="index"></param>
public void RemoveSocket(int index)
{
if (Sockets.TryGetValue(index,out var client))
{
Debug.Log($"[{index}] 卸载 Socket");
client.OnClose();
}
Sockets.Remove(index);
}
public void RemoveSocket(string server)
{
Sockets.ToArray().ForEach(tile =>
{
if (tile.Value.TileServer != server) return;
tile.Value.OnClose();
Sockets.Remove(tile.Key);
});
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 34430b78df32409ea2142a49f0e36683
timeCreated: 1722241862

View File

@@ -0,0 +1,202 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AppGame.Systems;
using AppGame.Systems.CServer;
using Cysharp.Threading.Tasks;
using DotRecast.Core.Collections;
using Game.Input;
using Game.JNGFrame.Logic;
using Game.JNGFrame.Logic.Entity;
using Game.JNGFrame.View;
using Game.JNGState.Logic.Data;
using Game.Logic.System;
using Game.Logic.System.Logic;
using Game.Logic.System.Usual;
using JNGame.Sync.State.Tile;
using JNGame.Sync.State.Tile.Entity;
using JNGame.Sync.System;
using JNGame.Sync.System.Data;
using JNGame.Util;
using Plugins.JNGame.Network.Action;
using UnityEngine;
namespace AppGame.Sync
{
/// <summary>
/// 瓦片状态同步[从服务器]
/// </summary>
public class JNGTileSlaveServerSystem : JNSSTileServerService
{
protected List<JNFrameInput> Inputs = new();
protected override int[][] Tiles => new[]
{
new[] { 1, 2, 3 },
new[] { 4, 5, 6 },
new[] { 7, 8, 9 },
};
protected override int TileSize => 100;
/// <summary>
/// 主服务器
/// </summary>
public JNGTileClient Master { get; private set; }
public override TileMasterSlaveEnum MSRole => TileMasterSlaveEnum.Slave;
public override SLogicSystem[] NewLogicSystems()
{
return new SLogicSystem[]
{
//基础数据
new DInputSystem(), //游戏输入
new DDataSystem(), //游戏数据
//逻辑层
new DMapSystem(), //游戏地图
new DWorldSystem(), //游戏逻辑
new DPlayerSystem(), //玩家逻辑
new DBossSystem(), //Boss逻辑
};
}
public override SDataSystemBase[] NewDataSystems()
{
return new SDataSystemBase[] {
new EDNodeDataSystem(SStateDataEnum.Server), //游戏数据
new EDPlayerDataSystem(SStateDataEnum.Server), //游戏数据
new EDBossDataSystem(SStateDataEnum.Server), //游戏数据
};
}
// #if UNITY_EDITOR
/// <summary>
/// 编辑器显示视图层
/// </summary>
/// <returns></returns>
public override SViewSystem[] NewViewSystems()
{
return new SViewSystem[]
{
//视图层
new DViewSystem(), //游戏视图
};
}
// #endif
protected override JNTileContexts CreateTileContexts()
{
return new EDContexts();
}
/// <summary>
/// 初始化服务器
/// </summary>
/// <returns></returns>
protected override async Task OnInit()
{
RandomSize = (await App.GAPI.NSyncTileRandomId).data;
await base.OnInit();
//添加Tile从服务器
App.Business.Send((int)NActionEnum.NAddTileServer,new JNAddTileServer()
{
Tile = TID,
Ip = "127.0.0.1",
Port = App.Server.Port,
Master = false
});
//更新主服务器
Timers.Instance.SetInterval(1f, UpdateTileSocket);
}
protected override async UniTask<int> FetchTileId()
{
await UniTask.NextFrame();
return 1;
}
protected override void OnRunSimulate()
{
//插入未处理输入
lock (Inputs)
{
foreach (var input in Inputs)
{
GetSystem<DInputSystem>().Enqueue(input);
}
Inputs.Clear();
}
base.OnRunSimulate();
}
/// <summary>
/// 添加输入
/// </summary>
public void AddInput(JNStateTileInputs info)
{
lock (Inputs)
{
info.Message.Inputs.ForEach(child =>
{
Inputs.Add(child);
});
}
}
/// <summary>
/// 连接主服务器
/// </summary>
private void UpdateTileSocket()
{
//如果有连接则直接返回
if (Master is not null) return;
//连接主服务器
OnMasterConnect();
}
private async void OnMasterConnect()
{
var message = (await App.GAPI.NSyncTileServer(TID));
if (Master is not null) return;
if (message.data is null) return;
Master = new JNGTileClient();
var info = message.data;
Debug.Log($"[JNGTileSlaveServerSystem {TID}] 连接 Socket");
Master.SetRole(JNGClientRole.SlaveServer);
Master.SetPoint($"{info.ip}:{info.port}");
Master.SetTileServer(info.server);
await Master.OnInit();
}
/// <summary>
/// 删除Socket
/// </summary>
public void RemoveSocket(string server)
{
if (Master is not null && Master.TileServer == server)
{
Master.OnClose();
Master = null;
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 234cd20f90624e6aa602e3ce4cd5c800
timeCreated: 1724639704