From 8cec9b3f6f277399c6da23a04d8afb0c68cf8a2b Mon Sep 17 00:00:00 2001 From: David Gay Date: Wed, 26 May 2021 20:57:47 -0400 Subject: Basic usage of hearth amenities --- app/controllers/characters/items_controller.rb | 1 + app/controllers/hearth_amenities_controller.rb | 32 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 app/controllers/hearth_amenities_controller.rb (limited to 'app/controllers') diff --git a/app/controllers/characters/items_controller.rb b/app/controllers/characters/items_controller.rb index 4d47ebb..e1dfd5c 100644 --- a/app/controllers/characters/items_controller.rb +++ b/app/controllers/characters/items_controller.rb @@ -35,6 +35,7 @@ class Characters::ItemsController < ApplicationController @item.whatnot[:use_effects]&.each do |effect| case effect[:type] when "change_wounds" + # This is basically duplicated in HearthAmenityController Character.transaction do wounds_change = [effect[:value], -current_char.wounds].max current_char.shift_item(@item, -1) diff --git a/app/controllers/hearth_amenities_controller.rb b/app/controllers/hearth_amenities_controller.rb new file mode 100644 index 0000000..09ea7bf --- /dev/null +++ b/app/controllers/hearth_amenities_controller.rb @@ -0,0 +1,32 @@ +class HearthAmenitiesController < ApplicationController + def use + @hearth_amenity = HearthAmenity.find(params[:hearth_amenity_id]) + built_hearth_amenity = current_char.hearth.built_hearth_amenities.find_by(hearth_amenity: @hearth_amenity) + unless built_hearth_amenity&.usable? + flash[:alert] = "You can't use that." + redirect_to character_hearth_path(current_char) and return + end + if built_hearth_amenity.on_cooldown? + flash[:alert] = "You need to wait another #{built_hearth_amenity.seconds_until_cooled_down.ceil} seconds." + redirect_to character_hearth_path(current_char) and return + end + built_hearth_amenity.hearth_amenity.whatnot[:use_effects].each do |use_effect| + case use_effect[:type] + when "change_wounds" + # This is basically duplicated in ItemsController + Character.transaction do + wounds_change = [use_effect[:value], -current_char.wounds].max + current_char.wounds = current_char.wounds + wounds_change + current_char.save! + flash[:notice] = "#{use_effect[:message]}" + heal_or_gain = wounds_change.positive? ? "gain" : "heal" + flash[:notice] += " You #{heal_or_gain} #{wounds_change.abs} wound(s)." + end + else + raise "Invalid use effect type (#{use_effect[:type]}" + end + end + built_hearth_amenity.update(last_used_at: Time.now) + redirect_to character_hearth_path(current_char) + end +end -- cgit v1.2.3