# Conflicts:
#	Config/NetworkProtocol/Outer/data/MapProtoData.proto
#	Entity/Generate/NetworkProtocol/MapProtoData.cs
#	Entity/Generate/NetworkProtocol/OuterOpcode.cs
This commit is contained in:
2025-09-25 23:23:18 +08:00
22 changed files with 521 additions and 229 deletions

View File

@@ -3,9 +3,9 @@ using Fantasy.Entitas.Interface;
namespace NB.Game;
public sealed class ItemContainerDestroySystem : DestroySystem<ItemContainer>
public sealed class ItemContainerDestroySystem : DestroySystem<PlayerItemContainer>
{
protected override void Destroy(ItemContainer self)
protected override void Destroy(PlayerItemContainer self)
{
self.CellCountMax = 0;
@@ -31,7 +31,7 @@ public static class ItemContainerSystem
/// <param name="id"></param>
/// <param name="item"></param>
/// <returns></returns>
public static bool GetItemById(this ItemContainer self, long id, out Item? item)
public static bool GetItemById(this PlayerItemContainer self, long id, out Item? item)
{
return self.Items.TryGetValue(id, out item);
}
@@ -42,7 +42,7 @@ public static class ItemContainerSystem
/// <param name="self"></param>
/// <param name="configId"></param>
/// <param name="item"></param>
public static bool GetFistItemByConfigId(this ItemContainer self, int configId, out Item? item)
public static bool GetFistItemByConfigId(this PlayerItemContainer self, int configId, out Item? item)
{
foreach (var (_, it) in self.Items)
{
@@ -100,7 +100,7 @@ public static class ItemContainerSystem
#region Add
public static void Add(this ItemContainer self, int configId, int count)
public static void Add(this PlayerItemContainer self, int configId, int count)
{
var item = ItemFactory.Create(self.Scene, configId, count);
self.Items.Add(item.Id, item);
@@ -110,7 +110,7 @@ public static class ItemContainerSystem
#region
public static List<ItemInfo> GetItemInfos(this ItemContainer self)
public static List<ItemInfo> GetItemInfos(this PlayerItemContainer self)
{
List<ItemInfo> ret = new List<ItemInfo>();
foreach (var (_, item) in self.Items)
@@ -121,7 +121,7 @@ public static class ItemContainerSystem
return ret;
}
public static List<GearInfo> GetGearInfos(this ItemContainer self)
public static List<GearInfo> GetGearInfos(this PlayerItemContainer self)
{
List<GearInfo> ret = new List<GearInfo>();

View File

@@ -12,8 +12,6 @@ public sealed class PlayerManageComponentAwakeSystem : AwakeSystem<PlayerManageC
{
protected override void Awake(PlayerManageComponent self)
{
self.AutoSaveTimerId =
self.Scene.TimerComponent.Net.RepeatedTimer(1000 * 60, () => { _ = self.CheckAutoSave(); });
}
}
@@ -27,8 +25,6 @@ public sealed class PlayerManageComponentDestroySystem : DestroySystem<PlayerMan
}
self.Players.Clear();
self.Scene.TimerComponent.Net.Remove(self.AutoSaveTimerId);
self.AutoSaveTimerId = 0;
}
}
@@ -57,7 +53,6 @@ public static class PlayerManageComponentSystem
// 如果不存在,表示这是一个新的账号,需要创建一下这个账号。
account = PlayerFactory.Create(self.Scene, accountId);
account.Level = 99;
account.NickName = "王麻子";
account.Country = "cn";
@@ -103,8 +98,8 @@ public static class PlayerManageComponentSystem
}
// account.Statistics.LoginTime = TimeHelper.Now;
account.SetTestData();
account.SetTestData();
await account.Save();
return account;
@@ -157,20 +152,29 @@ public static class PlayerManageComponentSystem
#endregion
#region
#region
public static async FTask CheckAutoSave(this PlayerManageComponent self)
/// <summary>
/// 尝试获取或增加相关组件
/// </summary>
/// <param name="player"></param>
/// <typeparam name="T"></typeparam>
public static async FTask TryComponent<T>(this Player player) where T : Entity, new()
{
foreach (var (_, player) in self.Players)
if (player.GetComponent<T>() == null)
{
if (player.NeedSave)
var component = await player.Scene.World.DataBase.Query<T>(player.Id, true);
if (component == null)
{
await player.SaveImmediately();
//如果没有组件
player.AddComponent<T>();
}
else
{
player.AddComponent(component);
}
}
}
#endregion
}

View File

@@ -13,12 +13,10 @@ public static class PlayerFactory
/// </summary>
/// <param name="scene"></param>
/// <param name="aId">ToKen令牌传递过来的aId</param>
/// <param name="isSaveDataBase">是否在创建的过程中保存到数据库</param>
/// <returns></returns>
public static Player Create(Scene scene, long aId)
{
var player = Entity.Create<Player>(scene, aId, true, true);
player.InitializeChildEntity();
return player;
}
}

View File

@@ -9,57 +9,9 @@ namespace NB.Game;
public static class PlayerHelper
{
#region MyRegion
public static void InitializeChildEntity(this Player self)
{
if (self.Vip == null)
{
self.Vip = Entity.Create<PlayerVip>(self.Scene, true, true);
}
if (self.Wallet == null)
{
self.Wallet = Entity.Create<PlayerWallet>(self.Scene, true, true);
}
if (self.ItemContainer == null)
{
self.ItemContainer = Entity.Create<ItemContainer>(self.Scene, true, true);
}
if (self.FishContainer == null)
{
self.FishContainer = Entity.Create<FishContainer>(self.Scene, true, true);
}
if (self.SkillContainer == null)
{
self.SkillContainer = Entity.Create<SkillContainer>(self.Scene, true, true);
}
if (self.AchievementContainer == null)
{
self.AchievementContainer = Entity.Create<AchievementContainer>(self.Scene, true, true);
}
// if (self.Statistics == null)
// {
// self.Statistics = Entity.Create<PlayerStatistics>(self.Scene, true, true);
// self.Statistics.LoginTime = self.Statistics.CreateTime = TimeHelper.Now;
// }
}
public static async FTask SaveImmediately(this Player self)
{
await self.Scene.World.DataBase.Save(self);
self.NeedSave = false;
Log.Info($"player id:{self.Id} save data to dataBase");
}
public static async FTask Save(this Player self)
{
self.NeedSave = true;
self.NeedSaveTime = TimeHelper.Now + AppConfig.PlayerDataAutoSaveTime;
//先立马保存,后续做缓存
await self.Scene.World.DataBase.Save(self);
}
@@ -97,7 +49,7 @@ public static class PlayerHelper
public static async FTask Disconnect(this Player self)
{
// 保存该账号信息到数据库中。
await SaveImmediately(self);
await Save(self);
// 在缓存中移除自己并且执行自己的Dispose方法。
self.Scene.GetComponent<PlayerManageComponent>().Remove(self.Id);
}
@@ -176,7 +128,6 @@ public static class PlayerHelper
var info = new RoleInfo();
info.BaseInfo = GetRoleBaseInfo(self);
info.Items = self.ItemContainer.GetItemInfos();
info.Gears = self.ItemContainer.GetGearInfos();
info.RoleId = self.RouteId;
return info;
}