Browse Source

maintenance rewrite - needs completeing for r8

lakebane-master
FatBoy-DOTC 6 months ago
parent
commit
ab1e69a6ef
  1. 354
      src/engine/gameManager/MaintenanceManager.java

354
src/engine/gameManager/MaintenanceManager.java

@ -17,6 +17,8 @@ import org.pmw.tinylog.Logger;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
public enum MaintenanceManager { public enum MaintenanceManager {
@ -29,303 +31,113 @@ public enum MaintenanceManager {
} }
public static void processBuildingMaintenance() { public static void dailyMaintenance() {
ArrayList<AbstractGameObject> buildingList;
ArrayList<Building> maintList;
ArrayList<Building> derankList = new ArrayList<>();
Logger.info("Starting Maintenance on Player Buildings");
// Build list of buildings to apply maintenance on.
buildingList = new ArrayList(DbManager.getList(Enum.GameObjectType.City));
maintList = buildMaintList(buildingList);
// Deduct upkeep and build list of buildings
// which did not have funds available
for (Building building : maintList) {
if (chargeUpkeep(building) == false)
derankList.add(building);
}
// Reset maintenance dates for these buildings
for (Building building : maintList) { Logger.info("Maintenance has started");
if(derankList.contains(building) == false)
setMaintDateTime(building, LocalDateTime.now().plusDays(7));
else
setMaintDateTime(building, LocalDateTime.now().plusDays(1));
} // Run maintenance on player buildings
// Derak or destroy buildings that did not
// have funds available.
for (Building building : derankList) if (ConfigManager.MB_WORLD_MAINTENANCE.getValue().equalsIgnoreCase("true"))
building.destroyOrDerank(null); processMaintenance();
else
Logger.info("Maintenance Costings: DISABLED");
Logger.info("Structures: " + buildingList.size() + " Maint: " + maintList.size() + " Derank: " + derankList.size()); Logger.info("Maintenance has completed!");
} }
// Iterate over all buildings in game and apply exclusion rules public static void processMaintenance() {
// returning a list of building for which maintenance is due.
private static ArrayList<Building> buildMaintList(ArrayList<AbstractGameObject> buildingList) {
ArrayList<Building> maintList = new ArrayList<>();
for (AbstractGameObject gameObject : buildingList) {
Building building = ((City)gameObject).getTOL();//(Building) gameObject;
if(building == null)
continue;
// No maintenance on NPC owned buildings (Cache loaded) //create list of all cities
ConcurrentHashMap<Integer, AbstractGameObject> worldCities = DbManager.getMap(Enum.GameObjectType.City);
if (building.getProtectionState() == Enum.ProtectionState.NPC) //loop all cities
continue; for (AbstractGameObject ago : worldCities.values()) {
if (ago.getObjectType().equals(Enum.GameObjectType.City)) {
City city = (City) ago;
if(city == null)
continue;
// No maintenance on constructing meshes Building tol = city.getTOL();
if(tol == null)
continue;
if (building.getRank() < 1) if(tol.maintDateTime.toLocalDate().equals(LocalDateTime.now().toLocalDate())){
continue; // today is maintenance day
processTolMaintenance(tol, city.getWarehouse());
// No Maintenance on furniture }
if (building.parentBuildingID != 0)
continue;
// No Blueprint?
if (building.getBlueprint() == null) {
Logger.error("Blueprint missing for uuid: " + building.getObjectUUID());
continue;
} }
// No maintenance on banestones omfg
if (building.getBlueprint().getBuildingGroup().equals(Enum.BuildingGroup.BANESTONE))
continue;
// no maintenance on Mines omfg
if (building.getBlueprint().getBuildingGroup().equals(Enum.BuildingGroup.MINE))
continue;
// Null Maintenance date?
if (building.maintDateTime == null) {
Logger.error("Null maint date for building UUID: " + building.getObjectUUID());
continue;
}
// Maintenance date is in the future
if (building.maintDateTime.isAfter(LocalDateTime.now()))
continue;
//no maintenance if day of week doesnt match
if (LocalDateTime.now().getDayOfWeek().ordinal() != building.maintDateTime.getDayOfWeek().ordinal()) {
continue;
}
// Add building to maintenance queue
maintList.add(building);
} }
return maintList;
} }
// Method removes the appropriate amount of gold/resources from public static void processTolMaintenance(Building tol, Warehouse warehouse){
// a building according to it's maintenance schedule. True/False //create the required resource maintenance list
// is returned indicating if the building had enough funds to cover. HashMap<Integer,Integer> maintCosts = new HashMap<>();
maintCosts.put(7,3000000);
public static boolean chargeUpkeep(Building building) { if(tol.getRank() == 8){
maintCosts.put(1580000,3000);//stone
City city = null; maintCosts.put(1580004,3000);//lumber
Warehouse warehouse = null; maintCosts.put(1580017,5);//galvor
int maintCost = 0; maintCosts.put(1580018,5);//wormwood
int overDraft = 0; }else {
boolean hasFunds = false; //not r8, only need to process gold value and we're done
boolean hasResources = false; int maintenanceDue = maintCosts.get(7);
int resourceValue = 0; int strongboxGold = tol.getStrongboxValue();
if (strongboxGold > 0) {
city = building.getCity(); if (strongboxGold > maintenanceDue) {
//enough gold in strongbox to cover all of maintenance
if (city != null) maintenanceDue = 0;
warehouse = city.getWarehouse(); strongboxGold -= maintenanceDue;
tol.setStrongboxValue(strongboxGold); //update strongbox value
// Cache maintenance cost value setMaintDateTime(tol, tol.maintDateTime.plusDays(7)); //maintenance paid, set next maintenance date for 1 week from today
return; //maintenance is paid, all done
//maintCost = building.getMaintCost(); } else {
maintCost = 3000000; maintenanceDue -= strongboxGold;
strongboxGold = 0;
// Something went wrong. Missing buildinggroup from switch? }
if (maintCost == 0) {
Logger.error("chargeUpkeep", "Error retrieving rankcost for " + building.getName() + " uuid:" + building.getObjectUUID() + "buildinggroup:" + building.getBlueprint().getBuildingGroup().name());
// check if there is enough gold on the building
return true;
}
if (building.getStrongboxValue() >= maintCost)
hasFunds = true;
// If we cannot cover with just the strongbox
// see if there is a warehouse that will cover
// the overdraft for us.
if (hasFunds == false && (building.assetIsProtected() || building.getBlueprint().getBuildingGroup() == Enum.BuildingGroup.WAREHOUSE)) {
overDraft = maintCost - building.getStrongboxValue();
}
if ((overDraft > 0))
if ((building.getBlueprint().getBuildingGroup().equals(Enum.BuildingGroup.SHRINE) == false) &&
(warehouse != null) && building.assetIsProtected() == true &&
(warehouse.getResources().get(ItemBase.GOLD_ITEM_BASE)) >= overDraft) {
hasFunds = true;
} }
//now we need to take the remaining maintenance from the warehouse
// If this is an R8 tree, validate that we can int warehouseGold = 0;
// cover the resources required if (warehouse != null) {
warehouseGold = warehouse.getResources().get(ItemBase.getItemBase(7));
if (building.getRank() == 8) {
hasResources = true;
if (warehouse == null)
hasResources = false;
else {
resourceValue = warehouse.getResources().get(Warehouse.stoneIB);
if (resourceValue < 1500)
hasResources = false;
resourceValue = warehouse.getResources().get(Warehouse.lumberIB);
if (resourceValue < 1500)
hasResources = false;
resourceValue = warehouse.getResources().get(Warehouse.galvorIB);
if (resourceValue < 5)
hasResources = false;
resourceValue = warehouse.getResources().get(Warehouse.wormwoodIB);
if (resourceValue < 5)
hasResources = false;
} }
} if (warehouseGold >= maintenanceDue) {
// Validation completed but has failed. We can derank //we have enough gold to process maintenance
// the target building and early exit tol.setStrongboxValue(strongboxGold); //update the strongbox now that we are sure maintenance is being paid
warehouseGold -= maintenanceDue;
if ((hasFunds == false) || warehouse.getResources().put(ItemBase.getItemBase(7),warehouseGold);
((building.getRank() == 8) && !hasResources)) { DbManager.WarehouseQueries.updateGold(warehouse, warehouseGold);
setMaintDateTime(tol, tol.maintDateTime.plusDays(7)); //maintenance paid, set next maintenance date for 1 week from today
// Add cash back to strongbox for lost rank if the building isn't being destroyed //maintenance is paid, all done
// and it's not an R8 deranking
//if ((building.getRank() > 1) && (building.getRank() < 8)) {
// building.setStrongboxValue(building.getStrongboxValue() + building.getBlueprint().getRankCost(Math.min(building.getRank(), 7)));
//}
return false; // Early exit for having failed to meet maintenance
}
// Remove cash and resources
// withdraw what we can from the building
building.setStrongboxValue(building.getStrongboxValue() - (maintCost - overDraft));
// withdraw overdraft from the whorehouse
if (overDraft > 0) {
resourceValue = warehouse.getResources().get(Warehouse.goldIB);
if (DbManager.WarehouseQueries.updateGold(warehouse, resourceValue - overDraft) == true) {
warehouse.getResources().put(Warehouse.goldIB, resourceValue - overDraft);
warehouse.AddTransactionToWarehouse(Enum.GameObjectType.Building, building.getObjectUUID(), Enum.TransactionType.WITHDRAWL, Resource.GOLD, overDraft);
} else { } else {
Logger.error("gold update failed for warehouse of UUID:" + warehouse.getObjectUUID()); //failed maintenance, derank asset
return true; HandleMaintenanceDerank(tol); //handle derank or potential destruction of the city
} }
return;
} }
// Early exit as we're done if we're not an R8 tree //handle r8 ToL maintenance here after the fact
HashMap<Integer,Integer> updatedValues = new HashMap<>();
if (building.getRank() < 8) Boolean success = true;
return true; for(Integer ibId : maintCosts.keySet()){
if (warehouse != null) {
// Now for the resources if it's an R8 tree int resourceCount = warehouse.getResources().get(ItemBase.getItemBase(ibId));
if(resourceCount < maintCosts.get(ibId))
// Withdraw Stone success = false;
}else{
resourceValue = warehouse.getResources().get(Warehouse.stoneIB); success = false;
}
if (DbManager.WarehouseQueries.updateStone(warehouse, resourceValue - 1500) == true) {
warehouse.getResources().put(Warehouse.stoneIB, resourceValue - 1500);
warehouse.AddTransactionToWarehouse(Enum.GameObjectType.Building, building.getObjectUUID(), Enum.TransactionType.WITHDRAWL, Resource.STONE, 1500);
} else {
Logger.error("stone update failed for warehouse of UUID:" + warehouse.getObjectUUID());
return true;
}
// Withdraw Lumber
resourceValue = warehouse.getResources().get(Warehouse.lumberIB);
if (DbManager.WarehouseQueries.updateLumber(warehouse, resourceValue - 1500) == true) {
warehouse.getResources().put(Warehouse.lumberIB, resourceValue - 1500);
warehouse.AddTransactionToWarehouse(Enum.GameObjectType.Building, building.getObjectUUID(), Enum.TransactionType.WITHDRAWL, Resource.LUMBER, 1500);
} else {
Logger.error("lumber update failed for warehouse of UUID:" + warehouse.getObjectUUID());
return true;
}
// Withdraw Galvor
resourceValue = warehouse.getResources().get(Warehouse.galvorIB);
if (DbManager.WarehouseQueries.updateGalvor(warehouse, resourceValue - 5) == true) {
warehouse.getResources().put(Warehouse.galvorIB, resourceValue - 5);
warehouse.AddTransactionToWarehouse(Enum.GameObjectType.Building, building.getObjectUUID(), Enum.TransactionType.WITHDRAWL, Resource.GALVOR, 5);
} else {
Logger.error("galvor update failed for warehouse of UUID:" + warehouse.getObjectUUID());
return true;
} }
if(success = true){
//determined there is enough of each resourceType to withdraw maintenance
resourceValue = warehouse.getResources().get(Warehouse.wormwoodIB); }else{
HandleMaintenanceDerank(tol);//handle derank or potential destruction of the city
if (DbManager.WarehouseQueries.updateWormwood(warehouse, resourceValue - 5) == true) {
warehouse.getResources().put(Warehouse.wormwoodIB, resourceValue - 5);
warehouse.AddTransactionToWarehouse(Enum.GameObjectType.Building, building.getObjectUUID(), Enum.TransactionType.WITHDRAWL, Resource.WORMWOOD, 5);
} else {
Logger.error("wyrmwood update failed for warehouse of UUID:" + warehouse.getObjectUUID());
} }
return true;
} }
public static void dailyMaintenance() { public static void HandleMaintenanceDerank(Building tol){
setMaintDateTime(tol, tol.maintDateTime.plusDays(1)); //failed to pay maintenance, set next date for tomorrow
Logger.info("Maintenance has started");
// Run maintenance on player buildings
if (ConfigManager.MB_WORLD_MAINTENANCE.getValue().equalsIgnoreCase("true"))
processBuildingMaintenance();
else
Logger.info("Maintenance Costings: DISABLED");
Logger.info("Maintenance has completed!");
} }
} }

Loading…
Cancel
Save