using System; using System.Collections.Generic; namespace ET.Server { /// /// 选角绑定角色 /// [MessageHandler(SceneType.Game)] public class C2G_BindPlayerHandler: AMRpcHandler { protected override async ETTask Run(Session session, C2G_BindPlayer request, G2C_BindPlayer response, Action reply) { if (string.IsNullOrEmpty(request.UserId.ToString().Trim())) { Log.Debug($"参数错误,账号id为空"); response.Error = ErrorCode.ERR_ParameterError; reply(); return; } Scene scene = session.DomainScene(); // 预先创建数据 long _id = IdGenerater.Instance.GenerateUnitId(scene.DomainZone()); PlayerInfo playerInfo = new PlayerInfo(); playerInfo.Id = _id; playerInfo.UserId = session.GetComponent().UserId; playerInfo.Name = "玩家-" + _id; playerInfo.Sex = 0; playerInfo.Pro = (int)PlayerProType.CANG_LANG; playerInfo.Level = 1; playerInfo.Exp = 0; // 简易版登录,直接创建一个player if (request.PlayerId <= 0) { // 角色列表是否有数据 List playerList = session.GetComponent().PlayerList; if (playerList is { Count: > 0 }) { playerInfo = playerList[0]; } else { using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.CreatePlayer, _id.ToString().Trim().GetHashCode())) { await DBManagerComponent.Instance.GetZoneDB(session.DomainZone()).Save(playerInfo); } } } else { // 正常选角,判断参数 if (!session.GetComponent().IsExist(request.PlayerId)) { Log.Debug($"选角出错,没有选择的角色"); response.Error = ErrorCode.ERR_BindPlayerError; reply(); return; } playerInfo = session.GetComponent().Get(request.PlayerId); if (playerInfo == null) { Log.Debug($"选角出错,没有选择的角色"); response.Error = ErrorCode.ERR_BindPlayerError; reply(); return; } long bindPlayerId = session.GetComponent().PlayerId; switch (bindPlayerId) { case > 0 when bindPlayerId == request.PlayerId: Log.Debug($"该角色已经绑定"); response.Error = ErrorCode.ERR_PlayerIdIsBind; reply(); return; case > 0 when bindPlayerId != request.PlayerId: // todo 老角色下线 WNPlayer oldPlayer = scene.GetComponent().Get(bindPlayerId); if (oldPlayer != null) { oldPlayer.Dispose(); scene.GetComponent().Remove(bindPlayerId); } break; } } WNPlayer player = scene.GetComponent().Get(request.PlayerId); if (player != null) { // todo 顶号 player.Dispose(); scene.GetComponent().Remove(request.PlayerId); } player = await PlayerFactory.CreatPlayer(session, playerInfo); // 绑定选择的玩家 session.GetComponent().BindPlayerId(request.PlayerId > 0 ? request.PlayerId : player.GetId()); // todo 分配场景 Map map = MapHelper.BindPlayerDispatch(player); MapConfig mapConfig = map.Prop; // 登录数据 player.OnLogin(); map.OnPlayerLogin(player); response.Player = PlayerHelper.PlayerInfoToProto(player); response.Player.areaId = mapConfig.TemplateID; response.Player.instanceId = map.Id; response.Player.mapId = map.MapId; reply(); } } }