feat: Add player preferences modal for character configuration and enhance sprite selection functionality

This commit is contained in:
Z. Cliffe Schreuders
2026-02-17 01:25:44 +00:00
parent 3d1570a030
commit 8dfc5f04f4
9 changed files with 610 additions and 26 deletions

View File

@@ -2,6 +2,8 @@ require 'open3'
module BreakEscape
class GamesController < ApplicationController
helper PlayerPreferencesHelper
before_action :set_game, only: [:show, :scenario, :scenario_map, :ink, :room, :container, :sync_state, :update_room, :unlock, :inventory, :objectives, :complete_task, :update_task_progress, :submit_flag]
# GET /games/new?mission_id=:id
@@ -99,6 +101,15 @@ module BreakEscape
def show
authorize @game if defined?(Pundit)
@mission = @game.mission
# Load player preference data for the in-game modal
@player_preference = current_player_preference || create_default_preference
@available_sprites = PlayerPreference::AVAILABLE_SPRITES
# Debug logging
Rails.logger.info "[BreakEscape] Loading game#show for player: #{current_player.class.name}##{current_player.id}"
Rails.logger.info "[BreakEscape] Player preference: #{@player_preference.inspect}"
Rails.logger.info "[BreakEscape] Selected sprite: #{@player_preference.selected_sprite.inspect}"
end
# GET /games/:id/scenario
@@ -1335,7 +1346,8 @@ module BreakEscape
if current_player.respond_to?(:break_escape_preference)
current_player.break_escape_preference
elsif current_player.respond_to?(:preference)
current_player.preference
# Reload association to ensure fresh data
current_player.reload.preference
end
end

View File

@@ -11,20 +11,52 @@ module BreakEscape
# PATCH /break_escape/configuration
def update
Rails.logger.info "[BreakEscape] Updating preference for player: #{current_player.class.name}##{current_player.id}"
Rails.logger.info "[BreakEscape] Params: #{player_preference_params.inspect}"
if @player_preference.update(player_preference_params)
flash[:notice] = 'Character configuration saved!'
Rails.logger.info "[BreakEscape] Preference updated successfully: selected_sprite=#{@player_preference.selected_sprite}, in_game_name=#{@player_preference.in_game_name}"
respond_to do |format|
format.html do
flash[:notice] = 'Character configuration saved!'
# Redirect to game if came from validation flow
if params[:game_id].present?
redirect_to game_path(params[:game_id])
else
redirect_to configuration_path
# Redirect to game if came from validation flow
if params[:game_id].present?
redirect_to game_path(params[:game_id])
else
redirect_to configuration_path
end
end
format.json do
render json: {
success: true,
message: 'Character configuration saved!',
data: {
selected_sprite: @player_preference.selected_sprite,
in_game_name: @player_preference.in_game_name
}
}
end
end
else
flash.now[:alert] = 'Please select a character sprite.'
@available_sprites = PlayerPreference::AVAILABLE_SPRITES
@scenario = load_scenario_if_validating
render :show, status: :unprocessable_entity
Rails.logger.error "[BreakEscape] Failed to update preference: #{@player_preference.errors.full_messages.join(', ')}"
respond_to do |format|
format.html do
flash.now[:alert] = 'Please select a character sprite.'
@available_sprites = PlayerPreference::AVAILABLE_SPRITES
@scenario = load_scenario_if_validating
render :show, status: :unprocessable_entity
end
format.json do
render json: {
success: false,
error: 'Please select a character sprite.',
errors: @player_preference.errors.full_messages
}, status: :unprocessable_entity
end
end
end
end
@@ -32,13 +64,15 @@ module BreakEscape
def set_player_preference
@player_preference = current_player_preference || create_default_preference
Rails.logger.info "[BreakEscape] set_player_preference: #{@player_preference.inspect}"
end
def current_player_preference
if current_player.respond_to?(:break_escape_preference)
current_player.break_escape_preference
elsif current_player.respond_to?(:preference)
current_player.preference
# Reload association to ensure fresh data
current_player.reload.preference
end
end