C2G_LoginGateHandler.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using Model;
  3. namespace Hotfix
  4. {
  5. [MessageHandler(AppType.Gate)]
  6. public class C2G_LoginGateHandler : AMRpcHandler<C2G_LoginGate, G2C_LoginGate>
  7. {
  8. protected override async void Run(Session session, C2G_LoginGate message, Action<G2C_LoginGate> reply)
  9. {
  10. G2C_LoginGate response = new G2C_LoginGate();
  11. try
  12. {
  13. string account = Game.Scene.GetComponent<GateSessionKeyComponent>().Get(message.Key);
  14. if (account == null)
  15. {
  16. response.Error = ErrorCode.ERR_ConnectGateKeyError;
  17. response.Message = "Gate key验证失败!";
  18. reply(response);
  19. return;
  20. }
  21. Player player = EntityFactory.Create<Player, string>(account);
  22. Game.Scene.GetComponent<PlayerComponent>().Add(player);
  23. session.AddComponent<SessionPlayerComponent>().Player = player;
  24. await session.AddComponent<ActorComponent, IEntityActorHandler>(new GateSessionEntityActorHandler()).AddLocation();
  25. // 在map服务器上创建战斗Unit
  26. string mapAddress = Game.Scene.GetComponent<StartConfigComponent>().MapConfigs[0].GetComponent<InnerConfig>().Address;
  27. Session mapSession = Game.Scene.GetComponent<NetInnerComponent>().Get(mapAddress);
  28. M2G_CreateUnit createUnit = await mapSession.Call<M2G_CreateUnit>(new G2M_CreateUnit() { PlayerId = player.Id, GateSessionId = session.Id });
  29. player.UnitId = createUnit.UnitId;
  30. response.PlayerId = player.Id;
  31. response.UnitId = createUnit.UnitId;
  32. reply(response);
  33. }
  34. catch (Exception e)
  35. {
  36. ReplyError(response, e, reply);
  37. }
  38. }
  39. }
  40. }