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

165 lines
6.6 KiB
C#
Executable File

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;
int zone = scene.DomainZone();
NumericComponent num;
PlayerData data;
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);
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>();
//!存档
await UnitHelper.AddComponentFromDB(unit,user.JobId);
//!自动技能AI
unit.AddComponent<SkillAI>();
unit.AddComponent<SkillMgrComponent>();
unit.AddComponent<ModifierContainerComponent>();
unit.AddComponent<MapMonsterComponent>();
//!通讯相关
unit.AddComponent<MailBoxComponent>();
await unit.AddLocation();
unit.AddComponent<UserGateComponent, long>(request.GateSessionId);
unit.AddComponent<BrocastComponent, bool>(true);
MapScene map = unit.GetMapByUnitScene();
await map.Enter(unit);
//!队伍
TeamComponent.Instance.CreateTeam(unit.Id);
//!战斗组件
unit.AddComponent<BattleComponent>().BattleType = BattleType.None;
unit.AddComponent<AttackComponent>();
unit.AddComponent<TargetableUnitComponent>();
unit.AddComponent<Combat>();
var pet = unit.GetComponent<Pet>();
Game.EventSystem.Change(pet);
}
Log.Debug($"【{ UserComponent.Instance.Get(unit.Id)?.NickName} ({ unit.Id})】 组件添加完毕");
if (isNewUser)
{
UserComponent.Instance.Add(user);
UserComponent.Instance.Save(user);
}
response.IsOnLine = isOnline;
reply();
//添加技能
SkillMgrComponent skillMgr = unit.GetComponent<SkillMgrComponent>();
UnitSkillComponent skillComponent = unit.GetComponent<UnitSkillComponent>();
foreach (UnitSkill unitSkill in skillComponent.GetLearnedSkills())
{
skillMgr.AddSkill(unit, unitSkill);
}
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}的数据");
Unit unit = await DBComponent.Instance.Query<Unit>(user.Id);
if (unit == null) return false;
unit.Domain = Game.Scene;
PlayerData data = unit.GetComponent<PlayerData>();
Character character = unit.GetComponent<Character>();
NumericComponent num = unit.GetComponent<NumericComponent>();
UnitSkillComponent unitSkill = unit.GetComponent<UnitSkillComponent>();
UserSetting setting = unit.GetComponent<UserSetting>();
UnitTask unitTask = unit.GetComponent<UnitTask>();
Bag bag = unit.GetComponent<Bag>();
UnitScene unitScene = unit.GetComponent<UnitScene>();
if (unitScene == null)
unitScene = EntityFactory.CreateWithId<UnitScene>(Game.Scene, user.Id);
unitScene.Id = user.Id;
UnitHelper.SaveComponenet(data);
UnitHelper.SaveComponenet(character);
UnitHelper.SaveComponenet(num);
UnitHelper.SaveComponenet(unitSkill);
UnitHelper.SaveComponenet(setting);
UnitHelper.SaveComponenet(unitTask);
UnitHelper.SaveComponenet(bag);
UnitHelper.SaveComponenet(unitScene);
unit.Dispose();
user.isChangeDB = true;
UserComponent.Instance.Save(user);
return false;
}
}
}