Browse Source

Bonus code removed.

master
MagicBot 2 years ago
parent
commit
1bf9fde684
  1. 610
      src/engine/db/handlers/dbNPCHandler.java

610
src/engine/db/handlers/dbNPCHandler.java

@ -10,10 +10,13 @@
package engine.db.handlers; package engine.db.handlers;
import engine.Enum.ProfitType; import engine.Enum.ProfitType;
import engine.gameManager.DbManager;
import engine.objects.*; import engine.objects.*;
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.ArrayList; import java.util.ArrayList;
@ -21,301 +24,314 @@ import java.util.HashMap;
public class dbNPCHandler extends dbHandlerBase { public class dbNPCHandler extends dbHandlerBase {
public dbNPCHandler() { public dbNPCHandler() {
this.localClass = NPC.class; this.localClass = NPC.class;
this.localObjectType = engine.Enum.GameObjectType.valueOf(this.localClass.getSimpleName()); this.localObjectType = engine.Enum.GameObjectType.valueOf(this.localClass.getSimpleName());
} }
public NPC ADD_NPC(NPC toAdd, boolean isMob) { public NPC ADD_NPC(NPC toAdd, boolean isMob) {
prepareCallable("CALL `npc_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
setLong(1, toAdd.getParentZoneID()); NPC npc = null;
setString(2, toAdd.getName());
setInt(3, toAdd.getContractID()); try (Connection connection = DbManager.getConnection();
setInt(4, toAdd.getGuildUUID()); PreparedStatement preparedStatement = connection.prepareStatement("CALL `npc_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")) {
setFloat(5, toAdd.getSpawnX());
setFloat(6, toAdd.getSpawnY()); preparedStatement.setLong(1, toAdd.getParentZoneID());
setFloat(7, toAdd.getSpawnZ()); preparedStatement.setString(2, toAdd.getName());
setInt(8, toAdd.getLevel()); preparedStatement.setInt(3, toAdd.getContractID());
setFloat(9, toAdd.getBuyPercent()); preparedStatement.setInt(4, toAdd.getGuildUUID());
setFloat(10, toAdd.getSellPercent()); preparedStatement.setFloat(5, toAdd.getSpawnX());
if (toAdd.getBuilding() != null) { preparedStatement.setFloat(6, toAdd.getSpawnY());
setInt(11, toAdd.getBuilding().getObjectUUID()); preparedStatement.setFloat(7, toAdd.getSpawnZ());
} else { preparedStatement.setInt(8, toAdd.getLevel());
setInt(11, 0); preparedStatement.setFloat(9, toAdd.getBuyPercent());
} preparedStatement.setFloat(10, toAdd.getSellPercent());
int objectUUID = (int) getUUID(); if (toAdd.getBuilding() != null)
if (objectUUID > 0) { preparedStatement.setInt(11, toAdd.getBuilding().getObjectUUID());
return GET_NPC(objectUUID); else
} preparedStatement.setInt(11, 0);
return null;
} ResultSet rs = preparedStatement.executeQuery();
public int DELETE_NPC(final NPC npc) { int objectUUID = (int) rs.getLong("UID");
if (npc.isStatic()) {
return DELETE_STATIC_NPC(npc); if (objectUUID > 0)
} npc = GET_NPC(objectUUID);
npc.removeFromZone(); } catch (SQLException e) {
prepareCallable("DELETE FROM `object` WHERE `UID` = ?"); throw new RuntimeException(e);
setLong(1, (long) npc.getDBID()); }
return executeUpdate();
} return npc;
}
private int DELETE_STATIC_NPC(final NPC npc) {
npc.removeFromZone(); public int DELETE_NPC(final NPC npc) {
prepareCallable("DELETE FROM `_init_npc` WHERE `ID` = ?");
setInt(1, npc.getDBID()); int row_count = 0;
return executeUpdate();
} npc.removeFromZone();
public ArrayList<NPC> GET_ALL_NPCS_FOR_ZONE(Zone zone) { try (Connection connection = DbManager.getConnection();
prepareCallable("SELECT `obj_npc`.*, `object`.`parent` FROM `object` INNER JOIN `obj_npc` ON `obj_npc`.`UID` = `object`.`UID` WHERE `object`.`parent` = ?;"); PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM `object` WHERE `UID` = ?")) {
setLong(1, (long) zone.getObjectUUID());
return getLargeObjectList(); preparedStatement.setLong(1, npc.getDBID());
} row_count = preparedStatement.executeUpdate();
public NPC GET_NPC(final int objectUUID) { } catch (SQLException e) {
prepareCallable("SELECT `obj_npc`.*, `object`.`parent` FROM `object` INNER JOIN `obj_npc` ON `obj_npc`.`UID` = `object`.`UID` WHERE `object`.`UID` = ?;"); Logger.error(e);
setLong(1, (long) objectUUID);
return (NPC) getObjectSingle(objectUUID); }
} return row_count;
}
public int MOVE_NPC(long npcID, long parentID, float locX, float locY, float locZ) {
prepareCallable("UPDATE `object` INNER JOIN `obj_npc` On `object`.`UID` = `obj_npc`.`UID` SET `object`.`parent`=?, `obj_npc`.`npc_spawnX`=?, `obj_npc`.`npc_spawnY`=?, `obj_npc`.`npc_spawnZ`=? WHERE `obj_npc`.`UID`=?;"); public ArrayList<NPC> GET_ALL_NPCS_FOR_ZONE(Zone zone) {
setLong(1, parentID); prepareCallable("SELECT `obj_npc`.*, `object`.`parent` FROM `object` INNER JOIN `obj_npc` ON `obj_npc`.`UID` = `object`.`UID` WHERE `object`.`parent` = ?;");
setFloat(2, locX); setLong(1, zone.getObjectUUID());
setFloat(3, locY); return getLargeObjectList();
setFloat(4, locZ); }
setLong(5, npcID);
return executeUpdate(); public NPC GET_NPC(final int objectUUID) {
} prepareCallable("SELECT `obj_npc`.*, `object`.`parent` FROM `object` INNER JOIN `obj_npc` ON `obj_npc`.`UID` = `object`.`UID` WHERE `object`.`UID` = ?;");
setLong(1, objectUUID);
return (NPC) getObjectSingle(objectUUID);
public String SET_PROPERTY(final NPC n, String name, Object new_value) { }
prepareCallable("CALL npc_SETPROP(?,?,?)");
setLong(1, (long) n.getDBID()); public int MOVE_NPC(long npcID, long parentID, float locX, float locY, float locZ) {
setString(2, name); prepareCallable("UPDATE `object` INNER JOIN `obj_npc` On `object`.`UID` = `obj_npc`.`UID` SET `object`.`parent`=?, `obj_npc`.`npc_spawnX`=?, `obj_npc`.`npc_spawnY`=?, `obj_npc`.`npc_spawnZ`=? WHERE `obj_npc`.`UID`=?;");
setString(3, String.valueOf(new_value)); setLong(1, parentID);
return getResult(); setFloat(2, locX);
} setFloat(3, locY);
setFloat(4, locZ);
public String SET_PROPERTY(final NPC n, String name, Object new_value, Object old_value) { setLong(5, npcID);
prepareCallable("CALL npc_GETSETPROP(?,?,?,?)"); return executeUpdate();
setLong(1, (long) n.getDBID()); }
setString(2, name);
setString(3, String.valueOf(new_value));
setString(4, String.valueOf(old_value)); public String SET_PROPERTY(final NPC n, String name, Object new_value) {
return getResult(); prepareCallable("CALL npc_SETPROP(?,?,?)");
} setLong(1, n.getDBID());
setString(2, name);
public void updateDatabase(final NPC npc) { setString(3, String.valueOf(new_value));
prepareCallable("UPDATE obj_npc SET npc_name=?, npc_contractID=?, npc_typeID=?, npc_guildID=?," return getResult();
+ " npc_spawnX=?, npc_spawnY=?, npc_spawnZ=?, npc_level=? ," }
+ " npc_buyPercent=?, npc_sellPercent=?, npc_buildingID=? WHERE UID = ?");
setString(1, npc.getName()); public String SET_PROPERTY(final NPC n, String name, Object new_value, Object old_value) {
setInt(2, (npc.getContract() != null) ? npc.getContract().getObjectUUID() : 0); prepareCallable("CALL npc_GETSETPROP(?,?,?,?)");
setInt(3, 0); setLong(1, n.getDBID());
setInt(4, (npc.getGuild() != null) ? npc.getGuild().getObjectUUID() : 0); setString(2, name);
setFloat(5, npc.getBindLoc().x); setString(3, String.valueOf(new_value));
setFloat(6, npc.getBindLoc().y); setString(4, String.valueOf(old_value));
setFloat(7, npc.getBindLoc().z); return getResult();
setShort(8, npc.getLevel()); }
setFloat(9, npc.getBuyPercent());
setFloat(10, npc.getSellPercent()); public void updateDatabase(final NPC npc) {
setInt(11, (npc.getBuilding() != null) ? npc.getBuilding().getObjectUUID() : 0); prepareCallable("UPDATE obj_npc SET npc_name=?, npc_contractID=?, npc_typeID=?, npc_guildID=?,"
setInt(12, npc.getDBID()); + " npc_spawnX=?, npc_spawnY=?, npc_spawnZ=?, npc_level=? ,"
executeUpdate(); + " npc_buyPercent=?, npc_sellPercent=?, npc_buildingID=? WHERE UID = ?");
} setString(1, npc.getName());
setInt(2, (npc.getContract() != null) ? npc.getContract().getObjectUUID() : 0);
public boolean updateUpgradeTime(NPC npc, DateTime upgradeDateTime) { setInt(3, 0);
setInt(4, (npc.getGuild() != null) ? npc.getGuild().getObjectUUID() : 0);
try { setFloat(5, npc.getBindLoc().x);
setFloat(6, npc.getBindLoc().y);
prepareCallable("UPDATE obj_npc SET upgradeDate=? " setFloat(7, npc.getBindLoc().z);
+ "WHERE UID = ?"); setShort(8, npc.getLevel());
setFloat(9, npc.getBuyPercent());
if (upgradeDateTime == null) setFloat(10, npc.getSellPercent());
setNULL(1, java.sql.Types.DATE); setInt(11, (npc.getBuilding() != null) ? npc.getBuilding().getObjectUUID() : 0);
else setInt(12, npc.getDBID());
setTimeStamp(1, upgradeDateTime.getMillis()); executeUpdate();
}
setInt(2, npc.getObjectUUID());
executeUpdate(); public boolean updateUpgradeTime(NPC npc, DateTime upgradeDateTime) {
} catch (Exception e) {
Logger.error("UUID: " + npc.getObjectUUID()); try {
return false;
} prepareCallable("UPDATE obj_npc SET upgradeDate=? "
return true; + "WHERE UID = ?");
}
if (upgradeDateTime == null)
public boolean UPDATE_MOBBASE(NPC npc, int mobBaseID) { setNULL(1, java.sql.Types.DATE);
prepareCallable("UPDATE `obj_npc` SET `npc_raceID`=? WHERE `UID`=?"); else
setLong(1, mobBaseID); setTimeStamp(1, upgradeDateTime.getMillis());
setLong(2, npc.getObjectUUID());
return (executeUpdate() > 0); setInt(2, npc.getObjectUUID());
} executeUpdate();
} catch (Exception e) {
public boolean UPDATE_EQUIPSET(NPC npc, int equipSetID) { Logger.error("UUID: " + npc.getObjectUUID());
prepareCallable("UPDATE `obj_npc` SET `equipsetID`=? WHERE `UID`=?"); return false;
setInt(1, equipSetID); }
setLong(2, npc.getObjectUUID()); return true;
return (executeUpdate() > 0); }
}
public boolean UPDATE_MOBBASE(NPC npc, int mobBaseID) {
public boolean UPDATE_NAME(NPC npc,String name) { prepareCallable("UPDATE `obj_npc` SET `npc_raceID`=? WHERE `UID`=?");
prepareCallable("UPDATE `obj_npc` SET `npc_name`=? WHERE `UID`=?"); setLong(1, mobBaseID);
setString(1, name); setLong(2, npc.getObjectUUID());
setLong(2, npc.getObjectUUID()); return (executeUpdate() > 0);
return (executeUpdate() > 0); }
}
public boolean UPDATE_EQUIPSET(NPC npc, int equipSetID) {
public void LOAD_PIRATE_NAMES() { prepareCallable("UPDATE `obj_npc` SET `equipsetID`=? WHERE `UID`=?");
setInt(1, equipSetID);
String pirateName; setLong(2, npc.getObjectUUID());
int mobBase; return (executeUpdate() > 0);
int recordsRead = 0; }
prepareCallable("SELECT * FROM static_piratenames"); public boolean UPDATE_NAME(NPC npc, String name) {
prepareCallable("UPDATE `obj_npc` SET `npc_name`=? WHERE `UID`=?");
try { setString(1, name);
ResultSet rs = executeQuery(); setLong(2, npc.getObjectUUID());
return (executeUpdate() > 0);
while (rs.next()) { }
recordsRead++; public void LOAD_PIRATE_NAMES() {
mobBase = rs.getInt("mobbase");
pirateName = rs.getString("first_name"); String pirateName;
int mobBase;
// Handle new mobbbase entries int recordsRead = 0;
if (NPC._pirateNames.get(mobBase) == null) { prepareCallable("SELECT * FROM static_piratenames");
NPC._pirateNames.putIfAbsent(mobBase, new ArrayList<>());
} try {
ResultSet rs = executeQuery();
// Insert name into proper arraylist
while (rs.next()) {
NPC._pirateNames.get(mobBase).add(pirateName);
recordsRead++;
} mobBase = rs.getInt("mobbase");
pirateName = rs.getString("first_name");
Logger.info("names read: " + recordsRead + " for "
+ NPC._pirateNames.size() + " mobBases"); // Handle new mobbbase entries
} catch (SQLException e) { if (NPC._pirateNames.get(mobBase) == null) {
Logger.error(e.getErrorCode() + ' ' + e.getMessage(), e); NPC._pirateNames.putIfAbsent(mobBase, new ArrayList<>());
} finally { }
closeCallable();
} // Insert name into proper arraylist
}
NPC._pirateNames.get(mobBase).add(pirateName);
public boolean ADD_TO_PRODUCTION_LIST(final long ID,final long npcUID, final long itemBaseID, DateTime dateTime, String prefix, String suffix, String name, boolean isRandom, int playerID) { }
prepareCallable("INSERT INTO `dyn_npc_production` (`ID`,`npcUID`, `itemBaseID`,`dateToUpgrade`, `isRandom`, `prefix`, `suffix`, `name`,`playerID`) VALUES (?,?,?,?,?,?,?,?,?)");
setLong(1,ID); Logger.info("names read: " + recordsRead + " for "
setLong(2, npcUID); + NPC._pirateNames.size() + " mobBases");
setLong(3, itemBaseID);
setTimeStamp(4, dateTime.getMillis()); } catch (SQLException e) {
setBoolean(5, isRandom); Logger.error(e.getErrorCode() + ' ' + e.getMessage(), e);
setString(6, prefix); } finally {
setString(7, suffix); closeCallable();
setString(8, name); }
setInt(9,playerID); }
return (executeUpdate() > 0);
}
public boolean ADD_TO_PRODUCTION_LIST(final long ID, final long npcUID, final long itemBaseID, DateTime dateTime, String prefix, String suffix, String name, boolean isRandom, int playerID) {
public boolean REMOVE_FROM_PRODUCTION_LIST(final long ID,final long npcUID) { prepareCallable("INSERT INTO `dyn_npc_production` (`ID`,`npcUID`, `itemBaseID`,`dateToUpgrade`, `isRandom`, `prefix`, `suffix`, `name`,`playerID`) VALUES (?,?,?,?,?,?,?,?,?)");
prepareCallable("DELETE FROM `dyn_npc_production` WHERE `ID`=? AND `npcUID`=?;"); setLong(1, ID);
setLong(1,ID); setLong(2, npcUID);
setLong(2, npcUID); setLong(3, itemBaseID);
return (executeUpdate() > 0); setTimeStamp(4, dateTime.getMillis());
} setBoolean(5, isRandom);
setString(6, prefix);
public boolean UPDATE_ITEM_TO_INVENTORY(final long ID,final long npcUID) { setString(7, suffix);
prepareCallable("UPDATE `dyn_npc_production` SET `inForge`=? WHERE `ID`=? AND `npcUID`=?;"); setString(8, name);
setByte(1, (byte)0); setInt(9, playerID);
setLong(2, ID); return (executeUpdate() > 0);
setLong(3, npcUID); }
return (executeUpdate() > 0);
} public boolean REMOVE_FROM_PRODUCTION_LIST(final long ID, final long npcUID) {
prepareCallable("DELETE FROM `dyn_npc_production` WHERE `ID`=? AND `npcUID`=?;");
public boolean UPDATE_ITEM_PRICE(final long ID,final long npcUID, int value) { setLong(1, ID);
prepareCallable("UPDATE `dyn_npc_production` SET `value`=? WHERE `ID`=? AND `npcUID`=?;"); setLong(2, npcUID);
setInt(1, value); return (executeUpdate() > 0);
setLong(2, ID); }
setLong(3, npcUID);
public boolean UPDATE_ITEM_TO_INVENTORY(final long ID, final long npcUID) {
return (executeUpdate() > 0); prepareCallable("UPDATE `dyn_npc_production` SET `inForge`=? WHERE `ID`=? AND `npcUID`=?;");
} setByte(1, (byte) 0);
setLong(2, ID);
public boolean UPDATE_ITEM_ID(final long ID,final long npcUID,final long value) { setLong(3, npcUID);
prepareCallable("UPDATE `dyn_npc_production` SET `ID`=? WHERE `ID`=? AND `npcUID`=? LIMIT 1;"); return (executeUpdate() > 0);
setLong(1, value); }
setLong(2, ID);
setLong(3, npcUID); public boolean UPDATE_ITEM_PRICE(final long ID, final long npcUID, int value) {
prepareCallable("UPDATE `dyn_npc_production` SET `value`=? WHERE `ID`=? AND `npcUID`=?;");
return (executeUpdate() > 0); setInt(1, value);
} setLong(2, ID);
setLong(3, npcUID);
public void LOAD_ALL_ITEMS_TO_PRODUCE(NPC npc) {
return (executeUpdate() > 0);
if (npc == null) }
return;
public boolean UPDATE_ITEM_ID(final long ID, final long npcUID, final long value) {
prepareCallable("SELECT * FROM `dyn_npc_production` WHERE `npcUID` = ?"); prepareCallable("UPDATE `dyn_npc_production` SET `ID`=? WHERE `ID`=? AND `npcUID`=? LIMIT 1;");
setInt(1,npc.getObjectUUID()); setLong(1, value);
setLong(2, ID);
try { setLong(3, npcUID);
ResultSet rs = executeQuery();
return (executeUpdate() > 0);
//shrines cached in rs for easy cache on creation. }
while (rs.next()) {
ProducedItem producedItem = new ProducedItem(rs); public void LOAD_ALL_ITEMS_TO_PRODUCE(NPC npc) {
npc.forgedItems.add(producedItem);
} if (npc == null)
return;
} catch (SQLException e) {
Logger.error(e.getErrorCode() + ' ' + e.getMessage(), e); prepareCallable("SELECT * FROM `dyn_npc_production` WHERE `npcUID` = ?");
} finally { setInt(1, npc.getObjectUUID());
closeCallable();
} try {
} ResultSet rs = executeQuery();
public boolean UPDATE_PROFITS(NPC npc,ProfitType profitType, float value){ //shrines cached in rs for easy cache on creation.
prepareCallable("UPDATE `dyn_npc_profits` SET `" + profitType.dbField + "` = ? WHERE `npcUID`=?"); while (rs.next()) {
setFloat(1, value); ProducedItem producedItem = new ProducedItem(rs);
setInt(2, npc.getObjectUUID()); npc.forgedItems.add(producedItem);
return (executeUpdate() > 0); }
}
} catch (SQLException e) {
public void LOAD_NPC_PROFITS() { Logger.error(e.getErrorCode() + ' ' + e.getMessage(), e);
} finally {
HashMap<Integer, ArrayList<BuildingRegions>> regions; closeCallable();
NPCProfits npcProfit; }
}
prepareCallable("SELECT * FROM dyn_npc_profits"); public boolean UPDATE_PROFITS(NPC npc, ProfitType profitType, float value) {
prepareCallable("UPDATE `dyn_npc_profits` SET `" + profitType.dbField + "` = ? WHERE `npcUID`=?");
try { setFloat(1, value);
ResultSet rs = executeQuery(); setInt(2, npc.getObjectUUID());
return (executeUpdate() > 0);
while (rs.next()) { }
public void LOAD_NPC_PROFITS() {
npcProfit = new NPCProfits(rs);
NPCProfits.ProfitCache.put(npcProfit.npcUID, npcProfit); HashMap<Integer, ArrayList<BuildingRegions>> regions;
} NPCProfits npcProfit;
} catch (SQLException e) {
Logger.error(": " + e.getErrorCode() + ' ' + e.getMessage(), e); prepareCallable("SELECT * FROM dyn_npc_profits");
} finally {
closeCallable(); try {
} ResultSet rs = executeQuery();
}
while (rs.next()) {
public boolean CREATE_PROFITS(NPC npc){
prepareCallable("INSERT INTO `dyn_npc_profits` (`npcUID`) VALUES (?)");
setLong(1,npc.getObjectUUID()); npcProfit = new NPCProfits(rs);
return (executeUpdate() > 0); NPCProfits.ProfitCache.put(npcProfit.npcUID, npcProfit);
} }
} catch (SQLException e) {
Logger.error(": " + e.getErrorCode() + ' ' + e.getMessage(), e);
} finally {
closeCallable();
}
}
public boolean CREATE_PROFITS(NPC npc) {
prepareCallable("INSERT INTO `dyn_npc_profits` (`npcUID`) VALUES (?)");
setLong(1, npc.getObjectUUID());
return (executeUpdate() > 0);
}
} }

Loading…
Cancel
Save