forked from bippity/AdvancedWarpplates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarpplatedb.cs
232 lines (204 loc) · 8.77 KB
/
warpplatedb.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using System;
using System.Collections.Generic;
using Terraria;
using TShockAPI;
using TShockAPI.DB;
using MySql.Data.MySqlClient;
using System.Threading;
using System.Data;
using System.Linq;
using Microsoft.Xna.Framework;
using System.Threading.Tasks;
namespace AdvancedWarpplates
{
public class WarpplateDB
{
private readonly IDbConnection database;
private readonly object syncLock = new object();
public WarpplateDB(IDbConnection db)
{
database = db;
var table = new SqlTable("Warpplates",
new SqlColumn("X1", MySqlDbType.Int32),
new SqlColumn("Y1", MySqlDbType.Int32),
new SqlColumn("width", MySqlDbType.Int32),
new SqlColumn("height", MySqlDbType.Int32),
new SqlColumn("WarpplateName", MySqlDbType.VarChar, 50) { Primary = true },
new SqlColumn("WorldID", MySqlDbType.Text),
new SqlColumn("UserIds", MySqlDbType.Text),
new SqlColumn("Protected", MySqlDbType.Int32),
new SqlColumn("WarpplateDestination", MySqlDbType.VarChar, 50),
new SqlColumn("Type", MySqlDbType.Int32) { DefaultValue = "0", NotNull = true },
new SqlColumn("Delay", MySqlDbType.Int32),
new SqlColumn("Label", MySqlDbType.Text)
);
var creator = new SqlTableCreator(db, db.GetSqlType() == SqlType.Sqlite ? (IQueryBuilder)new SqliteQueryCreator() : new MysqlQueryCreator());
creator.EnsureTableStructure(table);
Task.Run(async () =>
{
await ReloadAllWarpplates();
});
}
internal async Task<QueryResult> QueryReader(string query, params object[] args)
{
return await Task.Run(() =>
{
try
{
lock (syncLock)
{
return database.QueryReader(query, args);
}
}
catch (Exception ex)
{
TShock.Log.Error(ex.ToString());
return null;
}
});
}
internal async Task<int> Query(string query, params object[] args)
{
return await Task.Run(() =>
{
try
{
lock (syncLock)
{
return database.Query(query, args);
}
}
catch (Exception ex)
{
TShock.Log.Error(ex.ToString());
return 0;
}
});
}
public async Task ConvertDB()
{
try
{
await Query("UPDATE Warpplates SET WorldID=@0, UserIds='', Delay=4", Main.worldID.ToString());
await ReloadAllWarpplates();
}
catch (Exception ex)
{
TShock.Log.Error(ex.ToString());
}
}
public async Task<List<Warpplate>> ReloadAllWarpplates()
{
List<Warpplate> warpplates = new List<Warpplate>();
try
{
using (var reader = await QueryReader("SELECT * FROM Warpplates WHERE WorldID=@0", Main.worldID.ToString()))
{
while (reader.Read())
{
int X1 = reader.Get<int>("X1");
int Y1 = reader.Get<int>("Y1");
int height = reader.Get<int>("height");
int width = reader.Get<int>("width");
int Protected = reader.Get<int>("Protected");
string mergedids = reader.Get<string>("UserIds");
string name = reader.Get<string>("WarpplateName");
int type = reader.Get<int>("Type");
string warpdest = reader.Get<string>("WarpplateDestination");
int Delay = reader.Get<int>("Delay");
string label = reader.Get<string>("Label");
string[] splitids = mergedids.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
Warpplate r = new Warpplate(new Vector2(X1, Y1), new Rectangle(X1, Y1, width, height), name, warpdest, Protected != 0, Main.worldID.ToString(), label, type)
{
Delay = Delay
};
try
{
for (int i = 0; i < splitids.Length; i++)
{
if (Int32.TryParse(splitids[i], out int id)) // if unparsable, it's not an int, so silently skip
r.AllowedIDs.Add(id);
else
TShock.Log.Warn("One of your UserIDs is not a usable integer: " + splitids[i]);
}
}
catch (Exception e)
{
TShock.Log.Error("Your database contains invalid UserIDs (they should be ints).");
TShock.Log.Error("A lot of things will fail because of this. You must manually delete and re-create the allowed field.");
TShock.Log.Error(e.ToString());
TShock.Log.Error(e.StackTrace);
}
warpplates.Add(r);
}
}
}
catch (Exception ex)
{
TShock.Log.Error(ex.ToString());
}
return warpplates;
}
public async Task<bool> AddWarpplate(int tx, int ty, int width, int height, string Warpplatename, string Warpdest, string worldid)
{
try
{
int result = await Query("INSERT INTO Warpplates (X1, Y1, width, height, WarpplateName, WorldID, UserIds, Protected, WarpplateDestination, Delay, Label, Type) VALUES (@0, @1, @2, @3, @4, @5, @6, @7, @8, @9, @10, 0);",
tx, ty, width, height, Warpplatename, worldid, "", 1, Warpdest, 4, "");
return result > 0;
}
catch (Exception ex)
{
TShock.Log.Error(ex.ToString());
}
return false;
}
public async Task<bool> DeleteWarpplate(Warpplate warpplate)
{
if (warpplate != null)
{
int result = await Query("DELETE FROM Warpplates WHERE WarpplateName=@0 AND WorldID=@1", warpplate.Name, Main.worldID.ToString());
return result > 0;
}
return false;
}
public async Task<bool> SetWarpplateState(Warpplate warpplate, bool state)
{
try
{
int result = await Query("UPDATE Warpplates SET Protected=@0 WHERE WarpplateName=@1 AND WorldID=@2", state ? 1 : 0, warpplate.Name, Main.worldID.ToString());
return result > 0;
}
catch (Exception ex)
{
TShock.Log.Error(ex.ToString());
}
return false;
}
public async Task<bool> UpdateWarpplate(Warpplate warpplate)
{
try
{
int result = await Query("UPDATE Warpplates SET width=@0, height=@1, Delay=@2, Label=@3 WHERE WarpplateName=@4 AND WorldID=@5",
warpplate.Area.Width, warpplate.Area.Height, warpplate.Delay, warpplate.Label, warpplate.Name, Main.worldID.ToString());
return result > 0;
}
catch (Exception ex)
{
TShock.Log.Error(ex.ToString());
}
return false;
}
public async Task<bool> RemoveDestination(Warpplate warpplate)
{
int result = await Query("UPDATE Warpplates SET WarpplateDestination=@0 WHERE WarpplateName=@1 AND WorldID=@2", "", warpplate.Name, Main.worldID.ToString());
return result > 0;
}
public async Task<bool> SetDestination(Warpplate warpplate, string warpplateDestination, int type = 0)
{
int result = await Query("UPDATE Warpplates SET WarpplateDestination=@0, Type=@3 WHERE WarpplateName=@1 AND WorldID=@2;", warpplateDestination, warpplate.Name, Main.worldID.ToString(), type);
warpplate.Destination = warpplateDestination;
return result > 0;
}
}
}