123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- using System;
- namespace ET.Server
- {
- /// <summary>
- /// 创建角色
- /// </summary>
- [MessageHandler(SceneType.Game)]
- public class C2G_CreatePlayerHandler: AMRpcHandler<C2G_CreatePlayer, G2C_CreatePlayer>
- {
- protected override async ETTask Run(Session session, C2G_CreatePlayer request, G2C_CreatePlayer response, Action reply)
- {
- // 重复请求
- if (session.GetComponent<SessionLockComponent>() != null)
- {
- response.Error = ErrorCode.ERR_RequestRepeatedly;
- reply();
- return;
- }
- session.AddComponent<SessionLockComponent>();
- 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<SessionPlayerComponent>().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<GamePlayerComponent>()
- .AddChildWithId<WNPlayer, Session, PlayerInfo>(playerInfo.Id, session, playerInfo);
- // 添加绑定session角色
- session.GetComponent<SessionPlayerComponent>().Add(playerInfo);
- Log.Info($"创建角色, playerId={playerInfo.Id}, name={playerInfo.Name}, userId={playerInfo.UserId}");
- response.Player = PlayerHelper.PlayerInfoToProto(player);
- reply();
- }
- }
- }
|