forked from MagicBane/Server
Refactor to remove abstraction.
This commit is contained in:
@@ -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<>();
|
||||||
|
|
||||||
|
try (Connection connection = DbManager.getConnection();
|
||||||
setInt(1, parentZoneID);
|
PreparedStatement preparedStatement = connection.prepareStatement("CALL `shrine_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,? ,? ,?, ?,?);")) {
|
||||||
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) {
|
preparedStatement.setInt(1, parentZoneID);
|
||||||
setTimeStamp(13, upgradeDate.getMillis());
|
preparedStatement.setInt(2, OwnerUUID);
|
||||||
} else {
|
preparedStatement.setString(3, name);
|
||||||
setNULL(13, java.sql.Types.DATE);
|
preparedStatement.setInt(4, meshUUID);
|
||||||
}
|
preparedStatement.setFloat(5, location.x);
|
||||||
|
preparedStatement.setFloat(6, location.y);
|
||||||
|
preparedStatement.setFloat(7, location.z);
|
||||||
|
preparedStatement.setFloat(8, meshScale);
|
||||||
|
preparedStatement.setInt(9, currentHP);
|
||||||
|
preparedStatement.setString(10, protectionState.name());
|
||||||
|
preparedStatement.setInt(11, currentGold);
|
||||||
|
preparedStatement.setInt(12, rank);
|
||||||
|
|
||||||
setInt(14, blueprintUUID);
|
if (upgradeDate != null)
|
||||||
setFloat(15, w);
|
preparedStatement.setTimestamp(13, new java.sql.Timestamp(upgradeDate.getMillis()));
|
||||||
setFloat(16, rotY);
|
else
|
||||||
setString(17, shrineType);
|
preparedStatement.setNull(13, java.sql.Types.DATE);
|
||||||
|
|
||||||
ArrayList<AbstractGameObject> list = new ArrayList<>();
|
preparedStatement.setInt(14, blueprintUUID);
|
||||||
//System.out.println(this.cs.get().toString());
|
preparedStatement.setFloat(15, w);
|
||||||
try {
|
preparedStatement.setFloat(16, rotY);
|
||||||
boolean work = execute();
|
preparedStatement.setString(17, shrineType);
|
||||||
if (work) {
|
|
||||||
ResultSet rs = this.callableStatement.get().getResultSet();
|
preparedStatement.execute();
|
||||||
while (rs.next()) {
|
|
||||||
addObject(list, rs);
|
ResultSet rs = preparedStatement.getResultSet();
|
||||||
}
|
|
||||||
rs.close();
|
while (rs.next())
|
||||||
} else {
|
addObject(shrineList, rs);
|
||||||
Logger.info("Shrine Creation Failed: " + this.callableStatement.get().toString());
|
|
||||||
return list; //city creation failure
|
while (preparedStatement.getMoreResults()) {
|
||||||
}
|
rs = preparedStatement.getResultSet();
|
||||||
while (this.callableStatement.get().getMoreResults()) {
|
|
||||||
ResultSet rs = this.callableStatement.get().getResultSet();
|
while (rs.next())
|
||||||
while (rs.next()) {
|
addObject(shrineList, rs);
|
||||||
addObject(list, rs);
|
|
||||||
}
|
|
||||||
rs.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} 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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user