sharp-chat/SharpChat/Packet/UserDisconnectPacket.cs

57 lines
1.7 KiB
C#

using System;
using System.Text;
namespace SharpChat.Packet {
public enum UserDisconnectReason {
Leave,
TimeOut,
Kicked,
Flood,
}
public class UserDisconnectPacket : ServerPacket {
public DateTimeOffset Disconnected { get; private set; }
public ChatUser User { get; private set; }
public UserDisconnectReason Reason { get; private set; }
public UserDisconnectPacket(DateTimeOffset disconnected, ChatUser user, UserDisconnectReason reason) {
Disconnected = disconnected;
User = user ?? throw new ArgumentNullException(nameof(user));
Reason = reason;
}
public override string Pack() {
StringBuilder sb = new();
sb.Append('3');
sb.Append('\t');
sb.Append(User.UserId);
sb.Append('\t');
sb.Append(User.LegacyNameWithStatus);
sb.Append('\t');
switch(Reason) {
case UserDisconnectReason.Leave:
default:
sb.Append("leave");
break;
case UserDisconnectReason.TimeOut:
sb.Append("timeout");
break;
case UserDisconnectReason.Kicked:
sb.Append("kick");
break;
case UserDisconnectReason.Flood:
sb.Append("flood");
break;
}
sb.Append('\t');
sb.Append(Disconnected.ToUnixTimeSeconds());
sb.Append('\t');
sb.Append(SequenceId);
return sb.ToString();
}
}
}