From f48b9f10e168349b2de267e203bb36a49535a0a1 Mon Sep 17 00:00:00 2001 From: MagicBot Date: Tue, 23 Apr 2024 14:25:19 -0400 Subject: [PATCH] Code written to refactor db call out of effect costs. --- .../dbEffectsResourceCostHandler.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/engine/db/handlers/dbEffectsResourceCostHandler.java b/src/engine/db/handlers/dbEffectsResourceCostHandler.java index 8525144c..998cdc66 100644 --- a/src/engine/db/handlers/dbEffectsResourceCostHandler.java +++ b/src/engine/db/handlers/dbEffectsResourceCostHandler.java @@ -12,6 +12,7 @@ package engine.db.handlers; import engine.gameManager.DbManager; import engine.mbEnums; import engine.objects.EffectsResourceCosts; +import org.json.JSONObject; import org.pmw.tinylog.Logger; import java.sql.Connection; @@ -19,6 +20,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.HashMap; public class dbEffectsResourceCostHandler extends dbHandlerBase { @@ -27,6 +29,67 @@ public class dbEffectsResourceCostHandler extends dbHandlerBase { this.localObjectType = mbEnums.GameObjectType.valueOf(this.localClass.getSimpleName()); } + public void GENERATE_COST_DATA() { + + } + + public JSONObject GET_EFFECT_COSTMAP(String effectID) { + HashMap costMap = new HashMap<>(); + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT DISTINCT `IDString` FROM `static_power_effectcost` WHERE `IDString` = ?")) { + + preparedStatement.setString(1, effectID); + ResultSet rs = preparedStatement.executeQuery(); + + while (rs.next()) { + mbEnums.ResourceType resourceType = mbEnums.ResourceType.resourceLookup.get(rs.getInt("resource")); + int value = rs.getInt("amount"); + costMap.put(resourceType, value); + } + + } catch (SQLException e) { + Logger.error(e); + } + return new JSONObject(costMap); + } + + public ArrayList GET_EFFECTS_WITH_COST() { + + ArrayList effectList = new ArrayList<>(); + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT DISTINCT `IDString` FROM `static_power_effectcost`")) { + + ResultSet rs = preparedStatement.executeQuery(); + + while (rs.next()) { + effectList.add(rs.getString("IdString")); + } + + } catch (SQLException e) { + Logger.error(e); + } + return effectList; + } + + public boolean WRITE_COSTMAP(String effectID, JSONObject costmap) { + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO `static_effect_costmaps` (`effectID`, `costMap`) VALUES (?, ?) " + + "ON DUPLICATE KEY UPDATE `costmap` = VALUES(`costmap`)")) { + + preparedStatement.setString(1, effectID); + preparedStatement.setString(2, costmap.toString()); + + return (preparedStatement.executeUpdate() > 0); + + } catch (SQLException e) { + Logger.error(e); + } + return false; + } + public ArrayList GET_ALL_EFFECT_RESOURCES(String idString) { ArrayList effectsResourceCosts = new ArrayList<>();