Send periodic notifications to the client for ongoing forges.

This commit is contained in:
ImmuState 2022-06-06 16:55:29 -07:00 committed by Melledy
parent 36e7e028f7
commit e30516d698
3 changed files with 55 additions and 4 deletions

View File

@ -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;
}
}

View File

@ -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));
}
}

View File

@ -1194,6 +1194,9 @@ public class Player {
this.save();
this.sendPacket(new PacketAvatarExpeditionDataNotify(this));
}
// Send updated forge queue data, if necessary.
this.getForgingManager().sendPlayerForgingUpdate();
}