Refactored out duplicate db interface.

This commit is contained in:
2024-03-14 15:22:38 -04:00
parent 4646d03bdd
commit f2d28fd7af
13 changed files with 86 additions and 1576 deletions
@@ -84,22 +84,6 @@ public abstract class AbstractGameObject {
return GameObjectType.values()[ordinal];
}
/**
* Generates a {@link PreparedStatementShared} based on the specified query.
* <p>
* If {@link AbstractGameObject} Database functions will properly release
* the PreparedStatementShared upon completion. If these functions are not
* used, then {@link PreparedStatementShared#release release()} must be
* called when finished with this object.
*
* @param sql The SQL string used to generate the PreparedStatementShared
* @return {@link PreparedStatementShared}
* @throws {@link SQLException}
**/
protected static PreparedStatementShared prepareStatement(String sql) throws SQLException {
return new PreparedStatementShared(sql);
}
public static AbstractGameObject getFromTypeAndID(long compositeID) {
int objectTypeID = extractTypeOrdinal(compositeID);
int tableID = extractTableID(objectTypeID, compositeID);
-71
View File
@@ -1,71 +0,0 @@
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.objects;
import engine.server.MBServerStatics;
import org.pmw.tinylog.Logger;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.ConcurrentHashMap;
public class LevelDefault {
public static ConcurrentHashMap<Byte, LevelDefault> defaults = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW);
public final int level;
public final float health;
public final float mana;
public final float stamina;
public final float atr;
public final float def;
public final float minDamage;
public final float maxDamage;
public final int goldMin;
public final int goldMax;
/**
* ResultSet Constructor
*/
public LevelDefault(ResultSet rs) throws SQLException {
super();
this.level = rs.getInt("level");
this.health = rs.getFloat("health");
this.mana = (float) rs.getInt("mana");
this.stamina = (float) rs.getInt("stamina");
this.atr = (float) rs.getInt("atr");
this.def = (float) rs.getInt("def");
this.minDamage = (float) rs.getInt("minDamage");
this.maxDamage = (float) rs.getInt("maxDamage");
this.goldMin = rs.getInt("goldMin");
this.goldMax = rs.getInt("goldMax");
}
public static LevelDefault getLevelDefault(byte level) {
LevelDefault ret = null;
if (LevelDefault.defaults.containsKey(level))
return LevelDefault.defaults.get(level);
PreparedStatementShared ps = null;
try {
ps = new PreparedStatementShared("SELECT * FROM `static_npc_level_defaults` WHERE level = ?;");
ps.setInt(1, (int) level);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
ret = new LevelDefault(rs);
LevelDefault.defaults.put(level, ret);
}
} catch (SQLException e) {
Logger.error("SQL Error number: " + e.getErrorCode() + ' ' + e.getMessage());
} finally {
ps.release();
}
return ret;
}
}
+16 -12
View File
@@ -9,9 +9,12 @@
package engine.objects;
import engine.gameManager.DbManager;
import engine.server.MBServerStatics;
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;
@@ -86,18 +89,26 @@ public class PowerGrant extends AbstractGameObject {
}
public static void fillGrantedPowers() {
PowerGrant.grantedPowers = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW);
PreparedStatementShared ps = null;
try {
ps = prepareStatement("SELECT * FROM static_power_powergrant");
ResultSet rs = ps.executeQuery();
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_power_powergrant")) {
ResultSet rs = preparedStatement.executeQuery();
if (PowerGrant.grantedPowers.size() > 0) {
rs.close();
return;
}
while (rs.next()) {
int token = rs.getInt("powerToken");
PowerGrant pg = null;
PowerGrant pg;
if (PowerGrant.grantedPowers.containsKey(token)) {
pg = PowerGrant.grantedPowers.get(token);
pg.addRuneGrant(rs.getInt("runeID"), rs.getShort("grantAmount"));
@@ -109,16 +120,9 @@ public class PowerGrant extends AbstractGameObject {
rs.close();
} catch (SQLException e) {
Logger.error("SQL Error number: " + e.getErrorCode(), e);
} finally {
ps.release();
}
}
public ConcurrentHashMap<Integer, Short> getRuneGrants() {
return this.runeGrants;
}
/*
* Database
*/
+12 -7
View File
@@ -9,11 +9,14 @@
package engine.objects;
import engine.gameManager.DbManager;
import engine.gameManager.PowersManager;
import engine.powers.PowersBase;
import engine.server.MBServerStatics;
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;
@@ -87,15 +90,18 @@ public class PowerReq extends AbstractGameObject implements Comparable<PowerReq>
}
public static ConcurrentHashMap<Integer, ArrayList<PowerReq>> fillRunePowers() {
PowerReq.runePowers = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW);
PreparedStatementShared ps = null;
try {
ps = prepareStatement("SELECT * FROM static_power_powerrequirement");
ResultSet rs = ps.executeQuery();
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_power_powerrequirement")) {
ResultSet rs = preparedStatement.executeQuery();
if (PowerReq.runePowers.size() > 0) {
rs.close();
return PowerReq.runePowers;
}
while (rs.next()) {
ArrayList<PowerReq> runePR = null;
int runeID = rs.getInt("runeID");
@@ -130,9 +136,8 @@ public class PowerReq extends AbstractGameObject implements Comparable<PowerReq>
}
} catch (SQLException e) {
Logger.error("SQL Error number: " + e.getErrorCode(), e);
} finally {
ps.release();
}
return PowerReq.runePowers;
}
File diff suppressed because it is too large Load Diff