Files
BattleBane/src/engine/db/archive/PvpRecord.java
T

313 lines
12 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;
2022-04-30 09:41:17 -04:00
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 {
2023-07-15 09:23:48 -04:00
private static final LinkedBlockingQueue<PvpRecord> recordPool = new LinkedBlockingQueue<>();
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
private PlayerCharacter player;
private PlayerCharacter victim;
private Vector3fImmutable location;
private boolean pvpExp;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
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;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public static PvpRecord borrow(PlayerCharacter player, PlayerCharacter victim, Vector3fImmutable location, boolean pvpExp) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
PvpRecord pvpRecord;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
pvpRecord = recordPool.poll();
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
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;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
return pvpRecord;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
private static PreparedStatement buildHistoryStatement(Connection connection, int charUUID, PvpHistoryType historyType) throws SQLException {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
PreparedStatement outStatement = null;
String queryString = "";
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
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;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
outStatement = connection.prepareStatement(queryString);
outStatement.setString(1, DataWarehouse.hasher.encrypt(charUUID));
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 LinkedList<Integer> getCharacterPvPHistory(int charUUID, PvpHistoryType historyType) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Member variable declaration
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
LinkedList<Integer> outList = new LinkedList<>();
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement statement = buildHistoryStatement(connection, charUUID, historyType);
ResultSet rs = statement.executeQuery()) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
while (rs.next()) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
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;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
private static PreparedStatement buildLuaHistoryQueryStatement(Connection connection, int charUUID) throws SQLException {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
PreparedStatement outStatement = null;
String queryString = "CALL `pvpHistory`(?)";
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
outStatement = connection.prepareStatement(queryString);
outStatement.setString(1, DataWarehouse.hasher.encrypt(charUUID));
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 String getPvpHistoryString(int charUUID) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
String outString;
String dividerString;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
String newLine = System.getProperty("line.separator");
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
outString = "[LUA_PVP() DATA WAREHOUSE]" + newLine;
dividerString = "--------------------------------" + newLine;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
try (Connection connection = DbManager.getConnection();
PreparedStatement statement = buildLuaHistoryQueryStatement(connection, charUUID);
ResultSet rs = statement.executeQuery()) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
while (rs.next()) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
int killCount;
int deathCount;
float killRatio;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
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;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
killCount = rs.getInt("KILLCOUNT");
deathCount = rs.getInt("DEATHCOUNT");
if (deathCount == 0)
killRatio = (float) killCount;
else
killRatio = (float) killCount / deathCount;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
try {
outString += String.format("%-8s %-8d %-8d %.2f %n", "Total", killCount, deathCount, killRatio);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
killCount = rs.getInt("DAILYKILLS");
deathCount = rs.getInt("DAILYDEATHS");
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (deathCount == 0)
killRatio = (float) killCount;
else
killRatio = (float) killCount / deathCount;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
outString += String.format("%-8s %-8d %-8d %.2f %n", "24hrs", killCount, deathCount, killRatio);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
killCount = rs.getInt("HOURLYKILLS");
deathCount = rs.getInt("HOURLYDEATHS");
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (deathCount == 0)
killRatio = (float) killCount;
else
killRatio = (float) killCount / deathCount;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
outString += String.format("%-8s %-8d %-8d %.2f %n", "1hr", killCount, deathCount, killRatio);
} catch (Exception e) {
Logger.error(e.toString());
}
}
} catch (SQLException e) {
e.printStackTrace();
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
return outString;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public static PreparedStatement buildPvpPushStatement(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_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(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
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 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("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"));
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Bind victim data
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
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"));
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
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"));
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 buildPvpQueryStatement(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_pvphistory` WHERE `event_number` > ?";
outStatement = connection.prepareStatement(queryString);
outStatement.setInt(1, WarehousePushThread.pvpIndex);
return outStatement;
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
void reset() {
this.player = null;
this.victim = null;
this.location = Vector3fImmutable.ZERO;
pvpExp = false;
}
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
private PreparedStatement buildPvPInsertStatement(Connection connection) throws SQLException {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
Guild charGuild;
Guild victimGuild;
Zone zone;
PreparedStatement outStatement = null;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
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(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
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
charGuild = this.player.getGuild();
victimGuild = this.victim.getGuild();
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Use a proxy in the situation where a char guild is null (errant)
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Retrieve the zone name where the PvP event occurred
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
zone = ZoneManager.findSmallestZone(this.location);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
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());
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Bind victim data
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
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());
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
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()));
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 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 = buildPvPInsertStatement(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) {
Logger.error(e.toString());
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
// Warehouse record for this pvp event written if code path reaches here.
// Time to update the respective kill counters.
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
CharacterRecord.advanceKillCounter(this.player);
CharacterRecord.advanceDeathCounter(this.victim);
}
2022-04-30 09:41:17 -04:00
}