Browse Source

updated maintenance system

lakebane-master
FatBoy-DOTC 5 months ago
parent
commit
f30a02a6d5
  1. 459
      src/engine/gameManager/MaintenanceManager.java

459
src/engine/gameManager/MaintenanceManager.java

@ -5,327 +5,206 @@
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀ // ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022 // Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com // www.magicbane.com
package engine.gameManager; package engine.gameManager;
// Defines static methods which comprise the magicbane // Defines static methods which comprise the magicbane
// building maintenance system. // building maintenance system.
import engine.Enum; import engine.Enum;
import engine.objects.*; import engine.objects.*;
import org.pmw.tinylog.Logger; 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 {
MAINTENANCEMANAGER; MAINTENANCEMANAGER;
public static void setMaintDateTime(Building building, LocalDateTime maintDate) { public static void setMaintDateTime(Building building, LocalDateTime maintDate) {
building.maintDateTime = maintDate; building.maintDateTime = maintDate;
DbManager.BuildingQueries.updateMaintDate(building); DbManager.BuildingQueries.updateMaintDate(building);
} }
public static void dailyMaintenance() {
public static void processBuildingMaintenance() { Logger.info("Maintenance has started");
// Run maintenance on player buildings
ArrayList<AbstractGameObject> buildingList; if (ConfigManager.MB_WORLD_MAINTENANCE.getValue().equalsIgnoreCase("true"))
ArrayList<Building> maintList; processMaintenance();
ArrayList<Building> derankList = new ArrayList<>(); else
Logger.info("Maintenance Costings: DISABLED");
Logger.info("Starting Maintenance on Player Buildings"); Logger.info("Maintenance has completed!");
}
// Build list of buildings to apply maintenance on. public static void processMaintenance() {
//create list of all cities
buildingList = new ArrayList(DbManager.getList(Enum.GameObjectType.City)); ConcurrentHashMap<Integer, AbstractGameObject> worldCities = DbManager.getMap(Enum.GameObjectType.City);
maintList = buildMaintList(buildingList); //loop all cities
for (AbstractGameObject ago : worldCities.values()) {
// Deduct upkeep and build list of buildings if (ago.getObjectType().equals(Enum.GameObjectType.City)) {
// which did not have funds available City city = (City) ago;
if(city == null || !city.getParent().isPlayerCity())
for (Building building : maintList) { continue;
Building tol = city.getTOL();
if (chargeUpkeep(building) == false) if(tol == null)
derankList.add(building); continue;
} LocalDateTime maintenanceDueDate = tol.maintDateTime.withHour(1).withMinute(0).withSecond(0);
// Reset maintenance dates for these buildings LocalDateTime now = LocalDateTime.now();
if(now.isAfter(maintenanceDueDate))
for (Building building : maintList) { processTolMaintenance(tol, city.getWarehouse());
if(derankList.contains(building) == false) }
setMaintDateTime(building, LocalDateTime.now().plusDays(7));
else
setMaintDateTime(building, LocalDateTime.now().plusDays(1));
} }
// Derak or destroy buildings that did not
// have funds available.
for (Building building : derankList)
building.destroyOrDerank(null);
Logger.info("Structures: " + buildingList.size() + " Maint: " + maintList.size() + " Derank: " + derankList.size());
} }
public static void processTolMaintenance(Building tol, Warehouse warehouse){
// Iterate over all buildings in game and apply exclusion rules if(tol == null)
// returning a list of building for which maintenance is due. return;
if(tol.getRank() == 8)
private static ArrayList<Building> buildMaintList(ArrayList<AbstractGameObject> buildingList) { handleR8(tol,warehouse);
else
ArrayList<Building> maintList = new ArrayList<>(); handleNormal(tol,warehouse);
}
for (AbstractGameObject gameObject : buildingList) { public static void handleNormal(Building tol, Warehouse warehouse){
//handle r7 and lower ToL maintenance
Building building = ((City)gameObject).getTOL();//(Building) gameObject; int goldDue = 3000000;
//enough on strongbox alone to pay
if(building == null) if (tol.getStrongboxValue() >= goldDue) {
continue; tol.maintDateTime = LocalDateTime.now().plusDays(7);
if(DbManager.BuildingQueries.updateMaintDate(tol)) {
// No maintenance on NPC owned buildings (Cache loaded) tol.setStrongboxValue(tol.getStrongboxValue() - goldDue);
if (building.getProtectionState() == Enum.ProtectionState.NPC)
continue;
// No maintenance on constructing meshes
if (building.getRank() < 1)
continue;
// No Maintenance on furniture
if (building.parentBuildingID != 0)
continue;
// No Blueprint?
if (building.getBlueprint() == null) {
Logger.error("Blueprint missing for uuid: " + building.getObjectUUID());
continue;
} }
return;
// No maintenance on banestones omfg }
int newStrongboxValue = tol.getStrongboxValue();
if (building.getBlueprint().getBuildingGroup().equals(Enum.BuildingGroup.BANESTONE)) int newWarehouseGold = 0;
continue; if(warehouse != null && warehouse.getResources().get(ItemBase.getItemBase(7)) != null) {
newWarehouseGold = warehouse.getResources().get(ItemBase.getItemBase(7));
// no maintenance on Mines omfg } else{
//wasnt enough on strongbox and gold in warehouse is empty
if (building.getBlueprint().getBuildingGroup().equals(Enum.BuildingGroup.MINE)) tol.maintDateTime = LocalDateTime.now().plusDays(1);
continue; if(DbManager.BuildingQueries.updateMaintDate(tol)) {
tol.destroyOrDerank(null);
// Null Maintenance date?
if (building.maintDateTime == null) {
Logger.error("Null maint date for building UUID: " + building.getObjectUUID());
continue;
} }
return;
// Maintenance date is in the future }
//some on strongbox to pay
if (building.maintDateTime.isAfter(LocalDateTime.now())) if (tol.getStrongboxValue() > 0) {
continue; newStrongboxValue = 0;
goldDue -= tol.getStrongboxValue();
}
//no maintenance if day of week doesnt match if(newWarehouseGold < goldDue){
if (LocalDateTime.now().getDayOfWeek().ordinal() != building.maintDateTime.getDayOfWeek().ordinal()) { //not enough gold to pay, you miss maintenance
continue; tol.maintDateTime = LocalDateTime.now().plusDays(1);
if(DbManager.BuildingQueries.updateMaintDate(tol)) {
tol.destroyOrDerank(null);
} }
// Add building to maintenance queue return;
maintList.add(building);
} }
newWarehouseGold -= goldDue;
return maintList; tol.maintDateTime = LocalDateTime.now().plusDays(7);
} if(DbManager.BuildingQueries.updateMaintDate(tol) && DbManager.WarehouseQueries.updateGold(warehouse,newWarehouseGold)) {
warehouse.getResources().put(ItemBase.getItemBase(7), newWarehouseGold);
// Method removes the appropriate amount of gold/resources from tol.setStrongboxValue(newStrongboxValue);
// a building according to it's maintenance schedule. True/False
// is returned indicating if the building had enough funds to cover.
public static boolean chargeUpkeep(Building building) {
City city = null;
Warehouse warehouse = null;
int maintCost = 0;
int overDraft = 0;
boolean hasFunds = false;
boolean hasResources = false;
int resourceValue = 0;
city = building.getCity();
if (city != null)
warehouse = city.getWarehouse();
// Cache maintenance cost value
//maintCost = building.getMaintCost();
maintCost = 3000000;
// 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) public static void handleR8(Building tol, Warehouse warehouse){
hasFunds = true; //handle r8 ToL maintenance
//cannot pay r8 maintenance without a warehouse
// If we cannot cover with just the strongbox if(warehouse == null && DbManager.BuildingQueries.updateMaintDate(tol)) {
// see if there is a warehouse that will cover tol.destroyOrDerank(null);
// the overdraft for us. tol.maintDateTime = LocalDateTime.now().plusDays(1);
return;
if (hasFunds == false && (building.assetIsProtected() || building.getBlueprint().getBuildingGroup() == Enum.BuildingGroup.WAREHOUSE)) {
overDraft = maintCost - building.getStrongboxValue();
} }
//handle resource processing
if ((overDraft > 0)) int goldDue = 3000000;
if ((building.getBlueprint().getBuildingGroup().equals(Enum.BuildingGroup.SHRINE) == false) && int galvorDue = 5;
(warehouse != null) && building.assetIsProtected() == true && int wormwoodDue = 5;
(warehouse.getResources().get(ItemBase.GOLD_ITEM_BASE)) >= overDraft) { int stoneDue = 5000;
hasFunds = true; int lumberDue = 5000;
int goldStrongBox = tol.getStrongboxValue();
int goldWarehouse = 0;
int galvorWarehouse;
int wormwoodWarehouse;
int stoneWarehouse;
int lumberWarehouse;
if(warehouse.getResources().get(Warehouse.galvorIB) != null) {
galvorWarehouse = warehouse.getResources().get(Warehouse.galvorIB);
}else {
tol.maintDateTime = LocalDateTime.now().plusDays(1);
if(DbManager.BuildingQueries.updateMaintDate(tol)) {
tol.destroyOrDerank(null);
} }
return;
// If this is an R8 tree, validate that we can }
// cover the resources required if(warehouse.getResources().get(Warehouse.stoneIB) != null) {
stoneWarehouse = warehouse.getResources().get(Warehouse.stoneIB);
if (building.getRank() == 8) { }else {
tol.maintDateTime = LocalDateTime.now().plusDays(1);
hasResources = true; if(DbManager.BuildingQueries.updateMaintDate(tol)) {
tol.destroyOrDerank(null);
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;
} }
return;
} }
// Validation completed but has failed. We can derank if(warehouse.getResources().get(Warehouse.wormwoodIB) != null) {
// the target building and early exit wormwoodWarehouse = warehouse.getResources().get(Warehouse.wormwoodIB);
}else {
if ((hasFunds == false) || tol.maintDateTime = LocalDateTime.now().plusDays(1);
((building.getRank() == 8) && !hasResources)) { if(DbManager.BuildingQueries.updateMaintDate(tol)) {
tol.destroyOrDerank(null);
// Add cash back to strongbox for lost rank if the building isn't being destroyed }
// and it's not an R8 deranking return;
//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
} }
if(warehouse.getResources().get(Warehouse.lumberIB) != null) {
// Remove cash and resources lumberWarehouse = warehouse.getResources().get(Warehouse.lumberIB);
}else {
// withdraw what we can from the building tol.maintDateTime = LocalDateTime.now().plusDays(1);
if(DbManager.BuildingQueries.updateMaintDate(tol)) {
building.setStrongboxValue(building.getStrongboxValue() - (maintCost - overDraft)); tol.destroyOrDerank(null);
// 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 {
Logger.error("gold update failed for warehouse of UUID:" + warehouse.getObjectUUID());
return true;
} }
return;
} }
boolean canPay = true;
// Early exit as we're done if we're not an R8 tree if(goldStrongBox >= goldDue){
goldStrongBox -= goldDue;
if (building.getRank() < 8) goldDue = 0;
return true;
// Now for the resources if it's an R8 tree
// Withdraw Stone
resourceValue = warehouse.getResources().get(Warehouse.stoneIB);
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;
} }
if (tol.getStrongboxValue() > 0) {
// Withdraw Lumber goldStrongBox = 0;
goldDue -= tol.getStrongboxValue();
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;
} }
if(warehouse.getResources().get(Warehouse.goldIB) != null) {
// Withdraw Galvor goldWarehouse = warehouse.getResources().get(Warehouse.goldIB);
}else if(goldDue > 0){
resourceValue = warehouse.getResources().get(Warehouse.galvorIB); tol.maintDateTime = LocalDateTime.now().plusDays(1);
if(DbManager.BuildingQueries.updateMaintDate(tol)) {
if (DbManager.WarehouseQueries.updateGalvor(warehouse, resourceValue - 5) == true) { tol.destroyOrDerank(null);
warehouse.getResources().put(Warehouse.galvorIB, resourceValue - 5); }
warehouse.AddTransactionToWarehouse(Enum.GameObjectType.Building, building.getObjectUUID(), Enum.TransactionType.WITHDRAWL, Resource.GALVOR, 5); return;
} else {
Logger.error("galvor update failed for warehouse of UUID:" + warehouse.getObjectUUID());
return true;
} }
if(wormwoodDue > wormwoodWarehouse)
resourceValue = warehouse.getResources().get(Warehouse.wormwoodIB); canPay = false;
if(galvorDue > galvorWarehouse)
if (DbManager.WarehouseQueries.updateWormwood(warehouse, resourceValue - 5) == true) { canPay = false;
warehouse.getResources().put(Warehouse.wormwoodIB, resourceValue - 5); if(lumberDue > lumberWarehouse)
warehouse.AddTransactionToWarehouse(Enum.GameObjectType.Building, building.getObjectUUID(), Enum.TransactionType.WITHDRAWL, Resource.WORMWOOD, 5); canPay = false;
} else { if(stoneDue > stoneWarehouse)
Logger.error("wyrmwood update failed for warehouse of UUID:" + warehouse.getObjectUUID()); canPay = false;
if(goldDue > goldWarehouse)
canPay = false;
if(!canPay){
tol.maintDateTime = LocalDateTime.now().plusDays(1);
if(DbManager.BuildingQueries.updateMaintDate(tol)) {
tol.destroyOrDerank(null);
}
return;
}
tol.setStrongboxValue(goldStrongBox);
if(DbManager.WarehouseQueries.updateGold(warehouse,goldWarehouse - goldDue)){
if(DbManager.WarehouseQueries.updateStone(warehouse,stoneWarehouse - stoneDue)){
if(DbManager.WarehouseQueries.updateLumber(warehouse,lumberWarehouse - lumberDue)){
if(DbManager.WarehouseQueries.updateGalvor(warehouse,galvorWarehouse - galvorDue)){
if(DbManager.WarehouseQueries.updateWormwood(warehouse,wormwoodWarehouse - wormwoodDue)){
tol.maintDateTime = LocalDateTime.now().plusDays(1);
if(DbManager.BuildingQueries.updateMaintDate(tol)) {
return;
}
}
}
}
}
} }
return true;
}
public static void dailyMaintenance() {
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