· 8 years ago · Feb 15, 2018, 09:16 PM
1enum TABLES {
2 MAIN = "items"
3};
4
5function onCreated() {
6 createTables();
7}
8
9function out(msg) {
10 echo("[" @ this.name @ "]:" SPC msg);
11}
12
13/**
14 * Creates all of the necessary SQL tables
15**/
16function createTables() {
17 temp.sql_table =
18 "CREATE TABLE IF NOT EXISTS '" @ TABLES.MAIN @ "' (
19 item_id TEXT,
20 item_name TEXT,
21 item_image TEXT,
22 item_weapon TEXT,
23 item_properties TEXT
24 )";
25
26 temp.req_table = requestSQL(sql_table, false);
27}
28
29/**
30 * Checks if an item exists in the item database
31 * @param (string) id
32 * @returns (bool) does/doesn't exist
33**/
34function itemExists(temp.item_id) {
35 temp.sql =
36 "SELECT item_id FROM '" @ TABLES.MAIN @ "'
37 WHERE item_id='" @ item_id @ "'";
38
39 temp.req = requestSQL(sql, true);
40 if (req.rows.size() > 0) {
41 return true;
42 } else return false;
43}
44
45/**
46 * Creates a new item in the database
47 * @param (string) id
48 * @param (string) name
49 * @param (string) image
50 * @param (string) weapon
51 * @param (array) properties
52**/
53function createItem(temp.item_id, temp.item_name, temp.item_image, temp.item_weapon, temp.item_properties) {
54 if (itemExists(item_id)) return out(format("Couldn't create new item: Item with ID %s already exists!", item_id));
55
56 temp.properties = new TStaticVar();
57
58 for (p : temp.item_properties) {
59 temp.property_name = p[0];
60 temp.property_value = p[1];
61
62 temp.properties.(@property_name) = property_value;
63 }
64 temp.properties = properties.saveVarsToArray(true);
65
66 temp.sql =
67 "INSERT INTO '" @ TABLES.MAIN @ "' (
68 item_id,
69 item_name,
70 item_image,
71 item_weapon,
72 item_properties
73 )
74
75 VALUES (
76 '" @ item_id @ "',
77 '" @ item_name @ "',
78 '" @ item_image @ "',
79 '" @ item_weapon @ "',
80 '" @ properties @ "'
81 )";
82
83 temp.req = requestSQL(sql, false);
84
85 out(format("Successfully created new item: %s", item_id));
86}
87
88/**
89 * Deletes an item from the database
90 * @param (string) item id
91**/
92function deleteItem(temp.item_id) {
93 if (!itemExists(item_id)) return out(format("Couldn't delete item: Item with ID %s doesn't exist", item_id));
94 if (item_id == null) return out("Couldn't delete item: Deletion aborted because null item ID was passed");
95
96 temp.sql =
97 "DELETE FROM '" @ TABLES.MAIN @ "'
98 WHERE item_id='" @ item_id @ "'";
99
100 temp.req = requestSQL(sql, false);
101
102 out(format("Successfully delete item %s", item_id));
103}
104
105/**
106 * Gets a specified detail of an item entry in the database
107 * @param (string) item id
108 * @param (string) requested info
109 * @returns (any) requested info value
110**/
111public function getInfo(temp.item_id, temp.item_info) {
112 if (!itemExists(item_id)) return out(format("Couldn't fetch info: Item with ID %s doesn't exist", item_id));
113
114 temp.sql =
115 "SELECT item_" @ item_info @ " from '" @ TABLES.MAIN @ "'
116 WHERE item_id='" @ item_id @ "'";
117
118 temp.req = requestSQL(sql, true);
119 temp.result = temp.req.rows[0][0];
120
121 return result;
122}
123
124/**
125 * Returns an array of info for an item entry in the database
126 * @param (string) id
127 * @returns (array) requested info
128**/
129function getInfoAll(temp.item_id) {
130 if (!itemExists(item_id)) return out(format("Couldn't fetch all info: Item with ID %s doesn't exist", item_id));
131
132 temp.sql =
133 "SELECT * FROM '" @ TABLES.MAIN @ "'
134 WHERE item_id='" @ item_id @ "'";
135
136 temp.req = requestSQL(sql, true);
137 temp.result = temp.req.rows[0];
138
139 return result;
140}
141
142/**
143 * Gets a specified property of an item entry in the database
144 * @param (string) id
145 * @param (string) requested property
146 * @returns (any) requested property value
147**/
148function getProperty(temp.item_id, temp.item_property) {
149 if (!itemExists(item_id)) return out(format("Couldn't fetch property: Item with ID %s doesn't exist", item_id));
150
151 temp.sql =
152 "SELECT item_properties FROM '" @ TABLES.MAIN @ "'
153 WHERE item_id='" @ item_id @ "'";
154
155 temp.req = requestSQL(sql, true);
156 temp.properties.loadVarsFromArray(req.rows[0]);
157
158 return temp.properties.(@item_property)[0];
159}
160
161/**
162 * Returns all item ids in the database
163 * @returns (array) all items
164**/
165public function getAllItems() {
166 temp.sql = "SELECT item_id FROM '" @ TABLES.MAIN @ "'";
167 temp.req = requestSQL(sql, true);
168
169 for (i : temp.req.rows) {
170 temp.items.add(i.substring(0, i.length() - 1));
171 }
172
173 return items;
174}
175
176/**
177 * Adds a specified item to a player with a given quantity
178 * @param (string) id
179 * @param (number) quantity
180**/
181function addItem(temp.account, temp.item_id, temp.quantity) {
182 if (!itemExists(item_id)) return out(format("Couldn't add item to player: Item with ID %s doesn't exist", item_id));
183
184 temp.pl = findplayer(account);
185
186 if (pl == null) {
187 temp.pl = new TServerPlayer(@account);
188 temp.offline = true;
189 }
190
191 temp.has_item = (pl.clientr.("item_" @ item_id)[0] <= 0 ? false : true);
192
193 if (has_item) {
194 pl.clientr.("item_" @ item_id)[0] += quantity;
195 } else {
196 temp.flag = {quantity};
197
198 for (info : getInfoAll(item_id)) {
199 temp.flag.add(info);
200 }
201
202 pl.clientr.("item_" @ item_id) = temp.flag;
203 }
204
205 if (offline) pl.destroy();
206}
207
208}
209
210function out(msg) {
211 echo("[" @ this.name @ "]:" SPC msg);
212}
213
214/**
215 * Creates all of the necessary SQL tables
216**/
217function createTables() {
218 temp.sql_table =
219 "CREATE TABLE IF NOT EXISTS '" @ TABLES.MAIN @ "' (
220 item_id TEXT,
221 item_name TEXT,
222 item_image TEXT,
223 item_weapon TEXT,
224 item_properties TEXT
225 )";
226
227 temp.req_table = requestSQL(sql_table, false);
228}
229
230/**
231 * Checks if an item exists in the item database
232 * @param (string) id
233 * @returns (bool) does/doesn't exist
234**/
235public function itemExists(temp.item_id) {
236 temp.sql =
237 "SELECT item_id FROM '" @ TABLES.MAIN @ "'
238 WHERE item_id='" @ item_id @ "'";
239
240 temp.req = requestSQL(sql, true);
241 if (req.rows.size() > 0) {
242 return true;
243 } else return false;
244}
245
246/**
247 * Creates a new item in the database
248 * @param (string) id
249 * @param (string) name
250 * @param (string) image
251 * @param (string) weapon
252 * @param (array) properties
253**/
254public function createItem(temp.item_id, temp.item_name, temp.item_image, temp.item_weapon, temp.item_properties) {
255 if (itemExists(item_id)) return out(format("Couldn't create new item: Item with ID %s already exists!", item_id));
256
257 temp.properties = new TStaticVar();
258
259 for (p : temp.item_properties) {
260 temp.property_name = p[0];
261 temp.property_value = p[1];
262
263 temp.properties.(@property_name) = property_value;
264 }
265 temp.properties = properties.saveVarsToArray(true);
266
267 temp.sql =
268 "INSERT INTO '" @ TABLES.MAIN @ "' (
269 item_id,
270 item_name,
271 item_image,
272 item_weapon,
273 item_properties
274 )
275
276 VALUES (
277 '" @ item_id @ "',
278 '" @ item_name @ "',
279 '" @ item_image @ "',
280 '" @ item_weapon @ "',
281 '" @ properties @ "'
282 )";
283
284 temp.req = requestSQL(sql, false);
285
286 out(format("Successfully created new item: %s", item_id));
287}
288
289/**
290 * Deletes an item from the database
291 * @param (string) item id
292**/
293public function deleteItem(temp.item_id) {
294 if (!itemExists(item_id)) return out(format("Couldn't delete item: Item with ID %s doesn't exist", item_id));
295 if (item_id == null) return out("Couldn't delete item: Deletion aborted because null item ID was passed");
296
297 temp.sql =
298 "DELETE FROM '" @ TABLES.MAIN @ "'
299 WHERE item_id='" @ item_id @ "'";
300
301 temp.req = requestSQL(sql, false);
302
303 out(format("Successfully delete item %s", item_id));
304}
305
306/**
307 * Gets a specified detail of an item entry in the database
308 * @param (string) item id
309 * @param (string) requested info
310 * @returns (any) requested info value
311**/
312public function getInfo(temp.item_id, temp.item_info) {
313 if (!itemExists(item_id)) return out(format("Couldn't fetch info: Item with ID %s doesn't exist", item_id));
314
315 temp.sql =
316 "SELECT item_" @ item_info @ " from '" @ TABLES.MAIN @ "'
317 WHERE item_id='" @ item_id @ "'";
318
319 temp.req = requestSQL(sql, true);
320 temp.result = temp.req.rows[0][0];
321
322 return result;
323}
324
325/**
326 * Returns an array of info for an item entry in the database
327 * @param (string) id
328 * @returns (array) requested info
329**/
330public function getInfoAll(temp.item_id) {
331 if (!itemExists(item_id)) return out(format("Couldn't fetch all info: Item with ID %s doesn't exist", item_id));
332
333 temp.sql =
334 "SELECT * FROM '" @ TABLES.MAIN @ "'
335 WHERE item_id='" @ item_id @ "'";
336
337 temp.req = requestSQL(sql, true);
338 temp.result = temp.req.rows[0];
339
340 return result;
341}
342
343/**
344 * Gets a specified property of an item entry in the database
345 * @param (string) id
346 * @param (string) requested property
347 * @returns (any) requested property value
348**/
349public function getProperty(temp.item_id, temp.item_property) {
350 if (!itemExists(item_id)) return out(format("Couldn't fetch property: Item with ID %s doesn't exist", item_id));
351
352 temp.sql =
353 "SELECT item_properties FROM '" @ TABLES.MAIN @ "'
354 WHERE item_id='" @ item_id @ "'";
355
356 temp.req = requestSQL(sql, true);
357 temp.properties.loadVarsFromArray(req.rows[0]);
358
359 return temp.properties.(@item_property)[0];
360}
361
362/**
363 * Adds a specified item to a player with a given quantity
364 * @param (string) id
365 * @param (number) quantity
366**/
367public function addItem(temp.account, temp.item_id, temp.quantity) {
368 if (!itemExists(item_id)) return out(format("Couldn't add item to player: Item with ID %s doesn't exist", item_id));
369
370 temp.pl = findplayer(account);
371 if (pl == null) {
372 temp.pl = new TServerPlayer(@account);
373 temp.offline = true;
374 }
375
376 temp.has_item = (pl.clientr.("item." @ item_id)[0] <= 0 ? false : true);
377
378 if (has_item) {
379 pl.clientr.("item." @ item_id)[0] += quantity;
380 } else {
381 temp.flag = {quantity};
382
383 for (info : getInfoAll(item_id)) {
384 temp.flag.add(info);
385 }
386
387 pl.clientr.("item." @ item_id) = temp.flag;
388 }
389
390 if (offline) pl.destroy();
391}
392
393/**
394 * Removes a quantity of an item from a player
395 * @param (string) account
396 * @param (string) id
397 * @param (number) quantity
398**/
399public function removeItem(temp.account, temp.item_id, temp.quantity) {
400 if (!(itemExists(item_id)) return out(format("Couldn't remove item from player: Item with ID %s doesn't exist", item_id));
401 if (quantity <= 0) return out(format("Couldn't remove item from player: Invalid quantity '%s' was given", quantity));
402
403 temp.pl = findplayer(account);
404 if (temp.pl == null) {
405 temp.pl = new TServerPlayer(@account);
406 temp.offline = true;
407 }
408
409 temp.has_item = (pl.clientr.("item." @ itemid)[0] <= 0 ? false : true);
410 if (has_item) {
411 pl.clientr.("item." @ item_id)[0] -= quantity;
412 } else return;
413
414 if (offline) pl.destroy();
415}