2025-02-17 22:12:35 -05:00

111 lines
4.6 KiB
C#

using Microsoft.AspNetCore.SignalR;
using QuestShare.Server.Managers;
namespace QuestShare.Server.Hubs
{
public partial class ShareHub : Hub
{
[HubMethodName(nameof(Resume))]
public async Task Server_Resume(Resume.Request request)
{
if (BanManager.IsBanned(Context))
{
Log.Error($"[RESUME] Client {Context.ConnectionId} is banned.");
Context.Abort();
return;
}
var error = Error.None;
var client = ClientManager.GetClient(Context.ConnectionId, request.Token);
if (client == null)
{
error = Error.Unauthorized;
if (BanManager.CheckBadRequests(Context, nameof(Resume)))
{
error = Error.BannedTooManyBadRequests;
}
}
else if (request.Token == "") error = Error.InvalidToken;
else if (request.Version != Common.Constants.Version) error = Error.InvalidVersion;
if (error != Error.None)
{
Log.Warning($"[RESUME] Client {Context.ConnectionId} failed resume with error {error}.");
await Clients.Caller.SendAsync(nameof(Resume), new Resume.Response
{
Success = false,
Error = error,
});
return;
}
var share = ShareManager.GetShare(client!);
if (share == null)
{
if (request.ShareCode == "")
{
Log.Warning($"[RESUME] Client {Context.ConnectionId} failed resume with error {error}.");
await Clients.Caller.SendAsync(nameof(Resume), new Resume.Response
{
Success = false,
Error = Error.ShareNotFound,
});
return;
}
share = ShareManager.GetShare(request.ShareCode);
if (share == null)
{
Log.Warning($"[RESUME] Client {Context.ConnectionId} failed resume with error {error}.");
await Clients.Caller.SendAsync(nameof(Resume), new Resume.Response
{
Success = false,
Error = Error.ShareNotFound,
});
return;
}
await Groups.AddToGroupAsync(Context.ConnectionId, share.ShareCode);
var members = await ShareManager.GetShareMembers(share);
await Clients.Caller.SendAsync(nameof(Resume), new Resume.Response
{
Success = true,
SharedQuestId = share.SharedQuestId,
SharedQuestStep = share.SharedQuestStep,
Members = members.Select(m => m.CharacterId).ToList(),
IsGroup = true,
ShareCode = share.ShareCode
});
await Clients.GroupExcept(share.ShareCode, Context.ConnectionId).SendAsync(nameof(Resume), new GroupNotify.GroupNotifyBroadcast
{
CharacterId = client!.CharacterId,
NotifyType = NotifyType.Rejoin,
ShareCode = share.ShareCode,
});
Log.Information($"[RESUME] Client {Context.ConnectionId} resumed share {share.ShareCode}. (Member)");
}
else
{
Log.Information($"[RESUME] Client {Context.ConnectionId} resumed share {share.ShareCode}. (Host)");
List<ulong> members = [];
if (share.BroadcastParty)
{
if (share.ShareHost.CharacterId == client!.CharacterId && ShareManager.HasBroadcastParty(share.ShareHost.CharacterId))
{
members = request.Members ?? [];
ShareManager.SetBroadcastPartyMembers(client!.CharacterId, members);
} else
{
members = ShareManager.GetBroadcastPartyMembers(share.ShareHost.CharacterId);
}
}
await Clients.Caller.SendAsync(nameof(Resume), new Resume.Response
{
Success = true,
SharedQuestId = share.SharedQuestId,
SharedQuestStep = share.SharedQuestStep,
Members = members ?? [],
IsHost = true,
ShareCode = share.ShareCode
});
}
}
}
}