From e30516d698d951bcc8fff08c9a7afa071cc3c4af Mon Sep 17 00:00:00 2001 From: ImmuState Date: Mon, 6 Jun 2022 16:55:29 -0700 Subject: [PATCH] Send periodic notifications to the client for ongoing forges. --- .../ForgingManager/ActiveForgeData.java | 24 +++++++++++--- .../ForgingManager/ForgingManager.java | 32 +++++++++++++++++++ .../emu/grasscutter/game/player/Player.java | 3 ++ 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/main/java/emu/grasscutter/game/managers/ForgingManager/ActiveForgeData.java b/src/main/java/emu/grasscutter/game/managers/ForgingManager/ActiveForgeData.java index ccee76927..7d11d1985 100644 --- a/src/main/java/emu/grasscutter/game/managers/ForgingManager/ActiveForgeData.java +++ b/src/main/java/emu/grasscutter/game/managers/ForgingManager/ActiveForgeData.java @@ -11,11 +11,9 @@ public class ActiveForgeData { private int startTime; private int forgeTime; - // private int finishedCount; - // private int unfinishedCount; - // private int nextFinishTimestamp; - // private int totalFinishTimestamp; + private int lastUnfinishedCount; + private boolean changed; public int getFinishedCount(int currentTime) { int timeDelta = currentTime - this.startTime; @@ -73,4 +71,22 @@ public class ActiveForgeData { public void setForgeTime(int value) { this.forgeTime = value; } + + public boolean isChanged() { + return this.changed; + } + public void setChanged(boolean value) { + this.changed = value; + } + + public boolean updateChanged(int currentTime) { + int currentUnfinished = this.getUnfinishedCount(currentTime); + + if (currentUnfinished != this.lastUnfinishedCount) { + this.changed = true; + this.lastUnfinishedCount = currentUnfinished; + } + + return this.changed; + } } diff --git a/src/main/java/emu/grasscutter/game/managers/ForgingManager/ForgingManager.java b/src/main/java/emu/grasscutter/game/managers/ForgingManager/ForgingManager.java index f74f53ba6..1baa88ac9 100644 --- a/src/main/java/emu/grasscutter/game/managers/ForgingManager/ForgingManager.java +++ b/src/main/java/emu/grasscutter/game/managers/ForgingManager/ForgingManager.java @@ -5,6 +5,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import com.mongodb.QueryBuilder; + import emu.grasscutter.Grasscutter; import emu.grasscutter.data.GameData; import emu.grasscutter.data.common.ItemParamData; @@ -289,4 +291,34 @@ public class ForgingManager { break; //Should never happen. } } + + /********** + Periodic forging updates. + **********/ + public void sendPlayerForgingUpdate() { + int currentTime = Utils.getCurrentSeconds(); + + // Determine if sending an update is necessary. + // We only send an update if there are forges in the forge queue + // that have changed since the last notification. + if (this.player.getActiveForges().size() <= 0) { + return; + } + + boolean hasChanges = this.player.getActiveForges().stream() + .filter(forge -> forge.updateChanged(currentTime)) + .findAny() + .isPresent(); + + if (!hasChanges) { + return; + } + + // Send notification. + this.sendForgeQueueDataNotify(); + + // Reset changed flags. + this.player.getActiveForges().stream() + .forEach(forge -> forge.setChanged(false)); + } } diff --git a/src/main/java/emu/grasscutter/game/player/Player.java b/src/main/java/emu/grasscutter/game/player/Player.java index 08b4ae274..3b90a45a4 100644 --- a/src/main/java/emu/grasscutter/game/player/Player.java +++ b/src/main/java/emu/grasscutter/game/player/Player.java @@ -1194,6 +1194,9 @@ public class Player { this.save(); this.sendPacket(new PacketAvatarExpeditionDataNotify(this)); } + + // Send updated forge queue data, if necessary. + this.getForgingManager().sendPlayerForgingUpdate(); }