-
Notifications
You must be signed in to change notification settings - Fork 0
Charge node upkeep daily and log manual node toggles #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/main/java/net/tfminecraft/dowsing/utils/NodeToggleLog.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package net.tfminecraft.dowsing.utils; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.StandardOpenOption; | ||
| import java.time.LocalDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.Locale; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
|
|
||
| import org.bukkit.Location; | ||
| import org.bukkit.entity.Player; | ||
|
|
||
| import net.tfminecraft.dowsing.DowsingMain; | ||
| import net.tfminecraft.dowsing.objects.Node; | ||
| import net.tfminecraft.simplefactions.managers.FactionManager; | ||
|
|
||
| /** | ||
| * Upkeep is charged for the nodes active at the new day, so switching a node off just | ||
| * before it and on again after skips a day's upkeep. Every manual toggle is recorded with | ||
| * the time left until the new day, and one close to it is logged as a warning. | ||
| */ | ||
| public final class NodeToggleLog { | ||
| public static final int WARN_SECONDS_BEFORE_NEW_DAY = 60 * 60; | ||
| private static final String FILE_NAME = "node-toggles.log"; | ||
| private static final DateTimeFormatter TIMESTAMP = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); | ||
|
|
||
| private NodeToggleLog() {} | ||
|
|
||
| public static void activated(Player player, Node node) { | ||
| record(player, node, "activated"); | ||
| } | ||
|
|
||
| public static void deactivated(Player player, Node node) { | ||
| record(player, node, "deactivated"); | ||
| } | ||
|
|
||
| private static void record(Player player, Node node, String action) { | ||
| if (player == null || node == null) return; | ||
| int secondsUntilNewDay = FactionManager.getSecondsUntilNewDay(); | ||
| String line = format(player.getName(), action, node, secondsUntilNewDay); | ||
| Logger logger = DowsingMain.plugin.getLogger(); | ||
| if (secondsUntilNewDay <= WARN_SECONDS_BEFORE_NEW_DAY) { | ||
| logger.warning(line); | ||
| } else { | ||
| logger.info(line); | ||
| } | ||
| append(LocalDateTime.now().format(TIMESTAMP) + " " + line); | ||
| } | ||
|
|
||
| static String format(String player, String action, Node node, int secondsUntilNewDay) { | ||
| String guild = node.getGuild() == null ? "none" : node.getGuild().getId(); | ||
| String resource = node.getBlock() == null ? "unknown" : node.getBlock().getResource(); | ||
| return String.format(Locale.ROOT, "[NodeToggle] %s %s %s node %s of guild %s at %s, upkeep %.2fd/day, %s until new day", | ||
| player, action, resource, node.getId(), guild, location(node.getLoc()), | ||
| node.getDailyUpkeep(), duration(secondsUntilNewDay)); | ||
| } | ||
|
|
||
| static String duration(int seconds) { | ||
| int clamped = Math.max(0, seconds); | ||
| return String.format(Locale.ROOT, "%dh %02dm", clamped / 3600, (clamped % 3600) / 60); | ||
| } | ||
|
|
||
| private static String location(Location loc) { | ||
| if (loc == null) return "unknown"; | ||
| String world = loc.getWorld() == null ? "?" : loc.getWorld().getName(); | ||
| return world + " " + loc.getBlockX() + "," + loc.getBlockY() + "," + loc.getBlockZ(); | ||
| } | ||
|
|
||
| private static void append(String line) { | ||
| File file = new File(DowsingMain.plugin.getDataFolder(), FILE_NAME); | ||
| try { | ||
| Files.writeString(file.toPath(), line + System.lineSeparator(), StandardCharsets.UTF_8, | ||
| StandardOpenOption.CREATE, StandardOpenOption.APPEND); | ||
| } catch (IOException e) { | ||
| DowsingMain.plugin.getLogger().log(Level.WARNING, "Could not write " + FILE_NAME, e); | ||
| } | ||
| } | ||
| } |
81 changes: 81 additions & 0 deletions
81
src/test/java/net/tfminecraft/dowsing/managers/NodeUpkeepTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package net.tfminecraft.dowsing.managers; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import net.tfminecraft.dowsing.objects.Node; | ||
| import net.tfminecraft.simplefactions.guild.Guild; | ||
|
|
||
| class NodeUpkeepTest { | ||
| private List<Node> previousNodes; | ||
| private Guild guild; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| previousNodes = NodeManager.nodes; | ||
| NodeManager.nodes = new ArrayList<>(); | ||
| guild = mock(Guild.class); | ||
| when(guild.getId()).thenReturn("The_Chisels"); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| NodeManager.nodes = previousNodes; | ||
| } | ||
|
|
||
| @Test | ||
| void onlyActiveNodesOweUpkeep() { | ||
| NodeManager.nodes.add(node(guild, true, 40.0, 1.0)); | ||
| NodeManager.nodes.add(node(guild, false, 25.0, 1.0)); | ||
|
|
||
| assertEquals(40.0, NodeManager.getTotalUpkeep(guild)); | ||
| } | ||
|
|
||
| @Test | ||
| void overCapacityMultiplierIsCharged() { | ||
| NodeManager.nodes.add(node(guild, true, 40.0, 1.5)); | ||
|
|
||
| assertEquals(60.0, NodeManager.getTotalUpkeep(guild)); | ||
| } | ||
|
|
||
| @Test | ||
| void upkeepRoundsHalfUpToTheCent() { | ||
| NodeManager.nodes.add(node(guild, true, 0.29, 1.5)); | ||
|
|
||
| assertEquals(0.44, NodeManager.getTotalUpkeep(guild)); | ||
| } | ||
|
|
||
| @Test | ||
| void otherGuildsNodesAreIgnored() { | ||
| Guild other = mock(Guild.class); | ||
| when(other.getId()).thenReturn("Other"); | ||
| NodeManager.nodes.add(node(other, true, 40.0, 1.0)); | ||
|
|
||
| assertEquals(0.0, NodeManager.getTotalUpkeep(guild)); | ||
| } | ||
|
|
||
| private static Node node(Guild owner, boolean active, double upkeep, double costIncrease) { | ||
| Node node = new Node(java.util.UUID.randomUUID(), null, null, null, active, 1, 0, null, 10, 0, 50) { | ||
| @Override | ||
| public Boolean hasGuild() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public Guild getGuild() { | ||
| return owner; | ||
| } | ||
| }; | ||
| node.setUpkeep(upkeep); | ||
| node.setCostIncrease(costIncrease); | ||
| return node; | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
src/test/java/net/tfminecraft/dowsing/objects/NodeCostIncreaseTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package net.tfminecraft.dowsing.objects; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class NodeCostIncreaseTest { | ||
|
|
||
| @Test | ||
| void withinCapacityCostsTheBaseAmount() { | ||
| assertEquals(1.0, Node.costIncreaseFor(1, 1)); | ||
| assertEquals(1.0, Node.costIncreaseFor(0, 3)); | ||
| } | ||
|
|
||
| @Test | ||
| void eachNodeOverCapacityAddsHalf() { | ||
| assertEquals(1.5, Node.costIncreaseFor(2, 1)); | ||
| assertEquals(2.0, Node.costIncreaseFor(3, 1)); | ||
| } | ||
|
|
||
| @Test | ||
| void buyingCapacityLowersTheMultiplier() { | ||
| assertEquals(1.5, Node.costIncreaseFor(3, 2)); | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/test/java/net/tfminecraft/dowsing/utils/NodeToggleLogTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package net.tfminecraft.dowsing.utils; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class NodeToggleLogTest { | ||
|
|
||
| @Test | ||
| void durationShowsHoursAndMinutes() { | ||
| assertEquals("0h 00m", NodeToggleLog.duration(0)); | ||
| assertEquals("0h 59m", NodeToggleLog.duration(3599)); | ||
| assertEquals("23h 30m", NodeToggleLog.duration(23 * 3600 + 30 * 60)); | ||
| assertEquals("0h 00m", NodeToggleLog.duration(-5)); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
Refresh upkeep multipliers when guild capacity changes.
If a guild buys capacity while its nodes are over capacity,
purchaseCapacity()changes the capacity but does not update those nodes.getTotalUpkeep()then continues to return their old, higher daily charges until another action updates each node. Recalculate the affected nodes’ upkeep when capacity changes, before a daily settlement can read this total.🤖 Prompt for AI Agents