using System;
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 (session.DomainScene().SceneType != SceneType.Game)
{
Log.Debug($"Game 请求的Scene错误...SceneType={session.DomainScene().SceneType}");
session.Dispose();
return;
}
Scene scene = session.DomainScene();
PlayerInfo playerInfo;
if (request.PlayerId <= 0)
{
long _id = IdGenerater.Instance.GenerateUnitId(scene.DomainZone());
// 简易版登录,直接创建一个player
using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.CreatePlayer, _id.ToString().Trim().GetHashCode()))
{
playerInfo = new PlayerInfo();
playerInfo.Id = _id;
playerInfo.UserId = request.UserId;
playerInfo.Name = "玩家-" + _id;
playerInfo.Sex = 0;
playerInfo.Pro = 0;
playerInfo.Level = 1;
playerInfo.Exp = 0;
await DBManagerComponent.Instance.GetZoneDB(session.DomainZone()).Save(playerInfo);
}
}
else
{
// 正常选角,判断参数
if (!session.GetComponent().PlayerList.ContainsKey(request.PlayerId))
{
Log.Debug($"选角出错,没有选择的角色");
response.Error = ErrorCode.ERR_BindPlayerError;
reply();
return;
}
playerInfo = session.GetComponent().PlayerList[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 老角色下线
break;
}
}
WNPlayer player = scene.GetComponent().Get(request.PlayerId);
if (player != null)
{
// todo 顶号
}
player ??= scene.GetComponent()
.AddChildWithId(playerInfo.Id, session, playerInfo);
session.AddComponent(MailboxType.GateSession);
session.GetComponent().PlayerId = request.PlayerId;
// 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.InstanceId;
response.Player.mapId = map.MapId;
reply();
await ETTask.CompletedTask;
}
}
}