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 { /// /// 瓦片状态同步[从服务器] /// public class JNGTileSlaveServerSystem : JNSSTileServerService { protected List Inputs = new(); protected override int[][] Tiles => new[] { new[] { 1, 2, 3 }, new[] { 4, 5, 6 }, new[] { 7, 8, 9 }, }; protected override int TileSize => 100; /// /// 主服务器 /// 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 /// /// 编辑器显示视图层 /// /// public override SViewSystem[] NewViewSystems() { return new SViewSystem[] { //视图层 new DViewSystem(), //游戏视图 }; } // #endif protected override JNTileContexts CreateTileContexts() { return new EDContexts(); } /// /// 初始化服务器 /// /// 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 FetchTileId() { await UniTask.NextFrame(); return 1; } protected override void OnRunSimulate() { //插入未处理输入 lock (Inputs) { foreach (var input in Inputs) { GetSystem().Enqueue(input); } Inputs.Clear(); } base.OnRunSimulate(); } /// /// 添加输入 /// public void AddInput(JNStateTileInputs info) { lock (Inputs) { info.Message.Inputs.ForEach(child => { Inputs.Add(child); }); } } /// /// 连接主服务器 /// 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(); } /// /// 删除Socket /// public void RemoveSocket(string server) { if (Master is not null && Master.TileServer == server) { Master.OnClose(); Master = null; } } } }