-
Notifications
You must be signed in to change notification settings - Fork 10
/
Database.cs
198 lines (195 loc) · 8.01 KB
/
Database.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
using MySql.Data.MySqlClient;
using Rocket.Core.Logging;
using SDG.Unturned;
using Steamworks;
using System;
using System.Collections.Generic;
namespace fr34kyn01535.ItemVault
{
internal class Database
{
private static MySqlConnection createConnection()
{
MySqlConnection result = null;
try
{
result = new MySqlConnection(String.Format("SERVER={0};DATABASE={1};UID={2};PASSWORD={3};PORT={4};", ItemVault.Instance.Configuration.Instance.DatabaseAddress, ItemVault.Instance.Configuration.Instance.DatabaseName, ItemVault.Instance.Configuration.Instance.DatabaseUsername, ItemVault.Instance.Configuration.Instance.DatabasePassword, ItemVault.Instance.Configuration.Instance.DatabasePort));
return result;
}
catch (Exception ex)
{
Logger.LogException(ex);
}
return result;
}
public static void CheckSchema()
{
try
{
MySqlConnection mySqlConnection = Database.createConnection();
MySqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText = "show tables like '" + ItemVault.Instance.Configuration.Instance.DatabaseTableName + "'";
mySqlConnection.Open();
if (mySqlCommand.ExecuteScalar() == null)
{
mySqlCommand.CommandText = "CREATE TABLE `" + ItemVault.Instance.Configuration.Instance.DatabaseTableName + "` (`id` int(11) NOT NULL AUTO_INCREMENT,`durability` int(3) NOT NULL,`stacksize` int(11) NULL,`x` int(11) NULL,`y` int(11) NULL,`rotation` int(11) NULL,`itemid` int(4) NOT NULL,`metadata` varchar(255) NOT NULL,`csteamid` varchar(32) NOT NULL,PRIMARY KEY (`id`)) ";
mySqlCommand.ExecuteNonQuery();
}
mySqlConnection.Close();
}
catch (Exception ex)
{
Logger.LogException(ex);
}
}
internal static List<ItemJar> RetrieveItems(CSteamID csteamid)
{
List<ItemJar> list = new List<ItemJar>();
try
{
int num = 0;
MySqlConnection mySqlConnection = Database.createConnection();
MySqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText = string.Concat(new object[]
{
"select count(*) from `",
ItemVault.Instance.Configuration.Instance.DatabaseTableName,
"` where `csteamid` = ",
csteamid,
";"
});
mySqlConnection.Open();
object obj = mySqlCommand.ExecuteScalar();
if (obj != null)
{
num = int.Parse(obj.ToString());
}
mySqlConnection.Close();
if (num != 0)
{
mySqlCommand.CommandText = string.Concat(new object[]
{
"select * from `",
ItemVault.Instance.Configuration.Instance.DatabaseTableName,
"` where `csteamid` = '",
csteamid,
"' order by id;"
});
mySqlConnection.Open();
MySqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
while (mySqlDataReader.Read())
{
list.Add(new ItemJar((byte)mySqlDataReader.GetInt32("x"), (byte)mySqlDataReader.GetInt32("y"), (byte)mySqlDataReader.GetInt32("rotation"), new Item(mySqlDataReader.GetUInt16("itemid"), true)
{
durability = (byte)mySqlDataReader.GetInt32("durability"),
metadata = Convert.FromBase64String(mySqlDataReader.GetString("metadata")),
amount = (byte)mySqlDataReader.GetInt32("stacksize")
}));
}
mySqlConnection.Close();
}
}
catch (Exception ex)
{
Logger.LogException(ex);
}
return list;
}
internal static void UpdateItem(CSteamID cSteamID, ItemJar item)
{
try
{
MySqlConnection mySqlConnection = Database.createConnection();
MySqlCommand mySqlCommand = mySqlConnection.CreateCommand();
string text = (item.item.metadata == null) ? "" : Convert.ToBase64String(item.item.metadata);
mySqlCommand.CommandText = string.Concat(new object[]
{
"update `",
ItemVault.Instance.Configuration.Instance.DatabaseTableName,
"` set `metadata`='"+text+"' where `csteamid`='"+cSteamID+"' and `itemid` = "+ item.item.id+" limit 1;"
});
mySqlConnection.Open();
mySqlCommand.ExecuteNonQuery();
mySqlConnection.Close();
}
catch (Exception ex)
{
Logger.LogException(ex);
}
}
internal static bool AddItem(CSteamID csteamid, ItemJar item)
{
try
{
MySqlConnection mySqlConnection = Database.createConnection();
MySqlCommand mySqlCommand = mySqlConnection.CreateCommand();
string text = (item.item.metadata == null) ? "" : Convert.ToBase64String(item.item.metadata);
mySqlCommand.CommandText = string.Concat(new object[]
{
"insert into `",
ItemVault.Instance.Configuration.Instance.DatabaseTableName,
"` (`csteamid`,`durability`,`x`,`y`,`rotation`,`metadata`,`itemid`,`stacksize`) values(",
csteamid,
",",
item.item.durability,
",",
item.x,
",",
item.y,
",",
item.rot,
",'",
text,
"',",
item.item.id,
",",
item.item.amount,
");"
});
mySqlConnection.Open();
mySqlCommand.ExecuteNonQuery();
mySqlConnection.Close();
return true;
}
catch (Exception ex)
{
Logger.LogException(ex);
}
return false;
}
internal static bool DeleteItem(CSteamID csteamid, ItemJar item)
{
try
{
MySqlConnection mySqlConnection = Database.createConnection();
MySqlCommand mySqlCommand = mySqlConnection.CreateCommand();
string text = (item.item.metadata == null) ? "" : Convert.ToBase64String(item.item.metadata);
mySqlCommand.CommandText = string.Concat(new object[]
{
"delete from `",
ItemVault.Instance.Configuration.Instance.DatabaseTableName,
"` where csteamid = ",
csteamid,
" and durability = ",
item.item.durability,
" and metadata = '",
text,
"' and itemid = ",
item.item.id,
" and stacksize = ",
item.item.amount,
" limit 1;"
});
mySqlConnection.Open();
mySqlCommand.ExecuteScalar();
mySqlConnection.Close();
return true;
}
catch (Exception ex)
{
Logger.LogException(ex);
}
return false;
}
}
}