forked from cartman-2000/PlayerInfoLib
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Extensions.cs
75 lines (68 loc) · 2.69 KB
/
Extensions.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
using MySql.Data.MySqlClient;
using SDG.Unturned;
using Steamworks;
using System;
namespace PlayerInfoLibrary
{
public static class Extensions
{
public static DateTime FromTimeStamp(this long timestamp)
{
return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(timestamp).ToLocalTime();
}
public static long ToTimeStamp(this DateTime datetime)
{
return (long)(datetime.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds);
}
public static bool IsDBNull (this MySqlDataReader reader, string fieldname)
{
return reader.IsDBNull(reader.GetOrdinal(fieldname));
}
public static string GetIP(this CSteamID cSteamID)
{
// Grab an active players ip address from CSteamID.
P2PSessionState_t sessionState;
SteamGameServerNetworking.GetP2PSessionState(cSteamID, out sessionState);
return Parser.getIPFromUInt32(sessionState.m_nRemoteIP);
}
// Returns a Steamworks.CSteamID on out from a string, and returns true if it is a CSteamID.
public static bool isCSteamID(this string sCSteamID, out CSteamID cSteamID)
{
ulong ulCSteamID;
cSteamID = (CSteamID)0;
if (ulong.TryParse(sCSteamID, out ulCSteamID))
{
if ((ulCSteamID >= 0x0110000100000000 && ulCSteamID <= 0x0170000000000000) || ulCSteamID == 0)
{
cSteamID = (CSteamID)ulCSteamID;
return true;
}
}
return false;
}
public static string Truncate(this string value, int maxLength)
{
if (string.IsNullOrEmpty(value)) return value;
return value.Length <= maxLength ? value : value.Substring(0, maxLength);
}
// Returns formatted string with how long they've played on the server in d, h, m, s.
public static string FormatTotalTime(this int totalTime)
{
string totalTimeFormated = "";
if (totalTime >= (60 * 60 * 24))
{
totalTimeFormated = ((int)(totalTime / (60 * 60 * 24))).ToString() + "d ";
}
if (totalTime >= (60 * 60))
{
totalTimeFormated += ((int)((totalTime / (60 * 60)) % 24)).ToString() + "h ";
}
if (totalTime >= 60)
{
totalTimeFormated += ((int)((totalTime / 60) % 60)).ToString() + "m ";
}
totalTimeFormated += ((int)(totalTime % 60)).ToString() + "s";
return totalTimeFormated;
}
}
}