Initial Repository Push

This commit is contained in:
2022-04-30 09:41:17 -04:00
parent d4eef9097a
commit bbfdde57a3
835 changed files with 168392 additions and 0 deletions
+383
View File
@@ -0,0 +1,383 @@
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.db.archive;
import engine.Enum;
import engine.objects.Bane;
import engine.objects.City;
import engine.workthreads.WarehousePushThread;
import org.joda.time.DateTime;
import org.pmw.tinylog.Logger;
import java.sql.*;
import java.time.LocalDateTime;
import java.util.concurrent.LinkedBlockingQueue;
import static engine.Enum.RecordEventType;
public class BaneRecord extends DataRecord {
private static final LinkedBlockingQueue<BaneRecord> recordPool = new LinkedBlockingQueue<>();
private RecordEventType eventType;
private String cityHash;
private String cityName;
private String cityGuildHash;
private String cityNationHash;
private String baneDropperHash;
private String baneGuildHash;
private String baneNationHash;
private DateTime baneLiveTime;
private DateTime baneDropTime;
private BaneRecord(Bane bane) {
this.recordType = Enum.DataRecordType.BANE;
this.eventType = RecordEventType.PENDING;
}
public static BaneRecord borrow(Bane bane, RecordEventType eventType) {
BaneRecord baneRecord;
baneRecord = recordPool.poll();
if (baneRecord == null) {
baneRecord = new BaneRecord(bane);
baneRecord.eventType = eventType;
}
else {
baneRecord.recordType = Enum.DataRecordType.BANE;
baneRecord.eventType = eventType;
}
baneRecord.cityHash = bane.getCity().getHash();
baneRecord.cityName = bane.getCity().getCityName();
baneRecord.cityGuildHash = bane.getCity().getGuild().getHash();
baneRecord.cityNationHash = bane.getCity().getGuild().getNation().getHash();
if (bane.getOwner() == null) {
baneRecord.baneDropperHash = "ERRANT";
baneRecord.baneGuildHash = "ERRANT";
baneRecord.baneNationHash = "ERRANT";
}
else {
baneRecord.baneDropperHash = DataWarehouse.hasher.encrypt(bane.getOwner().getObjectUUID()); // getPlayerCharacter didn't check hash first? OMFG
baneRecord.baneGuildHash = bane.getOwner().getGuild().getHash();
baneRecord.baneNationHash = bane.getOwner().getGuild().getNation().getHash();
baneRecord.baneLiveTime = bane.getLiveDate();
baneRecord.baneDropTime = bane.getPlacementDate();
}
return baneRecord;
}
public static PreparedStatement buildBanePushStatement(Connection connection, ResultSet rs) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "INSERT INTO `warehouse_banehistory` (`event_number`, `city_id`, `city_name`, `char_id`, `offGuild_id`, `offNat_id`, `defGuild_id`, `defNat_id`, `dropDatetime`, `liveDateTime`, `resolution`) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
java.util.Date sqlDateTime;
outStatement = connection.prepareStatement(queryString);
// Bind record data
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("char_id"));
outStatement.setString(5, rs.getString("offGuild_id"));
outStatement.setString(6, rs.getString("offNat_id"));
outStatement.setString(7, rs.getString("defGuild_id"));
outStatement.setString(8, rs.getString("defNat_id"));
sqlDateTime = rs.getTimestamp("dropDatetime");
if (sqlDateTime == null)
outStatement.setNull(9, Types.DATE);
else
outStatement.setTimestamp(9, rs.getTimestamp("dropDatetime"));
sqlDateTime = rs.getTimestamp("dropDatetime");
if (sqlDateTime == null)
outStatement.setNull(10, Types.DATE);
else
outStatement.setTimestamp(10, rs.getTimestamp("liveDateTime"));
outStatement.setString(11, rs.getString("resolution"));
return outStatement;
}
public static PreparedStatement buildBaneQueryStatement(Connection connection) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "SELECT * FROM `warehouse_banehistory` WHERE `event_number` > ?";
outStatement = connection.prepareStatement(queryString);
outStatement.setInt(1, WarehousePushThread.baneIndex);
return outStatement;
}
public static DateTime getLastBaneDateTime(City city) {
DateTime outDateTime = null;
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = buildDateTimeQueryStatement(connection, city);
ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
outDateTime = new DateTime(rs.getTimestamp("endDatetime"));
}
} catch (SQLException e) {
Logger.error( e.toString());
}
return outDateTime;
}
private static PreparedStatement buildDateTimeQueryStatement (Connection connection, City city) throws SQLException {
PreparedStatement outStatement;
String queryString = "SELECT `endDatetime` FROM `warehouse_banehistory` WHERE `city_id` = ? ORDER BY `endDatetime` DESC LIMIT 1";
outStatement = connection.prepareStatement(queryString);
outStatement.setString(1, city.getHash());
return outStatement;
}
public static void updateLiveDate(Bane bane, DateTime dateTime) {
if (bane == null)
return;
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = buildUpdateLiveDateStatement(connection, bane, dateTime)) {
statement.execute();
} catch (SQLException e) {
Logger.error( e.toString());
}
}
private static PreparedStatement buildUpdateLiveDateStatement(Connection connection, Bane bane, DateTime dateTime) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "UPDATE `warehouse_banehistory` SET `liveDatetime` = ?, `dirty` = 1 WHERE `city_id` = ? AND `resolution` = 'PENDING'";
outStatement = connection.prepareStatement(queryString);
outStatement.setTimestamp(1, new java.sql.Timestamp(dateTime.getMillis()));
outStatement.setString(2, bane.getCity().getHash());
return outStatement;
}
private static PreparedStatement buildUpdateResolutionStatement(Connection connection, Bane bane, RecordEventType eventType) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "UPDATE `warehouse_banehistory` SET `endDatetime` = ?, `resolution` = ?, `dirty` = 1 WHERE `city_id` = ? AND `resolution` = 'PENDING'";
outStatement = connection.prepareStatement(queryString);
outStatement.setTimestamp(1, Timestamp.valueOf(LocalDateTime.now()));
outStatement.setString(2, eventType.name());
outStatement.setString(3, bane.getCity().getHash());
return outStatement;
}
public static void updateResolution(Bane bane, RecordEventType eventType) {
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = buildUpdateResolutionStatement(connection, bane, eventType)) {
statement.execute();
} catch (SQLException e) {
Logger.error(e.toString());
}
}
public static String getBaneHistoryString() {
String outString;
String queryString;
String dividerString;
String newLine = System.getProperty("line.separator");
outString = "[LUA_BANES() DATA WAREHOUSE]" + newLine;
dividerString = "--------------------------------" + newLine;
queryString = "CALL `baneHistory`()";
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = connection.prepareCall(queryString);
ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
outString += "Magicbane unresolved banes: " + rs.getInt("PENDING") + '/' + rs.getInt("TOTAL") + newLine;
outString += dividerString;
outString += "Bane Resolution History" + newLine;
outString += dividerString;
outString += "Destruction: " + rs.getInt("DESTROY") + newLine;
outString += "Capture: " + rs.getInt("CAPTURE") + newLine;
outString += "Defended: " + rs.getInt("DEFEND") + newLine;
}
} catch (SQLException e) {
e.printStackTrace();
}
return outString;
}
public static void updateDirtyRecords() {
String queryString = "SELECT * FROM `warehouse_banehistory` where `dirty` = 1";
// Reset character delta
WarehousePushThread.baneDelta = 0;
try (Connection localConnection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = localConnection.prepareStatement(queryString, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); // Make this an updatable result set as we'll reset the dirty flag as we go along
ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
// Only update the index and dirty flag
// if the remote database update succeeded
if (updateDirtyRecord(rs) == true)
WarehousePushThread.baneDelta++;
else
continue;
// Reset the dirty flag in the local database
rs.updateInt("dirty", 0);
rs.updateRow();
}
} catch (SQLException e) {
Logger.error( e.toString());
}
}
private static boolean updateDirtyRecord(ResultSet rs) {
try (Connection remoteConnection = DataWarehouse.remoteConnectionPool.getConnection();
PreparedStatement statement = buildUpdateDirtyStatement(remoteConnection, rs)) {
statement.execute();
return true;
} catch (SQLException e) {
Logger.error( e.toString());
return false;
}
}
private static PreparedStatement buildUpdateDirtyStatement(Connection connection, ResultSet rs) throws SQLException {
PreparedStatement outStatement;
String queryString = "UPDATE `warehouse_banehistory` SET `liveDateTime` = ?, `endDateTime` = ?, `resolution` = ? WHERE `event_number` = ?";
java.util.Date sqlDateTime;
outStatement = connection.prepareStatement(queryString);
// Bind record data
sqlDateTime = rs.getTimestamp("liveDateTime");
if (sqlDateTime == null)
outStatement.setNull(1, Types.DATE);
else
outStatement.setTimestamp(1, rs.getTimestamp("liveDateTime"));
sqlDateTime = rs.getTimestamp("endDateTime");
if (sqlDateTime == null)
outStatement.setNull(2, Types.DATE);
else
outStatement.setTimestamp(2, rs.getTimestamp("endDateTime"));
outStatement.setString(3, rs.getString("resolution"));
outStatement.setInt(4, rs.getInt("event_number"));
return outStatement;
}
void reset() {
this.cityHash = null;
this.cityGuildHash = null;
this.cityNationHash = null;
this.baneDropperHash = null;
this.baneGuildHash = null;
this.baneNationHash = null;
this.baneLiveTime = null;
}
public void release() {
this.reset();
recordPool.add(this);
}
public void write() {
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = buildBaneInsertStatement(connection)) {
statement.execute();
} catch (SQLException e) {
Logger.error( e.toString());
}
}
private PreparedStatement buildBaneInsertStatement(Connection connection) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "INSERT INTO `warehouse_banehistory` (`city_id`, `city_name`, `char_id`, `offGuild_id`, `offNat_id`, `defGuild_id`, `defNat_id`, `dropDatetime`, `liveDateTime`, `resolution`) VALUES(?,?,?,?,?,?,?,?,?,?)";
outStatement = connection.prepareStatement(queryString);
outStatement.setString(1, this.cityHash);
outStatement.setString(2, this.cityName);
outStatement.setString(3, this.baneDropperHash);
outStatement.setString(4, this.baneGuildHash);
outStatement.setString(5, this.baneNationHash);
outStatement.setString(6, this.cityGuildHash);
outStatement.setString(7, this.cityNationHash);
if (this.baneDropTime == null)
outStatement.setNull(8, java.sql.Types.DATE);
else
outStatement.setTimestamp(8, new java.sql.Timestamp(this.baneDropTime.getMillis()));
if (this.baneLiveTime == null)
outStatement.setNull(9, java.sql.Types.DATE);
else
outStatement.setTimestamp(9, new java.sql.Timestamp(this.baneLiveTime.getMillis()));
outStatement.setString(10, this.eventType.name());
return outStatement;
}
} // END CLASS
+284
View File
@@ -0,0 +1,284 @@
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.db.archive;
import engine.Enum;
import engine.objects.Guild;
import engine.objects.PlayerCharacter;
import engine.workthreads.WarehousePushThread;
import org.pmw.tinylog.Logger;
import java.sql.*;
import java.time.LocalDateTime;
import java.util.concurrent.LinkedBlockingQueue;
/*
* This class warehouses character creation events. It also tracks
* updates to summary kills/death data and their promotion class.
*/
public class CharacterRecord extends DataRecord {
// Local object pool for class
private static final LinkedBlockingQueue<CharacterRecord> recordPool = new LinkedBlockingQueue<>();
private PlayerCharacter player;
private CharacterRecord(PlayerCharacter player) {
this.recordType = Enum.DataRecordType.CHARACTER;
this.player = player;
}
public static CharacterRecord borrow(PlayerCharacter player) {
CharacterRecord characterRecord;
characterRecord = recordPool.poll();
if (characterRecord == null) {
characterRecord = new CharacterRecord(player);
}
else {
characterRecord.recordType = Enum.DataRecordType.CHARACTER;
characterRecord.player = player;
}
return characterRecord;
}
private static PreparedStatement buildCharacterInsertStatement(Connection connection, PlayerCharacter player) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "INSERT INTO `warehouse_characterhistory` (`char_id`, `char_fname`, `char_lname`, `baseClass`, `race`, `promoteClass`, `startingGuild`, `datetime`) VALUES(?,?,?,?,?,?,?,?)";
Guild charGuild;
outStatement = connection.prepareStatement(queryString);
charGuild = player.getGuild();
// Bind character data
outStatement.setString(1, DataWarehouse.hasher.encrypt(player.getObjectUUID()));
outStatement.setString(2, player.getFirstName());
outStatement.setString(3, player.getLastName());
outStatement.setInt(4, player.getBaseClassID());
outStatement.setInt(5, player.getRaceID());
outStatement.setInt(6, player.getPromotionClassID());
outStatement.setString(7, DataWarehouse.hasher.encrypt(charGuild.getObjectUUID()));
outStatement.setTimestamp(8, Timestamp.valueOf(LocalDateTime.now()));
return outStatement;
}
public static PreparedStatement buildCharacterPushStatement(Connection connection, ResultSet rs) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "INSERT INTO `warehouse_characterhistory` (`event_number`, `char_id`, `char_fname`, `char_lname`, `baseClass`, `race`, `promoteClass`, `startingGuild`, `datetime`) VALUES(?,?,?,?,?,?,?,?,?)";
outStatement = connection.prepareStatement(queryString);
// Bind record data
outStatement.setInt(1, rs.getInt("event_number"));
outStatement.setString(2, rs.getString("char_id"));
outStatement.setString(3, rs.getString("char_fname"));
outStatement.setString(4, rs.getString("char_lname"));
outStatement.setInt(5, rs.getInt("baseClass"));
outStatement.setInt(6, rs.getInt("race"));
outStatement.setInt(7, rs.getInt("promoteClass"));
outStatement.setString(8, rs.getString("startingGuild"));
outStatement.setTimestamp(9, rs.getTimestamp("datetime"));
return outStatement;
}
public static PreparedStatement buildCharacterQueryStatement(Connection connection) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "SELECT * FROM `warehouse_characterhistory` WHERE `event_number` > ?";
outStatement = connection.prepareStatement(queryString);
outStatement.setInt(1, WarehousePushThread.charIndex);
return outStatement;
}
public static void advanceKillCounter(PlayerCharacter player) {
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = buildKillCounterStatement(connection, player)) {
statement.execute();
} catch (SQLException e) {
Logger.error( e.toString());
}
}
private static PreparedStatement buildKillCounterStatement(Connection connection, PlayerCharacter player) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "UPDATE `warehouse_characterhistory` SET `kills` = `kills` +1, `dirty` = 1 WHERE `char_id` = ?";
if (player == null)
return outStatement;
outStatement = connection.prepareStatement(queryString);
outStatement.setString(1, player.getHash());
return outStatement;
}
public static void advanceDeathCounter(PlayerCharacter player) {
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = buildDeathCounterStatement(connection, player)) {
statement.execute();
} catch (SQLException e) {
Logger.error( e.toString());
}
}
private static PreparedStatement buildDeathCounterStatement(Connection connection, PlayerCharacter player) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "UPDATE `warehouse_characterhistory` SET `deaths` = `deaths` +1, `dirty` = 1 WHERE `char_id` = ?";
if (player == null)
return outStatement;
outStatement = connection.prepareStatement(queryString);
outStatement.setString(1, player.getHash());
return outStatement;
}
public static void updatePromotionClass(PlayerCharacter player) {
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = buildUpdatePromotionStatement(connection, player)) {
statement.execute();
} catch (SQLException e) {
Logger.error( e.toString());
}
}
private static PreparedStatement buildUpdatePromotionStatement(Connection connection, PlayerCharacter player) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "UPDATE `warehouse_characterhistory` SET `promoteClass` = ?, `dirty` = 1 WHERE `char_id` = ?";
if (player == null)
return outStatement;
outStatement = connection.prepareStatement(queryString, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
outStatement.setInt(1, player.getPromotionClassID());
outStatement.setString(2, player.getHash());
return outStatement;
}
public static void updateDirtyRecords() {
String queryString = "SELECT * FROM `warehouse_characterhistory` where `dirty` = 1";
// Reset character delta
WarehousePushThread.charDelta = 0;
try (Connection localConnection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = localConnection.prepareStatement(queryString, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); // Make this an updatable result set as we'll reset the dirty flag as we go along
ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
// Only update the index and dirty flag
// if the remote database update succeeded
if (updateDirtyRecord(rs) == true)
WarehousePushThread.charDelta++;
else
continue;
// Reset the dirty flag in the local database
rs.updateInt("dirty", 0);
rs.updateRow();
}
} catch (SQLException e) {
Logger.error(e.toString());
}
}
private static boolean updateDirtyRecord(ResultSet rs) {
try (Connection remoteConnection = DataWarehouse.remoteConnectionPool.getConnection();
PreparedStatement statement = buildUpdateDirtyStatement(remoteConnection, rs)) {
statement.execute();
return true;
} catch (SQLException e) {
Logger.error(e.toString());
return false;
}
}
private static PreparedStatement buildUpdateDirtyStatement(Connection connection, ResultSet rs) throws SQLException {
PreparedStatement outStatement;
String queryString = "UPDATE `warehouse_characterhistory` SET `promoteClass` = ?, `kills` = ?, `deaths` = ? WHERE `char_id` = ?";
outStatement = connection.prepareStatement(queryString);
// Bind record data
outStatement.setInt(1, rs.getInt("promoteClass"));
outStatement.setInt(2, rs.getInt("kills"));
outStatement.setInt(3, rs.getInt("deaths"));
outStatement.setString(4, rs.getString("char_id"));
return outStatement;
}
void reset() {
this.player = null;
}
public void release() {
this.reset();
recordPool.add(this);
}
public void write() {
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = buildCharacterInsertStatement(connection, this.player)) {
statement.execute();
} catch (SQLException e) {
Logger.error( "Error writing character record " + e.toString());
}
}
}
+161
View File
@@ -0,0 +1,161 @@
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.db.archive;
import engine.Enum;
import engine.objects.City;
import engine.workthreads.WarehousePushThread;
import java.sql.*;
import java.util.concurrent.LinkedBlockingQueue;
public class CityRecord extends DataRecord {
private static final LinkedBlockingQueue<CityRecord> recordPool = new LinkedBlockingQueue<>();
private Enum.RecordEventType eventType;
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;
private CityRecord(City city) {
this.recordType = Enum.DataRecordType.CITY;
this.city = city;
this.eventType = Enum.RecordEventType.CREATE;
}
public static CityRecord borrow(City city, Enum.RecordEventType eventType) {
CityRecord cityRecord;
cityRecord = recordPool.poll();
if (cityRecord == null) {
cityRecord = new CityRecord(city);
cityRecord.eventType = eventType;
}
else {
cityRecord.recordType = Enum.DataRecordType.CITY;
cityRecord.eventType = eventType;
cityRecord.city = city;
}
if (cityRecord.city.getHash() == null)
cityRecord.city.setHash(DataWarehouse.hasher.encrypt(cityRecord.city.getObjectUUID()));
cityRecord.cityHash = cityRecord.city.getHash();
cityRecord.cityName = cityRecord.city.getCityName();
cityRecord.cityMotto = cityRecord.city.getMotto();
cityRecord.cityGuildHash = cityRecord.city.getGuild().getHash();
cityRecord.locX = cityRecord.city.getTOL().getLoc().x;
cityRecord.locY = -cityRecord.city.getTOL().getLoc().z; // flip sign on 'y' coordinate
cityRecord.zoneHash = cityRecord.city.getParent().getHash();
if (cityRecord.eventType.equals(Enum.RecordEventType.CREATE))
cityRecord.establishedDatetime = cityRecord.city.established;
else
cityRecord.establishedDatetime = java.time.LocalDateTime.now();
return cityRecord;
}
public static PreparedStatement buildCityPushStatement(Connection connection, ResultSet rs) throws SQLException {
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);
// Bind record data
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"));
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"));
return outStatement;
}
public static PreparedStatement buildCityQueryStatement(Connection connection) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "SELECT * FROM `warehouse_cityhistory` WHERE `event_number` > ?";
outStatement = connection.prepareStatement(queryString);
outStatement.setInt(1, WarehousePushThread.cityIndex);
return outStatement;
}
void reset() {
this.city = null;
this.cityHash = null;
this.cityGuildHash = null;
this.cityMotto = null;
this.zoneHash = null;
this.establishedDatetime = null;
}
public void release() {
this.reset();
recordPool.add(this);
}
public void write() {
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = this.buildCityInsertStatement(connection)) {
statement.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
private PreparedStatement buildCityInsertStatement(Connection connection) throws SQLException {
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(?,?,?,?,?,?,?,?,?)";
outStatement = connection.prepareStatement(queryString);
// Bind character data
outStatement.setString(1, this.cityHash);
outStatement.setString(2, this.cityName);
outStatement.setString(3, this.cityMotto);
outStatement.setString(4, this.cityGuildHash);
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));
return outStatement;
}
}
+23
View File
@@ -0,0 +1,23 @@
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.db.archive;
import engine.Enum;
class DataRecord {
public Enum.DataRecordType recordType;
DataRecord() {
}
}
+324
View File
@@ -0,0 +1,324 @@
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.db.archive;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import engine.gameManager.ConfigManager;
import engine.util.Hasher;
import org.pmw.tinylog.Logger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.LinkedBlockingQueue;
import static engine.Enum.DataRecordType;
public class DataWarehouse implements Runnable {
public static final Hasher hasher = new Hasher("Cthulhu Owns Joo");
private static final LinkedBlockingQueue<DataRecord> recordQueue = new LinkedBlockingQueue<>();
public static HikariDataSource connectionPool = null;
public static HikariDataSource remoteConnectionPool = null;
public DataWarehouse() {
Logger.info("Configuring local Database Connection Pool...");
configureConnectionPool();
// If WarehousePush is disabled
// then early exit
if ( ConfigManager.MB_WORLD_WAREHOUSE_PUSH.getValue().equals("false")) {
Logger.info("Warehouse Remote Connection disabled along with push");
return;
}
Logger.info( "Configuring remote Database Connection Pool...");
configureRemoteConnectionPool();
}
public static void bootStrap() {
Thread warehousingThread;
warehousingThread = new Thread(new DataWarehouse());
warehousingThread.setName("DataWarehouse");
warehousingThread.setPriority(Thread.NORM_PRIORITY - 1);
warehousingThread.start();
}
public static void pushToWarehouse(DataRecord dataRecord) {
DataWarehouse.recordQueue.add(dataRecord);
}
public static void writeHash(DataRecordType recordType, int uuid) {
// Member variable declaration
Connection connection = null;
PreparedStatement statement = null;
String queryString;
String hashString;
try {
connection = DataWarehouse.connectionPool.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
if (connection == null) {
Logger.error("Null connection when writing zone hash.");
return;
}
// Build query string
switch (recordType) {
case CHARACTER:
queryString = "UPDATE `obj_character` SET hash = ? WHERE `UID` = ?";
break;
case GUILD:
queryString = "UPDATE `obj_guild` SET hash = ? WHERE `UID` = ?";
break;
case ZONE:
queryString = "UPDATE `obj_zone` SET hash = ? WHERE `UID` = ?";
break;
case CITY:
queryString = "UPDATE `obj_city` SET hash = ? WHERE `UID` = ?";
break;
case REALM:
queryString = "UPDATE `obj_realm` SET hash = ? WHERE `realmID` = ?";
break;
default:
queryString = null;
break;
}
hashString = hasher.encrypt(uuid);
// Write this record to the warehouse
try {
statement = connection.prepareStatement(queryString);
statement.setString(1, hashString);
statement.setLong(2, uuid);
statement.execute();
} catch (SQLException e) {
Logger.error("Error writing hash for uuid" + uuid + " of type " + recordType.name() + ' ' + e.toString());
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public static boolean recordExists(DataRecordType recordType, int uuid) {
// Member variable declaration
Connection connection = null;
PreparedStatement statement = null;
String queryString;
ResultSet resultSet;
try {
connection = DataWarehouse.connectionPool.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
if (connection == null) {
Logger.error("Null connection during char record lookup");
return true; // False positive here, so as not to try and write the record twice.
// will refactor out once we write hashes to object tables
}
// Build query string
switch (recordType) {
case CHARACTER:
queryString = "SELECT COUNT(*) from warehouse_characterhistory where char_id = ?";
break;
case GUILD:
queryString = "SELECT COUNT(*) from warehouse_guildhistory where guild_id = ?";
break;
case CITY:
queryString = "SELECT COUNT(*) from warehouse_cityhistory where city_id = ?";
break;
case REALM:
queryString = "SELECT COUNT(*) from warehouse_realmhistory where realm_id = ?";
break;
case BANE:
queryString = "SELECT COUNT(*) from warehouse_banehistory where city_id = ? AND `resolution` = 'PENDING'";
break;
case ZONE: // Does not really exist but enum acts as a proxy for hash lookup
case MINE: // Does not really exist but enum acts as a proxy for hash lookup
default:
queryString = null;
break;
}
try {
statement = connection.prepareStatement(queryString);
statement.setString(1, DataWarehouse.hasher.encrypt(uuid));
resultSet = statement.executeQuery();
while (resultSet.next()) {
return resultSet.getInt("COUNT(*)") > 0;
}
} catch (SQLException e) {
Logger.error("Error in record lookup for " + recordType.name() + " of uuid:" + uuid + e.toString());
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return false;
}
public void run() {
// Working variable set
DataRecord dataRecord;
PvpRecord pvpRecord;
GuildRecord guildRecord;
CharacterRecord characterRecord;
CityRecord cityRecord;
BaneRecord baneRecord;
RealmRecord realmRecord;
MineRecord mineRecord;
Logger.info( "DataWarehouse is running.");
while (true) {
dataRecord = null;
pvpRecord = null;
guildRecord = null;
characterRecord = null;
cityRecord = null;
baneRecord = null;
realmRecord = null;
mineRecord = null;
try {
dataRecord = recordQueue.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
// Write record to appropriate warehousing table
if (dataRecord != null) {
switch (dataRecord.recordType) {
case PVP:
pvpRecord = (PvpRecord) dataRecord;
pvpRecord.write();
pvpRecord.release();
break;
case CHARACTER:
characterRecord = (CharacterRecord) dataRecord;
characterRecord.write();
characterRecord.release();
break;
case GUILD:
guildRecord = (GuildRecord) dataRecord;
guildRecord.write();
guildRecord.release();
break;
case CITY:
cityRecord = (CityRecord) dataRecord;
cityRecord.write();
cityRecord.release();
break;
case BANE:
baneRecord = (BaneRecord) dataRecord;
baneRecord.write();
baneRecord.release();
break;
case REALM:
realmRecord = (RealmRecord) dataRecord;
realmRecord.write();
realmRecord.release();
break;
case MINE:
mineRecord = (MineRecord) dataRecord;
mineRecord.write();
mineRecord.release();
break;
default:
Logger.error( "Unhandled record type");
break;
} // end switch
}
}
}
private static void configureConnectionPool() {
HikariConfig config = new HikariConfig();
config.setMaximumPoolSize(10);
config.setJdbcUrl("jdbc:mysql://" + ConfigManager.MB_DATABASE_ADDRESS.getValue() +
":" + ConfigManager.MB_DATABASE_PORT.getValue() + "/" +
ConfigManager.MB_DATABASE_NAME.getValue());
config.setUsername(ConfigManager.MB_DATABASE_USER.getValue());
config.setPassword( ConfigManager.MB_DATABASE_PASS.getValue());
config.addDataSourceProperty("characterEncoding", "utf8");
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
connectionPool = new HikariDataSource(config); // setup the connection pool
Logger.info("Local warehouse database connection configured");
}
private static void configureRemoteConnectionPool() {
HikariConfig config = new HikariConfig();
config.setMaximumPoolSize(1); // Only the server talks to remote, so yeah.
config.setJdbcUrl(ConfigManager.MB_WAREHOUSE_ADDR.getValue());
config.setUsername(ConfigManager.MB_WAREHOUSE_USER.getValue());
config.setPassword(ConfigManager.MB_WAREHOUSE_PASS.getValue());
config.addDataSourceProperty("characterEncoding", "utf8");
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
remoteConnectionPool = new HikariDataSource(config); // setup the connection pool
Logger.info("remote warehouse connection configured");
}
}
+215
View File
@@ -0,0 +1,215 @@
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.db.archive;
import engine.Enum;
import engine.Enum.RecordEventType;
import engine.objects.Guild;
import engine.workthreads.WarehousePushThread;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.concurrent.LinkedBlockingQueue;
public class GuildRecord extends DataRecord {
private static final LinkedBlockingQueue<GuildRecord> recordPool = new LinkedBlockingQueue<>();
private Enum.RecordEventType eventType;
private Guild guild;
public String guildHash;
private String guildName;
private String charterName;
private String GLHash;
private String guildMotto;
private int bgIcon;
private int bgColour1;
private int bgColour2;
private int fgIcon;
private int fgColour;
public int guildID;
private java.time.LocalDateTime eventDatetime;
public static HashMap<Integer, GuildRecord> GuildRecordCache = null;
private GuildRecord(Guild guild) {
this.recordType = Enum.DataRecordType.GUILD;
this.guild = guild;
this.eventType = Enum.RecordEventType.CREATE;
}
public GuildRecord(ResultSet rs) throws SQLException {
super();
this.eventType = RecordEventType.valueOf(rs.getString("eventType"));
this.guildHash = rs.getString("guild_id");
this.guildName = rs.getString("guild_name");
this.charterName = rs.getString("charter");
GLHash = rs.getString("guild_founder");
this.guildMotto = rs.getString("guild_motto");
this.bgIcon = rs.getInt("bgicon");
this.bgColour1 = rs.getInt("bgcoloura");
this.bgColour2 = rs.getInt("bgcolourb");
this.fgIcon = rs.getInt("fgicon");
this.fgColour = rs.getInt("fgcolour");
java.sql.Timestamp eventTimeStamp = rs.getTimestamp("upgradeDate");
if (eventTimeStamp != null)
this.eventDatetime = LocalDateTime.ofInstant(eventTimeStamp.toInstant(), ZoneId.systemDefault());
}
public static GuildRecord borrow(Guild guild, Enum.RecordEventType eventType) {
GuildRecord guildRecord;
//add
guildRecord = recordPool.poll();
if (guildRecord == null) {
guildRecord = new GuildRecord(guild);
guildRecord.eventType = eventType;
}
else {
guildRecord.guild = guild;
guildRecord.recordType = Enum.DataRecordType.GUILD;
guildRecord.eventType = eventType;
}
guildRecord.guildHash = guildRecord.guild.getHash();
guildRecord.guildID = guildRecord.guild.getObjectUUID();
guildRecord.guildName = guildRecord.guild.getName();
guildRecord.charterName = Enum.GuildType.getGuildTypeFromInt(guildRecord.guild.getCharter()).getCharterName();
guildRecord.GLHash = DataWarehouse.hasher.encrypt(guildRecord.guild.getGuildLeaderUUID());
guildRecord.guildMotto = guildRecord.guild.getMotto();
guildRecord.bgIcon = guildRecord.guild.getBgDesign();
guildRecord.bgColour1 = guildRecord.guild.getBgc1();
guildRecord.bgColour2 = guildRecord.guild.getBgc2();
guildRecord.fgIcon = guildRecord.guild.getSymbol();
guildRecord.fgColour = guildRecord.guild.getSc();
if (guild.getOwnedCity() != null)
guildRecord.eventDatetime = guild.getOwnedCity().established;
else
guildRecord.eventDatetime = LocalDateTime.now();
return guildRecord;
}
public static PreparedStatement buildGuildPushStatement(Connection connection, ResultSet rs) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "INSERT INTO `warehouse_guildhistory` (`event_number`, `guild_id`, `guild_name`, `guild_motto`, `guild_founder`, `charter`, `bgicon`, `bgcoloura`, `bgcolourb`, `fgicon`, `fgcolour`, `eventtype`, `datetime`) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
outStatement = connection.prepareStatement(queryString);
// Bind record data
outStatement.setInt(1, rs.getInt("event_number"));
outStatement.setString(2, rs.getString("guild_id"));
outStatement.setString(3, rs.getString("guild_name"));
outStatement.setString(4, rs.getString("guild_motto"));
outStatement.setString(5, rs.getString("guild_founder"));
outStatement.setString(6, rs.getString("charter"));
outStatement.setInt(7, rs.getInt("bgicon"));
outStatement.setInt(8, rs.getInt("bgcoloura"));
outStatement.setInt(9, rs.getInt("bgcolourb"));
outStatement.setInt(10, rs.getInt("fgicon"));
outStatement.setInt(11, rs.getInt("fgcolour"));
outStatement.setString(12, rs.getString("eventtype"));
outStatement.setTimestamp(13, rs.getTimestamp("datetime"));
return outStatement;
}
public static PreparedStatement buildGuildQueryStatement(Connection connection) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "SELECT * FROM `warehouse_guildhistory` WHERE `event_number` > ?";
outStatement = connection.prepareStatement(queryString);
outStatement.setInt(1, WarehousePushThread.guildIndex);
return outStatement;
}
void reset() {
this.guild = null;
this.guildHash = null;
this.GLHash = null;
this.guildMotto = null;
this.charterName = null;
this.eventDatetime = null;
}
public void release() {
this.reset();
recordPool.add(this);
}
public void write() {
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = this.buildGuildInsertStatement(connection)) {
statement.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
private PreparedStatement buildGuildInsertStatement(Connection connection) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "INSERT INTO `warehouse_guildhistory` (`guild_id`, `guild_name`, `guild_motto`, `guild_founder`, `charter`, `bgicon`, `bgcoloura`, `bgcolourb`, `fgicon`, `fgcolour`, `eventtype`, `datetime`) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
outStatement = connection.prepareStatement(queryString);
// Bind character data
outStatement.setString(1, this.guildHash);
outStatement.setString(2, this.guildName);
outStatement.setString(3, this.guildMotto);
outStatement.setString(4, this.GLHash);
outStatement.setString(5, this.charterName);
outStatement.setInt(6, this.bgIcon);
outStatement.setInt(7, this.bgColour1);
outStatement.setInt(8, this.bgColour2);
outStatement.setInt(9, this.fgIcon);
outStatement.setInt(10, this.fgColour);
outStatement.setString(11, this.eventType.name());
outStatement.setTimestamp(12, new java.sql.Timestamp( this.eventDatetime.atZone(ZoneId.systemDefault())
.toInstant().toEpochMilli()));
return outStatement;
}
// public static void InitializeGuildRecords(){
// GuildRecord.GuildRecordCache = DbManager.GuildQueries.GET_WAREHOUSE_GUILD_HISTORY();
// }
}
+166
View File
@@ -0,0 +1,166 @@
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.db.archive;
import engine.Enum;
import engine.objects.AbstractCharacter;
import engine.objects.Mine;
import engine.objects.PlayerCharacter;
import engine.workthreads.WarehousePushThread;
import java.sql.*;
import java.time.LocalDateTime;
import java.util.concurrent.LinkedBlockingQueue;
public class MineRecord extends DataRecord {
private static final LinkedBlockingQueue<MineRecord> recordPool = new LinkedBlockingQueue<>();
private Enum.RecordEventType eventType;
private String zoneHash;
private String charHash;
private String mineGuildHash;
private String mineNationHash;
private String mineType;
private float locX;
private float locY;
private MineRecord() {
this.recordType = Enum.DataRecordType.MINE;
this.eventType = Enum.RecordEventType.CAPTURE;
}
public static MineRecord borrow(Mine mine, AbstractCharacter character, Enum.RecordEventType eventType) {
MineRecord mineRecord;
mineRecord = recordPool.poll();
PlayerCharacter player;
if (mineRecord == null) {
mineRecord = new MineRecord();
mineRecord.eventType = eventType;
}
else {
mineRecord.recordType = Enum.DataRecordType.MINE;
mineRecord.eventType = eventType;
}
mineRecord.zoneHash = mine.getParentZone().getHash();
if (character.getObjectType().equals(Enum.GameObjectType.PlayerCharacter)) {
player = (PlayerCharacter) character;
mineRecord.charHash = player.getHash();
}
else
mineRecord.charHash = character.getName();
DataWarehouse.hasher.encrypt(0);
if (mine.getOwningGuild() == null)
mineRecord.mineGuildHash = "ERRANT";
else
mineRecord.mineGuildHash = mine.getOwningGuild().getHash();
if (mine.getOwningGuild() == null)
mineRecord.mineNationHash = "ERRANT";
else
mineRecord.mineNationHash = mine.getOwningGuild().getNation().getHash();
mineRecord.locX = mine.getParentZone().getLoc().x;
mineRecord.locY = -mine.getParentZone().getLoc().z;
mineRecord.mineType = mine.getMineType().name;
return mineRecord;
}
public static PreparedStatement buildMinePushStatement(Connection connection, ResultSet rs) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "INSERT INTO `warehouse_minehistory` (`event_number`, `zone_id`, `mine_type`, `char_id`, `mine_guildID`, `mine_nationID`, `loc_x`, `loc_y`, `eventType`, `datetime`) VALUES(?,?,?,?,?,?,?,?,?,?)";
outStatement = connection.prepareStatement(queryString);
// Bind record data
outStatement.setInt(1, rs.getInt("event_number"));
outStatement.setString(2, rs.getString("zone_id"));
outStatement.setString(3, rs.getString("char_id"));
outStatement.setString(4, rs.getString("mine_type"));
outStatement.setString(5, rs.getString("mine_guildID"));
outStatement.setString(6, rs.getString("mine_nationID"));
outStatement.setFloat(7, rs.getFloat("loc_x"));
outStatement.setFloat(8, rs.getFloat("loc_y"));
outStatement.setString(9, rs.getString("eventType"));
outStatement.setTimestamp(10, rs.getTimestamp("datetime"));
return outStatement;
}
public static PreparedStatement buildMineQueryStatement(Connection connection) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "SELECT * FROM `warehouse_minehistory` WHERE `event_number` > ?";
outStatement = connection.prepareStatement(queryString);
outStatement.setInt(1, WarehousePushThread.mineIndex);
return outStatement;
}
void reset() {
this.zoneHash = null;
this.charHash = null;
this.mineGuildHash = null;
this.mineNationHash = null;
this.mineType = null;
this.locX = 0.0f;
this.locY = 0.0f;
}
public void release() {
this.reset();
recordPool.add(this);
}
public void write() {
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = this.buildMineInsertStatement(connection)) {
statement.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
private PreparedStatement buildMineInsertStatement(Connection connection) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "INSERT INTO `warehouse_minehistory` (`zone_id`, `mine_type`, `char_id`, `mine_guildID`, `mine_nationID`, `loc_x`, `loc_y`, `eventType`, `datetime`) VALUES(?,?,?,?,?,?,?,?,?)";
outStatement = connection.prepareStatement(queryString);
// Bind character data
outStatement.setString(1, this.zoneHash);
outStatement.setString(2, this.mineType);
outStatement.setString(3, this.charHash);
outStatement.setString(4, this.mineGuildHash);
outStatement.setString(5, this.mineNationHash);
outStatement.setFloat(6, this.locX);
outStatement.setFloat(7, this.locY);
outStatement.setString(8, this.eventType.name());
outStatement.setTimestamp(9, Timestamp.valueOf(LocalDateTime.now()));
return outStatement;
}
}
+312
View File
@@ -0,0 +1,312 @@
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.db.archive;
import engine.gameManager.ZoneManager;
import engine.math.Vector3fImmutable;
import engine.objects.Guild;
import engine.objects.PlayerCharacter;
import engine.objects.Zone;
import engine.workthreads.WarehousePushThread;
import org.pmw.tinylog.Logger;
import java.sql.*;
import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.concurrent.LinkedBlockingQueue;
import static engine.Enum.DataRecordType;
import static engine.Enum.PvpHistoryType;
public class PvpRecord extends DataRecord {
private static final LinkedBlockingQueue<PvpRecord> recordPool = new LinkedBlockingQueue<>();
private PlayerCharacter player;
private PlayerCharacter victim;
private Vector3fImmutable location;
private boolean pvpExp;
private PvpRecord(PlayerCharacter player, PlayerCharacter victim, Vector3fImmutable location, boolean pvpExp) {
this.recordType = DataRecordType.PVP;
this.player = player;
this.victim = victim;
this.location = new Vector3fImmutable(location);
this.pvpExp = pvpExp;
}
public static PvpRecord borrow(PlayerCharacter player, PlayerCharacter victim, Vector3fImmutable location, boolean pvpExp) {
PvpRecord pvpRecord;
pvpRecord = recordPool.poll();
if (pvpRecord == null) {
pvpRecord = new PvpRecord(player, victim, location, pvpExp);
}
else {
pvpRecord.recordType = DataRecordType.PVP;
pvpRecord.player = player;
pvpRecord.victim = victim;
pvpRecord.location = new Vector3fImmutable(location);
pvpRecord.pvpExp = pvpExp;
}
return pvpRecord;
}
private static PreparedStatement buildHistoryStatement(Connection connection, int charUUID, PvpHistoryType historyType) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "";
switch (historyType) {
case KILLS:
queryString = "SELECT DISTINCT `victim_id`, `datetime` FROM warehouse_pvphistory where char_id = ? " +
"ORDER BY `datetime` DESC LIMIT 10";
break;
case DEATHS:
queryString = "SELECT DISTINCT `char_id`,`datetime` FROM warehouse_pvphistory where `victim_id` = ? " +
"ORDER BY `datetime` DESC LIMIT 10";
break;
}
outStatement = connection.prepareStatement(queryString);
outStatement.setString(1, DataWarehouse.hasher.encrypt(charUUID));
return outStatement;
}
public static LinkedList<Integer> getCharacterPvPHistory(int charUUID, PvpHistoryType historyType) {
// Member variable declaration
LinkedList<Integer> outList = new LinkedList<>();
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = buildHistoryStatement(connection, charUUID, historyType);
ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
switch (historyType) {
case KILLS:
outList.add((int) DataWarehouse.hasher.decrypt(rs.getString("victim_id"))[0]);
break;
case DEATHS:
outList.add((int) DataWarehouse.hasher.decrypt(rs.getString("char_id"))[0]);
break;
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return outList;
}
private static PreparedStatement buildLuaHistoryQueryStatement(Connection connection, int charUUID) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "CALL `pvpHistory`(?)";
outStatement = connection.prepareStatement(queryString);
outStatement.setString(1, DataWarehouse.hasher.encrypt(charUUID));
return outStatement;
}
public static String getPvpHistoryString(int charUUID) {
String outString;
String dividerString;
String newLine = System.getProperty("line.separator");
outString = "[LUA_PVP() DATA WAREHOUSE]" + newLine;
dividerString = "--------------------------------" + newLine;
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = buildLuaHistoryQueryStatement(connection, charUUID);
ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
int killCount;
int deathCount;
float killRatio;
outString += "Total Magicbane murdered souls: " + rs.getInt("TOTALDEATHS") + newLine;
outString += dividerString;
outString += String.format("%-8s %-8s %-8s %-8s %n", "Period", "Kills", "Deaths", "K/D");
outString += dividerString;
killCount = rs.getInt("KILLCOUNT");
deathCount = rs.getInt("DEATHCOUNT");
if (deathCount == 0)
killRatio = (float) killCount;
else
killRatio = (float) killCount / deathCount;
try {
outString += String.format("%-8s %-8d %-8d %.2f %n", "Total", killCount, deathCount, killRatio);
killCount = rs.getInt("DAILYKILLS");
deathCount = rs.getInt("DAILYDEATHS");
if (deathCount == 0)
killRatio = (float) killCount;
else
killRatio = (float) killCount / deathCount;
outString += String.format("%-8s %-8d %-8d %.2f %n", "24hrs", killCount, deathCount, killRatio);
killCount = rs.getInt("HOURLYKILLS");
deathCount = rs.getInt("HOURLYDEATHS");
if (deathCount == 0)
killRatio = (float) killCount;
else
killRatio = (float) killCount / deathCount;
outString += String.format("%-8s %-8d %-8d %.2f %n", "1hr", killCount, deathCount, killRatio);
} catch (Exception e) {
Logger.error(e.toString());
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return outString;
}
public static PreparedStatement buildPvpPushStatement(Connection connection, ResultSet rs) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "INSERT INTO `warehouse_pvphistory` (`event_number`, `char_id`, `char_guild_id`, `char_nation_id`, `char_level`," +
" `victim_id`, `victim_guild_id`, `victim_nation_id`, `victim_level`," +
" `zone_id`, `zone_name`, `loc_x`, `loc_y`, `gave_exp`, `datetime`) " +
" VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
outStatement = connection.prepareStatement(queryString);
// Bind record data
outStatement.setInt(1, rs.getInt("event_number"));
outStatement.setString(2, rs.getString("char_id"));
outStatement.setString(3, rs.getString("char_guild_id"));
outStatement.setString(4, rs.getString("char_nation_id"));
outStatement.setInt(5, rs.getInt("char_level"));
// Bind victim data
outStatement.setString(6, rs.getString("victim_id"));
outStatement.setString(7, rs.getString("victim_guild_id"));
outStatement.setString(8, rs.getString("victim_nation_id"));
outStatement.setInt(9, rs.getInt("victim_level"));
outStatement.setString(10, rs.getString("zone_id"));
outStatement.setString(11, rs.getString("zone_name"));
outStatement.setFloat(12, rs.getFloat("loc_x"));
outStatement.setFloat(13, rs.getFloat("loc_y"));
outStatement.setBoolean(14, rs.getBoolean("gave_exp"));
outStatement.setTimestamp(15, rs.getTimestamp("datetime"));
return outStatement;
}
public static PreparedStatement buildPvpQueryStatement(Connection connection) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "SELECT * FROM `warehouse_pvphistory` WHERE `event_number` > ?";
outStatement = connection.prepareStatement(queryString);
outStatement.setInt(1, WarehousePushThread.pvpIndex);
return outStatement;
}
void reset() {
this.player = null;
this.victim = null;
this.location = Vector3fImmutable.ZERO;
pvpExp = false;
}
public void release() {
this.reset();
recordPool.add(this);
}
private PreparedStatement buildPvPInsertStatement(Connection connection) throws SQLException {
Guild charGuild;
Guild victimGuild;
Zone zone;
PreparedStatement outStatement = null;
String queryString = "INSERT INTO `warehouse_pvphistory` (`char_id`, `char_guild_id`, `char_nation_id`, `char_level`," +
" `victim_id`, `victim_guild_id`, `victim_nation_id`, `victim_level`," +
" `zone_id`, `zone_name`, `loc_x`, `loc_y`, `gave_exp`, `datetime`) " +
" VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
outStatement = connection.prepareStatement(queryString);
charGuild = this.player.getGuild();
victimGuild = this.victim.getGuild();
// Use a proxy in the situation where a char guild is null (errant)
// Retrieve the zone name where the PvP event occurred
zone = ZoneManager.findSmallestZone(this.location);
outStatement.setString(1, DataWarehouse.hasher.encrypt(this.player.getObjectUUID()));
outStatement.setString(2, DataWarehouse.hasher.encrypt(charGuild.getObjectUUID()));
outStatement.setString(3, DataWarehouse.hasher.encrypt(charGuild.getNation().getObjectUUID()));
outStatement.setInt(4, this.player.getLevel());
// Bind victim data
outStatement.setString(5, DataWarehouse.hasher.encrypt(this.victim.getObjectUUID()));
outStatement.setString(6, DataWarehouse.hasher.encrypt(victimGuild.getObjectUUID()));
outStatement.setString(7, DataWarehouse.hasher.encrypt(victimGuild.getNation().getObjectUUID()));
outStatement.setInt(8, this.victim.getLevel());
outStatement.setString(9, DataWarehouse.hasher.encrypt(zone.getObjectUUID()));
outStatement.setString(10, zone.getName());
outStatement.setFloat(11, this.location.getX());
outStatement.setFloat(12, -this.location.getZ()); // flip sign on 'y' coordinate
outStatement.setBoolean(13, this.pvpExp);
outStatement.setTimestamp(14, Timestamp.valueOf(LocalDateTime.now()));
return outStatement;
}
public void write() {
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = buildPvPInsertStatement(connection)) {
statement.execute();
} catch (SQLException e) {
Logger.error( e.toString());
}
// Warehouse record for this pvp event written if code path reaches here.
// Time to update the respective kill counters.
CharacterRecord.advanceKillCounter(this.player);
CharacterRecord.advanceDeathCounter(this.victim);
}
}
+142
View File
@@ -0,0 +1,142 @@
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.db.archive;
import engine.Enum;
import engine.objects.Realm;
import engine.workthreads.WarehousePushThread;
import java.sql.*;
import java.time.LocalDateTime;
import java.util.concurrent.LinkedBlockingQueue;
public class RealmRecord extends DataRecord {
private static final LinkedBlockingQueue<RealmRecord> recordPool = new LinkedBlockingQueue<>();
private Realm realm;
private Enum.RecordEventType eventType;
private String cityHash;
private String guildHash;
private String charterType;
private LocalDateTime eventDateTime;
private RealmRecord(Realm realm) {
this.recordType = Enum.DataRecordType.REALM;
this.realm = realm;
this.eventType = Enum.RecordEventType.CAPTURE;
}
public static RealmRecord borrow(Realm realm, Enum.RecordEventType eventType) {
RealmRecord realmRecord;
realmRecord = recordPool.poll();
if (realmRecord == null) {
realmRecord = new RealmRecord(realm);
realmRecord.eventType = eventType;
}
else {
realmRecord.recordType = Enum.DataRecordType.REALM;
realmRecord.eventType = eventType;
realmRecord.realm = realm;
}
realmRecord.cityHash = realm.getRulingCity().getHash();
realmRecord.guildHash = realm.getRulingCity().getGuild().getHash();
realmRecord.charterType = Enum.CharterType.getCharterTypeByID(realmRecord.realm.getCharterType()).name();
if (realmRecord.eventType.equals(Enum.RecordEventType.CAPTURE))
realmRecord.eventDateTime = realm.ruledSince;
else
realmRecord.eventDateTime = LocalDateTime.now();
return realmRecord;
}
public static PreparedStatement buildRealmPushStatement(Connection connection, ResultSet rs) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "INSERT INTO `warehouse_realmhistory` (`event_number`, `realm_id`, `realm_name`, `charter`, `city_id`, `guild_id`, `eventType`, `datetime`) VALUES(?,?,?,?,?,?,?,?)";
outStatement = connection.prepareStatement(queryString);
// Bind record data
outStatement.setInt(1, rs.getInt("event_number"));
outStatement.setString(2, rs.getString("realm_id"));
outStatement.setString(3, rs.getString("realm_name"));
outStatement.setString(4, rs.getString("charter"));
outStatement.setString(5, rs.getString("city_id"));
outStatement.setString(6, rs.getString("guild_id"));
outStatement.setString(7, rs.getString("eventType"));
outStatement.setTimestamp(8, rs.getTimestamp("datetime"));
return outStatement;
}
public static PreparedStatement buildRealmQueryStatement(Connection connection) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "SELECT * FROM `warehouse_realmhistory` WHERE `event_number` > ?";
outStatement = connection.prepareStatement(queryString);
outStatement.setInt(1, WarehousePushThread.realmIndex);
return outStatement;
}
void reset() {
this.realm = null;
this.cityHash = null;
this.guildHash = null;
this.eventDateTime = null;
this.charterType = null;
}
public void release() {
this.reset();
recordPool.add(this);
}
private PreparedStatement buildRealmInsertStatement(Connection connection) throws SQLException {
PreparedStatement outStatement = null;
String queryString = "INSERT INTO `warehouse_realmhistory` (`realm_id`, `realm_name`, `charter`, `city_id`, `guild_id`, `eventType`, `datetime`) VALUES(?,?,?,?,?,?,?)";
outStatement = connection.prepareStatement(queryString);
// Bind Record Data
outStatement.setString(1, realm.getHash());
outStatement.setString(2, realm.getRealmName());
outStatement.setString(3, charterType);
outStatement.setString(4, cityHash);
outStatement.setString(5, guildHash);
outStatement.setString(6, eventType.name());
outStatement.setTimestamp(7, Timestamp.valueOf(this.eventDateTime));
return outStatement;
}
public void write() {
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = this.buildRealmInsertStatement(connection)) {
statement.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
}