From 19db2f530dc9eaef492e86d1baccbe80bcaba2b7 Mon Sep 17 00:00:00 2001 From: "Z. Cliffe Schreuders" Date: Sun, 30 Nov 2025 00:06:54 +0000 Subject: [PATCH] Refactor whitespace and comments for consistency in BreakEscape controllers and models - Cleaned up unnecessary whitespace in `games_controller.rb`, `missions_controller.rb`, `game.rb`, `mission.rb`, `routes.rb`, `seeds.rb`, and migration files to enhance code readability. - Standardized comment formatting across various files to maintain consistency and improve clarity. --- .../break_escape/games_controller.rb | 8 ++--- .../break_escape/missions_controller.rb | 2 +- app/models/break_escape/game.rb | 36 +++++++++---------- app/models/break_escape/mission.rb | 6 ++-- config/routes.rb | 4 +-- ...128000001_remove_unique_game_constraint.rb | 1 - db/seeds.rb | 2 +- 7 files changed, 29 insertions(+), 30 deletions(-) diff --git a/app/controllers/break_escape/games_controller.rb b/app/controllers/break_escape/games_controller.rb index e98f360..6ea54fd 100644 --- a/app/controllers/break_escape/games_controller.rb +++ b/app/controllers/break_escape/games_controller.rb @@ -62,9 +62,9 @@ module BreakEscape elsif params[:standalone_flags].present? flags = if params[:standalone_flags].is_a?(Array) params[:standalone_flags] - else + else params[:standalone_flags].split(',').map(&:strip).reject(&:blank?) - end + end initial_player_state['standalone_flags'] = flags end @@ -424,7 +424,7 @@ module BreakEscape authorize @game if defined?(Pundit) task_id = params[:task_id] - + unless task_id.present? return render json: { success: false, error: 'Missing task_id' }, status: :bad_request end @@ -453,7 +453,7 @@ module BreakEscape end result = @game.update_task_progress!(task_id, progress) - + Rails.logger.debug "[BreakEscape] Task progress updated: #{task_id} = #{progress}" render json: result end diff --git a/app/controllers/break_escape/missions_controller.rb b/app/controllers/break_escape/missions_controller.rb index f7e684b..ceb84c8 100644 --- a/app/controllers/break_escape/missions_controller.rb +++ b/app/controllers/break_escape/missions_controller.rb @@ -27,7 +27,7 @@ module BreakEscape # - Standalone mode: Flag input form redirect_to "/break_escape/games/new?mission_id=#{@mission.id}" else - # Legacy behavior for non-VM missions - auto-create game + # Legacy behavior for non-VM missions - auto-create game @game = Game.find_or_create_by!( player: current_player, mission: @mission diff --git a/app/models/break_escape/game.rb b/app/models/break_escape/game.rb index 8d19429..468ea55 100644 --- a/app/models/break_escape/game.rb +++ b/app/models/break_escape/game.rb @@ -345,7 +345,7 @@ module BreakEscape # Initialize objectives state structure def initialize_objectives return unless scenario_data['objectives'].present? - + player_state['objectivesState'] ||= { 'aims' => {}, # { aimId: { status, completedAt } } 'tasks' => {}, # { taskId: { status, progress, completedAt } } @@ -356,15 +356,15 @@ module BreakEscape # Complete a task with server-side validation def complete_task!(task_id, validation_data = {}) initialize_objectives - + task = find_task_in_scenario(task_id) return { success: false, error: 'Task not found' } unless task - + # Check if already completed if player_state.dig('objectivesState', 'tasks', task_id, 'status') == 'completed' return { success: true, taskId: task_id, message: 'Already completed' } end - + # Validate based on task type case task['type'] when 'collect_items' @@ -390,22 +390,22 @@ module BreakEscape when 'custom' # Custom tasks are completed via ink tags - no validation needed end - + # Mark task complete player_state['objectivesState']['tasks'][task_id] = { 'status' => 'completed', 'completedAt' => Time.current.iso8601 } - + # Process onComplete actions process_task_completion(task) - + # Check if aim is now complete check_aim_completion(task['aimId']) - + # Update statistics self.tasks_completed = (self.tasks_completed || 0) + 1 - + save! { success: true, taskId: task_id } end @@ -413,11 +413,11 @@ module BreakEscape # Update task progress (for collect_items tasks) def update_task_progress!(task_id, progress) initialize_objectives - + player_state['objectivesState']['tasks'][task_id] ||= {} player_state['objectivesState']['tasks'][task_id]['progress'] = progress save! - + { success: true, taskId: task_id, progress: progress } end @@ -472,11 +472,11 @@ module BreakEscape # Process task.onComplete actions def process_task_completion(task) return unless task['onComplete'] - + if task['onComplete']['unlockTask'] unlock_objective_task!(task['onComplete']['unlockTask']) end - + if task['onComplete']['unlockAim'] unlock_objective_aim!(task['onComplete']['unlockAim']) end @@ -498,11 +498,11 @@ module BreakEscape def check_aim_completion(aim_id) aim = scenario_data['objectives']&.find { |a| a['aimId'] == aim_id } return unless aim - + all_complete = aim['tasks'].all? do |task| task_status(task['taskId']) == 'completed' end - + if all_complete player_state['objectivesState']['aims'][aim_id] = { 'status' => 'completed', @@ -551,9 +551,9 @@ module BreakEscape # Build VM context only if mission requires VMs and we're in Hacktivity mode vm_context = if mission&.requires_vms? && BreakEscape::Mission.hacktivity_mode? build_vm_context - else + else {} - end + end # Add flags_by_vm and vm_ips from player_state for standalone mode state = player_state.is_a?(Hash) ? player_state : {} @@ -618,7 +618,7 @@ module BreakEscape # Ensure it's a hash before attempting to access it state = player_state.is_a?(Hash) ? player_state : {} vm_set_id = state['vm_set_id'] - + return {} unless vm_set_id && BreakEscape::Mission.hacktivity_mode? vm_set = ::VmSet.find_by(id: vm_set_id) diff --git a/app/models/break_escape/mission.rb b/app/models/break_escape/mission.rb index 4e3eda2..05f731c 100644 --- a/app/models/break_escape/mission.rb +++ b/app/models/break_escape/mission.rb @@ -110,11 +110,11 @@ module BreakEscape require 'nokogiri' doc = Nokogiri::XML(xml_content) - + # Remove namespaces for simpler XPath queries # SecGen uses xmlns="http://www.github/cliffe/SecGen/marker" doc.remove_namespaces! - + flags_by_vm = {} doc.xpath('//system').each do |system| @@ -152,7 +152,7 @@ module BreakEscape vm = vm_context['vms'].find { |v| v['title'] == title } return vm.to_json if vm end - + # Standalone mode: use fallback, but override IP from vm_ips if available result = fallback.dup if vm_context && vm_context['vm_ips'] && vm_context['vm_ips'][title] diff --git a/config/routes.rb b/config/routes.rb index b7d17b1..19ddf23 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -25,12 +25,12 @@ BreakEscape::Engine.routes.draw do put 'sync_state' # Periodic state sync post 'unlock' # Validate unlock attempt post 'inventory' # Update inventory - + # Objectives system get 'objectives' # Get current objective state post 'objectives/tasks/:task_id', to: 'games#complete_task', as: 'complete_task' put 'objectives/tasks/:task_id', to: 'games#update_task_progress', as: 'update_task_progress' - + # VM/Flag integration post 'flags', to: 'games#submit_flag' # Submit CTF flag for validation end diff --git a/db/migrate/20251128000001_remove_unique_game_constraint.rb b/db/migrate/20251128000001_remove_unique_game_constraint.rb index a2638bc..a4715d9 100644 --- a/db/migrate/20251128000001_remove_unique_game_constraint.rb +++ b/db/migrate/20251128000001_remove_unique_game_constraint.rb @@ -16,4 +16,3 @@ class RemoveUniqueGameConstraint < ActiveRecord::Migration[7.0] name: 'index_games_on_player_and_mission_non_unique' end end - diff --git a/db/seeds.rb b/db/seeds.rb index 87d5e25..f9e122d 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -36,7 +36,7 @@ cybok_total = 0 scenario_dirs.each do |dir| scenario_name = File.basename(dir) - + if SKIP_DIRS.include?(scenario_name) puts " SKIP: #{scenario_name}" skipped_count += 1