fix(trades): normalize the rate against deuterium like the other resources - #39
Merged
Merged
Conversation
…urces `parseRate(rate, 'deut')` read the metal and crystal terms raw, where the metal and crystal branches divide by the term being sold. A rate is a ratio, so `4:3:2` is the same rate as `2:1.5:1` — but it came back scaled by the deuterium term, and `sellDeut` paid twice as much for the very same deuterium. It only ever looked right because rates are conventionally written against 1 deuterium, which makes the division a no-op. The README already described the function as normalizing. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
🎉 This PR is included in version 4.0.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This was referenced Aug 15, 2026
Merged
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The bug
parseRate(rate, type)is documented as normalizing a rate against a reference resource. Themetalandcrystalbranches do that — they divide the other two terms by the term being sold. Thedeutbranch reads them raw:A rate is a ratio, so
4:3:2states exactly what2:1.5:1states. After this function it did not:sellMetal(1000)sellDeut(1000)2:1.5:1{ deut: 200, crystal: 300 }{ metal: 1200, crystal: 600 }4:3:2(before){ deut: 200, crystal: 300 }{ metal: 2400, crystal: 1200 }❌4:3:2(after){ deut: 200, crystal: 300 }{ metal: 1200, crystal: 600 }✅Selling deuterium paid out scaled by the deuterium term. It only ever looked right because rates are conventionally written against 1 deuterium, which makes the division a no-op — every rate in the README, in the presets of
ogame-uiand in!ogcon the Discord bot is of that form.Found from the trade calculator in
ogame-ui, whose custom-rate form lets you type any three numbers.The fix
Divide by
deut, like the other two branches divide by their own term. Two lines.Compatibility
No change for any rate whose deuterium term is
1, which is every rate in the docs and every preset shipped by the callers. Only rates written against another scale move — and those were wrong.Tests
parseRate('4:3:2', 'deut')normalizes to{ rateMetal: 2, rateCrystal: 1.5, rateDeut: 1 }.4:3:2,10:7.5:5and2:1.5:1parse identically for all three reference resources.sellDeut(100000, 50, 50, '4:3:2')returns the same as at2:1.5:1.Lint, types and the 318 tests pass locally.
Noted, not fixed here
parseRatestill accepts terms it cannot use: a0term divides by zero and comes back asInfinity, an empty one ('2::1') reads as0, and a negative one yields negative resources —hasNaNcatches none of the three. That is a separate change with a wider blast radius (it turns cases that return a number into throws), so it is left out of this patch.ogame-uivalidates its input before calling.🤖 Generated with Claude Code