zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Server/Hotfix/Game/Helper/UnitHelper.cs

288 lines
10 KiB
C#
Raw Normal View History

2021-04-08 20:09:59 +08:00
using System;
using System.Collections.Generic;
using System.Text;
2021-08-06 15:04:29 +08:00
using Cal.DataTable;
2021-06-29 11:28:15 +08:00
using MongoDB.Bson;
2021-08-06 15:04:29 +08:00
using UnityEngine;
2021-04-08 20:09:59 +08:00
namespace ET
{
public class UnitHelper
{
public static async ETTask<Unit> Query(Entity domain, long id)
{
try
{
2021-08-06 15:04:29 +08:00
Unit unit = EntityFactory.CreateWithId<Unit>(domain, id);
2021-04-11 19:50:39 +08:00
PlayerData data = await DBComponent.Instance.Query<PlayerData>(id);
2021-09-25 21:04:37 +08:00
if (data)
unit.AddComponent(data);
2021-04-11 19:50:39 +08:00
Character character = await DBComponent.Instance.Query<Character>(id);
2021-09-25 21:04:37 +08:00
if (character)
unit.AddComponent(character);
2021-04-11 19:50:39 +08:00
NumericComponent num = await DBComponent.Instance.Query<NumericComponent>(id);
2021-09-25 21:04:37 +08:00
if (num)
unit.AddComponent(num);
2021-04-11 19:50:39 +08:00
UnitSkillComponent unitSkill = await DBComponent.Instance.Query<UnitSkillComponent>(id);
2021-09-25 21:04:37 +08:00
if (unitSkill)
unit.AddComponent(unitSkill);
2021-04-08 20:09:59 +08:00
//!玩家设置
2021-04-11 19:50:39 +08:00
UserSetting setting = await DBComponent.Instance.Query<UserSetting>(id);
2021-09-25 21:04:37 +08:00
if (setting)
unit.AddComponent(setting);
2021-04-08 20:09:59 +08:00
//!任务
2021-04-11 19:50:39 +08:00
UnitTask unitTask = await DBComponent.Instance.Query<UnitTask>(id);
2021-09-25 21:04:37 +08:00
if (unitTask)
unit.AddComponent(unitTask);
2021-04-08 20:09:59 +08:00
//!背包
2021-04-11 19:50:39 +08:00
Bag bag = await DBComponent.Instance.Query<Bag>(id);
2021-09-25 21:04:37 +08:00
if (bag)
unit.AddComponent(bag);
2021-04-11 19:50:39 +08:00
UnitScene unitScene = await DBComponent.Instance.Query<UnitScene>(id);
2021-09-25 21:04:37 +08:00
if (unitScene)
unit.AddComponent(unitScene);
2021-05-01 22:06:12 +08:00
StarSoulBag starSoulBag = await DBComponent.Instance.Query<StarSoulBag>(id);
2021-09-25 21:04:37 +08:00
if (starSoulBag)
unit.AddComponent(starSoulBag);
2021-04-11 19:50:39 +08:00
Pet pet = await DBComponent.Instance.Query<Pet>(id);
2021-09-25 21:04:37 +08:00
if (pet)
unit.AddComponent(pet);
2021-04-08 20:09:59 +08:00
unit.AddComponent<Combat>();
return unit;
}
catch (Exception e)
{
2021-08-06 15:04:29 +08:00
Log.Error(e);
2021-04-08 20:09:59 +08:00
}
2021-08-06 15:04:29 +08:00
2021-04-08 20:09:59 +08:00
return null;
}
2021-08-06 15:04:29 +08:00
public static async ETTask<T> Query<T>(int zone, long id) where T : Entity
2021-06-29 11:28:15 +08:00
{
try
{
long mapInstanceId = StartSceneConfigCategory.Instance.GetBySceneName(zone, SceneType.UserCache.ToString()).SceneId;
2021-08-06 15:04:29 +08:00
U2M_GetComponent getComponent =
(U2M_GetComponent) await MessageHelper.CallActor(mapInstanceId,
new M2U_GetComponent() { UserId = id, type = typeof (T).Name });
2021-06-29 11:28:15 +08:00
if (getComponent.component == null) return null;
T t = getComponent.component as T;
return t;
}
catch (Exception e)
{
Log.Error(e);
}
2021-08-06 15:04:29 +08:00
2021-06-29 11:28:15 +08:00
return null;
}
2021-08-06 15:04:29 +08:00
2021-06-29 11:28:15 +08:00
public static void Save<T>(Unit unit) where T : Entity
2021-04-08 20:09:59 +08:00
{
Entity entity = unit.GetComponent<T>();
2022-05-29 23:02:28 +08:00
if(entity==null)
return;
2021-08-06 15:04:29 +08:00
int zone = entity.DomainZone();
2022-05-29 23:02:28 +08:00
2021-08-06 15:04:29 +08:00
if (zone == 0)
{
zone = 1;
}
long mapInstanceId = StartSceneConfigCategory.Instance.GetBySceneName(zone, SceneType.UserCache.ToString()).SceneId;
MessageHelper.SendActor(mapInstanceId, new M2U_WriteComponent() { Id = entity.Id, type = typeof (T).Name, component = entity });
2021-04-08 20:09:59 +08:00
}
2021-06-29 11:28:15 +08:00
public static void SaveComponenet<T>(T t) where T : Entity
2021-04-08 20:09:59 +08:00
{
2021-08-06 15:04:29 +08:00
int zone = t.DomainZone();
if (zone == 0)
{
2021-09-07 16:20:46 +08:00
zone = 1;
}
long mapInstanceId = StartSceneConfigCategory.Instance.GetBySceneName(zone, SceneType.UserCache.ToString()).SceneId;
MessageHelper.SendActor(mapInstanceId, new M2U_WriteComponent() { Id = t.Id, type = typeof (T).Name, component = t });
}
2021-09-25 21:04:37 +08:00
public static void SaveComponenet<T>(int zone, T t) where T : Entity
2021-09-07 16:20:46 +08:00
{
if (zone == 0)
{
2021-08-06 15:04:29 +08:00
zone = 1;
}
2021-06-29 11:28:15 +08:00
// await DBComponent.Instance.Save(t);
2021-08-06 15:04:29 +08:00
long mapInstanceId = StartSceneConfigCategory.Instance.GetBySceneName(zone, SceneType.UserCache.ToString()).SceneId;
MessageHelper.SendActor(mapInstanceId, new M2U_WriteComponent() { Id = t.Id, type = typeof (T).Name, component = t });
2021-04-08 20:09:59 +08:00
}
2021-06-29 11:28:15 +08:00
2021-04-08 20:09:59 +08:00
/// <summary>
/// 设置位置信息以及人物信息
/// </summary>
/// <param name="unit"></param>
/// <param name="unitInfo"></param>
/// <param name="pvpUnitCharacter"></param>
public static void SetUnitInfo(Unit unit, UnitPosInfo unitInfo)
{
UnitScene unitScene = unit.GetComponent<UnitScene>();
if (unitScene == null)
{
Log.Error($"unitScene == null where id = {unit.Id}");
return;
}
2021-08-06 15:04:29 +08:00
2021-04-08 20:09:59 +08:00
unitInfo.X = unitScene.X;
unitInfo.Y = unitScene.Y;
unitInfo.YAngle = unitScene.YAngle;
}
2021-08-06 15:04:29 +08:00
2021-09-25 21:04:37 +08:00
public static async ETTask AddComponentFromDB(Unit unit, int jobId)
2021-08-06 15:04:29 +08:00
{
int zone = unit.DomainZone();
long id = unit.Id;
//!玩家数据
var data = await UnitHelper.Query<PlayerData>(zone, id);
if (data == null)
{
data = unit.AddComponent<PlayerData>();
data.IsNew = true;
data.UpdateMinstoryRecord(Sys_SceneId.Scene_MainStory1 - 1);
data.SkillPointKV = new KeyValuePair<int, int>(1, 1);
}
else
unit.AddComponent(data);
Character character = await UnitHelper.Query<Character>(zone, id);
if (character == null)
character = unit.AddComponent<Character>();
else
unit.AddComponent(character);
2021-09-25 21:04:37 +08:00
2021-08-06 15:04:29 +08:00
var num = await UnitHelper.Query<NumericComponent>(zone, id);
if (num == null)
{
num = unit.AddComponent<NumericComponent>();
CharacterHelper.InitData(num);
num.Set(NumericType.SkinId, jobId);
num.Set(NumericType.Energy, 1000);
}
else
unit.AddComponent(num);
//!exp
int energy = num.GetAsInt(NumericType.Energy);
data.ForbidExp = energy <= 0;
UnitSkillComponent unitSkill = await UnitHelper.Query<UnitSkillComponent>(zone, id);
if (unitSkill == null)
{
unitSkill = unit.AddComponent<UnitSkillComponent>();
JobType jobType = JobHelper.GetJobType(jobId);
unitSkill.InitSkill(jobType);
}
else
unit.AddComponent(unitSkill);
//!玩家设置
UserSetting setting = await UnitHelper.Query<UserSetting>(zone, id);
if (setting == null)
{
setting = unit.AddComponent<UserSetting>();
}
else
unit.AddComponent(setting);
setting.SetCD(MathHelper.RoundToInt(CharacterHelper.GetSkillCD(num.Get(NumericType.Spd))));
//!宠物
Pet pet = await UnitHelper.Query<Pet>(zone, id);
if (pet == null)
{
pet = unit.AddComponent<Pet>();
}
else
unit.AddComponent(pet);
if (pet.petId == 0)
{
pet.petId = 2101;
}
UnitScene unitScene = await UnitHelper.Query<UnitScene>(zone, id);
if (unitScene == null)
{
unitScene = UnitSceneFactory.Create(Game.Scene, id, new Vector2(0, 0), 0, Sys_SceneId.Scene_Beach * 100 + 1);
unit.AddComponent(unitScene);
}
else
unit.AddComponent(unitScene);
//!任务
UnitTask unitTask = await UnitHelper.Query<UnitTask>(zone, id);
if (unitTask == null)
{
unitTask = unit.AddComponent<UnitTask>();
}
else
unit.AddComponent(unitTask);
//!背包
Bag bag = await UnitHelper.Query<Bag>(zone, id);
if (bag == null)
{
bag = unit.AddComponent<Bag>();
}
else
unit.AddComponent(bag);
if (bag.ItemDic.Count == 0)
{
bag.InitBag();
}
2021-09-25 21:04:37 +08:00
2021-08-06 15:04:29 +08:00
bag.CheckSlot();
2021-09-25 21:04:37 +08:00
2021-08-06 15:04:29 +08:00
Store store = await StoreComponent.Instance.Query(unit.Id);
//!初始化仓库
if (store == null)
StoreComponent.Instance.Init(unit.Id).Coroutine();
store.CheckSlot();
//!背包
StarSoulBag StarSoulBag = await UnitHelper.Query<StarSoulBag>(zone, id);
if (StarSoulBag == null)
{
StarSoulBag = unit.AddComponent<StarSoulBag>();
}
else
unit.AddComponent(StarSoulBag);
}
2021-09-04 14:55:51 +08:00
public static async ETTask SendUnitInfo(Unit unit)
{
2021-09-25 21:04:37 +08:00
Log.Debug($"【{UserComponent.Instance.Get(unit.Id)?.NickName} ({unit.Id})】已经进入游戏");
2021-09-04 14:55:51 +08:00
PlayerData data = unit.GetComponent<PlayerData>();
var num = unit.GetComponent<NumericComponent>();
CharacterHelper.ResetAddNumeric(unit, num);
data.IsBattleIdle = false;
UnitScene unitScene = unit.GetComponent<UnitScene>();
if (unitScene == null)
{
Log.Error($"unitScene == null where id = {unit.Id}");
return;
}
2021-09-25 21:04:37 +08:00
if (unitScene.MapId / 100 == Sys_SceneId.Scene_BattleIdle)
2021-09-04 14:55:51 +08:00
{
unitScene.MapId = Sys_SceneId.Scene_MainCity * 100 + 1;
}
2021-09-25 21:04:37 +08:00
2021-09-04 14:55:51 +08:00
//!+任务相关
//!+发送Boss消息
Game.EventSystem.Publish(new EventType.AfterEnterGame() { unit = unit });
}
2021-04-08 20:09:59 +08:00
}
2021-08-06 15:04:29 +08:00
}