Commit Graph

23 Commits

Author SHA1 Message Date
Z. Cliffe Schreuders
26fc297ad8 Refactor tests and improve NPC handling
- Updated NPC ink loading tests to ensure proper handling of missing story files.
- Adjusted lazy loading tests for rooms to enhance clarity and maintainability.
- Enhanced unlock system tests by adding inventory checks for keys.
- Refined filtered scenario tests to ensure accurate preservation of game state.
- Improved game model tests to validate unlock functionality with various inventory scenarios.
2025-11-25 16:28:18 +00:00
Z. Cliffe Schreuders
3cc9fafcec feat: Enhance mission management with CyBOK integration and collection filtering
- Added `Cybok` model to manage CyBOK entries associated with missions.
- Implemented `by_collection` scope in `Mission` model for filtering missions by collection.
- Updated `missions_controller` to filter missions based on the selected collection.
- Introduced `CybokSyncService` for syncing CyBOK data from mission metadata to the database.
- Created new views and partials for displaying CyBOK information with tooltips using Tippy.js.
- Added metadata fields to `break_escape_missions` for `secgen_scenario` and `collection`.
- Enhanced mission seeding logic to support new metadata and CyBOK entries.
- Added multiple new mission scenarios with associated metadata.
2025-11-25 15:20:05 +00:00
Z. Cliffe Schreuders
b4b95928da feat: Enhance inventory management with server-side validation and CSRF protection 2025-11-24 11:17:39 +00:00
Z. Cliffe Schreuders
ef27265c8c Simplify NPC unlock to use standard unlock flow
PROBLEM:
Previous implementation had unnecessary complexity with npcUnlockedTargets
tracking. NPC unlocks should just work like any other unlock method.

SOLUTION:
1. Removed npcUnlockedTargets tracking (not needed)
2. NPC unlocks now use standard unlockedRooms/unlockedObjects arrays
3. Updated validate_unlock to check if already unlocked FIRST:
   - If in player_state unlocked list → grant access
   - If method='unlocked' → verify against scenario data locked field
   - Otherwise → validate normally

This fixes the race condition issue:
- NPC calls unlock API with method='npc'
- Server validates NPC has permission
- Server adds to unlockedRooms (normal unlock)
- Later when player opens door, client sends method='unlocked'
- Server sees it's already in unlockedRooms OR unlocked in scenario → grants access

Changes:
- app/models/break_escape/game.rb: Remove npc_unlock_target!/npc_unlocked?, check unlocked state first
- app/controllers/break_escape/games_controller.rb: Remove npc_unlock_target! calls
- test/integration/unlock_system_test.rb: Update tests for simplified approach

All 37 tests passing, 122 assertions
2025-11-22 00:46:56 +00:00
Z. Cliffe Schreuders
c5eca9cc60 Fix NPC unlock race condition with persistent server-side tracking
PROBLEM:
NPC unlocks had timing-dependent behavior:
- If NPC unlocked door BEFORE room loaded: client saw it as unlocked
- If NPC unlocked door AFTER room loaded: door sprite stayed locked

SOLUTION:
1. Server-side persistent tracking:
   - Added npcUnlockedTargets array to player_state
   - Track all NPC unlocks separately from unlockedRooms/unlockedObjects
   - Initialize npcUnlockedTargets in new games

2. Server merges NPC unlock state:
   - filtered_room_data checks npcUnlockedTargets
   - Marks doors/containers as unlocked if NPC unlocked them
   - Works regardless of when room is loaded

3. Client updates existing sprites:
   - NPC unlock handler finds ALL door sprites for target room
   - Updates sprite state immediately after server unlock
   - Handles both pre-loaded and late-loaded rooms

Changes:
- app/models/break_escape/game.rb: Add npc_unlock_target!, npc_unlocked?, merge state in filtered_room_data
- app/controllers/break_escape/games_controller.rb: Track NPC unlocks in unlock endpoint
- public/break_escape/js/minigames/person-chat/person-chat-conversation.js: Update all door sprites after NPC unlock
- public/break_escape/js/systems/doors.js: Export unlockDoor globally
- test/integration/unlock_system_test.rb: Add 4 tests for persistent NPC unlock state
2025-11-22 00:46:56 +00:00
Z. Cliffe Schreuders
77544520aa Return container contents in unlock API response
- Add find_object_in_scenario helper to locate objects by ID or name
- Include hasContents and contents fields in unlock response for containers
- Update password and PIN minigames to populate scenarioData.contents from server response
- This allows the container minigame to launch after successful server-side unlock

Without this, the contents field was filtered for security, preventing the container UI from launching.
2025-11-22 00:46:56 +00:00
Z. Cliffe Schreuders
b27cde13d0 Fix unlock detection to work with server-side data filtering
- Update unlock-system.js to check 'locked' field instead of 'requires' for lock detection
- Pass null for key/pin/password required values (server validates)
- Preserve 'requires' field for biometric/bluetooth locks (contains item identifiers, not answers)
- Update both Game model and controller filtering methods

Fixes issue where locked objects didn't prompt for unlock after server-side filtering was implemented.
2025-11-22 00:46:55 +00:00
Z. Cliffe Schreuders
cb2d342802 Fix Array into String conversion error in track_npc_encounters
Changes:
- Add player_state type checking before accessing encounteredNPCs
- Reset player_state to empty hash if not a Hash type
- Add comprehensive logging for type mismatches
- Change from concat to array addition (+) to avoid in-place mutation issues
- Wrap entire method in try-catch to prevent NPC tracking from breaking room loading
- Add detailed error logging with stack traces

Fixes:
- TypeError: no implicit conversion of Array into String
- Handles legacy data where player_state might be malformed
- Room loading continues even if NPC tracking fails
2025-11-22 00:46:55 +00:00
Z. Cliffe Schreuders
c2ed2fb033 Fix TypeError in track_npc_encounters and add robust error handling to room endpoint
Changes:
- Fix track_npc_encounters to handle case where encounteredNPCs is not an array
- Add type checking to ensure encounteredNPCs is always an array before operations
- Add comprehensive error handling and logging to room endpoint
- Add scenario_data presence check before processing room request
- Wrap room endpoint in try-catch to catch and log all errors

Fixes:
- TypeError: no implicit conversion of String into Array on line 312
- Prevents 500 errors when player_state has malformed data
- Provides clear error messages and stack traces for debugging
2025-11-22 00:46:55 +00:00
Z. Cliffe Schreuders
905c5643b5 Fix scenario endpoint to return filtered data and revert client to use /scenario
Changes:
- Revert client game.js to use /scenario endpoint instead of /scenario_map
- Update /scenario endpoint to return filtered_scenario_for_bootstrap
- Add filter_requires_recursive method to remove sensitive 'requires' fields
- Keep /scenario_map for potential future navigation queries
- Add error handling to scenario endpoint

Rationale:
- The game client expects full scenario structure (startRoom, rooms, startItemsInInventory, etc.)
- scenario_map returns minimal structure incompatible with game initialization
- filtered_scenario_for_bootstrap provides room metadata without objects (lazy-loaded)
- filter_requires_recursive ensures puzzle solutions aren't exposed to client
- Maintains security while providing necessary data for game initialization
2025-11-22 00:46:55 +00:00
Z. Cliffe Schreuders
25dff05d39 Add comprehensive error handling and logging to scenario_map endpoint
- Add try-catch blocks to prevent 500 errors
- Add logging to help diagnose issues
- Add nil checks for scenario_data and rooms
- Skip individual rooms that fail to process
- Return descriptive error messages for debugging
2025-11-22 00:46:55 +00:00
Z. Cliffe Schreuders
a945859730 Implement comprehensive server-side validation and data filtering for client actions
Server-side changes:
- Game model: Initialize starting items in player inventory from scenario
- Game model: Add filter_requires_and_contents_recursive to hide solutions and locked contents
- Game model: Fix filtered_room_data to preserve lockType while removing requires
- GamesController: Add scenario_map endpoint for minimal layout metadata
- GamesController: Update room endpoint with access control and NPC encounter tracking
- GamesController: Add container endpoint for lazy-loading locked container contents
- GamesController: Update inventory endpoint with comprehensive validation
  - Validates item exists in scenario
  - Checks item is takeable
  - Verifies container is unlocked if item is in container
  - Verifies room is unlocked if room is locked
  - Checks NPC is encountered if item held by NPC
- GamesController: Update unlock endpoint with transaction safety
- GamesController: Update sync_state to verify room accessibility
- Routes: Add scenario_map and container routes

Client-side changes:
- inventory.js: Make addToInventory async and add server validation before local updates
- container-minigame.js: Add lazy-loading of container contents from server
- game.js: Update to use scenario_map endpoint for reduced initial payload
- api-client.js: Add getScenarioMap method alongside getScenario

Security improvements:
- Prevents client-side cheating by validating all actions server-side
- Hides solution fields (requires) from client responses
- Hides locked container contents until unlocked
- Enforces room and container access controls
- Tracks NPC encounters automatically
- All validation failures return clear error messages

Implements plans from:
- planning_notes/validate_client_actions/GOALS_AND_DECISIONS.md
- planning_notes/validate_client_actions/IMPLEMENTATION_PLAN.md
2025-11-22 00:46:55 +00:00
Z. Cliffe Schreuders
b465592c4f feat: implement GamesController with sync_state, unlock, and inventory actions 2025-11-21 15:27:54 +00:00
Z. Cliffe Schreuders
09e0631b08 refactor: reorganize API routes and remove unused bootstrap endpoint 2025-11-21 15:27:54 +00:00
Z. Cliffe Schreuders
c2fadcd169 Add integration and model tests for BreakEscape game mechanics
- Implement RoomLazyLoadTest to verify room data retrieval and error handling for non-existent rooms.
- Create FilteredScenarioTest to ensure scenario data is filtered correctly for game initialization, preserving navigation structure while removing unnecessary details like objects and NPCs.
- Add tests for lock requirements and ensure original scenario data remains unmodified after filtering.
2025-11-21 15:27:54 +00:00
Z. Cliffe Schreuders
6e65b9030d Add integration tests for asset loading and NPC story file handling
- Added tests to verify serving of door tile image and key-operations minigame module.
- Created new NPCInkLoadingTest to check various scenarios for NPC story file loading:
  - Return 404 for NPCs without story files and for non-existent NPCs.
  - Validate handling of missing NPC parameter.
  - Ensure correct API endpoint construction in npc-lazy-loader.
  - Verify person-chat and phone-chat minigames use the correct story loading endpoint.
  - Confirm npc-manager loads stories via API endpoint without direct fetch of storyPath.
  - Check asset path resolution in person-chat-portraits and phone-chat-ui.
  - Ensure ink endpoint returns correct MIME type and handles special characters in NPC IDs.
2025-11-21 15:27:54 +00:00
Z. Cliffe Schreuders
9ac86a2c8d Add integration and unit tests for asset loading and content type determination
- Created `AssetLoadingIntegrationTest` to verify the loading of game assets in the correct order, including JavaScript, CSS, and sound files.
- Implemented tests to ensure proper handling of asset paths, security constraints, and response headers.
- Added `StaticFilesControllerUnitTest` to test the content type determination logic for various file extensions, ensuring case insensitivity and handling of multiple dots in filenames.
2025-11-21 15:27:54 +00:00
Z. Cliffe Schreuders
5b11fa9dbb Add static file serving and enhance game UI
- Implement StaticFilesController to serve CSS, JS, and asset files for the BreakEscape engine.
- Update routes to include static file paths for CSS, JS, and assets.
- Refactor game show view to load multiple CSS files and include Google Fonts.
- Remove application stylesheet link from the layout.
- Modify various CSS files to improve layout and styling, including HUD and inventory.
- Update JavaScript files to ensure asset paths are correctly prefixed with /break_escape/.
- Enhance minigame UI components, including notifications, modals, and overlays.
- Adjust game-over screen and health UI to use correct asset paths.
- Update constants and crypto workstation utility to reflect new asset paths.
2025-11-21 15:27:54 +00:00
Z. Cliffe Schreuders
4764f5881e test: Add comprehensive server response tests
- Add HTML response tests for missions index (checks mission cards rendered)
- Add game page tests (checks game container and config injection)
- Add API endpoint tests:
  * scenario endpoint returns JSON scenario data
  * bootstrap endpoint returns complete game state
  * sync_state endpoint updates player position
  * unlock endpoint validates and rejects invalid attempts
  * inventory endpoint adds items to player inventory
- Fix inventory API to use action_type instead of reserved 'action' param

All 23 tests passing with 64 assertions verifying server returns correct data!
2025-11-21 15:27:54 +00:00
Z. Cliffe Schreuders
e18950c6d7 feat: Configure dummy app for development with engine migrations
- Add root route that redirects to missions list
- Fix ApplicationController to fallback to demo user when current_user unavailable
- Configure dummy app to load engine migrations in application.rb
- Create test/dummy/db/migrate directory
- Successfully migrated and seeded database with 26 missions

Now visiting http://localhost:3000 will show the missions selection page!
2025-11-21 15:27:54 +00:00
Z. Cliffe Schreuders
f44c211d53 fix: Make tests database-agnostic and fix fixture loading
- Update migration to support both PostgreSQL (jsonb) and SQLite (json)
- Fix Rails 8 compatibility (remove config.assets)
- Configure Pundit to use current_player instead of current_user
- Add explicit fixture class mappings for engine fixtures
- Configure standalone mode for tests
- Update test fixtures with proper timestamps and structure
- Improve game test to create data programmatically

Tests now run with 10/12 assertions passing. Remaining errors are due to
missing test scenario files, which can be addressed separately.
2025-11-21 15:27:54 +00:00
Z. Cliffe Schreuders
f6c9ceb5e8 feat: Add controllers and routes
- Add MissionsController for scenario selection
- Add GamesController with scenario/ink endpoints
- Add JIT Ink compilation logic
- Add API::GamesController for game state management
- Configure routes with REST + API endpoints
- Add authorization hooks (Pundit)
- Add polymorphic player support (current_player helper)
2025-11-21 15:27:54 +00:00
Z. Cliffe Schreuders
680c7bfcae feat: Generate Rails Engine structure
- Create mountable engine with isolated namespace
- Configure Pundit authorization
- Set up gemspec with dependencies
- Configure generators for test_unit with fixtures
2025-11-21 15:27:53 +00:00