Browse Source

updated maintenance system

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

275
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 {
tol.maintDateTime = LocalDateTime.now().plusDays(1);
if(DbManager.BuildingQueries.updateMaintDate(tol)) {
tol.destroyOrDerank(null);
}
return;
}
if(warehouse.getResources().get(Warehouse.stoneIB) != null) {
stoneWarehouse = warehouse.getResources().get(Warehouse.stoneIB);
}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) { return;
//enough gold in strongbox to cover all of maintenance }
maintenanceDue = 0; if(warehouse.getResources().get(Warehouse.wormwoodIB) != null) {
strongboxGold -= maintenanceDue; wormwoodWarehouse = warehouse.getResources().get(Warehouse.wormwoodIB);
tol.setStrongboxValue(strongboxGold); //update strongbox value }else {
setMaintDateTime(tol, LocalDateTime.now().plusDays(7)); //maintenance paid, set next maintenance date for 1 week from today tol.maintDateTime = LocalDateTime.now().plusDays(1);
return; //maintenance is paid, all done if(DbManager.BuildingQueries.updateMaintDate(tol)) {
} else { tol.destroyOrDerank(null);
maintenanceDue -= strongboxGold; }
strongboxGold = 0; return;
} }
} if(warehouse.getResources().get(Warehouse.lumberIB) != null) {
//now we need to take the remaining maintenance from the warehouse lumberWarehouse = warehouse.getResources().get(Warehouse.lumberIB);
int warehouseGold = 0; }else {
if (warehouse != null) { tol.maintDateTime = LocalDateTime.now().plusDays(1);
warehouseGold = warehouse.getResources().get(ItemBase.getItemBase(7)); if(DbManager.BuildingQueries.updateMaintDate(tol)) {
} tol.destroyOrDerank(null);
if (warehouseGold >= maintenanceDue) {
//we have enough gold to process maintenance
tol.setStrongboxValue(strongboxGold); //update the strongbox now that we are sure maintenance is being paid
warehouseGold -= maintenanceDue;
warehouse.getResources().put(ItemBase.getItemBase(7),warehouseGold);
DbManager.WarehouseQueries.updateGold(warehouse, warehouseGold);
setMaintDateTime(tol, LocalDateTime.now().plusDays(7)); //maintenance paid, set next maintenance date for 1 week from today
//maintenance is paid, all done
} else {
//failed maintenance, derank asset
HandleMaintenanceDerank(tol); //handle derank or potential destruction of the city
} }
return; return;
} }
boolean canPay = true;
//handle r8 ToL maintenance here after the fact if(goldStrongBox >= goldDue){
boolean success = true; goldStrongBox -= goldDue;
goldDue = 0;
int strongboxGold = tol.getStrongboxValue(); }
int maintenanceGoldRequired = maintCosts.get(7); if (tol.getStrongboxValue() > 0) {
goldStrongBox = 0;
//remove any gold from strongbox to start paying maintenance goldDue -= tol.getStrongboxValue();
if(tol.getStrongboxValue() > 0){ }
if(tol.getStrongboxValue() > maintenanceGoldRequired){ if(warehouse.getResources().get(Warehouse.goldIB) != null) {
strongboxGold -= maintenanceGoldRequired; goldWarehouse = warehouse.getResources().get(Warehouse.goldIB);
maintCosts.put(7,0); }else if(goldDue > 0){
}else{ tol.maintDateTime = LocalDateTime.now().plusDays(1);
maintCosts.put(7,maintCosts.get(7) - strongboxGold); if(DbManager.BuildingQueries.updateMaintDate(tol)) {
strongboxGold = 0;
}
}
for(Integer ibId : maintCosts.keySet()){
if (warehouse != null) {
if(warehouse.getResources().get(ItemBase.getItemBase(ibId)) == null)
success = false; //this resource is not in the warehouse, failed to pay maintenance
int resourceCount = warehouse.getResources().get(ItemBase.getItemBase(ibId));
if(resourceCount < maintCosts.get(ibId))
success = false; //not enough of this type to pay maintenance, maintenance failed to pay
}else{
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); tol.destroyOrDerank(null);
} }
return;
}
if(wormwoodDue > wormwoodWarehouse)
canPay = false;
if(galvorDue > galvorWarehouse)
canPay = false;
if(lumberDue > lumberWarehouse)
canPay = false;
if(stoneDue > stoneWarehouse)
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;
}
}
}
}
}
}
}
} }
Loading…
Cancel
Save