Skip to content

Comments

Extract spell constants to dedicated module with type-safe API#26

Open
MaxBittker wants to merge 1 commit intomainfrom
claude/improve-castspellonnpc-api-DHgs4
Open

Extract spell constants to dedicated module with type-safe API#26
MaxBittker wants to merge 1 commit intomainfrom
claude/improve-castspellonnpc-api-DHgs4

Conversation

@MaxBittker
Copy link
Owner

Summary

Refactored spell handling by extracting spell constants into a dedicated sdk/spells.ts module and updated the API to accept spell names as strings in addition to numeric component IDs. This improves type safety, code organization, and developer experience.

Key Changes

  • New module sdk/spells.ts: Centralized spell constant definitions with 33 spells organized by tier (strikes, bolts, blasts, waves, and other)
  • Type-safe spell resolution: Added resolveSpell() function to accept both SpellName (string literals like 'WIND_STRIKE') and numeric component IDs
  • Reverse lookup utility: Added getSpellName() function to map component IDs back to spell names
  • Updated method signatures:
    • castSpellOnNpc(target, spell, timeout) - now accepts SpellName | number
    • sendSpellOnNpc(npcIndex, spell) - now accepts SpellName | number
    • sendSpellOnItem(slot, spell) - now accepts SpellName | number
  • Removed duplication: Eliminated spell constant definitions from sdk/test/utils/save-generator.ts in favor of re-exporting from the canonical source
  • Updated documentation: Added comprehensive Spells section to API.md with usage examples and full spell reference table
  • Updated test script: Modified scripts/magic/script.ts to import spells from the new module

Implementation Details

  • Exported SpellName type for compile-time type checking of spell names
  • All spell resolution methods include validation with error messages for unknown spells
  • Spell constants are organized with comments indicating spell tier and level requirements where applicable
  • The module is re-exported from sdk/actions.ts for convenient access alongside other SDK exports

https://claude.ai/code/session_01CjzdFWTHRru5kYDGeXgyZe

Addresses #24 - the castSpellOnNpc API was confusing because it required
opaque numeric spell component IDs with no guidance on valid values.

Changes:
- Add sdk/spells.ts as the canonical source for spell constants, exported
  as `Spells` with a `SpellName` type derived from its keys
- Update castSpellOnNpc, sendSpellOnNpc, and sendSpellOnItem to accept
  `SpellName | number` (e.g. 'WIND_STRIKE' or 1152), following the same
  pattern as sendTogglePrayer which accepts PrayerName | number
- Re-export Spells and SpellName from actions.ts for convenient imports
- Replace inline Spells definition in save-generator.ts with re-export
- Remove duplicate Spells constant from scripts/magic/script.ts
- Document all spell IDs in API.md with a new Constants section

Closes #24

https://claude.ai/code/session_01CjzdFWTHRru5kYDGeXgyZe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants