summaryrefslogtreecommitdiff
path: root/app/controllers/hearth_amenities_controller.rb
blob: 09ea7bf3ae1076a5ea193932294e76d5d70719eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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