// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ . // ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌· // ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀ // ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌ // ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀ // Magicbane Emulator Project © 2013 - 2022 // www.magicbane.com 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; 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 { public dbEffectsResourceCostHandler() { this.localClass = EffectsResourceCosts.class; this.localObjectType = mbEnums.GameObjectType.valueOf(this.localClass.getSimpleName()); } public void GENERATE_COST_DATA() { ArrayList effectList = GET_EFFECTS_WITH_COST(); for (String effectID : effectList) { JSONObject costMap = GET_EFFECT_COSTMAP(effectID); WRITE_COSTMAP(effectID, costMap); } } public JSONObject GET_EFFECT_COSTMAP(String effectID) { HashMap costMap = new HashMap<>(); try (Connection connection = DbManager.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement("SELECT * 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<>(); try (Connection connection = DbManager.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_power_effectcost` WHERE `IDString` = ?")) { preparedStatement.setString(1, idString); ResultSet rs = preparedStatement.executeQuery(); effectsResourceCosts = getObjectsFromRs(rs, 1000); } catch (SQLException e) { Logger.error(e); } return effectsResourceCosts; } }