Files
Server/src/engine/db/archive/CityRecord.java
T

162 lines
6.0 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.archive;
import engine.gameManager.DbManager;
import engine.mbEnums;
2022-04-30 09:41:17 -04:00
import engine.objects.City;
import engine.workthreads.WarehousePushThread;
import java.sql.*;
import java.util.concurrent.LinkedBlockingQueue;
public class CityRecord extends DataRecord {
2023-07-15 09:23:48 -04:00
private static final LinkedBlockingQueue<CityRecord> recordPool = new LinkedBlockingQueue<>();
private mbEnums.RecordEventType eventType;
2023-07-15 09:23:48 -04:00
private City city;
private String cityHash;
private String cityGuildHash;
private String cityName;
private String cityMotto;
private float locX;
private float locY;
private String zoneHash;
private java.time.LocalDateTime establishedDatetime;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
private CityRecord(City city) {
this.recordType = mbEnums.DataRecordType.CITY;
2023-07-15 09:23:48 -04:00
this.city = city;
this.eventType = mbEnums.RecordEventType.CREATE;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
}
2022-04-30 09:41:17 -04:00
public static CityRecord borrow(City city, mbEnums.RecordEventType eventType) {
2023-07-15 09:23:48 -04:00
CityRecord cityRecord;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
cityRecord = recordPool.poll();
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (cityRecord == null) {
cityRecord = new CityRecord(city);
cityRecord.eventType = eventType;
} else {
cityRecord.recordType = mbEnums.DataRecordType.CITY;
2023-07-15 09:23:48 -04:00
cityRecord.eventType = eventType;
cityRecord.city = city;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (cityRecord.city.getHash() == null)
cityRecord.city.setHash(DataWarehouse.hasher.encrypt(cityRecord.city.getObjectUUID()));
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
cityRecord.cityHash = cityRecord.city.getHash();
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
cityRecord.cityName = cityRecord.city.getCityName();
cityRecord.cityMotto = cityRecord.city.getMotto();
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
cityRecord.cityGuildHash = cityRecord.city.getGuild().getHash();
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
cityRecord.locX = cityRecord.city.getTOL().getLoc().x;
cityRecord.locY = -cityRecord.city.getTOL().getLoc().z; // flip sign on 'y' coordinate
2022-04-30 09:41:17 -04:00
2023-09-20 15:43:01 -04:00
cityRecord.zoneHash = cityRecord.city.getParent().hash;
2022-04-30 09:41:17 -04:00
if (cityRecord.eventType.equals(mbEnums.RecordEventType.CREATE))
2023-07-15 09:23:48 -04:00
cityRecord.establishedDatetime = cityRecord.city.established;
else
cityRecord.establishedDatetime = java.time.LocalDateTime.now();
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
return cityRecord;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public static PreparedStatement buildCityPushStatement(Connection connection, ResultSet rs) throws SQLException {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
PreparedStatement outStatement = null;
String queryString = "INSERT INTO `warehouse_cityhistory` (`event_number`, `city_id`, `city_name`, `city_motto`, `guild_id`, `loc_x`, `loc_y`, `zone_id`, `eventType`, `datetime`) VALUES(?,?,?,?,?,?,?,?,?,?)";
outStatement = connection.prepareStatement(queryString);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Bind record data
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
outStatement.setInt(1, rs.getInt("event_number"));
outStatement.setString(2, rs.getString("city_id"));
outStatement.setString(3, rs.getString("city_name"));
outStatement.setString(4, rs.getString("city_motto"));
outStatement.setString(5, rs.getString("guild_id"));
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
outStatement.setFloat(6, rs.getFloat("loc_x"));
outStatement.setFloat(7, rs.getFloat("loc_y"));
outStatement.setString(8, rs.getString("zone_id"));
outStatement.setString(9, rs.getString("eventType"));
outStatement.setTimestamp(10, rs.getTimestamp("datetime"));
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
return outStatement;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public static PreparedStatement buildCityQueryStatement(Connection connection) throws SQLException {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
PreparedStatement outStatement = null;
String queryString = "SELECT * FROM `warehouse_cityhistory` WHERE `event_number` > ?";
outStatement = connection.prepareStatement(queryString);
outStatement.setInt(1, WarehousePushThread.cityIndex);
return outStatement;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
void reset() {
this.city = null;
this.cityHash = null;
this.cityGuildHash = null;
this.cityMotto = null;
this.zoneHash = null;
this.establishedDatetime = null;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public void release() {
this.reset();
recordPool.add(this);
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public void write() {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement statement = this.buildCityInsertStatement(connection)) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
statement.execute();
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
} catch (SQLException e) {
e.printStackTrace();
}
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
private PreparedStatement buildCityInsertStatement(Connection connection) throws SQLException {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
PreparedStatement outStatement = null;
String queryString = "INSERT INTO `warehouse_cityhistory` (`city_id`, `city_name`, `city_motto`, `guild_id`, `loc_x`, `loc_y`, `zone_id`, `eventType`, `datetime`) VALUES(?,?,?,?,?,?,?,?,?)";
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
outStatement = connection.prepareStatement(queryString);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Bind character data
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
outStatement.setString(1, this.cityHash);
outStatement.setString(2, this.cityName);
outStatement.setString(3, this.cityMotto);
outStatement.setString(4, this.cityGuildHash);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
outStatement.setFloat(5, this.locX);
outStatement.setFloat(6, this.locY);
outStatement.setString(7, this.zoneHash);
outStatement.setString(8, this.eventType.name());
outStatement.setTimestamp(9, Timestamp.valueOf(this.establishedDatetime));
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
return outStatement;
}
2022-04-30 09:41:17 -04:00
}