forked from bippity/AdvancedWarpplates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWarpplateManager.cs
220 lines (201 loc) · 7.78 KB
/
WarpplateManager.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
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using TShockAPI;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
namespace AdvancedWarpplates
{
/// <summary>
/// Deals with keeping track of and providing access to the warpplates of the current world
/// </summary>
public class WarpplateManager
{
private readonly WarpplateDB Database;
private List<Warpplate> Warpplates = new List<Warpplate>();
public WarpplateManager(IDbConnection db)
{
Database = new WarpplateDB(db);
}
/// <summary>
/// Gets a Warpplate with the given name
/// </summary>
/// <param name="name">The name of the warpplate to find</param>
/// <returns>The warpplate found or null</returns>
public Warpplate GetWarpplateByName(string name)
{
return Warpplates.FirstOrDefault(warpplate => warpplate.Name.Equals(name));
}
/// <summary>
/// Gets if there is a warpplate that surrounds the given co-ordinates
/// </summary>
/// <param name="x">The x co-ordinate</param>
/// <param name="y">The y co-ordinate</param>
/// <returns>Whether or not there is a warpplate surrounding the given co-ordinates</returns>
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)
{
return true;
}
}
return false;
}
/// <summary>
/// Gets the name of the warpplate that surrounds the given co-ordinates
/// </summary>
/// <param name="x">The x co-ordinate</param>
/// <param name="y">The y co-ordinate</param>
/// <returns>The name of the warpplate or null</returns>
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)
{
return Warpplate.Name;
}
}
return null;
}
/// <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<String> ListAllWarpplates()
{
var warpplateNames = new List<String>();
try
{
foreach(Warpplate wp in Warpplates)
{
warpplateNames.Add(wp.Name);
}
}
catch (Exception ex)
{
TShock.Log.Error(ex.ToString());
}
return warpplateNames;
}
/// <summary>
/// Gets the label of the given warpplate
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public string GetLabel(String name)
{
Warpplate warpplate = GetWarpplateByName(name);
if (String.IsNullOrEmpty(warpplate.Label))
return name;
else
return warpplate.Label;
}
/// <summary>
/// Adds a warpplate to the current world
/// </summary>
/// <param name="x">The x co-ordinate of the warpplate</param>
/// <param name="y">The y co-orindate of the warpplate</param>
/// <param name="width">The width of the warpplate</param>
/// <param name="height">The height of the warpplate</param>
/// <param name="warpplateName">The name of the warpplate</param>
/// <param name="destinationName">The name of the destination warpplate</param>
/// <param name="worldid">The id of the world the warpplate is to be in</param>
/// <returns>Whether the warpplate was added successfully</returns>
public async Task<bool> AddWarpplate(int x, int y, int width, int height, string warpplateName, string destinationName, string worldid)
{
// If warpplate does not exist, then it can be added
if (GetWarpplateByName(warpplateName) == null)
{
bool success = await Database.AddWarpplate(x, y, width, height, warpplateName, destinationName, worldid);
if (success)
{
Warpplates.Add(new Warpplate(new Vector2(x, y), new Rectangle(x, y, width, height), warpplateName, worldid, true, destinationName, "", 0));
}
return success;
}
return false;
}
/// <summary>
/// Deletes a warpplate from the world
/// </summary>
/// <param name="name">The name of the warpplate to delete</param>
/// <returns>Whether the warpplate was deleted successfully</returns>
public async Task<bool> DeleteWarpplate(string name)
{
Warpplate warpplate = GetWarpplateByName(name);
if (warpplate != null)
{
bool success = await Database.DeleteWarpplate(warpplate);
if (success)
{
Warpplates.Remove(warpplate);
}
return success;
}
return false;
}
/// <summary>
/// Updates a warpplates information
/// </summary>
/// <param name="name">The name of the warpplate to update</param>
/// <returns>Whether or not the update was successful</returns>
public async Task<bool> UpdateWarpplate(string name)
{
var warpplate = GetWarpplateByName(name);
if (warpplate != null)
{
return await Database.UpdateWarpplate(warpplate);
}
return false;
}
/// <summary>
/// Removes a warpplates destination
/// </summary>
/// <param name="name">The name of the warpplate to remove the destination from</param>
/// <returns>Whether or not the removal of destination was successful</returns>
public async Task<bool> RemoveDestination(string warpplateName)
{
Warpplate warpplate = GetWarpplateByName(warpplateName);
if (warpplate != null)
{
bool success = await Database.RemoveDestination(warpplate);
if (success)
{
warpplate.Destination = "";
}
return success;
}
return false;
}
/// <summary>
/// Sets a warpplates destination
/// </summary>
/// <param name="name">The name of the warpplate to set the destination of</param>
/// <returns>Whether or not setting the destination was successful</returns>
public async Task<bool> SetDestination(string warpplateName, string warpplateDestination, int type = 0)
{
Warpplate warpplate = GetWarpplateByName(warpplateName);
if (warpplate != null)
{
return await Database.SetDestination(warpplate, warpplateDestination, type);
}
return false;
}
/// <summary>
/// Completely clears all warpplates and reloads them and their information from the database
/// </summary>
/// <returns>void</returns>
public async Task ReloadAllWarpplates()
{
Warpplates.Clear();
Warpplates = await Database.ReloadAllWarpplates();
}
}
}