72 lines
2.9 KiB
C#
72 lines
2.9 KiB
C#
using Microsoft.AspNetCore.RateLimiting;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using Newtonsoft.Json;
|
|
using QuestShare.Common.API;
|
|
using QuestShare.Common.API.Share;
|
|
using QuestShare.Server.Managers;
|
|
using QuestShare.Server.Models;
|
|
|
|
namespace QuestShare.Server.Hubs
|
|
{
|
|
public partial class ShareHub : Hub
|
|
{
|
|
[HubMethodName(nameof(Update))]
|
|
[EnableRateLimiting("ClientPolicy")]
|
|
public async Task Server_Update(Update.Request request)
|
|
{
|
|
Log.Debug($"[UPDATE] Client {Context.ConnectionId} requested update.");
|
|
if (BanManager.IsBanned(Context))
|
|
{
|
|
Context.Abort();
|
|
return;
|
|
}
|
|
if (ClientManager.GetClient(Context.ConnectionId) == null)
|
|
{
|
|
await Clients.Caller.SendAsync(nameof(Update), new Update.Response
|
|
{
|
|
Success = false,
|
|
Error = Error.Unauthorized,
|
|
});
|
|
return;
|
|
}
|
|
var client = ClientManager.GetClient(Context.ConnectionId);
|
|
var error = Error.None;
|
|
if (request.Token == "") error = Error.InvalidToken;
|
|
if (client == null) error = Error.Unauthorized;
|
|
else if (request.Version != Common.Constants.Version) error = Error.InvalidVersion;
|
|
var share = ShareManager.GetShare(client!);
|
|
if (share == null) error = Error.ShareNotFound;
|
|
if (error != Error.None)
|
|
{
|
|
Log.Warning($"[UPDATE] Client {Context.ConnectionId} failed update with error {error}.");
|
|
await Clients.Caller.SendAsync(nameof(Update), new Update.Response
|
|
{
|
|
Success = false,
|
|
Error = error,
|
|
});
|
|
return;
|
|
}
|
|
if (request.IsPartyChange)
|
|
{
|
|
ShareManager.SetBroadcastPartyMembers(share!.ShareHost.CharacterId, request.PartyMembers);
|
|
} else
|
|
{
|
|
await ShareManager.UpdateActiveQuest(share!.ShareCode, request.SharedQuestId, request.SharedQuestStep);
|
|
// broadcast to party
|
|
Log.Debug($"[UPDATE] Broadcasting quests to party members for share {share.ShareCode}, excluding {Context.ConnectionId}");
|
|
await Clients.GroupExcept(share.ShareCode, Context.ConnectionId).SendAsync(nameof(Update.UpdateBroadcast), new Update.UpdateBroadcast
|
|
{
|
|
ShareCode = share.ShareCode,
|
|
SharedQuestId = request.SharedQuestId,
|
|
SharedQuestStep = request.SharedQuestStep,
|
|
});
|
|
}
|
|
Log.Debug($"[UPDATE] Client {Context.ConnectionId} updated quest for share {share.ShareCode}.");
|
|
await Clients.Caller.SendAsync(nameof(Update), new Update.Response
|
|
{
|
|
Success = true,
|
|
});
|
|
}
|
|
}
|
|
}
|