forked from Darky2k1/Advanced-Warpplates
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwarpplatedb.cs
365 lines (329 loc) · 13 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
TShock, a server mod for Terraria
Copyright (C) 2011 The TShock Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using Terraria;
using Hooks;
using TShockAPI;
using TShockAPI.DB;
using MySql.Data.MySqlClient;
using System.Threading;
using System.ComponentModel;
using System.IO;
using System.Data;
using System.Drawing;
using System.Linq;
namespace PluginTemplate
{
public class WarpplateManager
{
public List<Warpplate> Warpplates = new List<Warpplate>();
private IDbConnection database;
public WarpplateManager(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("Delay", MySqlDbType.Int32),
new SqlColumn("Label", MySqlDbType.Text)
);
var creator = new SqlTableCreator(db, db.GetSqlType() == SqlType.Sqlite ? (IQueryBuilder)new SqliteQueryCreator() : new MysqlQueryCreator());
creator.EnsureExists(table);
ReloadAllWarpplates();
}
public void ConvertDB()
{
try
{
database.Query("UPDATE Warpplates SET WorldID=@0, UserIds='', Delay=4", Main.worldID.ToString());
ReloadAllWarpplates();
}
catch (Exception ex)
{
Log.Error(ex.ToString());
}
}
public void ReloadAllWarpplates()
{
try
{
using (var reader = database.QueryReader("SELECT * FROM Warpplates WHERE WorldID=@0", Main.worldID.ToString()))
{
Warpplates.Clear();
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");
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);
r.Delay = Delay;
try
{
for (int i = 0; i < splitids.Length; i++)
{
int id;
if (Int32.TryParse(splitids[i], out id)) // if unparsable, it's not an int, so silently skip
r.AllowedIDs.Add(id);
else
Log.Warn("One of your UserIDs is not a usable integer: " + splitids[i]);
}
}
catch (Exception e)
{
Log.Error("Your database contains invalid UserIDs (they should be ints).");
Log.Error("A lot of things will fail because of this. You must manually delete and re-create the allowed field.");
Log.Error(e.ToString());
Log.Error(e.StackTrace);
}
Warpplates.Add(r);
}
}
}
catch (Exception ex)
{
Log.Error(ex.ToString());
}
}
public bool AddWarpplate(int tx, int ty, int width, int height, string Warpplatename, string Warpdest, string worldid)
{
if (GetWarpplateByName(Warpplatename) != null)
{
return false;
}
try
{
database.Query("INSERT INTO Warpplates (X1, Y1, width, height, WarpplateName, WorldID, UserIds, Protected, WarpplateDestination, Delay, Label) VALUES (@0, @1, @2, @3, @4, @5, @6, @7, @8, @9, @10);",
tx, ty, width, height, Warpplatename, worldid, "", 1, Warpdest, 4, "");
Warpplates.Add(new Warpplate(new Vector2(tx, ty), new Rectangle(tx, ty, width, height), Warpplatename, worldid, true, Warpdest, ""));
return true;
}
catch (Exception ex)
{
Log.Error(ex.ToString());
}
return false;
}
public bool DeleteWarpplate(string name)
{
Warpplate r = GetWarpplateByName(name);
if (r != null)
{
int q = database.Query("DELETE FROM Warpplates WHERE WarpplateName=@0 AND WorldID=@1", name, Main.worldID.ToString());
Warpplates.Remove(r);
if (q > 0)
return true;
}
return false;
}
public bool SetWarpplateState(string name, bool state)
{
var Warpplate = GetWarpplateByName(name);
if (Warpplate != null)
{
try
{
Warpplate.DisableBuild = state;
database.Query("UPDATE Warpplates SET Protected=@0 WHERE WarpplateName=@1 AND WorldID=@2", state ? 1 : 0, name, Main.worldID.ToString());
return true;
}
catch (Exception ex)
{
Log.Error(ex.ToString());
}
}
return false;
}
public bool UpdateWarpplate(string name)
{
var wp = GetWarpplateByName(name);
if (wp != null)
{
try
{
database.Query("UPDATE Warpplates SET width=@0, height=@1, Delay=@2, Label=@3 WHERE WarpplateName=@4 AND WorldID=@5",
wp.Area.Width, wp.Area.Height, wp.Delay, wp.Label, name, Main.worldID.ToString());
return true;
}
catch (Exception ex)
{
Log.Error(ex.ToString());
}
}
return false;
}
public Warpplate FindWarpplate(string name)
{
try
{
foreach (Warpplate wp in Warpplates)
{
if (wp.Name == name)
return wp;
}
}
catch (Exception ex)
{
Log.Error(ex.ToString());
}
//return new Warpplate(); // <-- that's dumb
return null;
}
public bool InArea(int x, int y)
{
foreach (Warpplate Warpplate in Warpplates)
{
if (x >= Warpplate.Area.Left && x <= Warpplate.Area.Right &&
y >= Warpplate.Area.Top && y <= Warpplate.Area.Bottom &&
Warpplate.DisableBuild)
{
return true;
}
}
return false;
}
public string InAreaWarpplateName(int x, int y)
{
foreach (Warpplate Warpplate in Warpplates)
{
if (x >= Warpplate.Area.Left && x <= Warpplate.Area.Right &&
y >= Warpplate.Area.Top && y <= Warpplate.Area.Bottom &&
Warpplate.DisableBuild)
{
return Warpplate.Name;
}
}
return null;
}
public static List<string> ListIDs(string MergedIDs)
{
return MergedIDs.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
public bool removedestination(string WarpplateName)
{
Warpplate r = GetWarpplateByName(WarpplateName);
if (r != null)
{
int q = database.Query("UPDATE Warpplates SET WarpplateDestination=@0 WHERE WarpplateName=@1 AND WorldID=@2", "", WarpplateName, Main.worldID.ToString());
r.WarpDest = "";
if (q > 0)
return true;
}
return false;
}
public bool adddestination(string WarpplateName, String WarpDestination)
{
Warpplate r = GetWarpplateByName(WarpplateName);
if (r != null)
{
int q = database.Query("UPDATE Warpplates SET WarpplateDestination=@0 WHERE WarpplateName=@1 AND WorldID=@2;", WarpDestination, WarpplateName, Main.worldID.ToString());
r.WarpDest = WarpDestination;
if (q > 0)
return true;
}
return false;
}
/// <summary>
/// Gets all the Warpplates names from world
/// </summary>
/// <param name="worldid">World name to get Warpplates from</param>
/// <returns>List of Warpplates with only their names</returns>
public List<Warpplate> ListAllWarpplates(string worldid)
{
var WarpplatesTemp = new List<Warpplate>();
try
{
foreach (Warpplate wp in Warpplates)
{
WarpplatesTemp.Add(new Warpplate { Name = wp.Name });
}
}
catch (Exception ex)
{
Log.Error(ex.ToString());
}
return WarpplatesTemp;
}
public Warpplate GetWarpplateByName(String name)
{
return Warpplates.FirstOrDefault(r => r.Name.Equals(name) && r.WorldID == Main.worldID.ToString());
}
public string GetLabel(String name)
{
Warpplate wp = FindWarpplate(name);
if (String.IsNullOrEmpty(wp.Label))
return name;
else
return wp.Label;
}
}
public class Warpplate
{
public Rectangle Area { get; set; }
public Vector2 WarpplatePos { get; set; }
public string Name { get; set; }
public string WarpDest { get; set; }
public bool DisableBuild { get; set; }
public string WorldID { get; set; }
public List<int> AllowedIDs { get; set; }
public int Delay { get; set; }
public string Label { get; set; }
public Warpplate(Vector2 warpplatepos, Rectangle Warpplate, string name, string warpdest, bool disablebuild, string WarpplateWorldIDz, string label)
: this()
{
WarpplatePos = warpplatepos;
Area = Warpplate;
Name = name;
Label = label;
WarpDest = warpdest;
DisableBuild = disablebuild;
WorldID = WarpplateWorldIDz;
Delay = 4;
}
public Warpplate()
{
WarpplatePos = Vector2.Zero;
Area = Rectangle.Empty;
Name = string.Empty;
WarpDest = string.Empty;
DisableBuild = true;
WorldID = string.Empty;
AllowedIDs = new List<int>();
}
public bool InArea(Rectangle point)
{
if (Area.Contains(point.X, point.Y))
{
return true;
}
return false;
}
}
}