Implement on-chain fuel and energy management for ships - #57
Open
daatsuka wants to merge 1 commit into
Open
Conversation
Contributor
|
@daatsuka fix conflict |
Contributor
|
@daatsuka fix conflicts and cli falling checks |
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.
This PR introduces
src/energy_manager.rs, a new module that implements the full resource-consumption layer for ship energy in the Nebula Nomad contracts. The public surface is six functions —initialize_energy,consume_energy,recharge_energy,get_energy,set_base_recharge_rate, andapply_efficiency_bonus— all wired intoNebulaNomadContractviasrc/lib.rs. Internally, balances are stored per-ship under anEnergyKey::EnergyBalance(ship_id)persistent key, and every mutation emits a Soroban event so horizon indexers can track consumption and recharge flows without polling. A privaterequire_ship_existshelper gates every write path, ensuring that energy operations are only valid for minted ships already inShipDataKey::Ship.The recharge path was the trickiest part to get right:
recharge_energycomputesresource_amount * effective_rate / 100ini128space so intermediate products cannot overflow, then clamps the result back tou32before callingsaturating_addon the current balance.effective_rateitself ismin(base_rate + per_ship_bonus, 100), which means blueprint upgrades stack with the global rate but never exceed 100 %. This design keeps the math deterministic and bounded while still giving game designers two independent knobs — a global dial (set_base_recharge_rate) and a per-ship dial (apply_efficiency_bonus) — to tune the economy. The small edits ingovernance.rs,indexer_callbacks.rs, andtheme_customizer.rsare Sorobansymbol_short!truncations required because the macro enforces a 9-character limit and a few existing literals exceeded it; these are compile-required, not behavioral changes.A dedicated test file at
tests/test_energy_manager.rscovers the full lifecycle: init-then-read, successful and insufficient consumption, zero-amount rejection, default and bonus-augmented recharge, overflow saturation atu32::MAX, a multi-step consume-recharge cycle, 25 consecutive micro-consumptions, and access on a non-existent ship. All ten tests (plus the existing suite) pass locally undercargo testwith no panics or warnings, and the snapshot files checked in match the Soroban test-utils output exactly. I rancargo build --target wasm32-unknown-unknown --releaseas well to confirm the WASM artifact compiles clean.Closes #42