JisolGame/JNFrame2/Assets/Scripts/AppGame/Sync/Tile/JNGTileServerSystem.cs

292 lines
8.6 KiB
C#
Raw Normal View History

2024-08-31 15:35:12 +08:00
using System.Collections.Generic;
2024-08-17 14:27:18 +08:00
using System.Linq;
using System.Threading.Tasks;
using AppGame.Systems;
2024-08-31 15:35:12 +08:00
using AppGame.Systems.CServer;
2024-08-17 14:27:18 +08:00
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;
2024-08-31 15:35:12 +08:00
using Game.Logic.System.Logic;
using Game.Logic.System.Usual;
2024-08-17 14:27:18 +08:00
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
{
2024-09-13 04:06:25 +08:00
2024-08-17 14:27:18 +08:00
protected List<JNFrameInput> Inputs = new();
//区块Socket
public Dictionary<int, JNGTileClient> Sockets = new ();
2024-08-22 20:37:39 +08:00
//是否开始前尝试连接周围区块去恢复历史数据
public bool isRecover = true;
2024-08-31 15:35:12 +08:00
public override TileMasterSlaveEnum MSRole => TileMasterSlaveEnum.Master;
2024-08-22 20:37:39 +08:00
2024-08-17 14:27:18 +08:00
/// <summary>
/// 初始化服务器
/// </summary>
/// <returns></returns>
protected override async Task OnInit()
{
2024-08-23 10:48:19 +08:00
RandomSize = (await App.GAPI.NSyncTileRandomId).data;
2024-08-17 14:27:18 +08:00
await base.OnInit();
2024-08-22 20:37:39 +08:00
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] 获取到恢复数据成功 正在恢复数据");
2024-08-23 10:48:19 +08:00
var message = new Dictionary<ulong, byte[]>();
2024-08-22 20:37:39 +08:00
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;
2024-09-14 04:37:53 +08:00
child.OnInsertUBytes(message,true);
2024-08-22 20:37:39 +08:00
});
});
}
else
{
Debug.Log("[JNGTileServerSystem] 获取到恢复数据失败");
}
}
Debug.Log("[JNGTileServerSystem] 恢复数据结束");
}
2024-08-17 14:27:18 +08:00
//添加Tile服务器
App.Business.Send((int)NActionEnum.NAddTileServer,new JNAddTileServer()
{
Tile = TID,
Ip = "127.0.0.1",
2024-08-31 15:35:12 +08:00
Port = App.Server.Port,
Master = true
2024-08-17 14:27:18 +08:00
});
//定时更新Socket
Timers.Instance.SetInterval(1f, UpdateTileSocket);
}
2024-08-22 20:37:39 +08:00
2024-08-17 14:27:18 +08:00
public override SLogicSystem[] NewLogicSystems()
{
return new SLogicSystem[]
{
//基础数据
new DInputSystem(), //游戏输入
new DDataSystem(), //游戏数据
//逻辑层
new DMapSystem(), //游戏地图
new DWorldSystem(), //游戏逻辑
new DPlayerSystem(), //玩家逻辑
2024-08-31 15:35:12 +08:00
new DBossSystem(), //Boss逻辑
2024-08-17 14:27:18 +08:00
};
}
public override SDataSystemBase[] NewDataSystems()
{
return new SDataSystemBase[] {
2024-08-31 15:35:12 +08:00
new EDNodeDataSystem(SStateDataEnum.Server), //游戏数据
new EDPlayerDataSystem(SStateDataEnum.Server), //游戏数据
new EDBossDataSystem(SStateDataEnum.Server), //游戏数据
2024-08-17 14:27:18 +08:00
};
}
#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()
{
//插入未处理输入
2024-08-21 16:18:52 +08:00
lock (Inputs)
2024-08-17 14:27:18 +08:00
{
2024-08-21 16:18:52 +08:00
foreach (var input in Inputs)
{
GetSystem<DInputSystem>().Enqueue(input);
}
Inputs.Clear();
2024-08-17 14:27:18 +08:00
}
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();
}
2024-08-22 20:37:39 +08:00
protected async Task<JNGTileClient> AddSocket(int index)
2024-08-17 14:27:18 +08:00
{
2024-08-22 20:37:39 +08:00
if (IsTileConnect(index)) return null;
2024-08-17 14:27:18 +08:00
var client = new JNGTileClient();
Sockets.Add(index,client);
//获取连接
var message = (await App.GAPI.NSyncTileServer(index));
TileServerInfo info = message.data;
if (info is not null)
{
2024-08-21 16:18:52 +08:00
Debug.Log($"[{index}] 连接 Socket");
2024-08-31 15:35:12 +08:00
client.SetRole(JNGClientRole.Player);
2024-08-19 11:51:17 +08:00
client.SetPoint($"{info.ip}:{info.port}");
2024-08-22 20:37:39 +08:00
client.SetTileServer(info.server);
2024-08-17 14:27:18 +08:00
await client.OnInit();
2024-08-22 20:37:39 +08:00
return client;
2024-08-17 14:27:18 +08:00
}
else
{
Sockets.Remove(index);
}
2024-08-22 20:37:39 +08:00
return null;
2024-08-17 14:27:18 +08:00
}
2024-08-22 20:37:39 +08:00
/// <summary>
/// 销毁Socket
/// </summary>
/// <param name="index"></param>
public void RemoveSocket(int index)
2024-08-17 14:27:18 +08:00
{
if (Sockets.TryGetValue(index,out var client))
{
2024-08-21 16:18:52 +08:00
Debug.Log($"[{index}] 卸载 Socket");
2024-08-17 14:27:18 +08:00
client.OnClose();
}
Sockets.Remove(index);
}
2024-08-22 20:37:39 +08:00
public void RemoveSocket(string server)
{
Sockets.ToArray().ForEach(tile =>
{
if (tile.Value.TileServer != server) return;
tile.Value.OnClose();
Sockets.Remove(tile.Key);
});
}
2024-08-17 14:27:18 +08:00
}
}