Files
lakebane/src/engine/db/handlers/dbWarehouseHandler.java
T

158 lines
6.1 KiB
Java
Raw Normal View History

2022-04-30 09:41:17 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.db.handlers;
import engine.mbEnums;
import engine.mbEnums.GameObjectType;
import engine.mbEnums.TransactionType;
2022-04-30 09:41:17 -04:00
import engine.gameManager.DbManager;
2024-03-17 11:34:36 -04:00
import engine.objects.Building;
2024-03-17 09:01:35 -04:00
import engine.objects.City;
2024-03-14 14:47:20 -04:00
import engine.objects.Transaction;
import engine.objects.Warehouse;
2022-04-30 09:41:17 -04:00
import engine.server.MBServerStatics;
import org.joda.time.DateTime;
2024-03-17 11:55:00 -04:00
import org.json.simple.JSONArray;
2024-03-17 09:01:35 -04:00
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
2022-04-30 09:41:17 -04:00
import org.pmw.tinylog.Logger;
2023-05-23 09:17:06 -04:00
import java.sql.Connection;
import java.sql.PreparedStatement;
2022-04-30 09:41:17 -04:00
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
public class dbWarehouseHandler extends dbHandlerBase {
2023-07-15 09:23:48 -04:00
private static final ConcurrentHashMap<Integer, String> columns = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW);
public dbWarehouseHandler() {
}
public boolean CREATE_TRANSACTION(int warehouseBuildingID, GameObjectType targetType, int targetUUID, TransactionType transactionType, mbEnums.ResourceType resource, int amount, DateTime date) {
2023-05-23 09:39:27 -04:00
2023-07-15 09:23:48 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO `dyn_warehouse_transactions` (`warehouseUID`, `targetType`,`targetUID`, `type`,`resource`,`amount`,`date` ) VALUES (?,?,?,?,?,?,?)")) {
2023-05-23 09:39:27 -04:00
2023-07-15 09:23:48 -04:00
preparedStatement.setLong(1, warehouseBuildingID);
preparedStatement.setString(2, targetType.name());
preparedStatement.setLong(3, targetUUID);
preparedStatement.setString(4, transactionType.name());
preparedStatement.setString(5, resource.name());
preparedStatement.setInt(6, amount);
preparedStatement.setTimestamp(7, new java.sql.Timestamp(date.getMillis()));
2023-05-23 09:39:27 -04:00
2023-07-15 09:23:48 -04:00
return (preparedStatement.executeUpdate() > 0);
2023-05-23 09:39:27 -04:00
2023-07-15 09:23:48 -04:00
} catch (SQLException e) {
Logger.error(e);
}
return false;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public ArrayList<Transaction> GET_TRANSACTIONS_FOR_WAREHOUSE(final int warehouseUUID) {
2023-05-23 09:39:27 -04:00
2023-07-15 09:23:48 -04:00
ArrayList<Transaction> transactionsList = new ArrayList<>();
2022-04-30 09:41:17 -04:00
2023-05-23 09:39:27 -04:00
2023-07-15 09:23:48 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM dyn_warehouse_transactions WHERE `warehouseUID` = ?;")) {
2023-05-23 09:39:27 -04:00
2023-07-15 09:23:48 -04:00
preparedStatement.setInt(1, warehouseUUID);
2023-05-23 09:39:27 -04:00
2023-07-15 09:23:48 -04:00
ResultSet rs = preparedStatement.executeQuery();
2023-05-23 09:39:27 -04:00
2023-07-15 09:23:48 -04:00
while (rs.next()) {
Transaction transactions = new Transaction(rs);
transactionsList.add(transactions);
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
} catch (SQLException e) {
Logger.error(e);
}
2023-05-23 09:39:27 -04:00
2023-07-15 09:23:48 -04:00
return transactionsList;
}
2022-04-30 09:41:17 -04:00
2024-03-17 13:21:55 -04:00
public void DELETE_WAREHOUSE(Warehouse warehouse) {
try (Connection connection = DbManager.getConnection();
2024-03-30 11:46:56 -04:00
PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM `dyn_warehouse` WHERE `cityUUID` = ?;")) {
2024-03-17 13:21:55 -04:00
preparedStatement.setInt(1, warehouse.city.getObjectUUID());
preparedStatement.executeUpdate();
} catch (SQLException e) {
Logger.error(e);
}
}
2024-03-17 09:47:00 -04:00
public boolean UPDATE_WAREHOUSE(Warehouse warehouse) {
2024-03-17 11:55:00 -04:00
JSONObject warehouseJSON = new JSONObject();
JSONObject resources = new JSONObject(warehouse.resources);
warehouseJSON.put("resources", resources);
JSONArray locks = new JSONArray();
for (mbEnums.ResourceType resource : warehouse.locked)
2024-03-17 11:55:00 -04:00
locks.add(resource.name());
2024-03-17 12:09:37 -04:00
warehouseJSON.put("locked", locks);
2024-03-17 09:47:00 -04:00
try (Connection connection = DbManager.getConnection();
2024-03-17 10:42:36 -04:00
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO `dyn_warehouse` (`cityUUID`, `warehouse`) VALUES (?, ?) " +
"ON DUPLICATE KEY UPDATE `warehouse` = VALUES(`warehouse`)")) {
2024-03-17 09:47:00 -04:00
preparedStatement.setInt(1, warehouse.city.getObjectUUID());
2024-03-17 10:42:36 -04:00
preparedStatement.setString(2, warehouseJSON.toString());
2024-03-17 09:47:00 -04:00
return (preparedStatement.executeUpdate() > 0);
} catch (SQLException e) {
Logger.error(e);
}
return false;
}
2024-03-17 09:01:35 -04:00
public void LOAD_WAREHOUSES() {
2022-04-30 09:41:17 -04:00
2024-03-17 09:01:35 -04:00
JSONParser jsonParser = new JSONParser();
2023-05-23 09:39:27 -04:00
2023-07-15 09:23:48 -04:00
try (Connection connection = DbManager.getConnection();
2024-03-17 11:00:48 -04:00
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `dyn_warehouse`;");
2024-03-17 09:01:35 -04:00
ResultSet rs = preparedStatement.executeQuery()) {
2023-05-23 09:39:27 -04:00
2023-07-15 09:23:48 -04:00
while (rs.next()) {
2024-03-17 10:04:03 -04:00
int cityUID = rs.getInt("cityUUID");
JSONObject jsonObject = (JSONObject) jsonParser.parse(rs.getString("warehouse"));
2024-03-17 09:01:35 -04:00
City city = City.getCity(cityUID);
2024-03-17 09:51:50 -04:00
city.warehouse = new Warehouse(jsonObject);
2024-03-17 11:45:18 -04:00
city.warehouse.city = city;
2024-03-17 11:34:36 -04:00
// Locate warehouse building
for (Building building : city.parentZone.zoneBuildingSet) {
if (building.getBlueprint().getBuildingGroup().equals(mbEnums.BuildingGroup.WAREHOUSE)) {
2024-03-17 11:34:36 -04:00
city.warehouse.building = building;
break;
}
}
2023-07-15 09:23:48 -04:00
}
2022-04-30 09:41:17 -04:00
2024-03-17 09:01:35 -04:00
} catch (Exception e) {
2023-07-15 09:23:48 -04:00
Logger.error(e);
}
}
2022-04-30 09:41:17 -04:00
}