CTT/Server/Hotfix/Game/Handler/User/G2M_CreateUnitHandler.cs

248 lines
10 KiB
C#
Raw Normal View History

2021-04-08 20:09:59 +08:00
using System;
using System.Collections.Generic;
using Cal.DataTable;
using UnityEngine;
namespace ET
{
[ActorMessageHandler]
public class G2M_CreateUnitHandler : AMActorRpcHandler<Scene, G2M_CreateUnit, M2G_CreateUnit>
{
protected override async ETTask Run(Scene scene, G2M_CreateUnit request, M2G_CreateUnit response, Action reply)
{
try
{
Log.Debug($"*********************************************************UserId = {request.PlayerId} 开始进入游戏");
bool isNewUser = false;
User user = UserComponent.Instance.Get(request.PlayerId);
if (user == null)
{
isNewUser = true;
user = await UserComponent.Instance.Query(request.PlayerId);
}
if (user == null)
{
response.Message = $"没有这个玩家";
Log.Error($"服务器没有这个玩家Id={ request.PlayerId}");
reply();
return;
}
if (!await CheckChangeDB(user))
{
response.Message = $"数据调整完毕,请重新登录";
reply();
return;
}
Unit unit = MapUnitComponent.Instance.Get(user.Id);
long id = user.Id;
NumericComponent num;
PlayerData data;
Team team;
bool isOnline = false;
if (unit)
{
isOnline = true;
unit.isAgainOnLine = true;
unit.IsOffline = false;
Log.Debug($"【{ UserComponent.Instance.Get(unit.Id)?.NickName} ({ unit.Id})】 断线之后重新进入游戏了");
num = unit.GetComponent<NumericComponent>();
data = unit.GetComponent<PlayerData>();
unit.RemoveComponent<UserGateComponent>();
unit.AddComponent<UserGateComponent, long>(request.GateSessionId);
team = TeamComponent.Instance.Get(unit.TeamLeaderId);
}
else
{
Log.Debug($"【{request.PlayerId}】 新进入游戏");
unit = EntityFactory.CreateWithId<Unit, UnitType>(scene, user.Id, UnitType.Player);
unit.isAgainOnLine = false;
user.SkinId = 0;
//!移动
unit.AddComponent<MoveComponent>();
unit.AddComponent<Position>();
unit.AddComponent<UnitPathFindComponent>();
//!属性
JobType jobType = JobHelper.GetJobType(user.JobId);
//!玩家数据
data = await DBComponent.Instance.Query<PlayerData>(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);
var character = await DBComponent.Instance.Query<Character>(id);
if (character == null)
character = unit.AddComponent<Character>();
else
unit.AddComponent(character);
num = await DBComponent.Instance.Query<NumericComponent>(id);
if (num == null)
{
num = unit.AddComponent<NumericComponent>();
CharacterHelper.InitData(num);
num.Set(NumericType.SkinId, user.JobId);
num.Set(NumericType.Energy, 1000);
}
else
unit.AddComponent(num);
//!exp
int energy = num.GetAsInt(NumericType.Energy);
data.ForbidExp = energy<=0;
var unitSkill = await DBComponent.Instance.Query<UnitSkillComponent>(id);
if (unitSkill == null)
{
unitSkill = unit.AddComponent<UnitSkillComponent>();
unitSkill.InitSkill(jobType);
}
else
unit.AddComponent(unitSkill);
//!玩家设置
var setting = await DBComponent.Instance.Query<UserSetting>(id);
if (setting == null)
{
setting = unit.AddComponent<UserSetting>();
}
else
unit.AddComponent(setting);
setting.SetCD(MathHelper.RoundToInt(CharacterHelper.GetSkillCD(num.Get(NumericType.Spd))));
//!自动技能AI
unit.AddComponent<SkillAI>();
unit.AddComponent<SkillMgrComponent>();
unit.AddComponent<ModifierContainerComponent>();
//!通讯相关
unit.AddComponent<MailBoxComponent>();
await unit.AddLocation();
var unitGate = unit.AddComponent<UserGateComponent, long>(request.GateSessionId);
BrocastComponent brocastComponent = unit.AddComponent<BrocastComponent, bool>(true);
//!宠物
var pet = await DBComponent.Instance.Query<Pet>(id);
if (pet == null)
{
pet = unit.AddComponent<Pet>();
}
else
unit.AddComponent(pet);
var unitScene = await DBComponent.Instance.Query<UnitScene>(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);
MapScene map = unit.GetMapByUnitScene();
await map.Enter(unit);
//!队伍
team = TeamComponent.Instance.CreateTeam(unit.Id);
//!战斗组件
unit.AddComponent<BattleComponent>().BattleType = BattleType.None;
//!任务
var unitTask = await DBComponent.Instance.Query<UnitTask>(id);
if (unitTask == null)
{
unitTask = unit.AddComponent<UnitTask>();
}
else
unit.AddComponent(unitTask);
//!背包
var bag = await DBComponent.Instance.Query<Bag>(id);
if (bag == null)
{
bag = unit.AddComponent<Bag>();
}
else
unit.AddComponent(bag);
var store = await StoreComponent.Instance.Query(unit.Id);
//!初始化仓库
if (store == null)
StoreComponent.Instance.Init(unit.Id).Coroutine();
if (bag.ItemDic.Count == 0)
{
bag.InitBag();
}
store.CheckSlot();
bag.CheckSlot();
Combat combat = unit.AddComponent<Combat>();
2021-04-09 00:48:56 +08:00
unit.AddComponent<AIComponent>().InitNodes(new AINode[]{ new BuyInShopNode(),new MoveToCityTransPointNode(),new BackHomeNode() });
2021-04-08 20:09:59 +08:00
}
Log.Debug($"【{ UserComponent.Instance.Get(unit.Id)?.NickName} ({ unit.Id})】 组件添加完毕");
if (user.SkinId == 0)
{
user.SkinId = num.GetAsInt(NumericType.SkinId);
}
if (isNewUser)
UserComponent.Instance.Add(user);
if (isNewUser)
UserComponent.Instance.Save(user).Coroutine();
response.IsOnLine = isOnline;
reply();
CharacterHelper.SyncNumeric(unit);
CharacterHelper.RecoverUnit(unit);
Log.Debug($"【{ UserComponent.Instance.Get(unit.Id)?.NickName} ({ unit.Id})】请求进入地图完成");
}
catch (System.Exception e)
{
Log.Error(e);
}
}
private async ETTask<bool> CheckChangeDB(User user)
{
if (user.isChangeDB) return true;
Log.Warning($"开始修改{user.Id}的数据");
var unit = await DBComponent.Instance.Query<Unit>(user.Id);
if (unit == null) return false;
unit.Domain = Game.Scene;
var data = unit.GetComponent<PlayerData>();
var character = unit.GetComponent<Character>();
var num = unit.GetComponent<NumericComponent>();
var unitSkill = unit.GetComponent<UnitSkillComponent>();
var setting = unit.GetComponent<UserSetting>();
var unitTask = unit.GetComponent<UnitTask>();
var bag = unit.GetComponent<Bag>();
var unitScene = unit.GetComponent<UnitScene>();
if (unitScene == null)
unitScene = EntityFactory.CreateWithId<UnitScene>(Game.Scene, user.Id);
unitScene.Id = user.Id;
await DBComponent.Instance.Save(data);
await DBComponent.Instance.Save(character);
await DBComponent.Instance.Save(num);
await DBComponent.Instance.Save(unitSkill);
await DBComponent.Instance.Save(setting);
await DBComponent.Instance.Save(unitTask);
await DBComponent.Instance.Save(bag);
await DBComponent.Instance.Save(unitScene);
unit.Dispose();
user.isChangeDB = true;
await DBComponent.Instance.Save(user);
return false;
}
}
}