forked from Freenex1911/FeexHitman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeexHitmanDatabase.cs
214 lines (198 loc) · 9.07 KB
/
FeexHitmanDatabase.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using MySql.Data.MySqlClient;
using Rocket.Core.Logging;
using Steamworks;
using System;
namespace Freenex.FeexHitman
{
public class DatabaseManager
{
internal DatabaseManager()
{
new I18N.West.CP1250();
CreateCheckSchema();
}
private MySqlConnection CreateConnection()
{
MySqlConnection connection = null;
try
{
if (FeexHitman.Instance.Configuration.Instance.FeexHitmanDatabase.DatabasePort == 0) FeexHitman.Instance.Configuration.Instance.FeexHitmanDatabase.DatabasePort = 3306;
connection = new MySqlConnection(String.Format("SERVER={0};DATABASE={1};UID={2};PASSWORD={3};PORT={4};", FeexHitman.Instance.Configuration.Instance.FeexHitmanDatabase.DatabaseAddress, FeexHitman.Instance.Configuration.Instance.FeexHitmanDatabase.DatabaseName, FeexHitman.Instance.Configuration.Instance.FeexHitmanDatabase.DatabaseUsername, FeexHitman.Instance.Configuration.Instance.FeexHitmanDatabase.DatabasePassword, FeexHitman.Instance.Configuration.Instance.FeexHitmanDatabase.DatabasePort));
}
catch (Exception ex)
{
Logger.LogException(ex);
}
return connection;
}
public bool CheckExists(CSteamID id)
{
try
{
MySqlConnection connection = CreateConnection();
MySqlCommand command = connection.CreateCommand();
int exists = 0;
command.CommandText = "SELECT COUNT(1) FROM `" + FeexHitman.Instance.Configuration.Instance.FeexHitmanDatabase.DatabaseTableName + "` WHERE `steamId` = '" + id.ToString() + "';";
connection.Open();
object result = command.ExecuteScalar();
if (result != null) Int32.TryParse(result.ToString(), out exists);
connection.Close();
if (exists == 0) { return false; }
else { return true; }
}
catch (Exception ex)
{
Logger.LogException(ex);
return false;
}
}
public decimal GetBounty(CSteamID id)
{
decimal output = 0;
try
{
MySqlConnection connection = CreateConnection();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT `bounty` FROM `" + FeexHitman.Instance.Configuration.Instance.FeexHitmanDatabase.DatabaseTableName + "` WHERE `steamId` = '" + id.ToString() + "';";
connection.Open();
object result = command.ExecuteScalar();
if (result != null) Decimal.TryParse(result.ToString(), out output);
connection.Close();
}
catch (Exception ex)
{
Logger.LogException(ex);
}
return output;
}
public string GetBountyList()
{
System.Text.StringBuilder HitmanList = new System.Text.StringBuilder();
try
{
MySqlConnection connection = CreateConnection();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT * FROM (SELECT * FROM `" + FeexHitman.Instance.Configuration.Instance.FeexHitmanDatabase.DatabaseTableName + "` WHERE `lastUpdated` ORDER BY `lastUpdated` DESC LIMIT " + FeexHitman.Instance.Configuration.Instance.CommandListMaximum + ") AS tbl ORDER BY `lastUpdated` ASC;";
connection.Open();
MySqlDataReader Reader = command.ExecuteReader();
bool firstPlayer = true;
while (Reader.Read())
{
if (firstPlayer) { firstPlayer = false; }
else { HitmanList.Append(", "); }
HitmanList.Append(Reader.GetString(2));
if (GetBountyCount() > FeexHitman.Instance.Configuration.Instance.CommandListMaximum) { HitmanList.Append(", [...]"); }
}
Reader.Close();
connection.Close();
}
catch (Exception ex)
{
Logger.LogException(ex);
}
return HitmanList.ToString();
}
public int GetBountyCount()
{
int output = 0;
try
{
MySqlConnection connection = CreateConnection();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT COUNT(*) FROM `" + FeexHitman.Instance.Configuration.Instance.FeexHitmanDatabase.DatabaseTableName + "`;";
connection.Open();
object result = command.ExecuteScalar();
if (result != null) int.TryParse(result.ToString(), out output);
connection.Close();
}
catch (Exception ex)
{
Logger.LogException(ex);
}
return output;
}
public void AddUpdateVictimAccount(CSteamID id, decimal bounty, string lastDisplayName)
{
try
{
MySqlConnection connection = CreateConnection();
MySqlCommand command = connection.CreateCommand();
if (CheckExists(id))
{
command.CommandText = "UPDATE `" + FeexHitman.Instance.Configuration.Instance.FeexHitmanDatabase.DatabaseTableName + "` SET `bounty` = bounty + (" + bounty + "), `lastDisplayName` = @lastDisplayName, `lastUpdated` = NOW() WHERE `steamId` = '" + id.ToString() + "';";
}
else
{
command.CommandText = "INSERT IGNORE INTO `" + FeexHitman.Instance.Configuration.Instance.FeexHitmanDatabase.DatabaseTableName + "` (steamId,bounty,lastDisplayName,lastUpdated) VALUES('" + id.ToString() + "','" + bounty + "',@lastDisplayName,NOW());";
}
command.Parameters.AddWithValue("@lastDisplayName", lastDisplayName);
connection.Open();
IAsyncResult result = command.BeginExecuteNonQuery();
command.EndExecuteNonQuery(result);
connection.Close();
}
catch (Exception ex)
{
Logger.LogException(ex);
}
}
public bool RemoveVictimAccount(CSteamID id)
{
try
{
MySqlConnection connection = CreateConnection();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "DELETE FROM `" + FeexHitman.Instance.Configuration.Instance.FeexHitmanDatabase.DatabaseTableName + "` WHERE `steamId`='" + id.ToString() + "';";
connection.Open();
IAsyncResult result = command.BeginExecuteNonQuery();
command.EndExecuteNonQuery(result);
connection.Close();
return true;
}
catch (Exception ex)
{
Logger.LogException(ex);
return false;
}
}
public void UpdateVictimDisplayName(CSteamID id, string lastDisplayName)
{
try
{
MySqlConnection connection = CreateConnection();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "UPDATE `" + FeexHitman.Instance.Configuration.Instance.FeexHitmanDatabase.DatabaseTableName + "` SET `lastDisplayName` = @lastDisplayName WHERE `steamId` = '" + id.ToString() + "';";
command.Parameters.AddWithValue("@lastDisplayName", lastDisplayName);
connection.Open();
IAsyncResult result = command.BeginExecuteNonQuery();
command.EndExecuteNonQuery(result);
connection.Close();
}
catch (Exception ex)
{
Logger.LogException(ex);
}
}
internal void CreateCheckSchema()
{
try
{
MySqlConnection connection = CreateConnection();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "SHOW TABLES LIKE '" + FeexHitman.Instance.Configuration.Instance.FeexHitmanDatabase.DatabaseTableName + "';";
connection.Open();
object test = command.ExecuteScalar();
if (test == null)
{
command.CommandText = "CREATE TABLE `" + FeexHitman.Instance.Configuration.Instance.FeexHitmanDatabase.DatabaseTableName + "` (`steamId` varchar(32) NOT NULL, `bounty` decimal(15,2) NOT NULL DEFAULT '25.00', `lastDisplayName` varchar(32) NOT NULL, `lastUpdated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (`steamId`));";
command.ExecuteNonQuery();
}
connection.Close();
}
catch (Exception ex)
{
Logger.LogException(ex);
}
}
}
}