/
PlayerData.cs
189 lines (155 loc) · 7.51 KB
/
PlayerData.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
Copyright 2015 MCGalaxy
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using System.Data;
using MCGalaxy.SQL;
namespace MCGalaxy.DB {
/// <summary> Retrieves or sets player stats in the database. </summary>
public class PlayerData {
public const string ColumnDeaths = "totalDeaths";
public const string ColumnLogins = "totalLogin";
public const string ColumnMoney = "Money";
public const string ColumnKicked = "totalKicked";
public const string ColumnColor = "color";
public const string ColumnTitle = "title";
public const string ColumnTColor = "title_color";
public const string ColumnName = "Name";
public const string ColumnIP = "IP";
public const string ColumnID = "ID";
public const string ColumnFirstLogin = "FirstLogin";
public const string ColumnLastLogin = "LastLogin";
public const string ColumnTimeSpent = "TimeSpent";
public const string ColumnBlocks = "totalBlocks";
public const string ColumnDrawn = "totalCuboided";
public const string ColumnMessages = "Messages";
public string Name, Color, Title, TitleColor, IP;
public DateTime FirstLogin, LastLogin;
public int DatabaseID, Money, Deaths, Logins, Kicks, Messages;
public long TotalModified, TotalDrawn, TotalPlaced, TotalDeleted;
public TimeSpan TotalTime;
internal static void Create(Player p) {
p.prefix = "";
p.SetColor(p.group.Color);
p.FirstLogin = DateTime.Now;
p.TimesVisited = 1;
string now = DateTime.Now.ToString(Database.DateFormat);
Database.AddRow("Players", "Name, IP, FirstLogin, LastLogin, totalLogin, Title, " +
"totalDeaths, Money, totalBlocks, totalKicked, Messages, TimeSpent",
p.name, p.ip, now, now, 1, "", 0, 0, 0, 0, 0, (long)p.TotalTime.TotalSeconds);
int id = -200;
Database.ReadRows("Players", "ID",
record => id = record.GetInt32(0),
"WHERE Name=@0", p.name);
if (id != -200) {
p.DatabaseID = id;
} else {
p.DatabaseID = NameConverter.InvalidNameID(p.name);
}
}
/// <summary> Initialises the given player's stats from this instance. </summary>
public void ApplyTo(Player p) {
p.TimesVisited = Logins + 1;
p.TotalTime = TotalTime;
p.DatabaseID = DatabaseID;
p.FirstLogin = FirstLogin;
p.title = Title;
p.titlecolor = TitleColor;
string col = Color;
if (col.Length == 0) col = p.group.Color;
p.SetColor(col);
p.SetBaseTotalModified(TotalModified);
p.TotalDrawn = TotalDrawn;
p.TotalPlaced = TotalPlaced;
p.TotalDeleted = TotalDeleted;
p.TimesDied = Deaths;
p.TotalMessagesSent = Messages;
p.money = Money;
p.TimesBeenKicked = Kicks;
}
internal static PlayerData Parse(IDataRecord record) {
PlayerData data = new PlayerData();
data.Name = record.GetText(ColumnName);
data.IP = record.GetText(ColumnIP);
data.DatabaseID = record.GetInt(ColumnID);
// Backwards compatibility with old format
string rawTime = record.GetText(ColumnTimeSpent);
try {
long secs = long.Parse(rawTime);
data.TotalTime = TimeSpan.FromSeconds(secs);
} catch {
data.TotalTime = rawTime.ParseOldDBTimeSpent();
}
data.FirstLogin = ParseDateTime(record, ColumnFirstLogin);
data.LastLogin = ParseDateTime(record, ColumnLastLogin);
data.Title = record.GetText(ColumnTitle);
data.Title = data.Title.Cp437ToUnicode();
data.TitleColor = ParseColor(record.GetText(ColumnTColor));
data.Color = ParseColor(record.GetText(ColumnColor));
data.Money = record.GetInt(ColumnMoney);
data.Deaths = record.GetInt(ColumnDeaths);
data.Logins = record.GetInt(ColumnLogins);
data.Kicks = record.GetInt(ColumnKicked);
data.Messages = record.GetInt(ColumnMessages);
long blocks = record.GetLong(ColumnBlocks);
long drawn = record.GetLong(ColumnDrawn);
data.TotalModified = UnpackLo(blocks);
data.TotalPlaced = UnpackHi(blocks);
data.TotalDrawn = UnpackLo(drawn);
data.TotalDeleted = UnpackHi(drawn);
return data;
}
internal static long ParseLong(string value) {
return (value.Length == 0 || value.CaselessEq("null")) ? 0 : long.Parse(value);
}
internal static int ParseInt(string value) {
return (value.Length == 0 || value.CaselessEq("null")) ? 0 : int.Parse(value);
}
internal static string ParseColor(string raw) {
if (raw.Length == 0) return raw;
// Try parse color name, then color code
string col = Colors.Parse(raw);
if (col.Length > 0) return col;
return Colors.Name(raw).Length == 0 ? "" : raw;
}
static DateTime ParseDateTime(IDataRecord record, string name) {
int i = record.GetOrdinal(name);
// dates are a major pain
try {
string raw = record.GetStringValue(i);
return DateTime.ParseExact(raw, Database.DateFormat, null);
} catch {
try {
return record.GetDateTime(i);
} catch (Exception ex) {
Logger.LogError("Error parsing date", ex);
return DateTime.MinValue;
}
}
}
internal static long UnpackHi(long value) {
return (value >> HiBitsShift) & HiBitsMask;
}
internal static long UnpackLo(long value) {
return value & LoBitsMask;
}
internal static long Pack(long hi, long lo) {
return hi << HiBitsShift | lo;
}
public const int HiBitsShift = 38;
public const long LoBitsMask = (1L << HiBitsShift) - 1;
// convert negative to positive after shifting
public const long HiBitsMask = (1L << 26) - 1;
}
}