From 5db5c90a8b1c2c32a7219968801660bd4fa2f9d4 Mon Sep 17 00:00:00 2001 From: ImmuState Date: Sat, 4 Jun 2022 13:14:49 -0700 Subject: [PATCH] Start a new forge (no checking for now). --- .../java/emu/grasscutter/data/GameData.java | 4 + .../ForgingManager/ActiveForgeData.java | 96 ++++++++++++------- .../ForgingManager/ForgingManager.java | 73 +++++++++++--- 3 files changed, 126 insertions(+), 47 deletions(-) diff --git a/src/main/java/emu/grasscutter/data/GameData.java b/src/main/java/emu/grasscutter/data/GameData.java index 9863afdee..8fc78cf81 100644 --- a/src/main/java/emu/grasscutter/data/GameData.java +++ b/src/main/java/emu/grasscutter/data/GameData.java @@ -368,4 +368,8 @@ public class GameData { public static Int2ObjectMap getQuestDataMap() { return questDataMap; } + + public static Int2ObjectMap getForgeDataMap() { + return forgeDataMap; + } } 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 60ef7a167..ccee76927 100644 --- a/src/main/java/emu/grasscutter/game/managers/ForgingManager/ActiveForgeData.java +++ b/src/main/java/emu/grasscutter/game/managers/ForgingManager/ActiveForgeData.java @@ -1,44 +1,76 @@ package emu.grasscutter.game.managers.ForgingManager; import dev.morphia.annotations.Entity; +import emu.grasscutter.utils.Utils; @Entity public class ActiveForgeData { - private int forgeId; - private int avatarId; + private int forgeId; + private int avatarId; + private int count; - private int startTime; - private int forgeTime; - // private int finishedCount; - // private int unfinishedCount; - // private int nextFinishTimestamp; - // private int totalFinishTimestamp; + private int startTime; + private int forgeTime; + // private int finishedCount; + // private int unfinishedCount; + // private int nextFinishTimestamp; + // private int totalFinishTimestamp; - public int getForgeId() { - return this.forgeId; - } - public void setForgeId(int value) { - this.forgeId = value; - } - public int getAvatarId() { - return this.avatarId; - } - public void setAvatarId(int value) { - this.avatarId = value; - } + public int getFinishedCount(int currentTime) { + int timeDelta = currentTime - this.startTime; + int finishedCount = (int)Math.floor(timeDelta / this.forgeTime); - public int getStartTime() { - return this.startTime; - } - public void setStartTime(int value) { - this.startTime = value; - } + return Math.min(finishedCount, this.count); + } - public int getForgeTime() { - return this.forgeTime; - } - public void setForgeTime(int value) { - this.forgeTime = value; - } + public int getUnfinishedCount(int currentTime) { + return this.count - this.getFinishedCount(currentTime); + } + + public int getNextFinishTimestamp(int currentTime) { + return + (currentTime >= this.getTotalFinishTimestamp()) + ? this.getTotalFinishTimestamp() + : (this.getFinishedCount(currentTime) * this.forgeTime + this.forgeTime + this.startTime); + } + + public int getTotalFinishTimestamp() { + return this.startTime + this.forgeTime * this.count; + } + + public int getForgeId() { + return this.forgeId; + } + public void setForgeId(int value) { + this.forgeId = value; + } + + public int getAvatarId() { + return this.avatarId; + } + public void setAvatarId(int value) { + this.avatarId = value; + } + + public int getCount() { + return count; + } + public void setCount(int value) { + this.count = value; + } + + public int getStartTime() { + return this.startTime; + } + public void setStartTime(int value) { + this.startTime = value; + } + + public int getForgeTime() { + return this.forgeTime; + } + public void setForgeTime(int value) { + this.forgeTime = value; + } } 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 d07f0d68f..8f16233ab 100644 --- a/src/main/java/emu/grasscutter/game/managers/ForgingManager/ForgingManager.java +++ b/src/main/java/emu/grasscutter/game/managers/ForgingManager/ForgingManager.java @@ -3,6 +3,9 @@ package emu.grasscutter.game.managers.ForgingManager; import java.util.HashMap; import java.util.Map; +import emu.grasscutter.Grasscutter; +import emu.grasscutter.data.GameData; +import emu.grasscutter.data.excels.ForgeData; import emu.grasscutter.game.inventory.GameItem; import emu.grasscutter.game.player.Player; import emu.grasscutter.net.proto.ForgeStartReqOuterClass; @@ -12,6 +15,9 @@ import emu.grasscutter.server.packet.send.PacketForgeDataNotify; import emu.grasscutter.server.packet.send.PacketForgeFormulaDataNotify; import emu.grasscutter.server.packet.send.PacketForgeGetQueueDataRsp; import emu.grasscutter.server.packet.send.PacketForgeStartRsp; +import emu.grasscutter.utils.Utils; +import emu.grasscutter.utils.ConfigContainer.Game; +import net.bytebuddy.dynamic.TypeResolutionStrategy.Active; public class ForgingManager { private final Player player; @@ -53,19 +59,25 @@ public class ForgingManager { } private Map determineCurrentForgeQueueData() { - // Dummy for now. - ForgeQueueData data = ForgeQueueData.newBuilder() - .setQueueId(1) - .setForgeId(11001) - .setFinishCount(2) - .setUnfinishCount(3) - .setNextFinishTimestamp(0) - .setNextFinishTimestamp(0) - .setAvatarId(0) - .build(); - Map res = new HashMap<>(); - res.put(1, data); + int currentTime = Utils.getCurrentSeconds(); + + // Create queue information for all active forges. + for (int i = 0; i < this.player.getActiveForges().size(); i++) { + ActiveForgeData activeForge = this.player.getActiveForges().get(i); + + ForgeQueueData data = ForgeQueueData.newBuilder() + .setQueueId(i + 1) + .setForgeId(activeForge.getForgeId()) + .setFinishCount(activeForge.getFinishedCount(currentTime)) + .setUnfinishCount(activeForge.getUnfinishedCount(currentTime)) + .setTotalFinishTimestamp(activeForge.getTotalFinishTimestamp()) + .setNextFinishTimestamp(activeForge.getNextFinishTimestamp(currentTime)) + .setAvatarId(activeForge.getAvatarId()) + .build(); + + res.put(i + 1, data); + } return res; } @@ -93,8 +105,39 @@ public class ForgingManager { Initiate forging process. **********/ public void startForging(ForgeStartReqOuterClass.ForgeStartReq req) { - // Dummy for now. - this.player.sendPacket(new PacketForgeStartRsp(Retcode.RET_SUCC)); - } + // Refuse if all queues are already full. + if (this.player.getActiveForges().size() >= this.determineNumberOfQueues()) { + this.player.sendPacket(new PacketForgeStartRsp(Retcode.RET_FORGE_QUEUE_FULL)); + return; + } + // Get the required forging information for the target item. + if (!GameData.getForgeDataMap().containsKey(req.getForgeId())) { + this.player.sendPacket(new PacketForgeStartRsp(Retcode.RET_FAIL)); //ToDo: Probably the wrong return code. + Grasscutter.getLogger().error("Missing forgeId"); + return; + } + + ForgeData forgeData = GameData.getForgeDataMap().get(req.getForgeId()); + + // Check if we have enough of each material. + // ToDo. + + // Consume material. + // ToDo. + + // Create and add active forge. + ActiveForgeData activeForge = new ActiveForgeData(); + activeForge.setForgeId(req.getForgeId()); + activeForge.setAvatarId(req.getAvatarId()); + activeForge.setCount(req.getForgeCount()); + activeForge.setStartTime(Utils.getCurrentSeconds()); + activeForge.setForgeTime(forgeData.getForgeTime()); + + this.player.getActiveForges().add(activeForge); + + // Done. + this.player.sendPacket(new PacketForgeStartRsp(Retcode.RET_SUCC)); + this.sendForgeDataNotify(); + } }