123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- using System;
- using System.Collections.Generic;
- namespace ET.Server
- {
- /// <summary>
- /// 选角绑定角色
- /// </summary>
- [MessageHandler(SceneType.Game)]
- public class C2G_BindPlayerHandler: AMRpcHandler<C2G_BindPlayer, G2C_BindPlayer>
- {
- 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<SessionPlayerComponent>().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<PlayerInfo> playerList = session.GetComponent<SessionPlayerComponent>().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<SessionPlayerComponent>().IsExist(request.PlayerId))
- {
- Log.Debug($"选角出错,没有选择的角色");
- response.Error = ErrorCode.ERR_BindPlayerError;
- reply();
- return;
- }
- playerInfo = session.GetComponent<SessionPlayerComponent>().Get(request.PlayerId);
- if (playerInfo == null)
- {
- Log.Debug($"选角出错,没有选择的角色");
- response.Error = ErrorCode.ERR_BindPlayerError;
- reply();
- return;
- }
- long bindPlayerId = session.GetComponent<SessionPlayerComponent>().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<GamePlayerComponent>().Get(bindPlayerId);
- if (oldPlayer != null)
- {
- oldPlayer.Dispose();
- scene.GetComponent<GamePlayerComponent>().Remove(bindPlayerId);
- }
- break;
- }
- }
- WNPlayer player = scene.GetComponent<GamePlayerComponent>().Get(request.PlayerId);
- if (player != null)
- {
- // todo 顶号
- player.Dispose();
- scene.GetComponent<GamePlayerComponent>().Remove(request.PlayerId);
- }
- player = await PlayerFactory.CreatPlayer(session, playerInfo);
- // 绑定选择的玩家
- session.GetComponent<SessionPlayerComponent>().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();
- }
- }
- }
|