From 569db9a18911bf954914b0ace678ca79e98cf12a Mon Sep 17 00:00:00 2001 From: David Gay Date: Sat, 5 Jun 2021 16:32:30 -0400 Subject: DRY up the `#finish_activity` code a bit --- app/controllers/game_controller.rb | 119 +++++++++++++++---------------------- 1 file changed, 47 insertions(+), 72 deletions(-) (limited to 'app') diff --git a/app/controllers/game_controller.rb b/app/controllers/game_controller.rb index c85eecd..82b7688 100644 --- a/app/controllers/game_controller.rb +++ b/app/controllers/game_controller.rb @@ -53,10 +53,7 @@ class GameController < ApplicationController type = result[:type] case type when "xp" - skill = Skill.find_by_gid(result[:gid]) - amount = result[:base] - current_char.add_skill_xp(skill, amount) - @results.push({ type: "xp", skill: skill, xp: amount }) + handle_xp_result(result) when "monster" raise TooManyWoundsError unless current_char.can_fight? next if rand > (result[:chance] || 1) @@ -82,36 +79,7 @@ class GameController < ApplicationController end end when "item" - next if rand > (result[:chance] || 1) - - if result[:table] - table_roll = rand - - result[:table].sort_by { |t| -t[:score] }.each do |table_entry| - min_quantity = table_entry[:min_quantity] || table_entry[:quantity] || 1 - max_quantity = table_entry[:max_quantity] || table_entry[:quantity] || 1 - quantity = rand(min_quantity..max_quantity) - - score = table_entry[:score] - - if table_roll >= score - give_item(table_entry, quantity, with_xp: true) - - table_entry[:titles]&.each do |title_data| - title = Title.find_by_gid(title_data[:gid]) - if current_char.award_title(title) - @results.push({ type: "title", title: title }) - end - end - break - end - end - else - min_quantity = result[:min_quantity] || result[:quantity] || 1 - max_quantity = result[:max_quantity] || result[:quantity] || 1 - quantity = rand(min_quantity..max_quantity) - give_item(result, quantity, with_xp: true) - end + handle_item_result(result) when "hearth_amenity" bhi = current_char.hearth.built_hearth_amenities .find_or_initialize_by(hearth_amenity: HearthAmenity.find_by_gid(result[:gid])) @@ -243,54 +211,61 @@ class GameController < ApplicationController mon.whatnot[:awards]&.each do |award_data| case award_data[:type] when "title" - title = Title.find_by_gid(award_data[:gid]) - if current_char.award_title(title) - @results.push({ type: "title", title: title }) - end + handle_title_result(award_data) when "xp" - skill = Skill.find_by_gid(award_data[:skill]) - amount = award_data[:base] - char.add_skill_xp(skill, amount) - @results.push({ type: "xp", skill: skill, xp: amount }) + handle_xp_result(award_data) when "item" - # TODO: This is basically duplicated from earlier - next if rand > (award_data[:chance] || 1) + handle_item_result(award_data) + else + raise "Invalid award type string (#{award_data[:type]})" + end + end + end + break + end + end + end - if award_data[:table] - table_roll = rand + def handle_title_result(data) + if current_char.award_title(data[:gid]) + @results.push({ type: "title", title: title }) + end + end - award_data[:table].sort_by { |t| -t[:score] }.each do |table_entry| - min_quantity = table_entry[:min_quantity] || table_entry[:quantity] || 1 - max_quantity = table_entry[:max_quantity] || table_entry[:quantity] || 1 - quantity = rand(min_quantity..max_quantity) + def handle_xp_result(data) + skill = Skill.find_by_gid(data[:skill]) + amount = data[:base] + current_char.add_skill_xp(skill, amount) + @results.push({ type: "xp", skill: skill, xp: amount }) + end - score = table_entry[:score] + def handle_item_result(data) + return if rand > (data[:chance] || 1) - if table_roll >= score - give_item(table_entry, quantity) + if data[:table] + table_roll = rand - table_entry[:titles]&.each do |title_data| - title = Title.find_by_gid(title_data[:gid]) - if current_char.award_title(title) - @results.push({ type: "title", title: title }) - end - end - break - end - end - else - min_quantity = award_data[:min_quantity] || award_data[:quantity] || 1 - max_quantity = award_data[:max_quantity] || award_data[:quantity] || 1 - quantity = rand(min_quantity..max_quantity) - give_item(award_data, quantity) - end - else - raise "Invalid award type string (#{award_data[:type]})" - end + data[:table].sort_by { |t| -t[:score] }.each do |table_entry| + min_quantity = table_entry[:min_quantity] || table_entry[:quantity] || 1 + max_quantity = table_entry[:max_quantity] || table_entry[:quantity] || 1 + quantity = rand(min_quantity..max_quantity) + + score = table_entry[:score] + + if table_roll >= score + give_item(table_entry, quantity) + + table_entry[:titles]&.each do |title_data| + handle_title_result(title_data) end + break end - break end + else + min_quantity = data[:min_quantity] || data[:quantity] || 1 + max_quantity = data[:max_quantity] || data[:quantity] || 1 + quantity = rand(min_quantity..max_quantity) + give_item(data, quantity) end end end -- cgit v1.2.3