From b76a384e1fd21d0efa42c58950d5104ab2fc39f2 Mon Sep 17 00:00:00 2001 From: MagicBot Date: Sun, 17 Mar 2024 09:01:35 -0400 Subject: [PATCH] Refactor warehouse part two. --- src/engine/db/handlers/dbItemHandler.java | 5 +- .../db/handlers/dbWarehouseHandler.java | 479 +----------------- .../client/handlers/PlaceAssetMsgHandler.java | 34 +- src/engine/objects/City.java | 19 +- src/engine/objects/Mine.java | 6 +- src/engine/objects/Warehouse.java | 390 ++++---------- 6 files changed, 127 insertions(+), 806 deletions(-) diff --git a/src/engine/db/handlers/dbItemHandler.java b/src/engine/db/handlers/dbItemHandler.java index ce9bd268..c244bce0 100644 --- a/src/engine/db/handlers/dbItemHandler.java +++ b/src/engine/db/handlers/dbItemHandler.java @@ -171,9 +171,8 @@ public class dbItemHandler extends dbHandlerBase { JSONParser jsonParser = new JSONParser(); try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_item_templates`;")) { - - ResultSet rs = preparedStatement.executeQuery(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_item_templates`;"); + ResultSet rs = preparedStatement.executeQuery()) { while (rs.next()) { int templateID = rs.getInt("id"); diff --git a/src/engine/db/handlers/dbWarehouseHandler.java b/src/engine/db/handlers/dbWarehouseHandler.java index 6690f42b..786f1557 100644 --- a/src/engine/db/handlers/dbWarehouseHandler.java +++ b/src/engine/db/handlers/dbWarehouseHandler.java @@ -11,16 +11,15 @@ package engine.db.handlers; import engine.Enum; import engine.Enum.GameObjectType; -import engine.Enum.ProtectionState; import engine.Enum.TransactionType; import engine.gameManager.DbManager; -import engine.math.Vector3fImmutable; -import engine.objects.AbstractGameObject; -import engine.objects.Building; +import engine.objects.City; import engine.objects.Transaction; import engine.objects.Warehouse; import engine.server.MBServerStatics; import org.joda.time.DateTime; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; import org.pmw.tinylog.Logger; import java.sql.Connection; @@ -35,462 +34,9 @@ public class dbWarehouseHandler extends dbHandlerBase { private static final ConcurrentHashMap columns = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW); public dbWarehouseHandler() { - this.localClass = Warehouse.class; - this.localObjectType = engine.Enum.GameObjectType.valueOf(this.localClass.getSimpleName()); } - public static void addObject(ArrayList list, ResultSet rs) throws SQLException { - String type = rs.getString("type"); - switch (type) { - case "building": - Building building = new Building(rs); - DbManager.addToCache(building); - list.add(building); - break; - case "warehouse": - Warehouse warehouse = new Warehouse(rs); - DbManager.addToCache(warehouse); - list.add(warehouse); - break; - } - } - - public ArrayList CREATE_WAREHOUSE(int parentZoneID, int OwnerUUID, String name, int meshUUID, - Vector3fImmutable location, float meshScale, int currentHP, - ProtectionState protectionState, int currentGold, int rank, - DateTime upgradeDate, int blueprintUUID, float w, float rotY) { - - ArrayList warehouseList = new ArrayList<>(); - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("CALL `WAREHOUSE_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,? ,? ,?, ?);")) { - - preparedStatement.setInt(1, parentZoneID); - preparedStatement.setInt(2, OwnerUUID); - preparedStatement.setString(3, name); - preparedStatement.setInt(4, meshUUID); - preparedStatement.setFloat(5, location.x); - preparedStatement.setFloat(6, location.y); - preparedStatement.setFloat(7, location.z); - preparedStatement.setFloat(8, meshScale); - preparedStatement.setInt(9, currentHP); - preparedStatement.setString(10, protectionState.name()); - preparedStatement.setInt(11, currentGold); - preparedStatement.setInt(12, rank); - - if (upgradeDate != null) - preparedStatement.setTimestamp(13, new java.sql.Timestamp(upgradeDate.getMillis())); - else - preparedStatement.setNull(13, java.sql.Types.DATE); - - preparedStatement.setInt(14, blueprintUUID); - preparedStatement.setFloat(15, w); - preparedStatement.setFloat(16, rotY); - - preparedStatement.execute(); - ResultSet rs = preparedStatement.getResultSet(); - - while (rs.next()) - addObject(warehouseList, rs); - - while (preparedStatement.getMoreResults()) { - rs = preparedStatement.getResultSet(); - - while (rs.next()) - addObject(warehouseList, rs); - } - } catch (SQLException e) { - Logger.error(e); - } - - return warehouseList; - } - - public boolean updateLocks(final Warehouse wh, long locks) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_locks`=? WHERE `UID` = ?")) { - - preparedStatement.setLong(1, locks); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateGold(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_gold`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateStone(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_stone`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateTruesteel(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_truesteel`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateIron(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_iron`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateAdamant(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_adamant`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateLumber(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_lumber`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateOak(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_oak`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateBronzewood(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_bronzewood`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateMandrake(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_mandrake`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateCoal(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_coal`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateAgate(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_agate`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateDiamond(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_diamond`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateOnyx(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_onyx`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateAzoth(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_azoth`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateOrichalk(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_orichalk`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateAntimony(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_antimony`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateSulfur(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_sulfur`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateQuicksilver(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_quicksilver`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateGalvor(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_galvor`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateWormwood(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_wormwood`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateObsidian(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_obsidian`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateBloodstone(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_bloodstone`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - - public boolean updateMithril(final Warehouse wh, int amount) { - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_warehouse` SET `warehouse_mithril`=? WHERE `UID` = ?")) { - - preparedStatement.setInt(1, amount); - preparedStatement.setInt(2, wh.UID); - - return (preparedStatement.executeUpdate() > 0); - - } catch (SQLException e) { - Logger.error(e); - } - return false; - } - public boolean CREATE_TRANSACTION(int warehouseBuildingID, GameObjectType targetType, int targetUUID, TransactionType transactionType, Enum.ResourceType resource, int amount, DateTime date) { try (Connection connection = DbManager.getConnection(); @@ -536,23 +82,22 @@ public class dbWarehouseHandler extends dbHandlerBase { return transactionsList; } - public void LOAD_ALL_WAREHOUSES() { + public void LOAD_WAREHOUSES() { - Warehouse warehouse; + JSONParser jsonParser = new JSONParser(); try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("SELECT `obj_warehouse`.*, `object`.`parent`, `object`.`type` FROM `object` LEFT JOIN `obj_warehouse` ON `object`.`UID` = `obj_warehouse`.`UID` WHERE `object`.`type` = 'warehouse';")) { - - - ResultSet rs = preparedStatement.executeQuery(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT `*` FROM `dyn_warehouse`;"); + ResultSet rs = preparedStatement.executeQuery()) { while (rs.next()) { - warehouse = new Warehouse(rs); - warehouse.runAfterLoad(); - Warehouse.loadAllTransactions(warehouse); + int cityUID = rs.getInt("cityUID"); + JSONObject jsonObject = (JSONObject) jsonParser.parse(rs.getString("template")); + City city = City.getCity(cityUID); + Warehouse warehouse = new Warehouse(jsonObject); } - } catch (SQLException e) { + } catch (Exception e) { Logger.error(e); } } diff --git a/src/engine/net/client/handlers/PlaceAssetMsgHandler.java b/src/engine/net/client/handlers/PlaceAssetMsgHandler.java index 836e3d44..84adaf8b 100644 --- a/src/engine/net/client/handlers/PlaceAssetMsgHandler.java +++ b/src/engine/net/client/handlers/PlaceAssetMsgHandler.java @@ -1317,7 +1317,7 @@ public class PlaceAssetMsgHandler extends AbstractClientMsgHandler { Blueprint blueprint; Building newMesh = null; - ArrayList warehouseObjects; + Building warehouseBuilding; blueprint = Blueprint.getBlueprint(buildingInfo.getBlueprintUUID()); @@ -1331,38 +1331,30 @@ public class PlaceAssetMsgHandler extends AbstractClientMsgHandler { float buildingRotation = buildingInfo.getRot().y; float vendorRotation = buildingInfo.getW(); - warehouseObjects = DbManager.WarehouseQueries.CREATE_WAREHOUSE( + City city = City.getCity(currentZone.playerCityUUID); + + warehouseBuilding = DbManager.BuildingQueries.CREATE_BUILDING( currentZone.getObjectUUID(), player.getObjectUUID(), blueprint.getName(), blueprint.getMeshForRank(0), localLoc, 1.0f, blueprint.getMaxHealth(0), ProtectionState.NONE, 0, 0, DateTime.now().plusHours(blueprint.getRankTime(1)), blueprint.getMeshForRank(0), vendorRotation, buildingRotation); - if (warehouseObjects == null) { + if (warehouseBuilding == null) { PlaceAssetMsg.sendPlaceAssetError(player.getClientConnection(), 1, "A Serious error has occurred. Please post details for to ensure transaction integrity"); return false; } // Load the building into the simulation - for (AbstractGameObject ago : warehouseObjects) { - - if (ago.getObjectType() == GameObjectType.Building) { - newMesh = (Building) ago; - newMesh.setObjectTypeMask(MBServerStatics.MASK_BUILDING); - MaintenanceManager.setMaintDateTime(newMesh, LocalDateTime.now().plusDays(7)); - newMesh.setLoc(newMesh.getLoc()); - InterestManager.setObjectDirty(newMesh); - newMesh.runAfterLoad(); - } else if (ago.getObjectType() == GameObjectType.Warehouse) { - Warehouse warehouse = (Warehouse) ago; - City city = City.getCity(currentZone.playerCityUUID); + warehouseBuilding.setObjectTypeMask(MBServerStatics.MASK_BUILDING); + MaintenanceManager.setMaintDateTime(warehouseBuilding, LocalDateTime.now().plusDays(7)); + warehouseBuilding.setLoc(warehouseBuilding.getLoc()); + InterestManager.setObjectDirty(warehouseBuilding); + warehouseBuilding.runAfterLoad(); - if (city == null) - return true; + if (city == null) + return true; - city.setWarehouseBuildingID(newMesh.getObjectUUID()); - Warehouse.warehouseByBuildingUUID.put(newMesh.getObjectUUID(), warehouse); - } - } + city.setWarehouseBuildingID(newMesh.getObjectUUID()); return true; } diff --git a/src/engine/objects/City.java b/src/engine/objects/City.java index a53fe5cd..3ec2460c 100644 --- a/src/engine/objects/City.java +++ b/src/engine/objects/City.java @@ -81,6 +81,7 @@ public class City extends AbstractWorldObject { private int warehouseBuildingID = 0; private boolean open = false; private String hash; + public Warehouse warehouse; /** * ResultSet Constructor @@ -893,14 +894,6 @@ public class City extends AbstractWorldObject { } } - public Warehouse getWarehouse() { - - if (this.warehouseBuildingID == 0) - return null; - - return Warehouse.warehouseByBuildingUUID.get(this.warehouseBuildingID); - } - public Realm getRealm() { return Realm.getRealm(this.realmID); @@ -1355,10 +1348,10 @@ public class City extends AbstractWorldObject { if (msg.getResources().size() == 0) return true; - if (city.getWarehouse() == null) + if (city.warehouse == null) return true; - Warehouse ruledWarehouse = playerGuild.getOwnedCity().getWarehouse(); + Warehouse ruledWarehouse = playerGuild.getOwnedCity().warehouse; if (ruledWarehouse == null) return true; @@ -1374,7 +1367,7 @@ public class City extends AbstractWorldObject { resources.add(ResourceType.hashLookup.get(resourceHash)); for (ResourceType resourceType : resources) { - if (Warehouse.isAboveCap(ruledWarehouse, resourceType, (int) (city.getWarehouse().resources.get(resourceType) * taxPercent))) { + if (Warehouse.isAboveCap(ruledWarehouse, resourceType, (int) (city.warehouse.resources.get(resourceType) * taxPercent))) { ErrorPopupMsg.sendErrorMsg(player, "Your warehouse has enough " + resourceType.name() + " already!"); return true; } @@ -1387,7 +1380,7 @@ public class City extends AbstractWorldObject { } try { - Warehouse.transferResources(city.getWarehouse(), player, msg, resources, taxPercent); + Warehouse.transferResources(city.warehouse, player, msg, resources, taxPercent); } catch (Exception e) { Logger.info(e.getMessage()); } @@ -1396,7 +1389,7 @@ public class City extends AbstractWorldObject { ViewResourcesMessage vrm = new ViewResourcesMessage(player); vrm.setGuild(building.getGuild()); - vrm.setWarehouseBuilding(BuildingManager.getBuildingFromCache(building.getCity().getWarehouse().buildingUID)); + vrm.setWarehouseBuilding(BuildingManager.getBuildingFromCache(building.getCity().warehouse.buildingUID)); vrm.configure(); Dispatch dispatch = Dispatch.borrow(player, vrm); DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY); diff --git a/src/engine/objects/Mine.java b/src/engine/objects/Mine.java index a852408d..0946972c 100644 --- a/src/engine/objects/Mine.java +++ b/src/engine/objects/Mine.java @@ -271,7 +271,7 @@ public class Mine extends AbstractGameObject { if (guildCity == null) return false; - if (guildCity.getWarehouse() == null) { + if (guildCity.warehouse == null) { ErrorPopupMsg.sendErrorMsg(playerCharacter, "No Warehouse exists for this claim."); return false; } @@ -479,10 +479,10 @@ public class Mine extends AbstractGameObject { if (this.owningGuild.getOwnedCity() == null) return false; - if (this.owningGuild.getOwnedCity().getWarehouse() == null) + if (this.owningGuild.getOwnedCity().warehouse == null) return false; - return Warehouse.depositFromMine(this, Enum.ResourceType.resourceLookup.get(this.production.templateID), this.getModifiedProductionAmount(), this.owningGuild.getOwnedCity().getWarehouse()); + return Warehouse.depositFromMine(this, Enum.ResourceType.resourceLookup.get(this.production.templateID), this.getModifiedProductionAmount(), this.owningGuild.getOwnedCity().warehouse); } public boolean updateGuildOwner(PlayerCharacter playerCharacter) { diff --git a/src/engine/objects/Warehouse.java b/src/engine/objects/Warehouse.java index db1a534b..7c3e617f 100644 --- a/src/engine/objects/Warehouse.java +++ b/src/engine/objects/Warehouse.java @@ -9,9 +9,7 @@ package engine.objects; -import ch.claude_martin.enumbitset.EnumBitSet; import engine.Enum; -import engine.gameManager.BuildingManager; import engine.gameManager.ChatManager; import engine.gameManager.DbManager; import engine.net.Dispatch; @@ -20,56 +18,33 @@ import engine.net.client.ClientConnection; import engine.net.client.msg.*; import engine.server.MBServerStatics; import org.joda.time.DateTime; +import org.json.simple.JSONObject; import org.pmw.tinylog.Logger; -import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.EnumSet; import java.util.concurrent.ConcurrentHashMap; -public class Warehouse extends AbstractWorldObject { +public class Warehouse { - public static ConcurrentHashMap warehouseByBuildingUUID = new ConcurrentHashMap<>(); - public EnumBitSet lockedResourceTypes; - public int UID; + public EnumSet lockedResourceTypes; public int buildingUID; + public City city; public ArrayList transactions = new ArrayList<>(); public ConcurrentHashMap resources = new ConcurrentHashMap<>(); + public Warehouse(JSONObject warehouse) throws SQLException { + + + JSONObject resources = (JSONObject) warehouse.get("resources"); + + for (Object key : resources.keySet()) { + Enum.ResourceType resourceType = Enum.ResourceType.valueOf((String) key); + float value = ((Long) resources.get(key)).intValue(); + resources.put(resourceType, value); + } - /** - * ResultSet Constructor - */ - public Warehouse(ResultSet rs) throws SQLException { - super(rs); - this.UID = rs.getInt("UID"); - this.resources.put(Enum.ResourceType.STONE, rs.getInt("warehouse_stone")); - this.resources.put(Enum.ResourceType.TRUESTEEL, rs.getInt("warehouse_truesteel")); - this.resources.put(Enum.ResourceType.IRON, rs.getInt("warehouse_iron")); - this.resources.put(Enum.ResourceType.ADAMANT, rs.getInt("warehouse_adamant")); - this.resources.put(Enum.ResourceType.LUMBER, rs.getInt("warehouse_lumber")); - this.resources.put(Enum.ResourceType.OAK, rs.getInt("warehouse_oak")); - this.resources.put(Enum.ResourceType.BRONZEWOOD, rs.getInt("warehouse_bronzewood")); - this.resources.put(Enum.ResourceType.MANDRAKE, rs.getInt("warehouse_mandrake")); - this.resources.put(Enum.ResourceType.COAL, rs.getInt("warehouse_coal")); - this.resources.put(Enum.ResourceType.AGATE, rs.getInt("warehouse_agate")); - this.resources.put(Enum.ResourceType.DIAMOND, rs.getInt("warehouse_diamond")); - this.resources.put(Enum.ResourceType.ONYX, rs.getInt("warehouse_onyx")); - this.resources.put(Enum.ResourceType.AZOTH, rs.getInt("warehouse_azoth")); - this.resources.put(Enum.ResourceType.ORICHALK, rs.getInt("warehouse_orichalk")); - this.resources.put(Enum.ResourceType.ANTIMONY, rs.getInt("warehouse_antimony")); - this.resources.put(Enum.ResourceType.SULFUR, rs.getInt("warehouse_sulfur")); - this.resources.put(Enum.ResourceType.QUICKSILVER, rs.getInt("warehouse_quicksilver")); - this.resources.put(Enum.ResourceType.GALVOR, rs.getInt("warehouse_galvor")); - this.resources.put(Enum.ResourceType.WORMWOOD, rs.getInt("warehouse_wormwood")); - this.resources.put(Enum.ResourceType.OBSIDIAN, rs.getInt("warehouse_obsidian")); - this.resources.put(Enum.ResourceType.BLOODSTONE, rs.getInt("warehouse_bloodstone")); - this.resources.put(Enum.ResourceType.MITHRIL, rs.getInt("warehouse_mithril")); - this.resources.put(Enum.ResourceType.GOLD, rs.getInt("warehouse_gold")); - this.lockedResourceTypes = EnumBitSet.asEnumBitSet(rs.getLong("warehouse_locks"), Enum.ResourceType.class); - this.buildingUID = rs.getInt("parent"); - warehouseByBuildingUUID.put(this.buildingUID, this); } public static void warehouseDeposit(MerchantMsg msg, PlayerCharacter player, NPC npc) { @@ -95,7 +70,7 @@ public class Warehouse extends AbstractWorldObject { if (warehouseBuilding == null) return; - warehouse = warehouseByBuildingUUID.get(warehouseBuilding.getObjectUUID()); + warehouse = warehouseBuilding.getCity().warehouse; if (warehouse == null) return; @@ -129,7 +104,13 @@ public class Warehouse extends AbstractWorldObject { if (player.getGuild() != warehouseBuilding.getGuild() || !GuildStatusController.isInnerCouncil(player.getGuildStatus())) return; - warehouse = warehouseByBuildingUUID.get(warehouseBuilding.getObjectUUID()); + City city = warehouseBuilding.getCity(); + + if (city == null) + return; + ; + + warehouse = city.warehouse; if (warehouse == null) return; @@ -155,60 +136,65 @@ public class Warehouse extends AbstractWorldObject { } public static void warehouseLock(MerchantMsg msg, PlayerCharacter player, NPC npc) { - Building warehouse; + Building warehouseBuilding; + Warehouse warehouse; int hashID; Dispatch dispatch; hashID = msg.getHashID(); - warehouse = npc.getBuilding(); + warehouseBuilding = npc.getBuilding(); - if (warehouse == null) + if (warehouseBuilding == null) return; - if (player.getGuild() != warehouse.getGuild() || !GuildStatusController.isInnerCouncil(player.getGuildStatus())) + if (player.getGuild() != warehouseBuilding.getGuild() || !GuildStatusController.isInnerCouncil(player.getGuildStatus())) return; - Warehouse wh = warehouseByBuildingUUID.get(warehouse.getObjectUUID()); + City city = warehouseBuilding.getCity(); - if (wh == null) + if (city == null) return; + ; + + warehouse = city.warehouse; Enum.ResourceType resourceType = Enum.ResourceType.hashLookup.get(hashID); - if (isResourceLocked(wh, resourceType)) { - boolean worked; - EnumBitSet bitSet = EnumBitSet.asEnumBitSet(wh.lockedResourceTypes.toLong(), Enum.ResourceType.class); + // toggle lock - bitSet.remove(resourceType); + if (warehouse.lockedResourceTypes.contains(resourceType)) { - worked = DbManager.WarehouseQueries.updateLocks(wh, bitSet.toLong()); + boolean worked; + warehouse.lockedResourceTypes.remove(resourceType); + worked = DbManager.WarehouseQueries.updateWarehouse(warehouse); if (worked) { - wh.lockedResourceTypes.remove(resourceType); ViewResourcesMessage vrm = new ViewResourcesMessage(player); vrm.setGuild(player.getGuild()); - vrm.setWarehouseBuilding(warehouse); + vrm.setWarehouseBuilding(warehouseBuilding); vrm.configure(); dispatch = Dispatch.borrow(player, vrm); DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY); - } - return; - } - - EnumBitSet bitSet = EnumBitSet.asEnumBitSet(wh.lockedResourceTypes.toLong(), Enum.ResourceType.class); + } else + warehouse.lockedResourceTypes.add(resourceType); - bitSet.add(resourceType); - - if (!DbManager.WarehouseQueries.updateLocks(wh, bitSet.toLong())) return; + } - wh.lockedResourceTypes.add(resourceType); - ViewResourcesMessage vrm = new ViewResourcesMessage(player); - vrm.setGuild(player.getGuild()); - vrm.setWarehouseBuilding(warehouse); - vrm.configure(); - dispatch = Dispatch.borrow(player, vrm); - DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY); + boolean worked; + warehouse.lockedResourceTypes.add(resourceType); + worked = DbManager.WarehouseQueries.updateWarehouse(warehouse); + + if (worked) { + warehouse.lockedResourceTypes.add(resourceType); + ViewResourcesMessage vrm = new ViewResourcesMessage(player); + vrm.setGuild(player.getGuild()); + vrm.setWarehouseBuilding(warehouseBuilding); + vrm.configure(); + dispatch = Dispatch.borrow(player, vrm); + DispatchMessage.dispatchMsgDispatch(dispatch, Enum.DispatchChannel.SECONDARY); + } else + warehouse.lockedResourceTypes.remove(resourceType); } @@ -252,11 +238,8 @@ public class Warehouse extends AbstractWorldObject { int newAmount = oldAmount + amount; - if (newAmount > Enum.ResourceType.resourceLookup.get(resource.templateID).deposit_limit) { - //ChatManager.chatSystemInfo(pc, "The Warehouse is at it's maximum for this type of resource."); + if (newAmount > Enum.ResourceType.resourceLookup.get(resource.templateID).deposit_limit) return false; - } - if (removeFromInventory) { if (resourceType.equals(Enum.ResourceType.GOLD)) { @@ -267,10 +250,8 @@ public class Warehouse extends AbstractWorldObject { if (itemMan.getGoldInventory().getNumOfItems() - amount > MBServerStatics.PLAYER_GOLD_LIMIT) return false; - if (!itemMan.modifyInventoryGold(-amount)) { - //ChatManager.chatSystemError(pc, "You do not have this Gold."); + if (!itemMan.modifyInventoryGold(-amount)) return false; - } UpdateGoldMsg ugm = new UpdateGoldMsg(pc); ugm.configure(); @@ -284,13 +265,19 @@ public class Warehouse extends AbstractWorldObject { itemMan.updateInventory(); } } + itemMan.updateInventory(); - if (!DepositApproved(resourceType, amount, warehouse)) + if (newAmount > resourceType.deposit_limit) return false; warehouse.resources.put(resourceType, newAmount); + if (!DbManager.WarehouseQueries.updateWarehouse(warehouse)) { + warehouse.resources.put(resourceType, oldAmount); + return false; + } + if (resource.template.item_type.equals(Enum.ItemType.GOLD)) resourceType = Enum.ResourceType.GOLD; else @@ -310,114 +297,30 @@ public class Warehouse extends AbstractWorldObject { if (newAmount > resourceType.deposit_limit) return false; - if (!DepositApproved(resourceType, amount, warehouse)) - return false; - warehouse.resources.put(resourceType, newAmount); + if (!DbManager.WarehouseQueries.updateWarehouse(warehouse)) { + warehouse.resources.put(resourceType, oldAmount); + return false; + } + if (mine != null) AddTransactionToWarehouse(warehouse, Enum.GameObjectType.Building, mine.getBuildingID(), Enum.TransactionType.MINE, resourceType, amount); return true; } - public static boolean DepositApproved(Enum.ResourceType resourceType, int amount, Warehouse warehouse) { - - - if (warehouse.resources.get(resourceType) == null) - return false; - - int oldAmount = warehouse.resources.get(resourceType); - int newAmount = oldAmount + amount; - - if (newAmount > resourceType.deposit_limit) - return false; - - boolean worked = false; - - switch (resourceType) { - case GOLD: - worked = DbManager.WarehouseQueries.updateGold(warehouse, newAmount); - break; - case STONE: - worked = DbManager.WarehouseQueries.updateStone(warehouse, newAmount); - break; - case TRUESTEEL: - worked = DbManager.WarehouseQueries.updateTruesteel(warehouse, newAmount); - break; - case IRON: - worked = DbManager.WarehouseQueries.updateIron(warehouse, newAmount); - break; - case ADAMANT: - worked = DbManager.WarehouseQueries.updateAdamant(warehouse, newAmount); - break; - case LUMBER: - worked = DbManager.WarehouseQueries.updateLumber(warehouse, newAmount); - break; - case OAK: - worked = DbManager.WarehouseQueries.updateOak(warehouse, newAmount); - break; - case BRONZEWOOD: - worked = DbManager.WarehouseQueries.updateBronzewood(warehouse, newAmount); - break; - case MANDRAKE: - worked = DbManager.WarehouseQueries.updateMandrake(warehouse, newAmount); - break; - case COAL: - worked = DbManager.WarehouseQueries.updateCoal(warehouse, newAmount); - break; - case AGATE: - worked = DbManager.WarehouseQueries.updateAgate(warehouse, newAmount); - break; - case DIAMOND: - worked = DbManager.WarehouseQueries.updateDiamond(warehouse, newAmount); - break; - case ONYX: - worked = DbManager.WarehouseQueries.updateOnyx(warehouse, newAmount); - break; - case AZOTH: - worked = DbManager.WarehouseQueries.updateAzoth(warehouse, newAmount); - break; - case ORICHALK: - worked = DbManager.WarehouseQueries.updateOrichalk(warehouse, newAmount); - break; - case ANTIMONY: - worked = DbManager.WarehouseQueries.updateAntimony(warehouse, newAmount); - break; - case SULFUR: - worked = DbManager.WarehouseQueries.updateSulfur(warehouse, newAmount); - break; - case QUICKSILVER: - worked = DbManager.WarehouseQueries.updateQuicksilver(warehouse, newAmount); - break; - case GALVOR: - worked = DbManager.WarehouseQueries.updateGalvor(warehouse, newAmount); - break; - case WORMWOOD: - worked = DbManager.WarehouseQueries.updateWormwood(warehouse, newAmount); - break; - case OBSIDIAN: - worked = DbManager.WarehouseQueries.updateObsidian(warehouse, newAmount); - break; - case BLOODSTONE: - worked = DbManager.WarehouseQueries.updateBloodstone(warehouse, newAmount); - break; - case MITHRIL: - worked = DbManager.WarehouseQueries.updateMithril(warehouse, newAmount); - break; - } - return worked; - } - public static synchronized void depositRealmTaxes(PlayerCharacter taxer, Enum.ResourceType resourceType, int amount, Warehouse warehouse) { - if (!DepositApproved(resourceType, amount, warehouse)) - return; - int oldAmount = warehouse.resources.get(resourceType); int newAmount = oldAmount + amount; warehouse.resources.put(resourceType, newAmount); + if (!DbManager.WarehouseQueries.updateWarehouse(warehouse)) { + warehouse.resources.put(resourceType, oldAmount); + return; + } + AddTransactionToWarehouse(warehouse, taxer.getObjectType(), taxer.getObjectUUID(), Enum.TransactionType.TAXRESOURCEDEPOSIT, resourceType, amount); } @@ -433,18 +336,23 @@ public class Warehouse extends AbstractWorldObject { if (newAmount > resourceType.deposit_limit) return; - if (!DepositApproved(resourceType, amount, warehouse)) - return; - warehouse.resources.put(resourceType, newAmount); + if (!DbManager.WarehouseQueries.updateWarehouse(warehouse)) { + warehouse.resources.put(resourceType, oldAmount); + return; + } + if (building != null) AddTransactionToWarehouse(warehouse, Enum.GameObjectType.Building, building.getObjectUUID(), Enum.TransactionType.DEPOSIT, resourceType, amount); } - public static boolean WithdrawApproved(Enum.ResourceType resourceType, int amount, Warehouse warehouse) { + public static synchronized boolean withdraw(Warehouse warehouse, NPC npc, Enum.ResourceType resourceType, int amount, boolean transaction) { + + int oldAmount = warehouse.resources.get(resourceType); + int newAmount = oldAmount - amount; if (warehouse.resources.get(resourceType) == null) return false; @@ -452,98 +360,15 @@ public class Warehouse extends AbstractWorldObject { if (amount <= 0) return false; - int oldAmount = warehouse.resources.get(resourceType); - if (oldAmount < amount) return false; - int newAmount = oldAmount - amount; - boolean worked = false; - - switch (resourceType) { - case GOLD: - worked = DbManager.WarehouseQueries.updateGold(warehouse, newAmount); - break; - case STONE: - worked = DbManager.WarehouseQueries.updateStone(warehouse, newAmount); - break; - case TRUESTEEL: - worked = DbManager.WarehouseQueries.updateTruesteel(warehouse, newAmount); - break; - case IRON: - worked = DbManager.WarehouseQueries.updateIron(warehouse, newAmount); - break; - case ADAMANT: - worked = DbManager.WarehouseQueries.updateAdamant(warehouse, newAmount); - break; - case LUMBER: - worked = DbManager.WarehouseQueries.updateLumber(warehouse, newAmount); - break; - case OAK: - worked = DbManager.WarehouseQueries.updateOak(warehouse, newAmount); - break; - case BRONZEWOOD: - worked = DbManager.WarehouseQueries.updateBronzewood(warehouse, newAmount); - break; - case MANDRAKE: - worked = DbManager.WarehouseQueries.updateMandrake(warehouse, newAmount); - break; - case COAL: - worked = DbManager.WarehouseQueries.updateCoal(warehouse, newAmount); - break; - case AGATE: - worked = DbManager.WarehouseQueries.updateAgate(warehouse, newAmount); - break; - case DIAMOND: - worked = DbManager.WarehouseQueries.updateDiamond(warehouse, newAmount); - break; - case ONYX: - worked = DbManager.WarehouseQueries.updateOnyx(warehouse, newAmount); - break; - case AZOTH: - worked = DbManager.WarehouseQueries.updateAzoth(warehouse, newAmount); - break; - case ORICHALK: - worked = DbManager.WarehouseQueries.updateOrichalk(warehouse, newAmount); - break; - case ANTIMONY: - worked = DbManager.WarehouseQueries.updateAntimony(warehouse, newAmount); - break; - case SULFUR: - worked = DbManager.WarehouseQueries.updateSulfur(warehouse, newAmount); - break; - case QUICKSILVER: - worked = DbManager.WarehouseQueries.updateQuicksilver(warehouse, newAmount); - break; - case GALVOR: - worked = DbManager.WarehouseQueries.updateGalvor(warehouse, newAmount); - break; - case WORMWOOD: - worked = DbManager.WarehouseQueries.updateWormwood(warehouse, newAmount); - break; - case OBSIDIAN: - worked = DbManager.WarehouseQueries.updateObsidian(warehouse, newAmount); - break; - case BLOODSTONE: - worked = DbManager.WarehouseQueries.updateBloodstone(warehouse, newAmount); - break; - case MITHRIL: - worked = DbManager.WarehouseQueries.updateMithril(warehouse, newAmount); - break; - } - return worked; - } - - public static synchronized boolean withdraw(Warehouse warehouse, NPC npc, Enum.ResourceType resourceType, int amount, boolean transaction) { - - int oldAmount = warehouse.resources.get(resourceType); - - int newAmount = oldAmount - amount; + warehouse.resources.put(resourceType, newAmount); - if (!WithdrawApproved(resourceType, amount, warehouse)) + if (!DbManager.WarehouseQueries.updateWarehouse(warehouse)) { + warehouse.resources.put(resourceType, oldAmount); return false; - - warehouse.resources.put(resourceType, newAmount); + } if (transaction) AddTransactionToWarehouse(warehouse, npc.getObjectType(), npc.getObjectUUID(), Enum.TransactionType.WITHDRAWL, resourceType, amount); @@ -629,11 +454,12 @@ public class Warehouse extends AbstractWorldObject { int newAmount = oldAmount - amount; + warehouse.resources.put(resourceType, newAmount); - if (!WithdrawApproved(resourceType, amount, warehouse)) + if (!DbManager.WarehouseQueries.updateWarehouse(warehouse)) { + warehouse.resources.put(resourceType, oldAmount); return false; - - warehouse.resources.put(resourceType, newAmount); + } if (addToInventory) { if (resourceType.equals(Enum.ResourceType.GOLD)) { @@ -782,41 +608,7 @@ public class Warehouse extends AbstractWorldObject { public void updateDatabase() { // TODO Auto-generated method stub - } - - @Override - public void runAfterLoad() { - - try { - Building warehouseBuilding = BuildingManager.getBuilding(this.buildingUID); - Logger.info("configuring warehouse " + UID + " for city " + warehouseBuilding.getCity().getCityName() + " structure UUID " + this.buildingUID); - //Building is gone, but Warehouse still in DB?? Should never happen, sanity check anyway. - if (warehouseBuilding == null) { - Logger.error("Failed to load Building for Warehouse"); - return; - } - - Zone cityZone = warehouseBuilding.getParentZone(); - - if (cityZone == null) { - Logger.error("Failed to load Zone for Warehouse with UUID " + this.getObjectUUID()); - return; - } - - City city = City.getCity(cityZone.playerCityUUID); - - if (city == null) { - Logger.error("Failed to load City for Warehouse with UUID " + this.getObjectUUID()); - return; - } - - warehouseByBuildingUUID.put(this.buildingUID, this); - city.setWarehouseBuildingID(this.buildingUID); - } catch (Exception E) { - Logger.info(this.getObjectUUID() + " failed"); - - } } } \ No newline at end of file