Browse Source

Refactor to remove abstraction

master
MagicBot 2 years ago
parent
commit
4145124762
  1. 114
      src/engine/db/handlers/dbMobHandler.java

114
src/engine/db/handlers/dbMobHandler.java

@ -9,12 +9,15 @@
package engine.db.handlers; package engine.db.handlers;
import engine.gameManager.DbManager;
import engine.objects.Mob; import engine.objects.Mob;
import engine.objects.Zone; import engine.objects.Zone;
import engine.server.MBServerStatics; import engine.server.MBServerStatics;
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;
@ -26,72 +29,94 @@ public class dbMobHandler extends dbHandlerBase {
this.localObjectType = engine.Enum.GameObjectType.valueOf(this.localClass.getSimpleName()); this.localObjectType = engine.Enum.GameObjectType.valueOf(this.localClass.getSimpleName());
} }
public Mob ADD_MOB(Mob toAdd) public Mob ADD_MOB(Mob toAdd) {
{
prepareCallable("CALL `mob_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"); Mob mobile = null;
setLong(1, toAdd.getParentZoneID());
setInt(2, toAdd.getMobBaseID()); try (Connection connection = DbManager.getConnection();
setInt(3, toAdd.getGuildUUID()); PreparedStatement preparedStatement = connection.prepareStatement("CALL `mob_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")) {
setFloat(4, toAdd.getSpawnX());
setFloat(5, toAdd.getSpawnY()); preparedStatement.setLong(1, toAdd.getParentZoneID());
setFloat(6, toAdd.getSpawnZ()); preparedStatement.setInt(2, toAdd.getMobBaseID());
setInt(7, 0); preparedStatement.setInt(3, toAdd.getGuildUUID());
setFloat(8, toAdd.getSpawnRadius()); preparedStatement.setFloat(4, toAdd.getSpawnX());
setInt(9, toAdd.getTrueSpawnTime()); preparedStatement.setFloat(5, toAdd.getSpawnY());
preparedStatement.setFloat(6, toAdd.getSpawnZ());
preparedStatement.setInt(7, 0);
preparedStatement.setFloat(8, toAdd.getSpawnRadius());
preparedStatement.setInt(9, toAdd.getTrueSpawnTime());
if (toAdd.getContract() != null) if (toAdd.getContract() != null)
setInt(10, toAdd.getContract().getContractID()); preparedStatement.setInt(10, toAdd.getContract().getContractID());
else else
setInt(10, 0); preparedStatement.setInt(10, 0);
setInt(11, toAdd.getBuildingID());
setInt(12, toAdd.getLevel()); preparedStatement.setInt(11, toAdd.getBuildingID());
setString(13, toAdd.getFirstName()); preparedStatement.setInt(12, toAdd.getLevel());
int objectUUID = (int) getUUID(); preparedStatement.setString(13, toAdd.getFirstName());
ResultSet rs = preparedStatement.executeQuery();
int objectUUID = (int) rs.getLong("UID");
if (objectUUID > 0) if (objectUUID > 0)
return GET_MOB(objectUUID); mobile = GET_MOB(objectUUID);
return null;
} catch (SQLException e) {
throw new RuntimeException(e);
}
return mobile;
} }
public boolean updateUpgradeTime(Mob mob, DateTime upgradeDateTime) { public boolean updateUpgradeTime(Mob mob, DateTime upgradeDateTime) {
try { try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE obj_mob SET upgradeDate=? "
prepareCallable("UPDATE obj_mob SET upgradeDate=? " + "WHERE UID = ?")) {
+ "WHERE UID = ?");
if (upgradeDateTime == null) if (upgradeDateTime == null)
setNULL(1, java.sql.Types.DATE); preparedStatement.setNull(1, java.sql.Types.DATE);
else else
setTimeStamp(1, upgradeDateTime.getMillis()); preparedStatement.setTimestamp(1, new java.sql.Timestamp(upgradeDateTime.getMillis()));
setInt(2, mob.getObjectUUID()); preparedStatement.setInt(2, mob.getObjectUUID());
executeUpdate();
} catch (Exception e) { preparedStatement.execute();
Logger.error("Mob.updateUpgradeTime", "UUID: " + mob.getObjectUUID());
return false;
}
return true; return true;
} catch (SQLException e) {
Logger.error(e);
}
return false;
} }
public int DELETE_MOB(final Mob mob) { public int DELETE_MOB(final Mob mob) {
prepareCallable("DELETE FROM `object` WHERE `UID` = ?");
setLong(1, mob.getDBID()); int row_count = 0;
return executeUpdate(); try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM `object` WHERE `UID` = ?")) {
preparedStatement.setLong(1, mob.getDBID());
row_count = preparedStatement.executeUpdate();
} catch (SQLException e) {
Logger.error(e);
}
return row_count;
} }
public void LOAD_PATROL_POINTS(Mob captain) { public void LOAD_PATROL_POINTS(Mob captain) {
prepareCallable("SELECT * FROM `dyn_guards` WHERE `captainUID` = ?"); try (Connection connection = DbManager.getConnection();
setInt(1,captain.getObjectUUID()); PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `dyn_guards` WHERE `captainUID` = ?")) {
try { preparedStatement.setInt(1, captain.getObjectUUID());
ResultSet rs = executeQuery(); ResultSet rs = preparedStatement.executeQuery();
//shrines cached in rs for easy cache on creation.
while (rs.next()) { while (rs.next()) {
int mobBaseID = rs.getInt("mobBaseID");
String name = rs.getString("name"); String name = rs.getString("name");
Mob toCreate = Mob.createGuardMob(captain, captain.getGuild(), captain.getParentZone(), captain.building.getLoc(), captain.getLevel(), name); Mob toCreate = Mob.createGuardMob(captain, captain.getGuild(), captain.getParentZone(), captain.building.getLoc(), captain.getLevel(), name);
if (toCreate == null) if (toCreate == null)
return; return;
@ -102,11 +127,8 @@ public class dbMobHandler extends dbHandlerBase {
} }
} catch (SQLException e) { } catch (SQLException e) {
Logger.error( e.toString()); Logger.error(e);
} finally {
closeCallable();
} }
} }
public boolean ADD_TO_GUARDS(final long captainUID, final int mobBaseID, final String name, final int slot) { public boolean ADD_TO_GUARDS(final long captainUID, final int mobBaseID, final String name, final int slot) {

Loading…
Cancel
Save