mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-11-11 08:38:45 +00:00
提交新概念 Tile从服务器
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using AppGame.Systems;
|
||||
using AppGame.Systems.CServer;
|
||||
using Plugins.JNGame.System;
|
||||
using Plugins.JNGame.Util;
|
||||
using Service;
|
||||
@@ -13,6 +14,8 @@ namespace AppGame
|
||||
Client,
|
||||
Server,
|
||||
ServerClient,
|
||||
SlaveServer,
|
||||
SlaveServerClient,
|
||||
}
|
||||
public class App
|
||||
{
|
||||
@@ -35,6 +38,7 @@ namespace AppGame
|
||||
public static readonly JNGSocket Socket = new JNGSocket();
|
||||
public static readonly JAPI API = new(new JAPIConfig(){BaseURL = "http://127.0.0.1:8080"});
|
||||
public static readonly GAPI GAPI = new GAPI();
|
||||
public static readonly EventDispatcher Event = new();
|
||||
public static int ClientID => Client.ClientID;
|
||||
|
||||
public static SystemBase[] AllSystem()
|
||||
@@ -42,8 +46,8 @@ namespace AppGame
|
||||
return new SystemBase[]
|
||||
{
|
||||
Business,
|
||||
IsServer() || IsSlaveServer() ? Server : null,
|
||||
Client,
|
||||
IsServer() ? Server : null,
|
||||
Resource,
|
||||
Game,
|
||||
// Socket,
|
||||
@@ -52,12 +56,17 @@ namespace AppGame
|
||||
|
||||
public static bool IsClient()
|
||||
{
|
||||
return Env is AppEnv.Client or AppEnv.ServerClient;
|
||||
return Env is AppEnv.Client or AppEnv.ServerClient or AppEnv.SlaveServerClient;
|
||||
}
|
||||
public static bool IsServer()
|
||||
{
|
||||
return Env is AppEnv.Server or AppEnv.ServerClient;
|
||||
}
|
||||
|
||||
public static bool IsSlaveServer()
|
||||
{
|
||||
return Env is AppEnv.SlaveServer or AppEnv.SlaveServerClient;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -9,17 +9,18 @@ using UnityEngine;
|
||||
public class DApplication : MonoBehaviour
|
||||
{
|
||||
|
||||
public GameObject Player1;
|
||||
public GameObject Player;
|
||||
public GameObject Boss;
|
||||
public GameObject VWorld;
|
||||
public CinemachineFreeLook FreeLook;
|
||||
|
||||
private async void Awake()
|
||||
{
|
||||
|
||||
|
||||
await JNetGame.Instance.Init(App.AllSystem());
|
||||
|
||||
//绑定资源
|
||||
App.Resource.Register(VWorld,Player1,FreeLook);
|
||||
App.Resource.Register(VWorld,Player,Boss,FreeLook);
|
||||
|
||||
//开始运行同步
|
||||
if (App.IsServer())
|
||||
@@ -27,9 +28,9 @@ public class DApplication : MonoBehaviour
|
||||
var tileServer = App.Game.StartServer<JNGTileServerSystem>();
|
||||
JNTileServerDebuger.Instance.Add(tileServer);
|
||||
}
|
||||
if (App.IsSlaveServer()) App.Game.StartServer<JNGTileSlaveServerSystem>();
|
||||
if (App.IsClient()) App.Game.StartClient<JNGTileClientSystem>();
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void Update()
|
||||
@@ -43,10 +44,17 @@ public class DApplication : MonoBehaviour
|
||||
input.IsPlayerCreate = true;
|
||||
}
|
||||
|
||||
public void OnClickBossCreate()
|
||||
{
|
||||
var input = App.Game.GetInput<IDWorld>();
|
||||
input.IsBossCreate = true;
|
||||
}
|
||||
|
||||
public void OnClickButton()
|
||||
{
|
||||
var input = App.Game.GetInput<IDWorld>();
|
||||
input.IsAdd = true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Plugins.JNGame.Util;
|
||||
|
||||
@@ -7,9 +8,11 @@ namespace AppGame
|
||||
|
||||
public class TileServerInfo
|
||||
{
|
||||
public int tile;
|
||||
public String server;
|
||||
public String ip;
|
||||
public int port;
|
||||
public bool master;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -23,8 +26,10 @@ namespace AppGame
|
||||
public UniTask<NewsContext<int>> NSyncTileRandomId = App.API.GetNews<int>($"/sync/tile/random/id");
|
||||
//获取端口
|
||||
public UniTask<NewsContext<int>> NSyncTilePort = App.API.GetNews<int>($"/sync/tile/port");
|
||||
//获取默认连接
|
||||
//获取主连接
|
||||
public UniTask<NewsContext<TileServerInfo>> NSyncTileServer(int index) => App.API.GetNews<TileServerInfo>($"/sync/tile/server?index={index}");
|
||||
//获取所有连接
|
||||
public UniTask<NewsContext<List<TileServerInfo>>> NSyncTileListServer(int index) => App.API.GetNews<List<TileServerInfo>>($"/sync/tile/servers?index={index}");
|
||||
//获取玩家Id
|
||||
public UniTask<NewsContext<int>> NSyncTileClientId = App.API.GetNews<int>($"/sync/tile/client/id");
|
||||
|
||||
|
||||
@@ -6,7 +6,10 @@ namespace AppGame
|
||||
{
|
||||
|
||||
//绑定客户端Id
|
||||
BindClientID
|
||||
BindClientID = 10100,
|
||||
|
||||
//绑定客户端角色
|
||||
BindClientRole = 10101,
|
||||
|
||||
}
|
||||
}
|
||||
17
JNFrame2/Assets/Scripts/AppGame/GEvent.cs
Normal file
17
JNFrame2/Assets/Scripts/AppGame/GEvent.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace AppGame
|
||||
{
|
||||
public class GEvent
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 网络 : 有新的Tile服务器
|
||||
/// </summary>
|
||||
public static readonly string NetNewTileServer = "NetNewTileServer";
|
||||
|
||||
/// <summary>
|
||||
/// 游戏 : 切换角色所在的Tile区块
|
||||
/// </summary>
|
||||
public static readonly string GSwPlayerTile = "GSwPlayerTile";
|
||||
|
||||
}
|
||||
}
|
||||
3
JNFrame2/Assets/Scripts/AppGame/GEvent.cs.meta
Normal file
3
JNFrame2/Assets/Scripts/AppGame/GEvent.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fc13d1c3df5342a8a7b6bc3deb29c362
|
||||
timeCreated: 1724924093
|
||||
@@ -24,5 +24,16 @@ namespace AppGame
|
||||
SceneManager.LoadScene(1);
|
||||
}
|
||||
|
||||
public void SetSlaveServerEnv()
|
||||
{
|
||||
App.Env = AppEnv.SlaveServer;
|
||||
SceneManager.LoadScene(1);
|
||||
}
|
||||
public void SetSlaveServerClientEnv()
|
||||
{
|
||||
App.Env = AppEnv.SlaveServerClient;
|
||||
SceneManager.LoadScene(1);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -4,9 +4,10 @@ using DotRecast.Core.Collections;
|
||||
using Game.Input;
|
||||
using Game.JNGFrame.Logic;
|
||||
using Game.JNGFrame.Logic.Entity;
|
||||
using Game.JNGFrame.Logic.System;
|
||||
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;
|
||||
|
||||
@@ -2,10 +2,11 @@
|
||||
using Game.Input;
|
||||
using Game.JNGFrame.Logic;
|
||||
using Game.JNGFrame.Logic.Entity;
|
||||
using Game.JNGFrame.Logic.System;
|
||||
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;
|
||||
@@ -35,6 +36,7 @@ namespace AppGame.Sync
|
||||
new DMapSystem(), //游戏地图
|
||||
new DWorldSystem(), //游戏逻辑
|
||||
new DPlayerSystem(), //玩家逻辑
|
||||
new DBossSystem(), //Boss逻辑
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
@@ -23,8 +23,9 @@ namespace AppGame.Sync
|
||||
//区块Socket
|
||||
public Dictionary<int, JNGClient> Sockets = new ();
|
||||
|
||||
//玩家位置
|
||||
//玩家位置 和 区块
|
||||
public LVector3? PlayerPos;
|
||||
public int? PlayerTile;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
@@ -64,6 +65,7 @@ namespace AppGame.Sync
|
||||
return new SDataSystemBase[] {
|
||||
new EDNodeDataSystem(SStateDataEnum.Client), //游戏数据
|
||||
new EDPlayerDataSystem(SStateDataEnum.Client), //游戏数据
|
||||
new EDBossDataSystem(SStateDataEnum.Client), //游戏数据
|
||||
};
|
||||
}
|
||||
|
||||
@@ -111,6 +113,12 @@ namespace AppGame.Sync
|
||||
public void SetPlayerPosition(LVector3 pos)
|
||||
{
|
||||
PlayerPos = pos;
|
||||
int index = GetTileIndex(pos);
|
||||
if (PlayerTile != index)
|
||||
{
|
||||
PlayerTile = index;
|
||||
App.Event.Dispatch(GEvent.GSwPlayerTile);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -148,7 +156,7 @@ namespace AppGame.Sync
|
||||
});
|
||||
var keysToRemove = Sockets.Keys.Where(key => !TileShow.Contains(key)).ToList();
|
||||
foreach (var key in keysToRemove)
|
||||
{
|
||||
{
|
||||
RemoveSocket(key);
|
||||
}
|
||||
}
|
||||
@@ -163,7 +171,7 @@ namespace AppGame.Sync
|
||||
return Sockets.ContainsKey(index);
|
||||
}
|
||||
|
||||
protected async Task AddSocket(int index)
|
||||
protected async Task AddSocket(int index,TileServerInfo info = null)
|
||||
{
|
||||
if (IsTileConnect(index)) return;
|
||||
|
||||
@@ -171,8 +179,12 @@ namespace AppGame.Sync
|
||||
Sockets.Add(index,client);
|
||||
|
||||
//获取连接
|
||||
var message = (await App.GAPI.NSyncTileServer(index));
|
||||
TileServerInfo info = message.data;
|
||||
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}");
|
||||
@@ -191,19 +203,25 @@ namespace AppGame.Sync
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
//并且释放数据
|
||||
GetSystems<ISTileDataSystem>().ForEach(data =>
|
||||
{
|
||||
data.ClearTileData(index);
|
||||
});
|
||||
}
|
||||
Sockets.Remove(index);
|
||||
//并且释放数据
|
||||
GetSystems<ISTileDataSystem>().ForEach(data =>
|
||||
{
|
||||
data.ClearTileData(index);
|
||||
});
|
||||
}
|
||||
public void RemoveSocket(string server)
|
||||
{
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
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.Logic.System;
|
||||
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;
|
||||
@@ -28,11 +28,6 @@ namespace AppGame.Sync
|
||||
/// </summary>
|
||||
public class JNGTileServerSystem : JNSSTileServerService
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 测试标识
|
||||
/// </summary>
|
||||
private static int TileId = 1;
|
||||
|
||||
protected List<JNFrameInput> Inputs = new();
|
||||
|
||||
@@ -41,6 +36,8 @@ namespace AppGame.Sync
|
||||
|
||||
//是否开始前尝试连接周围区块去恢复历史数据
|
||||
public bool isRecover = true;
|
||||
|
||||
public override TileMasterSlaveEnum MSRole => TileMasterSlaveEnum.Master;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化服务器
|
||||
@@ -100,7 +97,8 @@ namespace AppGame.Sync
|
||||
{
|
||||
Tile = TID,
|
||||
Ip = "127.0.0.1",
|
||||
Port = App.Server.Port
|
||||
Port = App.Server.Port,
|
||||
Master = true
|
||||
});
|
||||
|
||||
//定时更新Socket
|
||||
@@ -121,6 +119,7 @@ namespace AppGame.Sync
|
||||
new DMapSystem(), //游戏地图
|
||||
new DWorldSystem(), //游戏逻辑
|
||||
new DPlayerSystem(), //玩家逻辑
|
||||
new DBossSystem(), //Boss逻辑
|
||||
|
||||
};
|
||||
}
|
||||
@@ -128,8 +127,9 @@ namespace AppGame.Sync
|
||||
public override SDataSystemBase[] NewDataSystems()
|
||||
{
|
||||
return new SDataSystemBase[] {
|
||||
new EDNodeDataSystem(SStateDataEnum.Server), //游戏数据
|
||||
new EDPlayerDataSystem(SStateDataEnum.Server), //游戏数据
|
||||
new EDNodeDataSystem(SStateDataEnum.Server), //游戏数据
|
||||
new EDPlayerDataSystem(SStateDataEnum.Server), //游戏数据
|
||||
new EDBossDataSystem(SStateDataEnum.Server), //游戏数据
|
||||
};
|
||||
}
|
||||
|
||||
@@ -253,6 +253,7 @@ namespace AppGame.Sync
|
||||
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();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 234cd20f90624e6aa602e3ce4cd5c800
|
||||
timeCreated: 1724639704
|
||||
11
JNFrame2/Assets/Scripts/AppGame/Systems/CServer/CServer.cs
Normal file
11
JNFrame2/Assets/Scripts/AppGame/Systems/CServer/CServer.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace AppGame.Systems.CServer
|
||||
{
|
||||
/// <summary>
|
||||
/// 客户端角色
|
||||
/// </summary>
|
||||
public enum JNGClientRole : int
|
||||
{
|
||||
Player, //玩家
|
||||
SlaveServer //从服务器
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 25def782de754dbfb15c0b04eaa50e66
|
||||
timeCreated: 1724642638
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using AppGame.Systems.CServer;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using DotRecast.Core.Collections;
|
||||
using JNGame.Network;
|
||||
@@ -57,10 +58,15 @@ namespace AppGame.Systems
|
||||
private void OnClientConnect(byte[] obj)
|
||||
{
|
||||
//向服务器发送玩家Id
|
||||
Send((int)GActionEnum.BindClientID,new RClientIDMessage()
|
||||
Send((int)GActionEnum.BindClientID,new GBindClientID()
|
||||
{
|
||||
ClientId = ClientId
|
||||
});
|
||||
//向服务器绑定角色
|
||||
Send((int)GActionEnum.BindClientRole,new GBindClientRole()
|
||||
{
|
||||
Role = (int)JNGClientRole.Player
|
||||
});
|
||||
//向服务器索要全量信息
|
||||
Send((int)NActionEnum.NSyncTileAllUpdate);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using JNGame.Network;
|
||||
using Plugins.JNGame.Network;
|
||||
|
||||
namespace AppGame.Systems.CServer
|
||||
{
|
||||
public partial class JNGServer : JNTCPServer
|
||||
{
|
||||
|
||||
//客户端绑定的Id
|
||||
private Dictionary<string, int> ids = new();
|
||||
|
||||
//客户端角色
|
||||
private Dictionary<string, JNGClientRole> _roles = new();
|
||||
public Dictionary<string, JNGClientRole> Roles => _roles;
|
||||
|
||||
public void OnInit_Game()
|
||||
{
|
||||
|
||||
AddListener((int)GActionEnum.BindClientID,OnBindClientID);
|
||||
AddListener((int)GActionEnum.BindClientRole,OnBindClientRole);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 绑定客户端Id
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
private void OnBindClientID(JNServerParam args)
|
||||
{
|
||||
var message = GBindClientID.Parser.ParseFrom(args.Message);
|
||||
ids[args.Client] = message.ClientId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 绑定客户端角色
|
||||
/// </summary>
|
||||
private void OnBindClientRole(JNServerParam args)
|
||||
{
|
||||
|
||||
var message = GBindClientRole.Parser.ParseFrom(args.Message);
|
||||
_roles[args.Client] = (JNGClientRole)message.Role;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 14c921a786654c3fb9bb74e0d0ba29bd
|
||||
timeCreated: 1724642902
|
||||
@@ -1,28 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading.Tasks;
|
||||
using AppGame.Sync;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using DotRecast.Core.Collections;
|
||||
using Google.Protobuf;
|
||||
using JNGame.Network;
|
||||
using JNGame.Sync.System;
|
||||
using JNGame.Sync.State.Tile;
|
||||
using JNGame.Sync.System.Data;
|
||||
using Plugins.JNGame.Network;
|
||||
using Plugins.JNGame.Network.Action;
|
||||
|
||||
namespace AppGame.Systems
|
||||
namespace AppGame.Systems.CServer
|
||||
{
|
||||
public class JNGServer : JNTCPServer
|
||||
|
||||
public partial class JNGServer : JNTCPServer
|
||||
{
|
||||
|
||||
private int _index = 1;
|
||||
|
||||
private bool isInit = false;
|
||||
|
||||
//客户端绑定的Id
|
||||
private Dictionary<string, int> ids = new();
|
||||
|
||||
public override async Task OnInit()
|
||||
{
|
||||
@@ -33,7 +31,8 @@ namespace AppGame.Systems
|
||||
AddListener((int)NActionEnum.NSyncTileInput,OnNSyncTileInput);
|
||||
AddListener((int)NActionEnum.NSyncTileAllUpdate,OnNSyncTileAllUpdate);
|
||||
AddListener((int)NActionEnum.NSyncTileGetTileInfo,OnNSyncTileGetTileInfo);
|
||||
AddListener((int)GActionEnum.BindClientID,OnBindClientID);
|
||||
|
||||
OnInit_Game();
|
||||
|
||||
//连接
|
||||
await base.OnInit();
|
||||
@@ -49,18 +48,6 @@ namespace AppGame.Systems
|
||||
{
|
||||
return (await App.GAPI.NSyncTilePort).data;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 绑定客户端Id
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
private void OnBindClientID(JNServerParam args)
|
||||
{
|
||||
var message = RClientIDMessage.Parser.ParseFrom(args.Message);
|
||||
ids[args.Client] = message.ClientId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 接收帧数入
|
||||
@@ -100,13 +87,16 @@ namespace AppGame.Systems
|
||||
private void OnNSyncTileAllUpdate(JNServerParam args)
|
||||
{
|
||||
|
||||
if (App.Game.Server is not JNGTileServerSystem tileServer) return;
|
||||
if (App.Game.Server is not JNSSTileServerService tileServer) return;
|
||||
|
||||
var allData = new JNStateTileAllData();
|
||||
|
||||
allData.TId = tileServer.TID;
|
||||
allData.Data = new JNStateAllData();
|
||||
|
||||
//获取角色 根据角色 返回 全量数据
|
||||
Roles.TryGetValue(args.Client, out var role);
|
||||
|
||||
tileServer.GetSystems<ISTileDataSystem>().ForEach(data =>
|
||||
{
|
||||
|
||||
@@ -114,7 +104,22 @@ namespace AppGame.Systems
|
||||
allData.Data.Data.Add(item);
|
||||
|
||||
item.NetID = ((ISStateDataSystem)data).NetID;
|
||||
data.GetHostDataBytes().ForEach(keyValue =>
|
||||
|
||||
Dictionary<ulong, byte[]> byteData;
|
||||
switch (role)
|
||||
{
|
||||
case JNGClientRole.Player:
|
||||
byteData = data.GetHostDataBytes();
|
||||
break;
|
||||
case JNGClientRole.SlaveServer:
|
||||
byteData = data.GetHostDataBytesFilterSlave();
|
||||
break;
|
||||
default:
|
||||
byteData = new ();
|
||||
break;
|
||||
}
|
||||
|
||||
byteData.ForEach(keyValue =>
|
||||
{
|
||||
item.Messages[keyValue.Key] = new JNStateData() { Data = ByteString.CopyFrom(keyValue.Value) };
|
||||
});
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading.Tasks;
|
||||
using AppGame.Systems.CServer;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using DotRecast.Core.Collections;
|
||||
using JNGame.Network;
|
||||
using Plugins.JNGame.Network;
|
||||
using Plugins.JNGame.Network.Action;
|
||||
|
||||
namespace AppGame.Systems
|
||||
@@ -19,10 +18,24 @@ namespace AppGame.Systems
|
||||
private string _tileServer;
|
||||
public string TileServer => _tileServer;
|
||||
|
||||
private JNGClientRole _role;
|
||||
public JNGClientRole Role => _role;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 设置IP
|
||||
/// </summary>
|
||||
public void SetPoint(string point)
|
||||
{
|
||||
_point = point;
|
||||
}
|
||||
/// <summary>
|
||||
/// 设置角色
|
||||
/// </summary>
|
||||
public void SetRole(JNGClientRole role)
|
||||
{
|
||||
_role = role;
|
||||
}
|
||||
public void SetTileServer(string tileServer)
|
||||
{
|
||||
_tileServer = tileServer;
|
||||
@@ -49,6 +62,11 @@ namespace AppGame.Systems
|
||||
|
||||
private void OnClientConnect(byte[] obj)
|
||||
{
|
||||
//向服务器绑定角色
|
||||
Send((int)GActionEnum.BindClientRole,new GBindClientRole()
|
||||
{
|
||||
Role = (int)Role
|
||||
});
|
||||
//向服务器索要全量信息
|
||||
Send((int)NActionEnum.NSyncTileAllUpdate);
|
||||
}
|
||||
@@ -79,7 +97,7 @@ namespace AppGame.Systems
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 区块全量更新指定区块
|
||||
/// 获取指定区块状态
|
||||
/// </summary>
|
||||
public async Task<JNStateTileAllData> NSyncTileGetTileInfo(int index)
|
||||
{
|
||||
|
||||
@@ -58,20 +58,12 @@ namespace AppGame.Systems
|
||||
/// <returns></returns>
|
||||
public T GetInput<T>() where T : JNInputBase, new()
|
||||
{
|
||||
return GetClient().GetSystem<JNInputSystem>().Input<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取第一个客户端
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public JNSyncDefaultService GetClient()
|
||||
{
|
||||
return client;
|
||||
return Client.GetSystem<JNInputSystem>().Input<T>();
|
||||
}
|
||||
public T GetClient<T>() where T : JNSyncDefaultService
|
||||
{
|
||||
return (T)client;
|
||||
if (!IsStartClient) return null;
|
||||
return client as T;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -86,11 +78,18 @@ namespace AppGame.Systems
|
||||
|
||||
public void AddTileInput(JNStateTileInputs frame)
|
||||
{
|
||||
if (server is JNGTileServerSystem system)
|
||||
if (server is JNGTileServerSystem system1)
|
||||
{
|
||||
if (system.TID == frame.TId || frame.TId == 0)
|
||||
if (system1.TID == frame.TId || frame.TId == 0)
|
||||
{
|
||||
system.AddInput(frame);
|
||||
system1.AddInput(frame);
|
||||
}
|
||||
}
|
||||
if (server is JNGTileSlaveServerSystem system2)
|
||||
{
|
||||
if (system2.TID == frame.TId || frame.TId == 0)
|
||||
{
|
||||
system2.AddInput(frame);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ public class JNGSocket : JNSocket
|
||||
{
|
||||
|
||||
AddListener((int)NActionEnum.ServerClientDisconnect,OnServerClientDisconnect);
|
||||
AddListener((int)NActionEnum.NAddTileServer,OnNAddTileServer);
|
||||
|
||||
await base.OnInit();
|
||||
}
|
||||
@@ -34,6 +35,16 @@ public class JNGSocket : JNSocket
|
||||
//断开Tile服务器连接
|
||||
(App.Game.Client as JNGTileClientSystem)?.RemoveSocket(disconnect.ClientId);
|
||||
(App.Game.Server as JNGTileServerSystem)?.RemoveSocket(disconnect.ClientId);
|
||||
(App.Game.Server as JNGTileSlaveServerSystem)?.RemoveSocket(disconnect.ClientId);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 有新的Tile服务器
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
private void OnNAddTileServer(byte[] obj)
|
||||
{
|
||||
App.Event.Dispatch(GEvent.NetNewTileServer);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user