Refactor to remove abstraction.
This commit is contained in:
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
package engine.db.handlers;
|
package engine.db.handlers;
|
||||||
|
|
||||||
|
import engine.gameManager.DbManager;
|
||||||
import engine.objects.Bane;
|
import engine.objects.Bane;
|
||||||
import engine.objects.Building;
|
import engine.objects.Building;
|
||||||
import engine.objects.City;
|
import engine.objects.City;
|
||||||
@@ -16,10 +17,10 @@ import engine.objects.PlayerCharacter;
|
|||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
import org.pmw.tinylog.Logger;
|
import org.pmw.tinylog.Logger;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.logging.Level;
|
|
||||||
|
|
||||||
public class dbBaneHandler extends dbHandlerBase {
|
public class dbBaneHandler extends dbHandlerBase {
|
||||||
|
|
||||||
@@ -29,78 +30,63 @@ public class dbBaneHandler extends dbHandlerBase {
|
|||||||
|
|
||||||
public boolean CREATE_BANE(City city, PlayerCharacter owner, Building stone) {
|
public boolean CREATE_BANE(City city, PlayerCharacter owner, Building stone) {
|
||||||
|
|
||||||
prepareCallable("INSERT INTO `dyn_banes` (`cityUUID`, `ownerUUID`, `stoneUUID`, `placementDate`) VALUES(?,?,?,?)");
|
try (Connection connection = DbManager.getConnection();
|
||||||
setLong(1, (long) city.getObjectUUID());
|
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO `dyn_banes` (`cityUUID`, `ownerUUID`, `stoneUUID`, `placementDate`) VALUES(?,?,?,?)")) {
|
||||||
setLong(2, (long) owner.getObjectUUID());
|
|
||||||
setLong(3, (long) stone.getObjectUUID());
|
|
||||||
setTimeStamp(4, System.currentTimeMillis());
|
|
||||||
|
|
||||||
return (executeUpdate() > 0);
|
preparedStatement.setLong(1, city.getObjectUUID());
|
||||||
|
preparedStatement.setLong(2, owner.getObjectUUID());
|
||||||
|
preparedStatement.setLong(3, stone.getObjectUUID());
|
||||||
|
preparedStatement.setTimestamp(4, new java.sql.Timestamp(System.currentTimeMillis()));
|
||||||
|
|
||||||
|
preparedStatement.execute();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
Logger.error(e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bane LOAD_BANE(int cityUUID) {
|
public Bane LOAD_BANE(int cityUUID) {
|
||||||
|
|
||||||
Bane newBane = null;
|
Bane bane = null;
|
||||||
|
|
||||||
try {
|
try (Connection connection = DbManager.getConnection();
|
||||||
|
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * from dyn_banes WHERE `dyn_banes`.`cityUUID` = ?")) {
|
||||||
|
|
||||||
prepareCallable("SELECT * from dyn_banes WHERE `dyn_banes`.`cityUUID` = ?");
|
preparedStatement.setLong(1, cityUUID);
|
||||||
|
ResultSet rs = preparedStatement.executeQuery();
|
||||||
setLong(1, (long) cityUUID);
|
|
||||||
ResultSet rs = executeQuery();
|
|
||||||
|
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
newBane = new Bane(rs);
|
bane = new Bane(rs);
|
||||||
Bane.addBane(newBane);
|
Bane.addBane(bane);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (SQLException ex) {
|
|
||||||
java.util.logging.Logger.getLogger(dbBaneHandler.class.getName()).log(Level.SEVERE, null, ex);
|
|
||||||
} finally {
|
|
||||||
closeCallable();
|
|
||||||
}
|
|
||||||
return newBane;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConcurrentHashMap<Integer, Bane> LOAD_ALL_BANES() {
|
|
||||||
|
|
||||||
ConcurrentHashMap<Integer, Bane> baneList;
|
|
||||||
Bane thisBane;
|
|
||||||
|
|
||||||
baneList = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
int recordsRead = 0;
|
|
||||||
|
|
||||||
prepareCallable("SELECT * FROM dyn_banes");
|
|
||||||
|
|
||||||
try {
|
|
||||||
ResultSet rs = executeQuery();
|
|
||||||
|
|
||||||
while (rs.next()) {
|
|
||||||
|
|
||||||
recordsRead++;
|
|
||||||
thisBane = new Bane(rs);
|
|
||||||
baneList.put(thisBane.getCityUUID(), thisBane);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger.info("read: " + recordsRead + " cached: " + baneList.size());
|
|
||||||
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
Logger.error( e.toString());
|
Logger.error(e);
|
||||||
} finally {
|
|
||||||
closeCallable();
|
|
||||||
}
|
}
|
||||||
return baneList;
|
|
||||||
|
return bane;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean SET_BANE_TIME(DateTime toSet, int cityUUID) {
|
public boolean SET_BANE_TIME(DateTime toSet, int cityUUID) {
|
||||||
prepareCallable("UPDATE `dyn_banes` SET `liveDate`=? WHERE `cityUUID`=?");
|
|
||||||
setTimeStamp(1, toSet.getMillis());
|
try (Connection connection = DbManager.getConnection();
|
||||||
setLong(2, cityUUID);
|
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `dyn_banes` SET `liveDate`=? WHERE `cityUUID`=?")) {
|
||||||
return (executeUpdate() > 0);
|
|
||||||
|
preparedStatement.setTimestamp(1, new java.sql.Timestamp(toSet.getMillis()));
|
||||||
|
preparedStatement.setLong(2, cityUUID);
|
||||||
|
|
||||||
|
preparedStatement.execute();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
Logger.error(e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean REMOVE_BANE(Bane bane) {
|
public boolean REMOVE_BANE(Bane bane) {
|
||||||
@@ -108,8 +94,17 @@ public class dbBaneHandler extends dbHandlerBase {
|
|||||||
if (bane == null)
|
if (bane == null)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
prepareCallable("DELETE FROM `dyn_banes` WHERE `cityUUID` = ?");
|
try (Connection connection = DbManager.getConnection();
|
||||||
setLong(1, (long) bane.getCity().getObjectUUID());
|
PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM `dyn_banes` WHERE `cityUUID` = ?")) {
|
||||||
return (executeUpdate() > 0);
|
|
||||||
|
preparedStatement.setLong(1, bane.getCity().getObjectUUID());
|
||||||
|
preparedStatement.execute();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
Logger.error(e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user