forked from Skippeh/Oxide.GettingOverItMP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerBan.cs
83 lines (68 loc) · 2.79 KB
/
PlayerBan.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Net;
using Newtonsoft.Json;
using ServerShared.Player;
namespace ServerShared
{
public class PlayerBan
{
public uint Ip => Identity.Ip;
public ulong SteamId => Identity.SteamId;
public IdentityType BanType => Identity.Type;
public PlayerIdentity Identity;
public DateTime? ExpirationDate;
public string Reason;
public string ReferenceName;
/// <param name="reason">Optional reason, null is allowed.</param>
/// <param name="referenceName">Optional name reference, null is allowed.</param>
public PlayerBan(ulong steamId, string reason, DateTime? expirationDate, string referenceName) : this(reason, expirationDate, referenceName)
{
Identity = new PlayerIdentity(steamId);
}
/// <param name="reason">Optional reason, null is allowed.</param>
/// <param name="referenceName">Optional name reference, null is allowed.</param>
public PlayerBan(uint ip, string reason, DateTime? expirationDate, string referenceName) : this(reason, expirationDate, referenceName)
{
Identity = new PlayerIdentity(ip);
}
private PlayerBan(string reason, DateTime? expirationDate, string referenceName)
{
Reason = reason;
ExpirationDate = expirationDate;
ReferenceName = referenceName;
}
/// <summary>Do not use. Exclusively used for serialization/deserialization</summary>
[JsonConstructor]
private PlayerBan() { }
public bool Expired()
{
if (ExpirationDate == null)
return false;
return DateTime.UtcNow >= ExpirationDate;
}
public string GetReasonWithExpiration()
{
string reason = Reason ?? "No reason given";
string result = $"Reason: {reason}.";
if (ExpirationDate != null)
{
TimeSpan timeLeft = ExpirationDate.Value - DateTime.UtcNow;
int hours = timeLeft.Hours;
int minutes = timeLeft.Minutes;
int seconds = timeLeft.Seconds;
string daysString = "";
if (timeLeft.TotalDays >= 1)
{
daysString = $"{(int) timeLeft.TotalDays} days, ";
}
result += $"\nExpires in: {daysString}{hours.ToString().PadLeft(2, '0')}h {minutes.ToString().PadLeft(2, '0')}m {seconds.ToString().PadLeft(2, '0')}s.";
}
return result;
}
/// <summary>Returns a user friendly string representing the ban type and the identifier of the ban (ip/steamid).</summary>
public string GetIdentifier()
{
return Identity.ToString();
}
}
}