using System; namespace ET.Server { /// /// 创建角色 /// [MessageHandler(SceneType.Game)] public class C2G_CreatePlayerHandler: AMRpcHandler { protected override async ETTask Run(Session session, C2G_CreatePlayer request, G2C_CreatePlayer response, Action reply) { // 重复请求 if (session.GetComponent() != null) { response.Error = ErrorCode.ERR_RequestRepeatedly; reply(); return; } session.AddComponent(); if (session.DomainScene().SceneType != SceneType.Game) { Log.Debug($"Game 请求的Scene错误...SceneType={session.DomainScene().SceneType}"); session.Dispose(); return; } // 判断参数 if (string.IsNullOrEmpty(request.Name.Trim())) { Log.Debug($"请输入角色名"); response.Error = ErrorCode.ERR_PlayerNameIsNull; reply(); return; } // todo 名字是否合法 if (request.Pro < (byte) PlayerProType.COMMON || request.Pro > (byte) PlayerProType.LI_NHU) { Log.Debug($"该职业有误"); response.Error = ErrorCode.ERR_ProNotProper; reply(); return; } if (request.Sex != (byte) SexType.Man && request.Sex != (byte) SexType.Woman) { Log.Debug($"该性别有误"); response.Error = ErrorCode.ERR_SexNotProper; reply(); return; } // todo 服务器是否已达上限 // 是否有配置 Character prop = CharacterCategory.Instance.Get(request.Pro); if (prop == null) { Log.Debug($"该职业未配置, pro={request.Pro}"); response.Error = ErrorCode.ERR_ProNotProper; reply(); return; } Scene scene = session.DomainScene(); // 创建角色 PlayerInfo playerInfo; long _id = IdGenerater.Instance.GenerateUnitId(scene.DomainZone()); using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.CreatePlayer, _id.ToString().Trim().GetHashCode())) { playerInfo = new PlayerInfo(); playerInfo.Id = _id; playerInfo.UserId = session.GetComponent().UserId; playerInfo.Name = request.Name; playerInfo.Sex = request.Sex; playerInfo.Pro = request.Pro; playerInfo.Level = prop.initLevel; playerInfo.Exp = 0; await DBManagerComponent.Instance.GetZoneDB(session.DomainZone()).Save(playerInfo); } // 创建玩家实体, 初始化各模块数据 WNPlayer player = scene.GetComponent() .AddChildWithId(playerInfo.Id, session, playerInfo); // 添加绑定session角色 session.GetComponent().Add(playerInfo); Log.Info($"创建角色, playerId={playerInfo.Id}, name={playerInfo.Name}, userId={playerInfo.UserId}"); response.Player = PlayerHelper.PlayerInfoToProto(player); reply(); } } }