Browse Source

Bonus code removed.

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

120
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;
@ -27,57 +30,70 @@ public class dbNPCHandler extends dbHandlerBase {
} }
public NPC ADD_NPC(NPC toAdd, boolean isMob) { public NPC ADD_NPC(NPC toAdd, boolean isMob) {
prepareCallable("CALL `npc_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
setLong(1, toAdd.getParentZoneID());
setString(2, toAdd.getName());
setInt(3, toAdd.getContractID());
setInt(4, toAdd.getGuildUUID());
setFloat(5, toAdd.getSpawnX());
setFloat(6, toAdd.getSpawnY());
setFloat(7, toAdd.getSpawnZ());
setInt(8, toAdd.getLevel());
setFloat(9, toAdd.getBuyPercent());
setFloat(10, toAdd.getSellPercent());
if (toAdd.getBuilding() != null) {
setInt(11, toAdd.getBuilding().getObjectUUID());
} else {
setInt(11, 0);
}
int objectUUID = (int) getUUID();
if (objectUUID > 0) {
return GET_NPC(objectUUID);
}
return null;
}
public int DELETE_NPC(final NPC npc) { NPC npc = null;
if (npc.isStatic()) {
return DELETE_STATIC_NPC(npc); try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("CALL `npc_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")) {
preparedStatement.setLong(1, toAdd.getParentZoneID());
preparedStatement.setString(2, toAdd.getName());
preparedStatement.setInt(3, toAdd.getContractID());
preparedStatement.setInt(4, toAdd.getGuildUUID());
preparedStatement.setFloat(5, toAdd.getSpawnX());
preparedStatement.setFloat(6, toAdd.getSpawnY());
preparedStatement.setFloat(7, toAdd.getSpawnZ());
preparedStatement.setInt(8, toAdd.getLevel());
preparedStatement.setFloat(9, toAdd.getBuyPercent());
preparedStatement.setFloat(10, toAdd.getSellPercent());
if (toAdd.getBuilding() != null)
preparedStatement.setInt(11, toAdd.getBuilding().getObjectUUID());
else
preparedStatement.setInt(11, 0);
ResultSet rs = preparedStatement.executeQuery();
int objectUUID = (int) rs.getLong("UID");
if (objectUUID > 0)
npc = GET_NPC(objectUUID);
} catch (SQLException e) {
throw new RuntimeException(e);
} }
npc.removeFromZone(); return npc;
prepareCallable("DELETE FROM `object` WHERE `UID` = ?");
setLong(1, (long) npc.getDBID());
return executeUpdate();
} }
private int DELETE_STATIC_NPC(final NPC npc) { public int DELETE_NPC(final NPC npc) {
int row_count = 0;
npc.removeFromZone(); npc.removeFromZone();
prepareCallable("DELETE FROM `_init_npc` WHERE `ID` = ?");
setInt(1, npc.getDBID()); try (Connection connection = DbManager.getConnection();
return executeUpdate(); PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM `object` WHERE `UID` = ?")) {
preparedStatement.setLong(1, npc.getDBID());
row_count = preparedStatement.executeUpdate();
} catch (SQLException e) {
Logger.error(e);
}
return row_count;
} }
public ArrayList<NPC> GET_ALL_NPCS_FOR_ZONE(Zone zone) { public ArrayList<NPC> GET_ALL_NPCS_FOR_ZONE(Zone zone) {
prepareCallable("SELECT `obj_npc`.*, `object`.`parent` FROM `object` INNER JOIN `obj_npc` ON `obj_npc`.`UID` = `object`.`UID` WHERE `object`.`parent` = ?;"); prepareCallable("SELECT `obj_npc`.*, `object`.`parent` FROM `object` INNER JOIN `obj_npc` ON `obj_npc`.`UID` = `object`.`UID` WHERE `object`.`parent` = ?;");
setLong(1, (long) zone.getObjectUUID()); setLong(1, zone.getObjectUUID());
return getLargeObjectList(); return getLargeObjectList();
} }
public NPC GET_NPC(final int objectUUID) { 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` = ?;"); prepareCallable("SELECT `obj_npc`.*, `object`.`parent` FROM `object` INNER JOIN `obj_npc` ON `obj_npc`.`UID` = `object`.`UID` WHERE `object`.`UID` = ?;");
setLong(1, (long) objectUUID); setLong(1, objectUUID);
return (NPC) getObjectSingle(objectUUID); return (NPC) getObjectSingle(objectUUID);
} }
@ -94,7 +110,7 @@ public class dbNPCHandler extends dbHandlerBase {
public String SET_PROPERTY(final NPC n, String name, Object new_value) { public String SET_PROPERTY(final NPC n, String name, Object new_value) {
prepareCallable("CALL npc_SETPROP(?,?,?)"); prepareCallable("CALL npc_SETPROP(?,?,?)");
setLong(1, (long) n.getDBID()); setLong(1, n.getDBID());
setString(2, name); setString(2, name);
setString(3, String.valueOf(new_value)); setString(3, String.valueOf(new_value));
return getResult(); return getResult();
@ -102,7 +118,7 @@ public class dbNPCHandler extends dbHandlerBase {
public String SET_PROPERTY(final NPC n, String name, Object new_value, Object old_value) { public String SET_PROPERTY(final NPC n, String name, Object new_value, Object old_value) {
prepareCallable("CALL npc_GETSETPROP(?,?,?,?)"); prepareCallable("CALL npc_GETSETPROP(?,?,?,?)");
setLong(1, (long) n.getDBID()); setLong(1, n.getDBID());
setString(2, name); setString(2, name);
setString(3, String.valueOf(new_value)); setString(3, String.valueOf(new_value));
setString(4, String.valueOf(old_value)); setString(4, String.valueOf(old_value));
@ -163,7 +179,7 @@ public class dbNPCHandler extends dbHandlerBase {
return (executeUpdate() > 0); return (executeUpdate() > 0);
} }
public boolean UPDATE_NAME(NPC npc,String name) { public boolean UPDATE_NAME(NPC npc, String name) {
prepareCallable("UPDATE `obj_npc` SET `npc_name`=? WHERE `UID`=?"); prepareCallable("UPDATE `obj_npc` SET `npc_name`=? WHERE `UID`=?");
setString(1, name); setString(1, name);
setLong(2, npc.getObjectUUID()); setLong(2, npc.getObjectUUID());
@ -210,9 +226,9 @@ public class dbNPCHandler extends dbHandlerBase {
} }
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 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 (?,?,?,?,?,?,?,?,?)"); prepareCallable("INSERT INTO `dyn_npc_production` (`ID`,`npcUID`, `itemBaseID`,`dateToUpgrade`, `isRandom`, `prefix`, `suffix`, `name`,`playerID`) VALUES (?,?,?,?,?,?,?,?,?)");
setLong(1,ID); setLong(1, ID);
setLong(2, npcUID); setLong(2, npcUID);
setLong(3, itemBaseID); setLong(3, itemBaseID);
setTimeStamp(4, dateTime.getMillis()); setTimeStamp(4, dateTime.getMillis());
@ -220,26 +236,26 @@ public class dbNPCHandler extends dbHandlerBase {
setString(6, prefix); setString(6, prefix);
setString(7, suffix); setString(7, suffix);
setString(8, name); setString(8, name);
setInt(9,playerID); setInt(9, playerID);
return (executeUpdate() > 0); return (executeUpdate() > 0);
} }
public boolean REMOVE_FROM_PRODUCTION_LIST(final long ID,final long npcUID) { public boolean REMOVE_FROM_PRODUCTION_LIST(final long ID, final long npcUID) {
prepareCallable("DELETE FROM `dyn_npc_production` WHERE `ID`=? AND `npcUID`=?;"); prepareCallable("DELETE FROM `dyn_npc_production` WHERE `ID`=? AND `npcUID`=?;");
setLong(1,ID); setLong(1, ID);
setLong(2, npcUID); setLong(2, npcUID);
return (executeUpdate() > 0); return (executeUpdate() > 0);
} }
public boolean UPDATE_ITEM_TO_INVENTORY(final long ID,final long npcUID) { public boolean UPDATE_ITEM_TO_INVENTORY(final long ID, final long npcUID) {
prepareCallable("UPDATE `dyn_npc_production` SET `inForge`=? WHERE `ID`=? AND `npcUID`=?;"); prepareCallable("UPDATE `dyn_npc_production` SET `inForge`=? WHERE `ID`=? AND `npcUID`=?;");
setByte(1, (byte)0); setByte(1, (byte) 0);
setLong(2, ID); setLong(2, ID);
setLong(3, npcUID); setLong(3, npcUID);
return (executeUpdate() > 0); return (executeUpdate() > 0);
} }
public boolean UPDATE_ITEM_PRICE(final long ID,final long npcUID, int value) { public boolean UPDATE_ITEM_PRICE(final long ID, final long npcUID, int value) {
prepareCallable("UPDATE `dyn_npc_production` SET `value`=? WHERE `ID`=? AND `npcUID`=?;"); prepareCallable("UPDATE `dyn_npc_production` SET `value`=? WHERE `ID`=? AND `npcUID`=?;");
setInt(1, value); setInt(1, value);
setLong(2, ID); setLong(2, ID);
@ -248,7 +264,7 @@ public class dbNPCHandler extends dbHandlerBase {
return (executeUpdate() > 0); return (executeUpdate() > 0);
} }
public boolean UPDATE_ITEM_ID(final long ID,final long npcUID,final long value) { public boolean UPDATE_ITEM_ID(final long ID, final long npcUID, final long value) {
prepareCallable("UPDATE `dyn_npc_production` SET `ID`=? WHERE `ID`=? AND `npcUID`=? LIMIT 1;"); prepareCallable("UPDATE `dyn_npc_production` SET `ID`=? WHERE `ID`=? AND `npcUID`=? LIMIT 1;");
setLong(1, value); setLong(1, value);
setLong(2, ID); setLong(2, ID);
@ -263,7 +279,7 @@ public class dbNPCHandler extends dbHandlerBase {
return; return;
prepareCallable("SELECT * FROM `dyn_npc_production` WHERE `npcUID` = ?"); prepareCallable("SELECT * FROM `dyn_npc_production` WHERE `npcUID` = ?");
setInt(1,npc.getObjectUUID()); setInt(1, npc.getObjectUUID());
try { try {
ResultSet rs = executeQuery(); ResultSet rs = executeQuery();
@ -281,7 +297,7 @@ public class dbNPCHandler extends dbHandlerBase {
} }
} }
public boolean UPDATE_PROFITS(NPC npc,ProfitType profitType, float value){ public boolean UPDATE_PROFITS(NPC npc, ProfitType profitType, float value) {
prepareCallable("UPDATE `dyn_npc_profits` SET `" + profitType.dbField + "` = ? WHERE `npcUID`=?"); prepareCallable("UPDATE `dyn_npc_profits` SET `" + profitType.dbField + "` = ? WHERE `npcUID`=?");
setFloat(1, value); setFloat(1, value);
setInt(2, npc.getObjectUUID()); setInt(2, npc.getObjectUUID());
@ -313,9 +329,9 @@ public class dbNPCHandler extends dbHandlerBase {
} }
} }
public boolean CREATE_PROFITS(NPC npc){ public boolean CREATE_PROFITS(NPC npc) {
prepareCallable("INSERT INTO `dyn_npc_profits` (`npcUID`) VALUES (?)"); prepareCallable("INSERT INTO `dyn_npc_profits` (`npcUID`) VALUES (?)");
setLong(1,npc.getObjectUUID()); setLong(1, npc.getObjectUUID());
return (executeUpdate() > 0); return (executeUpdate() > 0);
} }
} }

Loading…
Cancel
Save