Files
Fishing2/Assets/Scripts/Model/Login/LoginHelper.cs
2025-11-10 23:59:05 +08:00

83 lines
2.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 roleBag = role.GetOrAddComponent<RoleBag>();
await roleBag.GetBagInfo();
Game.SelfId = loginResponse.RoleId;
var mapId = role.Info.MapId;
if (mapId == 0)
{
Log.Warning("账号没有进入过地图,进入新手引导地图");
mapId = 99;
}
await MapHelper.EnterMap(mapId, role.RoomCode);
}
}
}