Browse Source

Refactor to remove abstraction

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

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

@ -17,173 +17,228 @@ import engine.objects.City; @@ -17,173 +17,228 @@ import engine.objects.City;
import engine.objects.Zone;
import org.pmw.tinylog.Logger;
import java.net.UnknownHostException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
public class dbCityHandler extends dbHandlerBase {
public dbCityHandler() {
this.localClass = City.class;
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) {
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");
switch (type) {
case "zone":
Zone zone = new Zone(rs);
DbManager.addToCache(zone);
list.add(zone);
break;
case "building":
Building building = new Building(rs);
DbManager.addToCache(building);
list.add(building);
break;
case "city":
City city = new City(rs);
DbManager.addToCache(city);
list.add(city);
break;
}
}
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();
}
public City GET_CITY(final int cityId) {
City city = (City) DbManager.getFromCache(Enum.GameObjectType.City, cityId);
if (city != null)
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) {
prepareCallable("CALL city_SETPROP(?,?,?)");
setLong(1, (long) c.getObjectUUID());
setString(2, name);
setString(3, String.valueOf(new_value));
return getResult();
}
public String SET_PROPERTY(final City c, String name, Object new_value, Object old_value) {
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) {
prepareCallable("UPDATE `obj_city` SET `forceRename`=?"
+ " WHERE `UID` = ?");
setByte(1, (value == true) ? (byte) 1 : (byte) 0);
setInt(2, city.getObjectUUID());
return (executeUpdate() > 0);
}
public boolean updateOpenCity(City city, boolean value) {
prepareCallable("UPDATE `obj_city` SET `open`=?"
+ " WHERE `UID` = ?");
setByte(1, (value == true) ? (byte) 1 : (byte) 0);
setInt(2, city.getObjectUUID());
return (executeUpdate() > 0);
}
public boolean updateTOL(City city, int tolID) {
prepareCallable("UPDATE `obj_city` SET `treeOfLifeUUID`=?"
+ " WHERE `UID` = ?");
setInt(1,tolID);
setInt(2, city.getObjectUUID());
return (executeUpdate() > 0);
}
public boolean renameCity(City city, String name) {
prepareCallable("UPDATE `obj_city` SET `name`=?"
+ " WHERE `UID` = ?");
setString(1, name);
setInt(2, city.getObjectUUID());
return (executeUpdate() > 0);
}
public boolean updateSiegesWithstood(City city, int value) {
prepareCallable("UPDATE `obj_city` SET `siegesWithstood`=?"
+ " WHERE `UID` = ?");
setInt(1, value);
setInt(2, city.getObjectUUID());
return (executeUpdate() > 0);
}
public boolean updateRealmTaxDate(City city, LocalDateTime localDateTime) {
prepareCallable("UPDATE `obj_city` SET `realmTaxDate` =?"
+ " WHERE `UID` = ?");
setLocalDateTime(1, localDateTime);
setInt(2,city.getObjectUUID());
return (executeUpdate() > 0);
}
public dbCityHandler() {
this.localClass = City.class;
this.localObjectType = Enum.GameObjectType.valueOf(this.localClass.getSimpleName());
}
public static void addObject(ArrayList<AbstractGameObject> list, ResultSet rs) throws SQLException {
String type = rs.getString("type");
switch (type) {
case "zone":
Zone zone = new Zone(rs);
DbManager.addToCache(zone);
list.add(zone);
break;
case "building":
Building building = new Building(rs);
DbManager.addToCache(building);
list.add(building);
break;
case "city":
City city = new City(rs);
DbManager.addToCache(city);
list.add(city);
break;
}
}
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) {
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) {
City city = (City) DbManager.getFromCache(Enum.GameObjectType.City, cityId);
if (city != null)
return city;
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`.`UID`=?;")) {
preparedStatement.setLong(1, cityId);
ResultSet rs = preparedStatement.executeQuery();
city = (City) getObjectFromRs(rs);
} catch (SQLException e) {
Logger.error(e);
}
return city;
}
public boolean updateforceRename(City city, boolean value) {
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_city` SET `forceRename`=?"
+ " WHERE `UID` = ?")) {
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) {
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_city` SET `open`=?"
+ " WHERE `UID` = ?")) {
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) {
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_city` SET `treeOfLifeUUID`=?"
+ " WHERE `UID` = ?")) {
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) {
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_city` SET `name`=?"
+ " WHERE `UID` = ?")) {
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) {
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_city` SET `name`=?"
+ " WHERE `UID` = ?")) {
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) {
try (Connection connection = DbManager.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE `obj_city` SET `realmTaxDate` =?"
+ " WHERE `UID` = ?")) {
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; @@ -34,7 +34,6 @@ import engine.workthreads.DestroyCityThread;
import engine.workthreads.TransferCityThread;
import org.pmw.tinylog.Logger;
import java.net.UnknownHostException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
@ -98,9 +97,9 @@ public class City extends AbstractWorldObject { @@ -98,9 +97,9 @@ public class City extends AbstractWorldObject {
* ResultSet Constructor
*/
public City(ResultSet rs) throws SQLException, UnknownHostException {
public City(ResultSet rs) throws SQLException {
super(rs);
try{
try {
this.cityName = rs.getString("name");
this.motto = rs.getString("motto");
this.isNpc = rs.getByte("isNpc");

Loading…
Cancel
Save