Public Repository for the Magicbane Shadowbane Emulator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
5.0 KiB

12 months ago
package engine.workthreads;
import engine.Enum;
import engine.InterestManagement.WorldGrid;
import engine.db.archive.DataWarehouse;
import engine.db.archive.MineRecord;
import engine.gameManager.BuildingManager;
import engine.gameManager.ChatManager;
import engine.net.DispatchMessage;
import engine.net.client.msg.chat.ChatSystemMsg;
import engine.objects.Building;
import engine.objects.Guild;
import engine.objects.Mine;
import engine.objects.PlayerCharacter;
import org.pmw.tinylog.Logger;
import java.time.LocalDateTime;
public class MineThread implements Runnable {
public MineThread(){
}
@Override
public void run() {
12 months ago
LocalDateTime nextPulse = LocalDateTime.now().withMinute(0).withSecond(0).withNano(0);
11 months ago
while (true) {
LocalDateTime now = LocalDateTime.now();
12 months ago
11 months ago
if (now.isAfter(nextPulse)) {
for (Mine mine : Mine.getMines()) {
// Reset mine at 4:00 AM
if (nextPulse.getHour() == 4 && nextPulse.getMinute() == 0) {
12 months ago
mine.wasClaimed = false;
mine.hasProduced = false;
}
11 months ago
Building mineTower = BuildingManager.getBuilding(mine.getBuildingID());
if (mineTower == null) {
12 months ago
continue;
11 months ago
}
12 months ago
11 months ago
int minute = mine.firstThirty ? 0 : 30;
LocalDateTime openTime = now.withHour(mine.liveTime).withMinute(minute).withSecond(0).withNano(0);
12 months ago
LocalDateTime closeTime = openTime.plusMinutes(29);
12 months ago
11 months ago
if (!mine.wasClaimed && !mine.wasOpened && now.isAfter(openTime.minusMinutes(1))) {
12 months ago
mineWindowOpen(mine);
continue;
}
11 months ago
if (mine.isActive && now.isAfter(closeTime)) {
if (mineTower.getRank() > 0 || (mineTower.getRank() < 1 && mine.lastClaimer != null)) {
mineWindowClose(mine);
}
12 months ago
}
}
nextPulse = nextPulse.plusMinutes(30);
12 months ago
}
11 months ago
try {
Thread.sleep(1000); // Avoid busy-waiting
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
12 months ago
}
}
public static void mineWindowOpen(Mine mine) {
mine.setActive(true);
ChatManager.chatSystemChannel(mine.getParentZone().getName() + "'s Mine is now Active!");
Logger.info(mine.getParentZone().getName() + "'s Mine is now Active!");
}
public static boolean mineWindowClose(Mine mine) {
// No need to end the window of a mine which never opened.
if (mine.isActive == false)
return false;
Building mineBuilding = BuildingManager.getBuildingFromCache(mine.getBuildingID());
if (mineBuilding == null) {
Logger.debug("Null mine building for Mine " + mine.getObjectUUID() + " Building " + mine.getBuildingID());
return false;
}
for(Integer id : mine._playerMemory ){
PlayerCharacter.getPlayerCharacter(id).ZergMultiplier = 1.0f;
}
for(Integer id : mine._recentMemory.keySet()){
PlayerCharacter.getPlayerCharacter(id).ZergMultiplier = 1.0f;
}
// Mine building still stands; nothing to do.
// We can early exit here.
if (mineBuilding.getRank() > 0) {
mine.setActive(false);
mine.lastClaimer = null;
return true;
}
// This mine does not have a valid claimer
// we will therefore set it to errant
// and keep the window open.
if (!Mine.validateClaimer(mine.lastClaimer)) {
mine.lastClaimer = null;
mine.updateGuildOwner(null);
mine.setActive(true);
return false;
}
//Update ownership to map
mine.guildName = mine.getOwningGuild().getName();
mine.guildTag = mine.getOwningGuild().getGuildTag();
Guild nation = mine.getOwningGuild().getNation();
mine.nationName = nation.getName();
mine.nationTag = nation.getGuildTag();
mineBuilding.rebuildMine(mine.capSize * 5000);
WorldGrid.updateObject(mineBuilding);
ChatSystemMsg chatMsg = new ChatSystemMsg(null, mine.lastClaimer.getName() + " has claimed the mine in " + mine.getParentZone().getName() + " for " + mine.getOwningGuild().getName() + ". The mine is no longer active.");
chatMsg.setMessageType(10);
chatMsg.setChannel(Enum.ChatChannelType.SYSTEM.getChannelID());
DispatchMessage.dispatchMsgToAll(chatMsg);
// Warehouse this claim event
MineRecord mineRecord = MineRecord.borrow(mine, mine.lastClaimer, Enum.RecordEventType.CAPTURE);
DataWarehouse.pushToWarehouse(mineRecord);
mineBuilding.setRank(mineBuilding.getRank());
mine.lastClaimer = null;
mine.setActive(false);
mine.wasClaimed = true;
return true;
}
}