Browse Source

updated maintenance system

lakebane-master
FatBoy-DOTC 5 months ago
parent
commit
0145a7264d
  1. 267
      src/engine/gameManager/MaintenanceManager.java

267
src/engine/gameManager/MaintenanceManager.java

@ -5,171 +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.HashMap;
import java.util.concurrent.ConcurrentHashMap; 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 dailyMaintenance() {
Logger.info("Maintenance has started"); Logger.info("Maintenance has started");
// Run maintenance on player buildings // Run maintenance on player buildings
if (ConfigManager.MB_WORLD_MAINTENANCE.getValue().equalsIgnoreCase("true")) if (ConfigManager.MB_WORLD_MAINTENANCE.getValue().equalsIgnoreCase("true"))
processMaintenance(); processMaintenance();
else else
Logger.info("Maintenance Costings: DISABLED"); Logger.info("Maintenance Costings: DISABLED");
Logger.info("Maintenance has completed!"); Logger.info("Maintenance has completed!");
} }
public static void processMaintenance() { public static void processMaintenance() {
//create list of all cities //create list of all cities
ConcurrentHashMap<Integer, AbstractGameObject> worldCities = DbManager.getMap(Enum.GameObjectType.City); ConcurrentHashMap<Integer, AbstractGameObject> worldCities = DbManager.getMap(Enum.GameObjectType.City);
//loop all cities //loop all cities
for (AbstractGameObject ago : worldCities.values()) { for (AbstractGameObject ago : worldCities.values()) {
if (ago.getObjectType().equals(Enum.GameObjectType.City)) { if (ago.getObjectType().equals(Enum.GameObjectType.City)) {
City city = (City) ago; City city = (City) ago;
if(city == null || city.getIsNpcOwned() == 1) if(city == null || !city.getParent().isPlayerCity())
continue; continue;
Building tol = city.getTOL(); Building tol = city.getTOL();
if(tol == null) if(tol == null)
continue; continue;
LocalDateTime maintenanceDueDate = tol.maintDateTime.withHour(1).withMinute(0).withSecond(0);
if(tol.maintDateTime.toLocalDate().equals(LocalDateTime.now().toLocalDate()) || LocalDateTime.now().toLocalDate().isAfter(tol.maintDateTime.toLocalDate())){ LocalDateTime now = LocalDateTime.now();
// today is maintenance day if(now.isAfter(maintenanceDueDate))
processTolMaintenance(tol, city.getWarehouse()); processTolMaintenance(tol, city.getWarehouse());
}
} }
} }
} }
public static void processTolMaintenance(Building tol, Warehouse warehouse){ public static void processTolMaintenance(Building tol, Warehouse warehouse){
//create the required resource maintenance list if(tol == null)
HashMap<Integer,Integer> maintCosts = new HashMap<>(); return;
maintCosts.put(7,3000000); if(tol.getRank() == 8)
if(tol.getRank() == 8){ handleR8(tol,warehouse);
maintCosts.put(1580000,3000);//stone else
maintCosts.put(1580004,3000);//lumber handleNormal(tol,warehouse);
maintCosts.put(1580017,5);//galvor }
maintCosts.put(1580018,5);//wormwood public static void handleNormal(Building tol, Warehouse warehouse){
//handle r7 and lower ToL maintenance
int goldDue = 3000000;
//enough on strongbox alone to pay
if (tol.getStrongboxValue() >= goldDue) {
tol.maintDateTime = LocalDateTime.now().plusDays(7);
if(DbManager.BuildingQueries.updateMaintDate(tol)) {
tol.setStrongboxValue(tol.getStrongboxValue() - goldDue);
}
return;
}
int newStrongboxValue = tol.getStrongboxValue();
int newWarehouseGold = 0;
if(warehouse != null && warehouse.getResources().get(ItemBase.getItemBase(7)) != null) {
newWarehouseGold = warehouse.getResources().get(ItemBase.getItemBase(7));
} else{
//wasnt enough on strongbox and gold in warehouse is empty
tol.maintDateTime = LocalDateTime.now().plusDays(1);
if(DbManager.BuildingQueries.updateMaintDate(tol)) {
tol.destroyOrDerank(null);
}
return;
}
//some on strongbox to pay
if (tol.getStrongboxValue() > 0) {
newStrongboxValue = 0;
goldDue -= tol.getStrongboxValue();
}
if(newWarehouseGold < goldDue){
//not enough gold to pay, you miss maintenance
tol.maintDateTime = LocalDateTime.now().plusDays(1);
if(DbManager.BuildingQueries.updateMaintDate(tol)) {
tol.destroyOrDerank(null);
}
return;
}
newWarehouseGold -= goldDue;
tol.maintDateTime = LocalDateTime.now().plusDays(7);
if(DbManager.BuildingQueries.updateMaintDate(tol) && DbManager.WarehouseQueries.updateGold(warehouse,newWarehouseGold)) {
warehouse.getResources().put(ItemBase.getItemBase(7), newWarehouseGold);
tol.setStrongboxValue(newStrongboxValue);
}
}
public static void handleR8(Building tol, Warehouse warehouse){
//handle r8 ToL maintenance
//cannot pay r8 maintenance without a warehouse
if(warehouse == null && DbManager.BuildingQueries.updateMaintDate(tol)) {
tol.destroyOrDerank(null);
tol.maintDateTime = LocalDateTime.now().plusDays(1);
return;
}
//handle resource processing
int goldDue = 3000000;
int galvorDue = 5;
int wormwoodDue = 5;
int stoneDue = 5000;
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 { }else {
//not r8, only need to process gold value and we're done tol.maintDateTime = LocalDateTime.now().plusDays(1);
int maintenanceDue = maintCosts.get(7); if(DbManager.BuildingQueries.updateMaintDate(tol)) {
int strongboxGold = tol.getStrongboxValue(); tol.destroyOrDerank(null);
if (strongboxGold > 0) {
if (strongboxGold > maintenanceDue) {
//enough gold in strongbox to cover all of maintenance
maintenanceDue = 0;
strongboxGold -= maintenanceDue;
tol.setStrongboxValue(strongboxGold); //update strongbox value
setMaintDateTime(tol, LocalDateTime.now().plusDays(7)); //maintenance paid, set next maintenance date for 1 week from today
return; //maintenance is paid, all done
} else {
maintenanceDue -= strongboxGold;
strongboxGold = 0;
}
} }
//now we need to take the remaining maintenance from the warehouse return;
int warehouseGold = 0; }
if (warehouse != null) { if(warehouse.getResources().get(Warehouse.stoneIB) != null) {
warehouseGold = warehouse.getResources().get(ItemBase.getItemBase(7)); stoneWarehouse = warehouse.getResources().get(Warehouse.stoneIB);
}else {
tol.maintDateTime = LocalDateTime.now().plusDays(1);
if(DbManager.BuildingQueries.updateMaintDate(tol)) {
tol.destroyOrDerank(null);
}
return;
}
if(warehouse.getResources().get(Warehouse.wormwoodIB) != null) {
wormwoodWarehouse = warehouse.getResources().get(Warehouse.wormwoodIB);
}else {
tol.maintDateTime = LocalDateTime.now().plusDays(1);
if(DbManager.BuildingQueries.updateMaintDate(tol)) {
tol.destroyOrDerank(null);
}
return;
}
if(warehouse.getResources().get(Warehouse.lumberIB) != null) {
lumberWarehouse = warehouse.getResources().get(Warehouse.lumberIB);
}else {
tol.maintDateTime = LocalDateTime.now().plusDays(1);
if(DbManager.BuildingQueries.updateMaintDate(tol)) {
tol.destroyOrDerank(null);
} }
if (warehouseGold >= maintenanceDue) { return;
//we have enough gold to process maintenance }
tol.setStrongboxValue(strongboxGold); //update the strongbox now that we are sure maintenance is being paid boolean canPay = true;
warehouseGold -= maintenanceDue; if(goldStrongBox >= goldDue){
warehouse.getResources().put(ItemBase.getItemBase(7),warehouseGold); goldStrongBox -= goldDue;
DbManager.WarehouseQueries.updateGold(warehouse, warehouseGold); goldDue = 0;
setMaintDateTime(tol, LocalDateTime.now().plusDays(7)); //maintenance paid, set next maintenance date for 1 week from today }
//maintenance is paid, all done if (tol.getStrongboxValue() > 0) {
} else { goldStrongBox = 0;
//failed maintenance, derank asset goldDue -= tol.getStrongboxValue();
HandleMaintenanceDerank(tol); //handle derank or potential destruction of the city }
if(warehouse.getResources().get(Warehouse.goldIB) != null) {
goldWarehouse = warehouse.getResources().get(Warehouse.goldIB);
}else if(goldDue > 0){
tol.maintDateTime = LocalDateTime.now().plusDays(1);
if(DbManager.BuildingQueries.updateMaintDate(tol)) {
tol.destroyOrDerank(null);
} }
return; return;
} }
if(wormwoodDue > wormwoodWarehouse)
//handle r8 ToL maintenance here after the fact canPay = false;
boolean success = true; if(galvorDue > galvorWarehouse)
canPay = false;
int strongboxGold = tol.getStrongboxValue(); if(lumberDue > lumberWarehouse)
int maintenanceGoldRequired = maintCosts.get(7); canPay = false;
if(stoneDue > stoneWarehouse)
//remove any gold from strongbox to start paying maintenance canPay = false;
if(tol.getStrongboxValue() > 0){ if(goldDue > goldWarehouse)
if(tol.getStrongboxValue() > maintenanceGoldRequired){ canPay = false;
strongboxGold -= maintenanceGoldRequired; if(!canPay){
maintCosts.put(7,0); tol.maintDateTime = LocalDateTime.now().plusDays(1);
}else{ if(DbManager.BuildingQueries.updateMaintDate(tol)) {
maintCosts.put(7,maintCosts.get(7) - strongboxGold); tol.destroyOrDerank(null);
strongboxGold = 0;
} }
return;
} }
for(Integer ibId : maintCosts.keySet()){ tol.setStrongboxValue(goldStrongBox);
if (warehouse != null) { if(DbManager.WarehouseQueries.updateGold(warehouse,goldWarehouse - goldDue)){
if(warehouse.getResources().get(ItemBase.getItemBase(ibId)) == null) if(DbManager.WarehouseQueries.updateStone(warehouse,stoneWarehouse - stoneDue)){
success = false; //this resource is not in the warehouse, failed to pay maintenance if(DbManager.WarehouseQueries.updateLumber(warehouse,lumberWarehouse - lumberDue)){
if(DbManager.WarehouseQueries.updateGalvor(warehouse,galvorWarehouse - galvorDue)){
int resourceCount = warehouse.getResources().get(ItemBase.getItemBase(ibId)); if(DbManager.WarehouseQueries.updateWormwood(warehouse,wormwoodWarehouse - wormwoodDue)){
if(resourceCount < maintCosts.get(ibId)) tol.maintDateTime = LocalDateTime.now().plusDays(1);
success = false; //not enough of this type to pay maintenance, maintenance failed to pay if(DbManager.BuildingQueries.updateMaintDate(tol)) {
}else{ return;
success = false; //warehouse is null, cannot pay resource maintenance }
}
}
}
} }
} }
if(success == true){
//determined there is enough of each resourceType to withdraw maintenance
tol.setStrongboxValue(strongboxGold);
int goldLeft = warehouse.getResources().get(ItemBase.getItemBase(7)) - maintCosts.get(7);
int galvorLeft = warehouse.getResources().get(ItemBase.getItemBase(1580017)) - maintCosts.get(1580017);
int lumberLeft = warehouse.getResources().get(ItemBase.getItemBase(1580004)) - maintCosts.get(1580004);
int stoneLeft = warehouse.getResources().get(ItemBase.getItemBase(1580000)) - maintCosts.get(1580000);
int wormwoodLeft = warehouse.getResources().get(ItemBase.getItemBase(1580018)) - maintCosts.get(1580018);
DbManager.WarehouseQueries.updateGold(warehouse, goldLeft);
DbManager.WarehouseQueries.updateStone(warehouse, stoneLeft);
DbManager.WarehouseQueries.updateLumber(warehouse, lumberLeft);
DbManager.WarehouseQueries.updateGalvor(warehouse, galvorLeft);
DbManager.WarehouseQueries.updateWormwood(warehouse, wormwoodLeft);
warehouse.getResources().put(ItemBase.getItemBase(7), goldLeft);
warehouse.getResources().put(ItemBase.getItemBase(1580017), galvorLeft);
warehouse.getResources().put(ItemBase.getItemBase(1580004), lumberLeft);
warehouse.getResources().put(ItemBase.getItemBase(1580000), stoneLeft);
warehouse.getResources().put(ItemBase.getItemBase(1580018), wormwoodLeft);
setMaintDateTime(tol, LocalDateTime.now().plusDays(7)); //maintenance paid, set next maintenance date for 1 week from today
}else{
HandleMaintenanceDerank(tol);//handle derank or potential destruction of the city
}
}
public static void HandleMaintenanceDerank(Building tol){
setMaintDateTime(tol, LocalDateTime.now().plusDays(1)); //failed to pay maintenance, set next date for tomorrow
tol.destroyOrDerank(null);
} }
} }
Loading…
Cancel
Save