Files
BattleBane/src/engine/db/handlers/dbBaneHandler.java
T

261 lines
9.9 KiB
Java
Raw Normal View History

2022-04-30 09:41:17 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.db.handlers;
2023-05-20 17:51:21 -04:00
import engine.gameManager.DbManager;
2024-12-28 05:18:24 -06:00
import engine.gameManager.ZoneManager;
import engine.math.Vector3fImmutable;
import engine.objects.*;
2022-04-30 09:41:17 -04:00
import org.joda.time.DateTime;
import org.pmw.tinylog.Logger;
2024-12-28 07:49:41 -06:00
import java.sql.*;
2022-04-30 09:41:17 -04:00
public class dbBaneHandler extends dbHandlerBase {
public dbBaneHandler() {
}
public boolean CREATE_BANE(City city, PlayerCharacter owner, Building stone) {
2023-05-20 17:51:21 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO `dyn_banes` (`cityUUID`, `ownerUUID`, `stoneUUID`, `placementDate`) VALUES(?,?,?,?)")) {
2022-04-30 09:41:17 -04:00
2023-05-20 17:51:21 -04:00
preparedStatement.setLong(1, city.getObjectUUID());
preparedStatement.setLong(2, owner.getObjectUUID());
preparedStatement.setLong(3, stone.getObjectUUID());
preparedStatement.setTimestamp(4, new java.sql.Timestamp(System.currentTimeMillis()));
2022-04-30 09:41:17 -04:00
2023-05-20 17:51:21 -04:00
preparedStatement.execute();
} catch (SQLException e) {
Logger.error(e);
return false;
}
return true;
2022-04-30 09:41:17 -04:00
}
public Bane LOAD_BANE(int cityUUID) {
2023-05-20 17:51:21 -04:00
Bane bane = null;
2022-04-30 09:41:17 -04:00
2023-05-20 17:51:21 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * from dyn_banes WHERE `dyn_banes`.`cityUUID` = ?")) {
2022-04-30 09:41:17 -04:00
2023-05-20 17:51:21 -04:00
preparedStatement.setLong(1, cityUUID);
ResultSet rs = preparedStatement.executeQuery();
2022-04-30 09:41:17 -04:00
if (rs.next()) {
2023-05-20 17:51:21 -04:00
bane = new Bane(rs);
Bane.addBane(bane);
2022-04-30 09:41:17 -04:00
}
2023-05-20 17:51:21 -04:00
} catch (SQLException e) {
Logger.error(e);
2022-04-30 09:41:17 -04:00
}
2023-05-20 17:51:21 -04:00
return bane;
2022-04-30 09:41:17 -04:00
}
2023-05-20 17:51:21 -04:00
public boolean SET_BANE_TIME(DateTime toSet, int cityUUID) {
2022-04-30 09:41:17 -04:00
2023-05-20 17:51:21 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `dyn_banes` SET `liveDate`=? WHERE `cityUUID`=?")) {
2022-04-30 09:41:17 -04:00
2023-05-20 17:51:21 -04:00
preparedStatement.setTimestamp(1, new java.sql.Timestamp(toSet.getMillis()));
preparedStatement.setLong(2, cityUUID);
2022-04-30 09:41:17 -04:00
2023-05-20 17:51:21 -04:00
preparedStatement.execute();
2022-04-30 09:41:17 -04:00
} catch (SQLException e) {
2023-05-20 17:51:21 -04:00
Logger.error(e);
return false;
2022-04-30 09:41:17 -04:00
}
2023-05-20 17:51:21 -04:00
return true;
2022-04-30 09:41:17 -04:00
}
2024-12-28 07:45:43 -06:00
public boolean SET_BANE_TIME_NEW(int hour, int cityUUID) {
2024-12-31 16:25:34 -06:00
hour += 12; // Adjust hour
2024-12-28 07:39:15 -06:00
try (Connection connection = DbManager.getConnection();
2024-12-28 07:49:41 -06:00
PreparedStatement getStatement = connection.prepareStatement("SELECT `placementDate`, `liveDate` FROM `dyn_banes` WHERE `cityUUID`=?");
2024-12-28 07:45:43 -06:00
PreparedStatement updateStatement = connection.prepareStatement("UPDATE `dyn_banes` SET `liveDate`=?, `time_set`=? WHERE `cityUUID`=?")) {
2024-12-28 07:49:41 -06:00
// Retrieve placementDate and liveDate
2024-12-28 07:45:43 -06:00
getStatement.setInt(1, cityUUID);
try (ResultSet rs = getStatement.executeQuery()) {
if (rs.next()) {
2024-12-28 07:49:41 -06:00
DateTime placementDate = new DateTime(rs.getTimestamp("placementDate").getTime());
Timestamp liveDateTimestamp = rs.getTimestamp("liveDate");
2024-12-31 16:25:34 -06:00
// Explicitly check if liveDate is null
2024-12-28 07:49:41 -06:00
DateTime toSet;
2024-12-31 16:25:34 -06:00
if (liveDateTimestamp == null) {
// If liveDate is null, default to placementDate
toSet = placementDate;
2024-12-28 07:49:41 -06:00
} else {
2024-12-31 16:25:34 -06:00
// If liveDate is not null, use it
DateTime liveDate = new DateTime(liveDateTimestamp.getTime());
toSet = liveDate;
2024-12-28 07:49:41 -06:00
}
2024-12-28 07:45:43 -06:00
2024-12-31 16:25:34 -06:00
// Adjust the time
toSet = toSet.withHourOfDay(hour).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
2024-12-28 07:45:43 -06:00
// Update liveDate and time_set flag
2024-12-28 07:49:41 -06:00
updateStatement.setTimestamp(1, new java.sql.Timestamp(toSet.getMillis()));
2024-12-28 07:45:43 -06:00
updateStatement.setInt(2, 1); // time_set flag
updateStatement.setInt(3, cityUUID);
updateStatement.execute();
return true;
}
}
2024-12-28 07:39:15 -06:00
} catch (SQLException e) {
Logger.error(e);
}
2024-12-28 07:45:43 -06:00
return false;
2024-12-28 07:39:15 -06:00
}
2024-12-28 07:45:43 -06:00
public boolean SET_BANE_DAY_NEW(int dayOffset, int cityUUID) {
2024-12-28 07:39:15 -06:00
try (Connection connection = DbManager.getConnection();
2024-12-28 07:49:41 -06:00
PreparedStatement getStatement = connection.prepareStatement("SELECT `placementDate`, `liveDate` FROM `dyn_banes` WHERE `cityUUID`=?");
2024-12-28 07:45:43 -06:00
PreparedStatement updateStatement = connection.prepareStatement("UPDATE `dyn_banes` SET `liveDate`=?, `day_set`=? WHERE `cityUUID`=?")) {
2024-12-28 07:49:41 -06:00
// Retrieve placementDate and liveDate
2024-12-28 07:45:43 -06:00
getStatement.setInt(1, cityUUID);
try (ResultSet rs = getStatement.executeQuery()) {
if (rs.next()) {
2024-12-28 07:49:41 -06:00
DateTime placementDate = new DateTime(rs.getTimestamp("placementDate").getTime());
2024-12-31 16:25:34 -06:00
Timestamp liveDateTimestamp = rs.getTimestamp("liveDate");
// Explicitly check if liveDate is null
DateTime liveDate;
if (liveDateTimestamp == null) {
// If liveDate is null, default to placementDate
liveDate = placementDate;
} else {
// If liveDate is not null, use it
liveDate = new DateTime(liveDateTimestamp.getTime());
}
2024-12-28 07:49:41 -06:00
// Calculate the new liveDate while preserving the time component
DateTime updatedDate = placementDate.plusDays(dayOffset)
.withHourOfDay(liveDate.getHourOfDay())
.withMinuteOfHour(liveDate.getMinuteOfHour())
.withSecondOfMinute(liveDate.getSecondOfMinute())
.withMillisOfSecond(liveDate.getMillisOfSecond());
2024-12-28 07:45:43 -06:00
// Update liveDate and day_set flag
updateStatement.setTimestamp(1, new java.sql.Timestamp(updatedDate.getMillis()));
updateStatement.setInt(2, 1); // day_set flag
updateStatement.setInt(3, cityUUID);
updateStatement.execute();
return true;
}
}
2024-12-28 07:39:15 -06:00
} catch (SQLException e) {
Logger.error(e);
}
2024-12-28 07:45:43 -06:00
return false;
2024-12-28 07:39:15 -06:00
}
2024-12-28 07:45:43 -06:00
2024-12-28 07:49:41 -06:00
2024-12-31 16:25:34 -06:00
2024-12-28 07:39:15 -06:00
public boolean SET_BANE_CAP_NEW(int count, int cityUUID) {
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `dyn_banes` SET `cap_size`=? WHERE `cityUUID`=?")) {
preparedStatement.setInt(1, count);
preparedStatement.setLong(2, cityUUID);
preparedStatement.execute();
} catch (SQLException e) {
Logger.error(e);
return false;
}
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `dyn_banes` SET `cap_set`=? WHERE `cityUUID`=?")) {
preparedStatement.setInt(1, 1);
preparedStatement.setLong(2, cityUUID);
preparedStatement.execute();
} catch (SQLException e) {
Logger.error(e);
return false;
}
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `dyn_banes` SET `cap_size`=? WHERE `cityUUID`=?")) {
preparedStatement.setInt(1, count);
preparedStatement.setLong(2, cityUUID);
preparedStatement.execute();
} catch (SQLException e) {
Logger.error(e);
return false;
}
return true;
}
2022-04-30 09:41:17 -04:00
public boolean REMOVE_BANE(Bane bane) {
if (bane == null)
return false;
2023-05-20 17:51:21 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM `dyn_banes` WHERE `cityUUID` = ?")) {
preparedStatement.setLong(1, bane.getCity().getObjectUUID());
preparedStatement.execute();
} catch (SQLException e) {
Logger.error(e);
return false;
}
2023-05-23 09:17:06 -04:00
return true;
2022-04-30 09:41:17 -04:00
}
2024-12-31 13:59:23 -06:00
public DateTime getLiveDate(int cityUUID) {
try (Connection connection = DbManager.getConnection();
PreparedStatement statement = connection.prepareStatement("SELECT `liveDate` FROM `dyn_banes` WHERE `cityUUID`=?")) {
statement.setInt(1, cityUUID);
try (ResultSet rs = statement.executeQuery()) {
if (rs.next()) {
Timestamp liveDateTimestamp = rs.getTimestamp("liveDate");
if (liveDateTimestamp != null) {
return new DateTime(liveDateTimestamp.getTime());
}
}
}
} catch (SQLException e) {
Logger.error(e);
}
return null; // Return null if liveDate is not found or an error occurs
}
2022-04-30 09:41:17 -04:00
}