Character Import and Update System Design¶
This document outlines the design for the character import and update system in StingbatBot, focusing on syncing with Shadowdarklings as the source of truth.
Overview¶
The system allows users to: - Import new characters from JSON files or URLs (primarily from Shadowdarklings) - Update existing characters from their source URLs or JSON files - Maintain character data for use in the bot - Automatically handle re-imports as updates when a character with the same name, ancestry, and class already exists
Immediate Goal: Character Updates¶
Current Focus¶
The immediate implementation priority is to enable users to update their existing characters from Shadowdarklings:
- Users create and modify their characters on Shadowdarklings (level ups, equipment changes, etc.)
- The bot serves as a tool for using these characters in Discord (rolling attacks, spells, etc.)
- The
!update
command syncs the bot's character data with the latest from Shadowdarklings or a provided JSON file - Re-importing an existing character automatically triggers the update process
Character Storage¶
Characters are stored in two ways: 1. Source JSON: The complete, unmodified JSON from Shadowdarklings 2. Parsed Representation: A processed version used by the bot's systems
Version Management¶
For update tracking, characters will use a simple version number:
- Import Creates V1: Initial import creates version 1 with approval timestamp
- Update Creates New Version: Each update creates a new unapproved version
- Approval Process: Updates use the same approval pattern as imports:
- New version is created without approval timestamp
- User approves: approval timestamp is set
- User rejects: new version is deleted
- Version Field: A simple integer field on the character schema tracks the current version
- Timeout Handling:
- Unapproved versions automatically timeout after 5 minutes
- On timeout: character is deleted and user is notified
- Prevents accumulation of unapproved versions
- Single Unapproved Version:
- Only one unapproved version allowed per character
- Before creating new version, any existing unapproved versions are deleted
- Prevents confusion from multiple pending updates
- Re-import Handling:
- When importing, check if character with same name/ancestry/class exists
- If exists: treat as an update instead of a new import
- Use the same approval process as regular updates
- Preserve the character's source URL if not provided in the new data
Character Context Functions¶
The character context will provide these functions for version management:
- Version Management Functions:
get_latest_version/1
- Get current approved version (only returns approved versions)get_unapproved_version/1
- Get any pending unapproved versiondelete_unapproved_versions/1
- Clean up any pending versionscreate_new_version/2
- Create new version with incremented number-
approve/1
- Set approval timestamp on a version -
Existing Functions:
get/1
- Get character by IDget_by_identity/2
- Get character by user_id and namedelete/1
- Delete character
Update Flow¶
- User runs
!update
with character JSON delete_unapproved_versions/1
- Clean up any existing unapproved versionsget_latest_version/1
- Get current approved version- Check if new data is identical to current version
- If identical: Send message and return
- If different: Continue with update process
-
create_new_version/2
- Create new version (current version + 1) with no approval timestamp -
User approves the update
get_unapproved_version/1
- Get the unapproved version-
approve/1
- Set approval timestamp -
User rejects the update
get_unapproved_version/1
- Get the unapproved version-
delete/1
- Remove the unapproved version -
Update times out
get_unapproved_version/1
- Get the unapproved versiondelete/1
- Remove the unapproved version
Re-import Flow¶
- User runs
!import
with existing character get_by_identity/2
- Find existing character (existing code)- If found, follow update flow above
- If not found, follow import flow
Implementation Notes¶
- Order of Operations:
- Always clean up unapproved versions before getting the latest version
- This ensures we don't have stale unapproved versions affecting our logic
- Check for identical data before creating a new version
-
Skip version creation and approval process if no changes detected
-
Version Functions:
get_latest_version/1
only returns approved versionsget_unapproved_version/1
makes the code more readable than usingget/1
-
approve/1
centralizes the approval logic -
Error Handling:
- All functions should return standard {:ok, result} or {:error, reason} tuples
-
Consistent error handling makes the code more predictable
-
Database Transactions:
- Consider wrapping related operations in transactions
- For example: cleanup + create new version could be atomic
This approach is simpler than a full versioning system because: - We always get the complete character state from Shadowdarklings - We don't need to track individual field changes - The version number serves as a simple counter for updates - Storage is more efficient than storing diffs between versions - We can reuse the same approval pattern as character imports - Users don't need to know the difference between import and update
Implementation Strategy¶
Initial Implementation¶
For the immediate implementation, we'll focus on the core update functionality:
- Command Structure:
!update
with no args updates the selected character- Both variants can accept a JSON file attachment as an alternative data source
- If no file is attached, requires that the character has a source URL stored
-
!import
automatically detects and handles re-imports as updates -
Update Process:
- For URL updates: Fetch JSON from stored source URL
- For file updates: Extract and parse JSON from the attachment
- Validate and parse as a character
- Delete any existing unapproved versions
- Create new unapproved version
- Show preview for user confirmation
-
Schedule timeout cleanup
-
Simple Versioning:
- Add version field to character schema
- Each update creates new unapproved version
- Approval sets timestamp, rejection deletes version
-
Timeout automatically cleans up unapproved versions
-
Source Tracking:
- Record whether the update came from a URL or file
- Maintain the original source URL even when updating from file
- Only update the source URL if explicitly provided in the new data
Technical Components¶
- Command Handler: Processes the update command with or without attachments
- HTTP Client: Fetches character data from URLs
- File Handler: Extracts and validates JSON from attachments
- Character Parser: Validates and processes character JSON
- Preview Generator: Shows character info for approval
- Version Management: Tracks character versions with simple integer field
- Timeout Handler: Automatically cleans up unapproved versions after 5 minutes
- Version Cleanup: Ensures only one unapproved version exists per character
- Re-import Detector: Identifies when an import should be treated as an update
- Change Detector: Compares new data with current version to avoid unnecessary updates