using System; using System.Collections.Generic; using Cal.DataTable; using UnityEngine; namespace ET { [ActorMessageHandler] public class G2M_CreateUnitHandler : AMActorRpcHandler { 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(); data = unit.GetComponent(); unit.RemoveComponent(); unit.AddComponent(request.GateSessionId); TeamComponent.Instance.Get(unit.TeamLeaderId); } else { Log.Debug($"【{request.PlayerId}】 新进入游戏"); unit = EntityFactory.CreateWithId(scene, user.Id, UnitType.Player); unit.isAgainOnLine = false; user.SkinId = 0; //!移动 unit.AddComponent(); unit.AddComponent(); unit.AddComponent(); //!存档 await UnitHelper.AddComponentFromDB(unit,user.JobId); //!自动技能AI unit.AddComponent(); unit.AddComponent(); unit.AddComponent(); unit.AddComponent(); //!通讯相关 unit.AddComponent(); await unit.AddLocation(); unit.AddComponent(request.GateSessionId); unit.AddComponent(true); MapScene map = unit.GetMapByUnitScene(); await map.Enter(unit); //!队伍 TeamComponent.Instance.CreateTeam(unit.Id); //!战斗组件 unit.AddComponent().BattleType = BattleType.None; unit.AddComponent(); unit.AddComponent(); unit.AddComponent(); var pet = unit.GetComponent(); 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(); UnitSkillComponent skillComponent = unit.GetComponent(); 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 CheckChangeDB(User user) { if (user.isChangeDB) return true; Log.Warning($"开始修改{user.Id}的数据"); Unit unit = await DBComponent.Instance.Query(user.Id); if (unit == null) return false; unit.Domain = Game.Scene; PlayerData data = unit.GetComponent(); Character character = unit.GetComponent(); NumericComponent num = unit.GetComponent(); UnitSkillComponent unitSkill = unit.GetComponent(); UserSetting setting = unit.GetComponent(); UnitTask unitTask = unit.GetComponent(); Bag bag = unit.GetComponent(); UnitScene unitScene = unit.GetComponent(); if (unitScene == null) unitScene = EntityFactory.CreateWithId(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; } } }