forked from MagicBane/Server
Refactor to remove abstraction
parent
7c57dcc73d
commit
a92ac8671b
|
|
@ -19,7 +19,6 @@ import engine.server.MBServerStatics;
|
||||||
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.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
|
|
@ -37,68 +36,71 @@ public class dbWarehouseHandler extends dbHandlerBase {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<AbstractGameObject> CREATE_WAREHOUSE( int parentZoneID, int OwnerUUID, String name, int meshUUID,
|
public static void addObject(ArrayList<AbstractGameObject> list, ResultSet rs) throws SQLException {
|
||||||
|
String type = rs.getString("type");
|
||||||
|
switch (type) {
|
||||||
|
case "building":
|
||||||
|
Building building = new Building(rs);
|
||||||
|
DbManager.addToCache(building);
|
||||||
|
list.add(building);
|
||||||
|
break;
|
||||||
|
case "warehouse":
|
||||||
|
Warehouse warehouse = new Warehouse(rs);
|
||||||
|
DbManager.addToCache(warehouse);
|
||||||
|
list.add(warehouse);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<AbstractGameObject> CREATE_WAREHOUSE(int parentZoneID, int OwnerUUID, String name, int meshUUID,
|
||||||
Vector3fImmutable location, float meshScale, int currentHP,
|
Vector3fImmutable location, float meshScale, int currentHP,
|
||||||
ProtectionState protectionState, int currentGold, int rank,
|
ProtectionState protectionState, int currentGold, int rank,
|
||||||
DateTime upgradeDate, int blueprintUUID, float w, float rotY) {
|
DateTime upgradeDate, int blueprintUUID, float w, float rotY) {
|
||||||
|
|
||||||
prepareCallable("CALL `WAREHOUSE_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,? ,? ,?, ?);");
|
ArrayList<AbstractGameObject> warehouseList = new ArrayList<>();
|
||||||
|
|
||||||
setInt(1, parentZoneID);
|
try (Connection connection = DbManager.getConnection();
|
||||||
setInt(2, OwnerUUID);
|
PreparedStatement preparedStatement = connection.prepareStatement("CALL `WAREHOUSE_CREATE`(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,? ,? ,?, ?);")) {
|
||||||
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
|
||||||
|
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();
|
|
||||||
if (work) {
|
preparedStatement.execute();
|
||||||
ResultSet rs = this.callableStatement.get().getResultSet();
|
ResultSet rs = preparedStatement.getResultSet();
|
||||||
while (rs.next()) {
|
|
||||||
addObject(list, rs);
|
while (rs.next())
|
||||||
}
|
addObject(warehouseList, rs);
|
||||||
rs.close();
|
|
||||||
} else {
|
while (preparedStatement.getMoreResults()) {
|
||||||
Logger.info("Warehouse Creation Failed: " + this.callableStatement.get().toString());
|
rs = preparedStatement.getResultSet();
|
||||||
return list; //city creation failure
|
|
||||||
}
|
while (rs.next())
|
||||||
while (this.callableStatement.get().getMoreResults()) {
|
addObject(warehouseList, rs);
|
||||||
ResultSet rs = this.callableStatement.get().getResultSet();
|
|
||||||
while (rs.next()) {
|
|
||||||
addObject(list, rs);
|
|
||||||
}
|
|
||||||
rs.close();
|
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
Logger.info("Warehouse Creation Failed, SQLException: " + this.callableStatement.get().toString() + e.toString());
|
Logger.error(e);
|
||||||
return list; //city creation failure
|
|
||||||
} catch (UnknownHostException e) {
|
|
||||||
Logger.info("Warehouse Creation Failed, UnknownHostException: " + this.callableStatement.get().toString());
|
|
||||||
return list; //city creation failure
|
|
||||||
} finally {
|
|
||||||
closeCallable();
|
|
||||||
}
|
}
|
||||||
return list;
|
|
||||||
|
|
||||||
|
return warehouseList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean updateLocks(final Warehouse wh, long locks) {
|
public boolean updateLocks(final Warehouse wh, long locks) {
|
||||||
|
|
@ -117,7 +119,7 @@ public class dbWarehouseHandler extends dbHandlerBase {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean updateGold(final Warehouse wh, int amount ) {
|
public boolean updateGold(final Warehouse wh, int amount) {
|
||||||
prepareCallable("UPDATE `obj_warehouse` SET `warehouse_gold`=? WHERE `UID` = ?");
|
prepareCallable("UPDATE `obj_warehouse` SET `warehouse_gold`=? WHERE `UID` = ?");
|
||||||
setInt(1, amount);
|
setInt(1, amount);
|
||||||
setInt(2, wh.getUID());
|
setInt(2, wh.getUID());
|
||||||
|
|
@ -268,7 +270,7 @@ public class dbWarehouseHandler extends dbHandlerBase {
|
||||||
return (executeUpdate() != 0);
|
return (executeUpdate() != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean updateWormwood(final Warehouse wh, int amount ) {
|
public boolean updateWormwood(final Warehouse wh, int amount) {
|
||||||
prepareCallable("UPDATE `obj_warehouse` SET `warehouse_wormwood`=? WHERE `UID` = ?");
|
prepareCallable("UPDATE `obj_warehouse` SET `warehouse_wormwood`=? WHERE `UID` = ?");
|
||||||
setInt(1, amount);
|
setInt(1, amount);
|
||||||
setInt(2, wh.getUID());
|
setInt(2, wh.getUID());
|
||||||
|
|
@ -300,7 +302,7 @@ public class dbWarehouseHandler extends dbHandlerBase {
|
||||||
return (executeUpdate() != 0);
|
return (executeUpdate() != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean CREATE_TRANSACTION(int warehouseBuildingID, GameObjectType targetType, int targetUUID, TransactionType transactionType,Resource resource, int amount,DateTime date){
|
public boolean CREATE_TRANSACTION(int warehouseBuildingID, GameObjectType targetType, int targetUUID, TransactionType transactionType, Resource resource, int amount, DateTime date) {
|
||||||
Transaction transactions = null;
|
Transaction transactions = null;
|
||||||
prepareCallable("INSERT INTO `dyn_warehouse_transactions` (`warehouseUID`, `targetType`,`targetUID`, `type`,`resource`,`amount`,`date` ) VALUES (?,?,?,?,?,?,?)");
|
prepareCallable("INSERT INTO `dyn_warehouse_transactions` (`warehouseUID`, `targetType`,`targetUID`, `type`,`resource`,`amount`,`date` ) VALUES (?,?,?,?,?,?,?)");
|
||||||
setLong(1, warehouseBuildingID);
|
setLong(1, warehouseBuildingID);
|
||||||
|
|
@ -308,27 +310,11 @@ public class dbWarehouseHandler extends dbHandlerBase {
|
||||||
setLong(3, targetUUID);
|
setLong(3, targetUUID);
|
||||||
setString(4, transactionType.name());
|
setString(4, transactionType.name());
|
||||||
setString(5, resource.name());
|
setString(5, resource.name());
|
||||||
setInt(6,amount);
|
setInt(6, amount);
|
||||||
setTimeStamp(7,date.getMillis());
|
setTimeStamp(7, date.getMillis());
|
||||||
return (executeUpdate() != 0);
|
return (executeUpdate() != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void addObject(ArrayList<AbstractGameObject> list, ResultSet rs) throws SQLException, UnknownHostException {
|
|
||||||
String type = rs.getString("type");
|
|
||||||
switch (type) {
|
|
||||||
case "building":
|
|
||||||
Building building = new Building(rs);
|
|
||||||
DbManager.addToCache(building);
|
|
||||||
list.add(building);
|
|
||||||
break;
|
|
||||||
case "warehouse":
|
|
||||||
Warehouse warehouse = new Warehouse(rs);
|
|
||||||
DbManager.addToCache(warehouse);
|
|
||||||
list.add(warehouse);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayList<Transaction> GET_TRANSACTIONS_FOR_WAREHOUSE(final int warehouseUUID) {
|
public ArrayList<Transaction> GET_TRANSACTIONS_FOR_WAREHOUSE(final int warehouseUUID) {
|
||||||
ArrayList<Transaction> transactionsList = new ArrayList<>();
|
ArrayList<Transaction> transactionsList = new ArrayList<>();
|
||||||
prepareCallable("SELECT * FROM dyn_warehouse_transactions WHERE `warehouseUID` = ?;");
|
prepareCallable("SELECT * FROM dyn_warehouse_transactions WHERE `warehouseUID` = ?;");
|
||||||
|
|
@ -343,7 +329,7 @@ public class dbWarehouseHandler extends dbHandlerBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
Logger.error( e.getErrorCode() + ' ' + e.getMessage(), e);
|
Logger.error(e.getErrorCode() + ' ' + e.getMessage(), e);
|
||||||
} finally {
|
} finally {
|
||||||
closeCallable();
|
closeCallable();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue