Browse Source

Refactor to remove abstraction.

master
MagicBot 2 years ago
parent
commit
478e472dc4
  1. 140
      src/engine/db/handlers/dbShrineHandler.java

140
src/engine/db/handlers/dbShrineHandler.java

@ -18,7 +18,8 @@ import engine.objects.Shrine;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.pmw.tinylog.Logger; import org.pmw.tinylog.Logger;
import java.net.UnknownHostException; 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;
@ -35,78 +36,77 @@ public class dbShrineHandler extends dbHandlerBase {
ProtectionState protectionState, int currentGold, int rank, ProtectionState protectionState, int currentGold, int rank,
DateTime upgradeDate, int blueprintUUID, float w, float rotY, String shrineType) { DateTime upgradeDate, int blueprintUUID, float w, float rotY, String shrineType) {
prepareCallable("CALL `shrine_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,? ,? ,?, ?,?);"); ArrayList<AbstractGameObject> shrineList = new ArrayList<>();
setInt(1, parentZoneID);
setInt(2, OwnerUUID);
setString(3, name);
setInt(4, meshUUID);
setFloat(5, location.x);
setFloat(6, location.y);
setFloat(7, location.z);
setFloat(8, meshScale);
setInt(9, currentHP);
setString(10, protectionState.name());
setInt(11, currentGold);
setInt(12, rank);
if (upgradeDate != null) {
setTimeStamp(13, upgradeDate.getMillis());
} else {
setNULL(13, java.sql.Types.DATE);
}
setInt(14, blueprintUUID); try (Connection connection = DbManager.getConnection();
setFloat(15, w); PreparedStatement preparedStatement = connection.prepareStatement("CALL `shrine_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,? ,? ,?, ?,?);")) {
setFloat(16, rotY);
setString(17, shrineType); preparedStatement.setInt(1, parentZoneID);
preparedStatement.setInt(2, OwnerUUID);
ArrayList<AbstractGameObject> list = new ArrayList<>(); preparedStatement.setString(3, name);
//System.out.println(this.cs.get().toString()); preparedStatement.setInt(4, meshUUID);
try { preparedStatement.setFloat(5, location.x);
boolean work = execute(); preparedStatement.setFloat(6, location.y);
if (work) { preparedStatement.setFloat(7, location.z);
ResultSet rs = this.callableStatement.get().getResultSet(); preparedStatement.setFloat(8, meshScale);
while (rs.next()) { preparedStatement.setInt(9, currentHP);
addObject(list, rs); preparedStatement.setString(10, protectionState.name());
} preparedStatement.setInt(11, currentGold);
rs.close(); preparedStatement.setInt(12, rank);
} else {
Logger.info("Shrine Creation Failed: " + this.callableStatement.get().toString()); if (upgradeDate != null)
return list; //city creation failure preparedStatement.setTimestamp(13, new java.sql.Timestamp(upgradeDate.getMillis()));
} else
while (this.callableStatement.get().getMoreResults()) { preparedStatement.setNull(13, java.sql.Types.DATE);
ResultSet rs = this.callableStatement.get().getResultSet();
while (rs.next()) { preparedStatement.setInt(14, blueprintUUID);
addObject(list, rs); preparedStatement.setFloat(15, w);
} preparedStatement.setFloat(16, rotY);
rs.close(); preparedStatement.setString(17, shrineType);
preparedStatement.execute();
ResultSet rs = preparedStatement.getResultSet();
while (rs.next())
addObject(shrineList, rs);
while (preparedStatement.getMoreResults()) {
rs = preparedStatement.getResultSet();
while (rs.next())
addObject(shrineList, rs);
} }
} catch (SQLException e) { } catch (SQLException e) {
Logger.info("Shrine Creation Failed, SQLException: " + this.callableStatement.get().toString() + e.toString()); Logger.error(e);
return list; //city creation failure
} catch (UnknownHostException e) {
Logger.info("Shrine Creation Failed, UnknownHostException: " + this.callableStatement.get().toString());
return list; //city creation failure
} finally {
closeCallable();
} }
return list;
return shrineList;
} }
public boolean updateFavors(Shrine shrine, int amount, int oldAmount) { public boolean updateFavors(Shrine shrine, int amount, int oldAmount) {
prepareCallable("UPDATE `obj_shrine` SET `shrine_favors`=? WHERE `UID` = ? AND `shrine_favors` = ?"); try (Connection connection = DbManager.getConnection();
setInt(1, amount); PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_shrine` SET `shrine_favors`=? WHERE `UID` = ? AND `shrine_favors` = ?")) {
setLong(2, (long) shrine.getObjectUUID());
setInt(3, oldAmount); preparedStatement.setInt(1, amount);
return (executeUpdate() != 0); preparedStatement.setLong(2, shrine.getObjectUUID());
preparedStatement.setInt(3, oldAmount);
return (preparedStatement.executeUpdate() > 0);
} catch (SQLException e) {
Logger.error(e);
return false;
}
} }
public static void addObject(ArrayList<AbstractGameObject> list, ResultSet rs) throws SQLException, UnknownHostException { public static void addObject(ArrayList<AbstractGameObject> list, ResultSet rs) throws SQLException {
String type = rs.getString("type"); String type = rs.getString("type");
switch (type) { switch (type) {
case "building": case "building":
Building building = new Building(rs); Building building = new Building(rs);
@ -123,25 +123,21 @@ public class dbShrineHandler extends dbHandlerBase {
public void LOAD_ALL_SHRINES() { public void LOAD_ALL_SHRINES() {
Shrine thisShrine; Shrine shrine;
prepareCallable("SELECT `obj_shrine`.*, `object`.`parent`, `object`.`type` FROM `object` LEFT JOIN `obj_shrine` ON `object`.`UID` = `obj_shrine`.`UID` WHERE `object`.`type` = 'shrine';"); try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT `obj_shrine`.*, `object`.`parent`, `object`.`type` FROM `object` LEFT JOIN `obj_shrine` ON `object`.`UID` = `obj_shrine`.`UID` WHERE `object`.`type` = 'shrine';")) {
try { ResultSet rs = preparedStatement.executeQuery();
ResultSet rs = executeQuery();
//shrines cached in rs for easy cache on creation.
while (rs.next()) { while (rs.next()) {
thisShrine = new Shrine(rs); shrine = new Shrine(rs);
thisShrine.getShrineType().addShrineToServerList(thisShrine); shrine.getShrineType().addShrineToServerList(shrine);
} }
} catch (SQLException e) { } catch (SQLException e) {
Logger.error( e.getErrorCode() + ' ' + e.getMessage(), e); Logger.error(e);
} finally {
closeCallable();
} }
} }
} }

Loading…
Cancel
Save