2023-02-16 21:16:06 +00:00
|
|
|
|
using SharpChat.Misuzu;
|
|
|
|
|
using SharpChat.Packet;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace SharpChat.PacketHandlers {
|
|
|
|
|
public class PingHandler : IChatPacketHandler {
|
|
|
|
|
private readonly MisuzuClient Misuzu;
|
|
|
|
|
|
|
|
|
|
private readonly object BumpAccess = new();
|
|
|
|
|
private readonly TimeSpan BumpInterval = TimeSpan.FromMinutes(1);
|
|
|
|
|
private DateTimeOffset LastBump = DateTimeOffset.MinValue;
|
|
|
|
|
|
|
|
|
|
public PingHandler(MisuzuClient msz) {
|
|
|
|
|
Misuzu = msz ?? throw new ArgumentNullException(nameof(msz));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsMatch(ChatPacketHandlerContext ctx) {
|
|
|
|
|
return ctx.CheckPacketId("0");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Handle(ChatPacketHandlerContext ctx) {
|
|
|
|
|
string[] parts = ctx.SplitText(2);
|
|
|
|
|
|
|
|
|
|
if(!int.TryParse(parts.FirstOrDefault(), out int pTime))
|
|
|
|
|
return;
|
|
|
|
|
|
2023-02-16 21:25:41 +00:00
|
|
|
|
ctx.Connection.BumpPing();
|
|
|
|
|
ctx.Connection.Send(new PongPacket(ctx.Connection.LastPing));
|
2023-02-16 21:16:06 +00:00
|
|
|
|
|
|
|
|
|
lock(BumpAccess) {
|
|
|
|
|
if(LastBump < DateTimeOffset.UtcNow - BumpInterval) {
|
|
|
|
|
(string, string)[] bumpList;
|
|
|
|
|
lock(ctx.Chat.UsersAccess)
|
|
|
|
|
bumpList = ctx.Chat.Users
|
2023-02-16 21:25:41 +00:00
|
|
|
|
.Where(u => u.HasConnections && u.Status == ChatUserStatus.Online)
|
2023-02-16 21:16:06 +00:00
|
|
|
|
.Select(u => (u.UserId.ToString(), u.RemoteAddresses.FirstOrDefault()?.ToString() ?? string.Empty))
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
if(bumpList.Any())
|
|
|
|
|
Task.Run(async () => {
|
|
|
|
|
await Misuzu.BumpUsersOnlineAsync(bumpList);
|
|
|
|
|
}).Wait();
|
|
|
|
|
|
|
|
|
|
LastBump = DateTimeOffset.UtcNow;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|