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 ++++++++++++++++++++++ app/models/built_hearth_amenity.rb | 25 +++++++++++++++++ app/views/characters/hearth/index.html.erb | 3 ++ config/routes.rb | 4 +++ data/hearth_amenities.yml | 10 +++++++ ...2_add_last_used_at_to_built_hearth_amenities.rb | 5 ++++ db/schema.rb | 3 +- 8 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 app/controllers/hearth_amenities_controller.rb create mode 100644 db/migrate/20210527002712_add_last_used_at_to_built_hearth_amenities.rb 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 diff --git a/app/models/built_hearth_amenity.rb b/app/models/built_hearth_amenity.rb index 5ae8a42..39b6679 100644 --- a/app/models/built_hearth_amenity.rb +++ b/app/models/built_hearth_amenity.rb @@ -3,4 +3,29 @@ class BuiltHearthAmenity < ApplicationRecord belongs_to :hearth_amenity validates :hearth_id, uniqueness: { scope: :hearth_amenity_id } + + def cooldown + # TODO: HACK: Update this so multiple use effects will work + self.hearth_amenity.whatnot[:use_effects].first[:cooldown] + end + + def usable? + self.hearth_amenity.whatnot[:use_effects].present? + end + + def on_cooldown? + if usable? && cooldown.present? + # If it's never been used, it's off cooldown + return false unless self.last_used_at + seconds_until_cooled_down > 0 + else + false + end + end + + def seconds_until_cooled_down + return nil unless usable? + seconds_until_cooled_down = cooldown - (Time.now - self.last_used_at) + [0, seconds_until_cooled_down].max + end end diff --git a/app/views/characters/hearth/index.html.erb b/app/views/characters/hearth/index.html.erb index 563d4b6..a5522ad 100644 --- a/app/views/characters/hearth/index.html.erb +++ b/app/views/characters/hearth/index.html.erb @@ -20,6 +20,9 @@ <%= f.submit "Go" %> <% end %> <% end %> + <% if built_amenity.usable? %> + <%= button_to "Use", hearth_amenity_use_path(built_amenity.hearth_amenity) %> + <% end %> <% end %> <% next_level = built_amenity ? built_amenity.level + 1 : 1 %> <% construct_activity = ha.construct_activity(next_level) %> diff --git a/config/routes.rb b/config/routes.rb index 95071d7..71ba34f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,6 +15,10 @@ Rails.application.routes.draw do resources :locations, only: [:index, :show] resources :activities, only: [:show] + resources :hearth_amenities, only: [] do + post "/use", to: "hearth_amenities#use" + end + resources :characters, only: [:show, :new, :create] do post "/combat_styles", to: "characters#set_combat_styles" scope module: :characters do diff --git a/data/hearth_amenities.yml b/data/hearth_amenities.yml index 33fab3c..a99b9a8 100644 --- a/data/hearth_amenities.yml +++ b/data/hearth_amenities.yml @@ -30,3 +30,13 @@ listern_font: gid: "construct_listern_font_level1" - level: 2 gid: "construct_listern_font_level2" + use_effects: + - type: "change_wounds" + value: -9999 + message: "Every wound heals, every moment a chance for rebirth." + cooldown: 3600 + effects: + - type: "stat_change" + level: 2 + stat: "max_wounds" + modifier: 1 diff --git a/db/migrate/20210527002712_add_last_used_at_to_built_hearth_amenities.rb b/db/migrate/20210527002712_add_last_used_at_to_built_hearth_amenities.rb new file mode 100644 index 0000000..7333ae5 --- /dev/null +++ b/db/migrate/20210527002712_add_last_used_at_to_built_hearth_amenities.rb @@ -0,0 +1,5 @@ +class AddLastUsedAtToBuiltHearthAmenities < ActiveRecord::Migration[6.1] + def change + add_column :built_hearth_amenities, :last_used_at, :timestamp + end +end diff --git a/db/schema.rb b/db/schema.rb index 74bb4e6..eae72c9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_05_26_225100) do +ActiveRecord::Schema.define(version: 2021_05_27_002712) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -46,6 +46,7 @@ ActiveRecord::Schema.define(version: 2021_05_26_225100) do t.integer "level" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.datetime "last_used_at" t.index ["hearth_amenity_id"], name: "index_built_hearth_amenities_on_hearth_amenity_id" t.index ["hearth_id"], name: "index_built_hearth_amenities_on_hearth_id" end -- cgit v1.2.3