77 lines
2.4 KiB
C#
77 lines
2.4 KiB
C#
using Assets.Scripts.Entity;
|
||
using Assets.Scripts.Hotfix;
|
||
using NBC;
|
||
using NBC.Network;
|
||
using NBF.Fishing2;
|
||
using Newtonsoft.Json;
|
||
|
||
namespace NBF
|
||
{
|
||
public static class LoginHelper
|
||
{
|
||
private static Session _session;
|
||
|
||
public static async FTask Login(string account)
|
||
{
|
||
var oldRole = App.Main.GetComponent<Role>();
|
||
if (oldRole != null)
|
||
{
|
||
App.Main.RemoveComponent<Role>();
|
||
}
|
||
|
||
_session = Net.CreateSession("127.0.0.1:20001");
|
||
|
||
// _session.Scene.AddComponent<UnitUnityComponent>();
|
||
|
||
var acc = account;
|
||
|
||
// 发送登录的请求给服务器
|
||
var response = (A2C_LoginResponse)await Net.Call(new C2A_LoginRequest()
|
||
{
|
||
Username = acc,
|
||
Password = acc,
|
||
LoginType = 1
|
||
});
|
||
|
||
if (response.ErrorCode != 0)
|
||
{
|
||
Log.Error($"登录发生错误{response.ErrorCode}");
|
||
return;
|
||
}
|
||
|
||
if (!App.Main.GetComponent<JWTParseComponent>().Parse(response.ToKen, out var payload))
|
||
{
|
||
return;
|
||
}
|
||
|
||
// 根据ToKen返回的Address登录到Gate服务器
|
||
_session = Net.CreateSession(payload.Address);
|
||
|
||
// 发送登录请求到Gate服务器
|
||
var loginResponse = (G2C_LoginResponse)await Net.Call(new C2G_LoginRequest()
|
||
{
|
||
ToKen = response.ToKen
|
||
});
|
||
if (loginResponse.ErrorCode != 0)
|
||
{
|
||
Log.Error($"登录发生错误{loginResponse.ErrorCode}");
|
||
return;
|
||
}
|
||
|
||
var role = App.Main.AddComponent<Role>(loginResponse.RoleId);
|
||
Log.Debug($"登录到Gate服务器成功!ErrorCode:{loginResponse.ErrorCode}");
|
||
await role.GetRoleInfo();
|
||
Log.Debug(
|
||
$"获取角色信息成功!roleId={role.Id} Room={role.RoomCode} RoleInfo={JsonConvert.SerializeObject(role.Info)}");
|
||
var mapId = role.Info.MapId;
|
||
if (mapId == 0)
|
||
{
|
||
Log.Warning("账号没有进入过地图,进入新手引导地图");
|
||
mapId = 99;
|
||
}
|
||
|
||
Game.SelfId = loginResponse.RoleId;
|
||
await MapHelper.EnterMap(mapId, role.RoomCode);
|
||
}
|
||
}
|
||
} |