Code written to refactor db call out of effect costs.

This commit is contained in:
2024-04-23 14:25:19 -04:00
parent 2607962038
commit f48b9f10e1
@@ -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<mbEnums.ResourceType, Integer> 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<String> GET_EFFECTS_WITH_COST() {
ArrayList<String> 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<EffectsResourceCosts> GET_ALL_EFFECT_RESOURCES(String idString) {
ArrayList<EffectsResourceCosts> effectsResourceCosts = new ArrayList<>();