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