238 lines
8.1 KiB
C#
238 lines
8.1 KiB
C#
using Fantasy;
|
||
using Fantasy.Async;
|
||
using Fantasy.Event;
|
||
using NB;
|
||
using NB.Authentication;
|
||
using NB.Chat;
|
||
using NB.Game;
|
||
using NB.Gate;
|
||
|
||
namespace NBF;
|
||
|
||
public sealed class OnCreateSceneEvent : AsyncEventSystem<OnCreateScene>
|
||
{
|
||
private static long _addressableSceneRuntimeId;
|
||
|
||
protected override async FTask Handler(OnCreateScene self)
|
||
{
|
||
var scene = self.Scene;
|
||
// 在这里执行你的初始化逻辑
|
||
Log.Info($"Scene created: SceneType={scene.SceneType}, SceneId={scene.Id} SceneConfigId={scene.SceneConfigId}");
|
||
|
||
switch (scene.SceneType)
|
||
{
|
||
case SceneType.Addressable:
|
||
{
|
||
// 保存 Addressable 场景的 RuntimeId,供其他场景使用
|
||
_addressableSceneRuntimeId = scene.RuntimeId;
|
||
await InitializeAddressableScene(scene);
|
||
break;
|
||
}
|
||
case SceneType.Authentication:
|
||
{
|
||
await InitializeAuthenticationScene(scene);
|
||
break;
|
||
}
|
||
case SceneType.Gate:
|
||
{
|
||
// Gate 场景初始化
|
||
await InitializeGateScene(scene);
|
||
break;
|
||
}
|
||
// case SceneType.Map:
|
||
// {
|
||
// await InitializeMapScene(scene);
|
||
// break;
|
||
// }
|
||
case SceneType.Social:
|
||
{
|
||
// Gate 场景初始化
|
||
await InitializeSocialScene(scene);
|
||
break;
|
||
}
|
||
case SceneType.Game:
|
||
{
|
||
await InitializeGameScene(scene);
|
||
break;
|
||
}
|
||
}
|
||
|
||
await FTask.CompletedTask;
|
||
}
|
||
|
||
private async FTask InitializeAuthenticationScene(Scene scene)
|
||
{
|
||
// 用于鉴权服务器注册和登录相关逻辑的组件
|
||
scene.AddComponent<AuthenticationComponent>().UpdatePosition();
|
||
// 用于颁发ToKen证书相关的逻辑。
|
||
scene.AddComponent<AuthenticationJwtComponent>();
|
||
await FTask.CompletedTask;
|
||
}
|
||
|
||
private async FTask InitializeGateScene(Scene scene)
|
||
{
|
||
// Gate 场景特定的初始化逻辑
|
||
Log.Info($"初始化 Gate 场景: {scene.Id}");
|
||
// 用于管理网关所有连接的组件
|
||
scene.AddComponent<GateUnitManageComponent>();
|
||
// 用于验证JWT是否合法的组件
|
||
scene.AddComponent<GateJWTComponent>();
|
||
|
||
await FTask.CompletedTask;
|
||
|
||
// var roomCode = RoomHelper.GenerateCode(scene.SceneConfigId, 1);
|
||
// Log.Info($"测试 roomCode:{roomCode}");
|
||
// uint serverId = 25255;
|
||
// for (int i = 1; i < 65535; i++)
|
||
// {
|
||
// var roomId = RoomHelper.GenerateCode(serverId, i);
|
||
// RoomHelper.ParseCode(roomId, out var pId, out var rId);
|
||
// Log.Info($"生成id测试,房间id={roomId} 原始服务id:{serverId} 原始房间id={i} 解析={pId} {rId}");
|
||
// }
|
||
|
||
// var tasks = new List<FTask>(2000);
|
||
// var session = scene.GetSession(_addressableSceneRunTimeId);
|
||
// var sceneNetworkMessagingComponent = scene.NetworkMessagingComponent;
|
||
// var g2ATestRequest = new G2A_TestRequest();
|
||
//
|
||
// async FTask Call()
|
||
// {
|
||
// await sceneNetworkMessagingComponent.CallInnerRouteBySession(session,_addressableSceneRunTimeId,g2ATestRequest);
|
||
// }
|
||
//
|
||
// for (int i = 0; i < 100000000000; i++)
|
||
// {
|
||
// tasks.Clear();
|
||
// for (int j = 0; j < tasks.Capacity; ++j)
|
||
// {
|
||
// tasks.Add(Call());
|
||
// }
|
||
// await FTask.WaitAll(tasks);
|
||
// }
|
||
|
||
// // 测试配置表
|
||
// var instanceList = UnitConfigData.Instance.List;
|
||
// var unitConfig = instanceList[0];
|
||
// Log.Debug(instanceList[0].Dic[1]);
|
||
}
|
||
|
||
// private async FTask InitializeMapScene(Scene scene)
|
||
// {
|
||
// // Map 场景特定的初始化逻辑
|
||
// Log.Info($"初始化 Map 场景: {scene.Id}");
|
||
// Log.Info("创建地图场景===");
|
||
// scene.AddComponent<MapUnitManageComponent>();
|
||
// var roomManageComponent = scene.AddComponent<RoomManageComponent>();
|
||
// scene.AddComponent<MapManageComponent>();
|
||
//
|
||
// var room = roomManageComponent.Create(361499030775398402);
|
||
// if (room != null)
|
||
// {
|
||
// roomManageComponent.TestRoomCode = room.Code;
|
||
// Log.Info($"测试房间代码 = {room.Code}");
|
||
// }
|
||
//
|
||
// await FTask.CompletedTask;
|
||
// }
|
||
|
||
private async FTask InitializeSocialScene(Scene scene)
|
||
{
|
||
Log.Info($"初始化 Social 场景: {scene.Id}");
|
||
//用于管理玩家的组件
|
||
scene.AddComponent<SocialUnitManageComponent>();
|
||
scene.AddComponent<MailManageComponent>();
|
||
//聊天
|
||
// 聊天频道中控中心组件。
|
||
scene.AddComponent<ChatChannelCenterComponent>();
|
||
await FTask.CompletedTask;
|
||
}
|
||
|
||
private async FTask InitializeGameScene(Scene scene)
|
||
{
|
||
// Chat 场景特定的初始化逻辑
|
||
Log.Info($"初始化 Game 场景: {scene.Id}");
|
||
//用于管理玩家的组件
|
||
scene.AddComponent<PlayerManageComponent>();
|
||
scene.AddComponent<PlayerBasicCacheManageComponent>();
|
||
|
||
|
||
Log.Info("创建地图场景===");
|
||
var roomManageComponent = scene.AddComponent<RoomManageComponent>();
|
||
scene.AddComponent<MapManageComponent>();
|
||
|
||
var room = roomManageComponent.Create(361499030775398402);
|
||
if (room != null)
|
||
{
|
||
// roomManageComponent.TestRoomCode = room.Code;
|
||
Log.Info($"测试房间代码 = {room.Code}");
|
||
}
|
||
|
||
await FTask.CompletedTask;
|
||
|
||
// var rod = RodConfig.Get(30001);
|
||
// Log.Info("rod config id=" + rod.Id);
|
||
// // Begins transaction
|
||
// using (var session = mongoClient.StartSession())
|
||
// {
|
||
// session.StartTransaction();
|
||
// try
|
||
// {
|
||
// // Creates sample data
|
||
// var book = new Book
|
||
// {
|
||
// Title = "Beloved",
|
||
// Author = "Toni Morrison",
|
||
// InStock = true
|
||
// };
|
||
// var film = new Film
|
||
// {
|
||
// Title = "Star Wars",
|
||
// Director = "George Lucas",
|
||
// InStock = true
|
||
// };
|
||
// // Inserts sample data
|
||
// books.InsertOne(session, book);
|
||
// films.InsertOne(session, film);
|
||
// // Commits our transaction
|
||
// session.CommitTransaction();
|
||
// }
|
||
// catch (Exception e)
|
||
// {
|
||
// Console.WriteLine("Error writing to MongoDB: " + e.Message);
|
||
// return;
|
||
// }
|
||
// // Prints a success message if no error thrown
|
||
// Console.WriteLine("Successfully committed transaction!");
|
||
// }
|
||
|
||
// var client = self.Scene.World.Database;
|
||
|
||
// List<FTask> tasks = new List<FTask>();
|
||
// Stopwatch stopwatch = new Stopwatch();
|
||
// stopwatch.Start();
|
||
// for (int i = 0; i < 100; i++)
|
||
// {
|
||
// var accountId = scene.EntityIdFactory.Create;
|
||
// var account = PlayerFactory.Create(scene, accountId);
|
||
//
|
||
// account.Level = 99;
|
||
// account.NickName = $"测试号{i + 1}";
|
||
// account.Country = "cn";
|
||
// account.Exp = 999;
|
||
// account.Head = "xxx.png";
|
||
// tasks.Add(account.Save());
|
||
// }
|
||
// await FTask.WaitAll(tasks);
|
||
// // self.Scene.World.Database.InsertBatch()
|
||
//
|
||
// stopwatch.Stop();
|
||
// Log.Info($"创建100个号入库耗时={stopwatch.ElapsedMilliseconds}ms");
|
||
}
|
||
|
||
private async FTask InitializeAddressableScene(Scene scene)
|
||
{
|
||
// Addressable 场景特定的初始化逻辑
|
||
Log.Info($"初始化 Addressable 场景: {scene.Id}");
|
||
await FTask.CompletedTask;
|
||
}
|
||
} |