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

235 lines
8.8 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;
2024-04-22 09:21:46 -04:00
import engine.gameManager.DbManager;
2024-04-22 16:02:22 -04:00
import engine.gameManager.ForgeManager;
2024-04-22 14:55:46 -04:00
import engine.loot.WorkOrder;
import engine.mbEnums;
import engine.mbEnums.GameObjectType;
import engine.mbEnums.TransactionType;
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-04-22 09:21:46 -04:00
import org.json.JSONArray;
import org.json.JSONObject;
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-04-22 09:21:46 -04:00
locks.put(resource.name());
2024-03-17 11:55:00 -04:00
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
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");
2024-04-22 09:21:46 -04:00
JSONObject jsonObject = new JSONObject(rs.getString("warehouse"));
2024-03-17 09:01:35 -04:00
City city = City.getCity(cityUID);
2024-06-18 11:56:19 -04:00
if (city == null) {
Logger.error("No city " + cityUID + " for warehouse");
continue;
}
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);
}
}
2024-04-22 14:55:46 -04:00
2024-04-27 07:24:57 -04:00
public boolean WRITE_WORKORDER(WorkOrder workOrder) {
2024-04-22 14:55:46 -04:00
JSONObject warehouseJSON = WorkOrder.toJson(workOrder);
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO `dyn_workorders` (`workorderID`, `workorder`) VALUES (?, ?) " +
2024-04-22 15:02:15 -04:00
"ON DUPLICATE KEY UPDATE `workorder` = VALUES(`workorder`)")) {
2024-04-22 14:55:46 -04:00
preparedStatement.setInt(1, workOrder.workOrderID);
preparedStatement.setString(2, warehouseJSON.toString());
return (preparedStatement.executeUpdate() > 0);
} catch (SQLException e) {
Logger.error(e);
}
return false;
}
2024-04-22 15:09:43 -04:00
public void DELETE_WORKORDER(WorkOrder workOrder) {
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM `dyn_workorders` WHERE `workorderID` = ?;")) {
preparedStatement.setInt(1, workOrder.workOrderID);
preparedStatement.executeUpdate();
} catch (SQLException e) {
Logger.error(e);
}
}
2024-04-22 15:43:13 -04:00
public void LOAD_WORKORDERS() {
2024-05-10 10:12:45 -04:00
// Method loads NPC workOrders from disk at bootstrap.
// A workOrder will persist until completed or junked
// via the client interface.
2024-04-22 15:49:57 -04:00
ArrayList<WorkOrder> submitList = new ArrayList<>();
2024-06-12 14:15:19 -04:00
ArrayList<WorkOrder> orphanList = new ArrayList<>();
2024-04-22 15:43:13 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `dyn_workorders`;");
ResultSet rs = preparedStatement.executeQuery()) {
while (rs.next()) {
JSONObject jsonObject = new JSONObject(rs.getString("workorder"));
WorkOrder workOrder = new WorkOrder(jsonObject);
2024-04-22 15:49:57 -04:00
submitList.add(workOrder);
2024-04-22 15:43:13 -04:00
}
} catch (Exception e) {
Logger.error(e);
}
2024-04-22 15:49:57 -04:00
2024-05-10 10:12:45 -04:00
// Submit new workOrders to the ForgeManager
2024-04-22 16:02:22 -04:00
2024-04-22 16:07:25 -04:00
for (WorkOrder workOrder : submitList) {
2024-06-12 14:15:19 -04:00
2024-05-10 09:50:14 -04:00
DbManager.WarehouseQueries.DELETE_WORKORDER(workOrder);
2024-06-12 14:15:19 -04:00
// Delete but do not reconstitute orphan workOrders
if (workOrder.vendor == null)
continue;
2024-04-25 09:22:50 -04:00
workOrder.workOrderID = ForgeManager.workOrderCounter.incrementAndGet();
2024-05-10 09:44:11 -04:00
DbManager.WarehouseQueries.WRITE_WORKORDER(workOrder);
2024-05-10 09:50:14 -04:00
ForgeManager.vendorWorkOrderLookup.get(workOrder.vendor).add(workOrder);
2024-05-10 09:39:43 -04:00
ForgeManager.forge.add(workOrder);
2024-04-22 16:02:22 -04:00
}
2024-05-10 09:39:43 -04:00
2024-04-22 15:43:13 -04:00
}
2022-04-30 09:41:17 -04:00
}