/
ItemDao.java
370 lines (308 loc) · 12.6 KB
/
ItemDao.java
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
366
367
368
369
370
package org.alexdev.kepler.dao.mysql;
import org.alexdev.kepler.dao.Storage;
import org.alexdev.kepler.game.item.Item;
import org.alexdev.kepler.game.item.public_items.PublicItemData;
import org.alexdev.kepler.game.item.base.ItemDefinition;
import org.alexdev.kepler.game.room.RoomData;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
public class ItemDao {
/**
* Get the item definitions.
*
* @return the list of item definitions
*/
public static Map<Integer, ItemDefinition> getItemDefinitions() {
Map<Integer, ItemDefinition> definitions = new HashMap<>();
Connection sqlConnection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
sqlConnection = Storage.getStorage().getConnection();
preparedStatement = Storage.getStorage().prepare("SELECT * FROM items_definitions", sqlConnection);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
ItemDefinition definition = new ItemDefinition(resultSet.getInt("id"), resultSet.getString("sprite"),
resultSet.getString("name"), resultSet.getString("description"),
resultSet.getString("behaviour"), resultSet.getString("interactor"), resultSet.getDouble("top_height"),
resultSet.getInt("length"), resultSet.getInt("width"), resultSet.getString("colour"), resultSet.getString("drink_ids"),
resultSet.getBoolean("is_recyclable"));
definitions.put(definition.getId(), definition);
}
} catch (Exception e) {
Storage.logError(e);
} finally {
Storage.closeSilently(resultSet);
Storage.closeSilently(preparedStatement);
Storage.closeSilently(sqlConnection);
}
return definitions;
}
/**
* Create new item entry with the definition id, user id and custom data. It will
* override the current item id with its database id.
*
* @param item the item to create
*/
public static void newItem(Item item) throws SQLException {
Connection sqlConnection = null;
PreparedStatement preparedStatement = null;
ResultSet row = null;
int itemId = 0;
try {
sqlConnection = Storage.getStorage().getConnection();
preparedStatement = Storage.getStorage().prepare("INSERT INTO items (user_id, definition_id, custom_data) VALUES (?,?,?)", sqlConnection);
preparedStatement.setInt(1, item.getOwnerId());
preparedStatement.setInt(2, item.getDefinition().getId());
preparedStatement.setString(3, item.getCustomData());
preparedStatement.executeUpdate();
row = preparedStatement.getGeneratedKeys();
if (row != null && row.next()) {
itemId = row.getInt(1);
}
} catch (SQLException e) {
Storage.logError(e);
throw e;
} finally {
Storage.closeSilently(row);
Storage.closeSilently(preparedStatement);
Storage.closeSilently(sqlConnection);
}
item.setId(itemId);
}
/**
* Get the inventory list of items.
*
* @param userId the id of the user to get the inventory for
* @return the list of items
*/
public static List<Item> getInventory(int userId) {
List<Item> items = new ArrayList<>();
Connection sqlConnection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
sqlConnection = Storage.getStorage().getConnection();
preparedStatement = Storage.getStorage().prepare("SELECT * FROM items WHERE user_id = ? AND room_id = 0 ORDER BY order_id ASC", sqlConnection);
preparedStatement.setInt(1, userId);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Item item = new Item();
fill(item, resultSet);
items.add(item);
}
} catch (Exception e) {
Storage.logError(e);
} finally {
Storage.closeSilently(resultSet);
Storage.closeSilently(preparedStatement);
Storage.closeSilently(sqlConnection);
}
return items;
}
/**
* Get the item by item id
*
* @param itemId the id of the item to to get
* @return the item
*/
public static Item getItem(int itemId) {
Item item = null;
Connection sqlConnection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
sqlConnection = Storage.getStorage().getConnection();
preparedStatement = Storage.getStorage().prepare("SELECT * FROM items WHERE id = ?", sqlConnection);
preparedStatement.setInt(1, itemId);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
item = new Item();
fill(item, resultSet);
}
} catch (Exception e) {
Storage.logError(e);
} finally {
Storage.closeSilently(resultSet);
Storage.closeSilently(preparedStatement);
Storage.closeSilently(sqlConnection);
}
return item;
}
/**
* Get the room list of items.
*
* @param roomData the room data instance
* @return the list of items
*/
public static List<Item> getRoomItems(RoomData roomData) {
List<Item> items = new ArrayList<>();
Connection sqlConnection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
sqlConnection = Storage.getStorage().getConnection();
preparedStatement = Storage.getStorage().prepare("SELECT * FROM items WHERE room_id = ?", sqlConnection);
preparedStatement.setInt(1, roomData.getId());
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Item item = new Item();
fill(item, resultSet);
items.add(item);
}
} catch (Exception e) {
Storage.logError(e);
} finally {
Storage.closeSilently(resultSet);
Storage.closeSilently(preparedStatement);
Storage.closeSilently(sqlConnection);
}
return items;
}
/**
* Delete an entire list of items at once.
*
* @param items the list of items
*/
public static void deleteItems(List<Integer> items) {
if (items.size() > 0) {
Connection sqlConnection = null;
PreparedStatement preparedStatement = null;
try {
sqlConnection = Storage.getStorage().getConnection();
preparedStatement = Storage.getStorage().prepare("DELETE FROM items WHERE id = ?", sqlConnection);
sqlConnection.setAutoCommit(false);
for (Integer itemId : items) {
preparedStatement.setInt(1, itemId);
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
sqlConnection.setAutoCommit(true);
} catch (Exception e) {
Storage.logError(e);
} finally {
Storage.closeSilently(preparedStatement);
Storage.closeSilently(sqlConnection);
}
}
}
/**
* Redeem credit furniture atomicly
*
* @param amount credit amount to increase by
* @param userID user ID
*/
public static int redeemCreditItem(int amount, int itemID, int userID) {
int updatedAmount = -1;
Connection conn = null;
PreparedStatement deleteQuery = null;
PreparedStatement updateQuery = null;
PreparedStatement fetchQuery = null;
ResultSet row = null;
try {
conn = Storage.getStorage().getConnection();
// We disable autocommit to make sure the following queries share the same atomic transaction
conn.setAutoCommit(false);
deleteQuery = Storage.getStorage().prepare("DELETE FROM items WHERE id = ?", conn);
deleteQuery.setInt(1, itemID);
deleteQuery.execute();
// Increase credits
updateQuery = Storage.getStorage().prepare("UPDATE users SET credits = credits + ? WHERE id = ?", conn);
updateQuery.setInt(1, amount);
updateQuery.setInt(2, userID);
updateQuery.execute();
// Fetch increased amount
fetchQuery = Storage.getStorage().prepare("SELECT credits FROM users WHERE id = ?", conn);
fetchQuery.setInt(1, userID);
row = fetchQuery.executeQuery();
// Commit these queries
conn.commit();
// Set amount
if (row != null && row.next()) {
updatedAmount = row.getInt("credits");
}
} catch (Exception e) {
// Reset amount
updatedAmount = -1;
try {
// Rollback these queries
conn.rollback();
} catch(SQLException re) {
Storage.logError(re);
}
Storage.logError(e);
} finally {
try {
conn.setAutoCommit(true);
} catch (SQLException ce) {
Storage.logError(ce);
}
Storage.closeSilently(row);
Storage.closeSilently(deleteQuery);
Storage.closeSilently(updateQuery);
Storage.closeSilently(fetchQuery);
Storage.closeSilently(conn);
}
return updatedAmount;
}
/**
* Update item by item instance.
*
* @param item the instance of the item to update it
*/
public static void updateItem(Item item) {
updateItems(List.of(item));
}
/**
* Update an entire list of items at once.
*
* @param items the list of items
*/
public static void updateItems(Collection<Item> items) {
Connection sqlConnection = null;
PreparedStatement preparedStatement = null;
try {
sqlConnection = Storage.getStorage().getConnection();
preparedStatement = Storage.getStorage().prepare("UPDATE items SET user_id = ?, room_id = ?, definition_id = ?, x = ?, y = ?, z = ?, rotation = ?, wall_position = ?, custom_data = ?, order_id = ?, is_hidden = ? WHERE id = ?", sqlConnection);
sqlConnection.setAutoCommit(false);
for (Item item : items) {
preparedStatement.setInt(1, item.getOwnerId());
preparedStatement.setInt(2, item.getRoomId());
preparedStatement.setInt(3, item.getDefinition().getId());
preparedStatement.setInt(4, item.getPosition().getX());
preparedStatement.setInt(5, item.getPosition().getY());
preparedStatement.setDouble(6, item.getPosition().getZ());
preparedStatement.setInt(7, item.getPosition().getRotation());
preparedStatement.setString(8, item.getWallPosition());
preparedStatement.setString(9, item.getCustomData());
preparedStatement.setInt(10, item.getOrderId());
preparedStatement.setInt(11, item.isHidden() ? 1 : 0);
preparedStatement.setLong(12, item.getId());
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
sqlConnection.setAutoCommit(true);
} catch (Exception e) {
Storage.logError(e);
} finally {
Storage.closeSilently(preparedStatement);
Storage.closeSilently(sqlConnection);
}
}
/**
* Fill item with data retrieved from the SQL query.
*
* @param item the item to fill data for
* @param resultSet the result set returned with the data
* @throws SQLException an exception if an error happened
*/
private static void fill(Item item, ResultSet resultSet) throws SQLException {
item.fill(resultSet.getInt("id"), resultSet.getInt("order_id"), resultSet.getInt("user_id"), resultSet.getInt("room_id"),
resultSet.getInt("definition_id"), resultSet.getInt("x"), resultSet.getInt("y"),
resultSet.getDouble("z"), resultSet.getInt("rotation"), resultSet.getString("wall_position"),
resultSet.getString("custom_data"), resultSet.getBoolean("is_hidden"));
}
}