From 6672f750d952c1d33dcede495fdaf68dd24a4059 Mon Sep 17 00:00:00 2001 From: MagicBot Date: Thu, 19 Jan 2023 10:38:49 -0500 Subject: [PATCH] Initial refactor: deleted all old code and replaced with something sensible. Driven from the guild's mine hour instead of calculating dates. --- src/engine/devcmd/cmds/InfoCmd.java | 5 - src/engine/devcmd/cmds/setOpenDateCmd.java | 94 ---------------- src/engine/gameManager/DevCmdManager.java | 1 - src/engine/jobs/MineActiveJob.java | 71 ------------- src/engine/objects/Mine.java | 112 ++++---------------- src/engine/server/world/WorldServer.java | 6 -- src/engine/workthreads/HourlyJobThread.java | 21 ++-- 7 files changed, 34 insertions(+), 276 deletions(-) delete mode 100644 src/engine/devcmd/cmds/setOpenDateCmd.java delete mode 100644 src/engine/jobs/MineActiveJob.java diff --git a/src/engine/devcmd/cmds/InfoCmd.java b/src/engine/devcmd/cmds/InfoCmd.java index 354d4410..beb37c93 100644 --- a/src/engine/devcmd/cmds/InfoCmd.java +++ b/src/engine/devcmd/cmds/InfoCmd.java @@ -201,11 +201,6 @@ public class InfoCmd extends AbstractDevCmd { output+= newline; output+= "Production type: " +mine.getProduction().name(); - output+= newline; - output+= "Open Date: "+ ( mine.openDate).toString(); - - output+= newline; - output+= "Open Date: "+ (mine.openDate).toString(); } } output += newline; diff --git a/src/engine/devcmd/cmds/setOpenDateCmd.java b/src/engine/devcmd/cmds/setOpenDateCmd.java deleted file mode 100644 index a74e9857..00000000 --- a/src/engine/devcmd/cmds/setOpenDateCmd.java +++ /dev/null @@ -1,94 +0,0 @@ -// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ . -// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌· -// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀ -// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌ -// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀ -// Magicbane Emulator Project © 2013 - 2022 -// www.magicbane.com - - -package engine.devcmd.cmds; - -import engine.Enum.BuildingGroup; -import engine.Enum.GameObjectType; -import engine.devcmd.AbstractDevCmd; -import engine.gameManager.BuildingManager; -import engine.objects.*; - -public class setOpenDateCmd extends AbstractDevCmd { - - public setOpenDateCmd() { - super("minedate"); - } - - @Override - protected void _doCmd(PlayerCharacter pcSender, String[] words, - AbstractGameObject target) { - - - if (words[0].equalsIgnoreCase("list")){ - for (int buildingID : Mine.towerMap.keySet()){ - Building building = BuildingManager.getBuildingFromCache(buildingID); - if (building == null){ - this.throwbackError(pcSender, "null building for ID " + buildingID); - continue; - } - - Zone zone = building.getParentZone(); - - Zone parentZone = zone.getParent(); - - Mine mine = Mine.towerMap.get(buildingID); - this.throwbackInfo(pcSender, "Mine UUID : " + mine.getObjectUUID() + " Mine Type: " + zone.getName() + " Zone : " + parentZone.getName() - + " Open Date : " + mine.openDate); - } - - } - if (target == null){ - this.throwbackError(pcSender, "null target"); - return; - } - if (target.getObjectType().equals(GameObjectType.Building) == false){ - this.throwbackError(pcSender, "target must be object type building"); - return; - } - - Building building = (Building)target; - if (building.getBlueprint() == null){ - this.throwbackError(pcSender, "null blueprint"); - return; - } - - if (building.getBlueprint().getBuildingGroup().equals(BuildingGroup.MINE) == false){ - - this.throwbackError(pcSender, "target not mine"); - return; - } - - Mine mine = Mine.getMineFromTower(building.getObjectUUID()); - - if (mine == null){ - this.throwbackError(pcSender, "null mine"); - return; - } - int days = Integer.parseInt(words[0]); - int hours = Integer.parseInt(words[1]); - - mine.openDate = mine.openDate.plusDays(days).plusHours(hours); - - this.throwbackInfo(pcSender, "Mine Open Date Changed to " + mine.openDate.toString()); - } - - - @Override - protected String _getUsageString() { - return "' /bounds'"; - } - - @Override - protected String _getHelpString() { - return "Audits all the mobs in a zone."; - - } - -} diff --git a/src/engine/gameManager/DevCmdManager.java b/src/engine/gameManager/DevCmdManager.java index e74cf12f..0b9ad676 100644 --- a/src/engine/gameManager/DevCmdManager.java +++ b/src/engine/gameManager/DevCmdManager.java @@ -151,7 +151,6 @@ public enum DevCmdManager { DevCmdManager.registerDevCmd(new RegionCmd()); DevCmdManager.registerDevCmd(new SetMaintCmd()); DevCmdManager.registerDevCmd(new ApplyBonusCmd()); - DevCmdManager.registerDevCmd(new setOpenDateCmd()); DevCmdManager.registerDevCmd(new AuditFailedItemsCmd()); } diff --git a/src/engine/jobs/MineActiveJob.java b/src/engine/jobs/MineActiveJob.java deleted file mode 100644 index b6ce1453..00000000 --- a/src/engine/jobs/MineActiveJob.java +++ /dev/null @@ -1,71 +0,0 @@ -// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ . -// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌· -// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀ -// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌ -// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀ -// Magicbane Emulator Project © 2013 - 2022 -// www.magicbane.com - - - - - -// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ . -// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌· -// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀ -// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌ -// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀ -// Magicbane Emulator Project © 2013 - 2022 -// www.magicbane.com - - -package engine.jobs; - -import engine.job.AbstractJob; -import engine.objects.Mine; -import org.pmw.tinylog.Logger; - -import java.time.LocalDateTime; -import java.util.ArrayList; - -public class MineActiveJob extends AbstractJob { - - public MineActiveJob() { - super(); - } - - @Override - protected void doJob() { - ArrayList mines = Mine.getMines(); - LocalDateTime now = LocalDateTime.now(); - - for (Mine mine : mines) { - try { - - if (mine.getOwningGuild() == null){ - mine.handleStartMineWindow(); - Mine.setLastChange(System.currentTimeMillis()); - continue; - } - - //handle claimed mines - LocalDateTime mineWindow = mine.openDate.withMinute(0).withSecond(0).withNano(0); - if (mineWindow != null && now.plusMinutes(1).isAfter(mineWindow)) - if (!mine.getIsActive()) { - Logger.info("activating mine. " + mineWindow.getHour() + " , " + now.getHour()); - mine.handleStartMineWindow(); - Mine.setLastChange(System.currentTimeMillis()); - - }else{ - if (mine.handleEndMineWindow()){ - Logger.info("Deactivating mine. " + mineWindow.getHour() + " , " + now.getHour()); - Mine.setLastChange(System.currentTimeMillis()); - } - - } - }catch (Exception e) { - Logger.error( "mineID: " + mine.getObjectUUID() + e); - } - } - } -} diff --git a/src/engine/objects/Mine.java b/src/engine/objects/Mine.java index 97eeef61..1e25ab47 100644 --- a/src/engine/objects/Mine.java +++ b/src/engine/objects/Mine.java @@ -59,7 +59,6 @@ public class Mine extends AbstractGameObject { private int buildingID; private Zone parentZone; private MineProduction mineType; - public LocalDateTime openDate; public boolean dirtyMine = false; //flags 1: never been claimed (make active). @@ -139,12 +138,6 @@ public class Mine extends AbstractGameObject { this.lastClaimerID = 0; this.lastClaimerSessionID = null; - - Timestamp mineOpenDateTime = rs.getTimestamp("mine_openDate"); - - if (mineOpenDateTime != null) - this.openDate = mineOpenDateTime.toLocalDateTime(); - } public static void SendMineAttackMessage(Building mine){ @@ -170,30 +163,6 @@ public class Mine extends AbstractGameObject { ChatManager.chatNationInfo(mine.getGuild().getNation(), mine.getName() + " in " + mine.getParentZone().getParent().getName() + " is Under attack!"); } - private void setNextMineWindow() { - - int nextMineHour = MBServerStatics.MINE_EARLY_WINDOW;; - LocalDateTime nextOpenDate = LocalDateTime.now().withMinute(0).withSecond(0).withNano(0); - - // If errant use mine stays open. - - if (this.owningGuild == null || this.owningGuild.isErrant() == true) - return; - - // Use the new owners Mine WOO. - - nextMineHour = this.owningGuild.getMineTime(); - - if ((this.openDate.getHour() == 0 || this.openDate.getHour() == 24) && - (this.owningGuild.getMineTime() != 0 && this.owningGuild.getMineTime() != 24)) - nextOpenDate = nextOpenDate.withHour(nextMineHour); - else - nextOpenDate = nextOpenDate.withHour(nextMineHour).plusDays(1); - - DbManager.MineQueries.CHANGE_MINE_TIME(this, nextOpenDate); - this.openDate = nextOpenDate; - } - public static void loadAllMines() { try{ @@ -207,7 +176,6 @@ try{ for (Mine mine : serverMines) { Mine.mineMap.put(mine, mine.buildingID); Mine.towerMap.put(mine.buildingID, mine); - mine.initializeMineTime(); } }catch (Exception e){ @@ -218,48 +186,6 @@ try{ /* * Getters */ - private void initializeMineTime(){ - - - Guild nation = null; - - // If errant use mine stays open. - - if (this.owningGuild == null || this.owningGuild.isErrant() == true) { - - // Update mesh - - Building mineBuilding = BuildingManager.getBuildingFromCache(this.buildingID); - - if (mineBuilding == null){ - Logger.debug( "Null mine building " + this.getObjectUUID() +". Unable to Load Building with UID " +this.buildingID); - return; - } - - mineBuilding.healthMax = (float) 1; - mineBuilding.meshUUID = mineBuilding.getBlueprint().getMeshForRank(-1); - mineBuilding.setRank(-1); - mineBuilding.setCurrentHitPoints((float) 1); - return; - } - - - nation = this.owningGuild.getNation(); - - int mineTime = (nation != null && !nation.isErrant()) ? nation.getMineTime() : MBServerStatics.MINE_EARLY_WINDOW; - - this.openDate = this.openDate.withHour(mineTime).withMinute(0).withSecond(0).withNano(0); - - //Failed to Update Database, default mine time. - - if (!MineQueries.CHANGE_MINE_TIME(this, openDate)){ - Logger.info("Mine with UUID " + this.getObjectUUID() + " failed to set Mine Window. Defaulting to Earliest."); - openDate = openDate.withHour(MBServerStatics.MINE_EARLY_WINDOW).withMinute(0).withSecond(0).withNano(0); - this.openDate = openDate; - return; - } - - } public boolean changeProductionType(Resource resource){ if (!this.validForMine(resource)) @@ -354,11 +280,27 @@ try{ writer.putInt(mine.getModifiedProductionAmount()); //TODO calculate range penalty here writer.putInt(3600); //window in seconds - LocalDateTime mw = mine.openDate; + // Errant mines are currently open. Set time to now. + + LocalDateTime mineTime = LocalDateTime.now().withMinute(0).withSecond(0).withNano(0); + Guild mineOwnerGuild = mine.getOwningGuild(); - writer.putLocalDateTime(mw); - mw = mw.plusHours(1); - writer.putLocalDateTime(mw); + // Adjust the serialized mine time based upon whether + // the Guild's mine window has passed or not. + + if (mineOwnerGuild != null) { + + int guildWOO = mineOwnerGuild.getMineTime(); + LocalDateTime guildMineTime = mineTime.withHour(guildWOO); + + if (mineTime.isAfter(guildMineTime)) + mineTime = guildMineTime.plusDays(1); + else + mineTime = guildMineTime; + } + + writer.putLocalDateTime(mineTime); + writer.putLocalDateTime(mineTime.plusHours(1)); writer.put(mine.isActive ? (byte) 0x01 : (byte) 0x00); writer.putFloat(mine.latitude); @@ -570,18 +512,13 @@ try{ if (this.dirtyMine && this.lastClaimerID == 0 && (this.owningGuild == null || this.owningGuild.isErrant())) return false; this.setActive(false); - setNextMineWindow(); return true; } PlayerCharacter claimer = PlayerCharacter.getFromCache(this.lastClaimerID); - if (!validClaimer(claimer)){ - LocalDateTime resetTime = LocalDateTime.now().withMinute(0).withSecond(0).withNano(0); - this.openDate = resetTime; + if (!validClaimer(claimer)) return false; - } - // //verify the player hasn't logged out since claim @@ -590,12 +527,8 @@ try{ // if (!SessionManager.getSession(claimer).getSessionID().equals(this.lastClaimerSessionID)) // return false; - if (this.owningGuild == null || this.owningGuild.isErrant() || this.owningGuild.getNation().isErrant()){ - LocalDateTime resetTime = LocalDateTime.now().withMinute(0).withSecond(0).withNano(0); - this.openDate = resetTime; + if (this.owningGuild == null || this.owningGuild.isErrant() || this.owningGuild.getNation().isErrant()) return false; - } - //Update ownership to map @@ -605,7 +538,6 @@ try{ this.nationName = nation.getName(); this.nationTag = nation.getGuildTag(); - setNextMineWindow(); setLastChange(System.currentTimeMillis()); if (mineBuilding.getRank() < 1){ diff --git a/src/engine/server/world/WorldServer.java b/src/engine/server/world/WorldServer.java index c00c0315..6d3009d2 100644 --- a/src/engine/server/world/WorldServer.java +++ b/src/engine/server/world/WorldServer.java @@ -25,7 +25,6 @@ import engine.gameManager.*; import engine.job.JobContainer; import engine.job.JobScheduler; import engine.jobs.LogoutCharacterJob; -import engine.jobs.MineActiveJob; import engine.loot.LootManager; import engine.net.Dispatch; import engine.net.DispatchMessage; @@ -436,11 +435,6 @@ public class WorldServer { if (ZoneManager.getHotZone() != null) WorldServer.setLastHZChange(System.currentTimeMillis()); - //Start Mines. - - MineActiveJob maj = new MineActiveJob(); - maj.run(); - Logger.info("Starting Mobile AI FSM"); MobileFSMManager.getInstance(); diff --git a/src/engine/workthreads/HourlyJobThread.java b/src/engine/workthreads/HourlyJobThread.java index bce2f4f4..0928c284 100644 --- a/src/engine/workthreads/HourlyJobThread.java +++ b/src/engine/workthreads/HourlyJobThread.java @@ -56,28 +56,31 @@ public class HourlyJobThread implements Runnable { try { ArrayList mines = Mine.getMines(); - LocalDateTime now = LocalDateTime.now(); for (Mine mine : mines) { try { + // Open Errant Mines + if (mine.getOwningGuild() == null) { mine.handleStartMineWindow(); Mine.setLastChange(System.currentTimeMillis()); continue; } - //handle claimed mines - LocalDateTime mineWindow = mine.openDate.withMinute(0).withSecond(0).withNano(0); + // Open Mines with a current guild hour - if (mineWindow != null && now.plusMinutes(1).isAfter(mineWindow)) - if (!mine.getIsActive()) { - mine.handleStartMineWindow(); - Mine.setLastChange(System.currentTimeMillis()); + if (mine.getOwningGuild().getMineTime() == + LocalDateTime.now().getHour()) { + mine.handleStartMineWindow(); + Mine.setLastChange(System.currentTimeMillis()); + continue; + } - } - else if (mine.handleEndMineWindow()) + // Close all other mines + if (mine.handleEndMineWindow()) Mine.setLastChange(System.currentTimeMillis()); + } catch (Exception e) { Logger.error ("mineID: " + mine.getObjectUUID(), e.toString()); }