Browse Source

Refactor to remove abstraction

master
MagicBot 2 years ago
parent
commit
4d08857de1
  1. 259
      src/engine/db/handlers/dbCityHandler.java
  2. 5
      src/engine/objects/City.java

259
src/engine/db/handlers/dbCityHandler.java

@ -17,9 +17,7 @@ import engine.objects.City;
import engine.objects.Zone; import engine.objects.Zone;
import org.pmw.tinylog.Logger; import org.pmw.tinylog.Logger;
import java.net.UnknownHostException; import java.sql.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
@ -30,54 +28,7 @@ public class dbCityHandler extends dbHandlerBase {
this.localObjectType = Enum.GameObjectType.valueOf(this.localClass.getSimpleName()); this.localObjectType = Enum.GameObjectType.valueOf(this.localClass.getSimpleName());
} }
public ArrayList<AbstractGameObject> CREATE_CITY(int ownerID, int parentZoneID, int realmID, float xCoord, float yCoord, float zCoord, float rotation, float W, String name, LocalDateTime established) { public static void addObject(ArrayList<AbstractGameObject> list, ResultSet rs) throws SQLException {
prepareCallable("CALL `city_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?,?,?)");
LocalDateTime upgradeTime = LocalDateTime.now().plusHours(2);
setLong(1, (long) ownerID); //objectUUID of owning player
setLong(2, (long) parentZoneID); //objectUUID of parent (continent) zone
setLong(3, (long) realmID); //objectUUID of realm city belongs in
setFloat(4, xCoord); //xOffset from parentZone center
setFloat(5, yCoord); //yOffset from parentZone center
setFloat(6, zCoord); //zOffset from parentZone center
setString(7, name); //city name
setLocalDateTime(8, established);
setFloat(9, rotation);
setFloat(10, W);
setLocalDateTime(11, upgradeTime);
ArrayList<AbstractGameObject> list = new ArrayList<>();
try {
boolean work = execute();
if (work) {
ResultSet rs = this.callableStatement.get().getResultSet();
while (rs.next()) {
addObject(list, rs);
}
rs.close();
} else {
Logger.info("City Placement Failed: " + this.callableStatement.get().toString());
return list; //city creation failure
}
while (this.callableStatement.get().getMoreResults()) {
ResultSet rs = this.callableStatement.get().getResultSet();
while (rs.next()) {
addObject(list, rs);
}
rs.close();
}
} catch (SQLException e) {
Logger.info("City Placement Failed, SQLException: " + this.callableStatement.get().toString() + e.toString());
return list; //city creation failure
} catch (UnknownHostException e) {
Logger.info("City Placement Failed, UnknownHostException: " + this.callableStatement.get().toString());
return list; //city creation failure
} finally {
closeCallable();
}
return list;
}
public static void addObject(ArrayList<AbstractGameObject> list, ResultSet rs) throws SQLException, UnknownHostException {
String type = rs.getString("type"); String type = rs.getString("type");
switch (type) { switch (type) {
case "zone": case "zone":
@ -98,92 +49,196 @@ public class dbCityHandler extends dbHandlerBase {
} }
} }
public ArrayList<AbstractGameObject> CREATE_CITY(int ownerID, int parentZoneID, int realmID, float xCoord, float yCoord, float zCoord, float rotation, float W, String name, LocalDateTime established) {
LocalDateTime upgradeTime = LocalDateTime.now().plusHours(2);
ArrayList<AbstractGameObject> objectList = new ArrayList<>();
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("CALL `city_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?,?,?)")) {
preparedStatement.setLong(1, ownerID); //objectUUID of owning player
preparedStatement.setLong(2, parentZoneID); //objectUUID of parent (continent) zone
preparedStatement.setLong(3, realmID); //objectUUID of realm city belongs in
preparedStatement.setFloat(4, xCoord); //xOffset from parentZone center
preparedStatement.setFloat(5, yCoord); //yOffset from parentZone center
preparedStatement.setFloat(6, zCoord); //zOffset from parentZone center
preparedStatement.setString(7, name); //city name
preparedStatement.setTimestamp(8, Timestamp.valueOf(established));
preparedStatement.setFloat(9, rotation);
preparedStatement.setFloat(10, W);
preparedStatement.setTimestamp(11, Timestamp.valueOf(upgradeTime));
boolean work = execute();
if (work) {
ResultSet rs = preparedStatement.getResultSet();
while (rs.next()) {
addObject(objectList, rs);
}
rs.close();
} else {
Logger.info("City Placement Failed: " + preparedStatement);
return objectList; //city creation failure
}
while (preparedStatement.getMoreResults()) {
ResultSet rs = preparedStatement.getResultSet();
while (rs.next()) {
addObject(objectList, rs);
}
rs.close();
}
} catch (SQLException e) {
Logger.error(e);
}
return objectList;
}
public ArrayList<City> GET_CITIES_BY_ZONE(final int objectUUID) { public ArrayList<City> GET_CITIES_BY_ZONE(final int objectUUID) {
prepareCallable("SELECT `obj_city`.*, `object`.`parent` FROM `obj_city` INNER JOIN `object` ON `object`.`UID` = `obj_city`.`UID` WHERE `object`.`parent`=?;");
setLong(1, (long) objectUUID);
return getObjectList(); ArrayList<City> cityList = new ArrayList<>();
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT `obj_city`.*, `object`.`parent` FROM `obj_city` INNER JOIN `object` ON `object`.`UID` = `obj_city`.`UID` WHERE `object`.`parent`=?;")) {
preparedStatement.setLong(1, objectUUID);
ResultSet rs = preparedStatement.executeQuery();
cityList = getObjectsFromRs(rs, 100);
} catch (SQLException e) {
Logger.error(e);
}
return cityList;
} }
public City GET_CITY(final int cityId) { public City GET_CITY(final int cityId) {
City city = (City) DbManager.getFromCache(Enum.GameObjectType.City, cityId); City city = (City) DbManager.getFromCache(Enum.GameObjectType.City, cityId);
if (city != null) if (city != null)
return city; return city;
prepareCallable("SELECT `obj_city`.*, `object`.`parent` FROM `obj_city` INNER JOIN `object` ON `object`.`UID` = `obj_city`.`UID` WHERE `object`.`UID`=?;");
setLong(1, (long) cityId);
city = (City) getObjectSingle(cityId);
return city;
}
public String SET_PROPERTY(final City c, String name, Object new_value) { try (Connection connection = DbManager.getConnection();
prepareCallable("CALL city_SETPROP(?,?,?)"); PreparedStatement preparedStatement = connection.prepareStatement("SELECT `obj_city`.*, `object`.`parent` FROM `obj_city` INNER JOIN `object` ON `object`.`UID` = `obj_city`.`UID` WHERE `object`.`UID`=?;")) {
setLong(1, (long) c.getObjectUUID());
setString(2, name); preparedStatement.setLong(1, cityId);
setString(3, String.valueOf(new_value));
return getResult(); ResultSet rs = preparedStatement.executeQuery();
city = (City) getObjectFromRs(rs);
} catch (SQLException e) {
Logger.error(e);
} }
public String SET_PROPERTY(final City c, String name, Object new_value, Object old_value) { return city;
prepareCallable("CALL city_GETSETPROP(?,?,?,?)");
setLong(1, (long) c.getObjectUUID());
setString(2, name);
setString(3, String.valueOf(new_value));
setString(4, String.valueOf(old_value));
return getResult();
} }
public boolean updateforceRename(City city, boolean value) { public boolean updateforceRename(City city, boolean value) {
prepareCallable("UPDATE `obj_city` SET `forceRename`=?" try (Connection connection = DbManager.getConnection();
+ " WHERE `UID` = ?"); PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_city` SET `forceRename`=?"
setByte(1, (value == true) ? (byte) 1 : (byte) 0); + " WHERE `UID` = ?")) {
setInt(2, city.getObjectUUID());
return (executeUpdate() > 0); preparedStatement.setByte(1, (value == true) ? (byte) 1 : (byte) 0);
preparedStatement.setInt(2, city.getObjectUUID());
return (preparedStatement.executeUpdate() > 0);
} catch (SQLException e) {
Logger.error(e);
return false;
}
} }
public boolean updateOpenCity(City city, boolean value) { public boolean updateOpenCity(City city, boolean value) {
prepareCallable("UPDATE `obj_city` SET `open`=?" try (Connection connection = DbManager.getConnection();
+ " WHERE `UID` = ?"); PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_city` SET `open`=?"
setByte(1, (value == true) ? (byte) 1 : (byte) 0); + " WHERE `UID` = ?")) {
setInt(2, city.getObjectUUID());
return (executeUpdate() > 0); preparedStatement.setByte(1, (value == true) ? (byte) 1 : (byte) 0);
preparedStatement.setInt(2, city.getObjectUUID());
return (preparedStatement.executeUpdate() > 0);
} catch (SQLException e) {
Logger.error(e);
return false;
}
} }
public boolean updateTOL(City city, int tolID) { public boolean updateTOL(City city, int tolID) {
prepareCallable("UPDATE `obj_city` SET `treeOfLifeUUID`=?" try (Connection connection = DbManager.getConnection();
+ " WHERE `UID` = ?"); PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_city` SET `treeOfLifeUUID`=?"
setInt(1,tolID); + " WHERE `UID` = ?")) {
setInt(2, city.getObjectUUID());
return (executeUpdate() > 0); preparedStatement.setInt(1, tolID);
preparedStatement.setInt(2, city.getObjectUUID());
return (preparedStatement.executeUpdate() > 0);
} catch (SQLException e) {
Logger.error(e);
return false;
}
} }
public boolean renameCity(City city, String name) { public boolean renameCity(City city, String name) {
prepareCallable("UPDATE `obj_city` SET `name`=?" try (Connection connection = DbManager.getConnection();
+ " WHERE `UID` = ?"); PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_city` SET `name`=?"
setString(1, name); + " WHERE `UID` = ?")) {
setInt(2, city.getObjectUUID());
return (executeUpdate() > 0); preparedStatement.setString(1, name);
preparedStatement.setInt(2, city.getObjectUUID());
return (preparedStatement.executeUpdate() > 0);
} catch (SQLException e) {
Logger.error(e);
return false;
}
} }
public boolean updateSiegesWithstood(City city, int value) { public boolean updateSiegesWithstood(City city, int value) {
prepareCallable("UPDATE `obj_city` SET `siegesWithstood`=?" try (Connection connection = DbManager.getConnection();
+ " WHERE `UID` = ?"); PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_city` SET `name`=?"
setInt(1, value); + " WHERE `UID` = ?")) {
setInt(2, city.getObjectUUID());
return (executeUpdate() > 0); preparedStatement.setInt(1, value);
preparedStatement.setInt(2, city.getObjectUUID());
return (preparedStatement.executeUpdate() > 0);
} catch (SQLException e) {
Logger.error(e);
return false;
}
} }
public boolean updateRealmTaxDate(City city, LocalDateTime localDateTime) { public boolean updateRealmTaxDate(City city, LocalDateTime localDateTime) {
prepareCallable("UPDATE `obj_city` SET `realmTaxDate` =?" try (Connection connection = DbManager.getConnection();
+ " WHERE `UID` = ?"); PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_city` SET `realmTaxDate` =?"
setLocalDateTime(1, localDateTime); + " WHERE `UID` = ?")) {
setInt(2,city.getObjectUUID());
return (executeUpdate() > 0); preparedStatement.setTimestamp(1, Timestamp.valueOf(localDateTime));
preparedStatement.setInt(2, city.getObjectUUID());
return (preparedStatement.executeUpdate() > 0);
} catch (SQLException e) {
Logger.error(e);
return false;
}
} }
} }

5
src/engine/objects/City.java

@ -34,7 +34,6 @@ import engine.workthreads.DestroyCityThread;
import engine.workthreads.TransferCityThread; import engine.workthreads.TransferCityThread;
import org.pmw.tinylog.Logger; import org.pmw.tinylog.Logger;
import java.net.UnknownHostException;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@ -98,9 +97,9 @@ public class City extends AbstractWorldObject {
* ResultSet Constructor * ResultSet Constructor
*/ */
public City(ResultSet rs) throws SQLException, UnknownHostException { public City(ResultSet rs) throws SQLException {
super(rs); super(rs);
try{ try {
this.cityName = rs.getString("name"); this.cityName = rs.getString("name");
this.motto = rs.getString("motto"); this.motto = rs.getString("motto");
this.isNpc = rs.getByte("isNpc"); this.isNpc = rs.getByte("isNpc");

Loading…
Cancel
Save