summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Gay <david@davidgay.org>2021-05-23 20:15:03 -0400
committerDavid Gay <david@davidgay.org>2021-05-23 20:15:03 -0400
commit82eec1f68172857b3b39e6ab3e3b837de16c1198 (patch)
treea1edb49f55ad01c3acaf41070bf09f207fcfaf22
parent6bf84733db22f6c5d5105acc3565b075fcc65917 (diff)
Item usage
-rw-r--r--app/controllers/characters/items_controller.rb29
-rw-r--r--app/models/character.rb2
-rw-r--r--app/models/item.rb5
-rw-r--r--app/views/characters/items/index.html.erb2
-rw-r--r--config/routes.rb1
-rw-r--r--data/items.yml12
-rw-r--r--db/migrate/20210523232805_remove_usable_from_items.rb5
-rw-r--r--db/schema.rb3
-rw-r--r--db/seeds.rb5
-rw-r--r--test/fixtures/items.yml6
10 files changed, 53 insertions, 17 deletions
diff --git a/app/controllers/characters/items_controller.rb b/app/controllers/characters/items_controller.rb
index 8df23c8..4d47ebb 100644
--- a/app/controllers/characters/items_controller.rb
+++ b/app/controllers/characters/items_controller.rb
@@ -21,4 +21,33 @@ class Characters::ItemsController < ApplicationController
ensure
redirect_to character_items_path(current_char)
end
+
+ def use
+ @item = Item.find(params[:item_id])
+ unless @item.usable?
+ flash[:alert] = "You can't use #{@item.name}."
+ redirect_to character_items_path(current_char) and return
+ end
+ unless current_char.character_items.exists?(item: @item)
+ flash[:alert] = "You don't have #{@item.name}."
+ redirect_to character_items_path(current_char) and return
+ end
+ @item.whatnot[:use_effects]&.each do |effect|
+ case effect[:type]
+ when "change_wounds"
+ Character.transaction do
+ wounds_change = [effect[:value], -current_char.wounds].max
+ current_char.shift_item(@item, -1)
+ current_char.wounds = current_char.wounds + wounds_change
+ current_char.save!
+ flash[:notice] = "#{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 string (#{effect[:type]})"
+ end
+ redirect_to character_items_path(current_char)
+ end
+ end
end
diff --git a/app/models/character.rb b/app/models/character.rb
index a6e8ff6..ef8768e 100644
--- a/app/models/character.rb
+++ b/app/models/character.rb
@@ -145,7 +145,7 @@ class Character < ApplicationRecord
def equipment_stats
stats = {}
self.equipment.each do |eq|
- eq.item.whatnot[:effects]&.each do |effect|
+ eq.item.whatnot[:equip_effects]&.each do |effect|
if effect[:type] == "stat"
stats[effect[:gid]] ||= 0
stats[effect[:gid]] += effect[:modifier]
diff --git a/app/models/item.rb b/app/models/item.rb
index 5e60f04..1ef5d7d 100644
--- a/app/models/item.rb
+++ b/app/models/item.rb
@@ -4,12 +4,15 @@ class Item < ApplicationRecord
enum equip_slot: [:mainhand, :offhand, :head, :neck, :back, :torso, :grip,
:left_ring, :right_ring, :waist, :legs, :feet, :curio]
validates :gid, :name, :description, presence: true
- validates :usable, inclusion: { in: [true, false] }
def equipment?
self.whatnot && self.whatnot[:equip_slots]&.any?
end
+ def usable?
+ self.whatnot && self.whatnot[:use_effects]&.any?
+ end
+
def equip_slots
return [] unless self.equipment?
self.whatnot[:equip_slots].map { |data| data.to_sym }
diff --git a/app/views/characters/items/index.html.erb b/app/views/characters/items/index.html.erb
index a7c932d..20b43c0 100644
--- a/app/views/characters/items/index.html.erb
+++ b/app/views/characters/items/index.html.erb
@@ -48,7 +48,7 @@
</td>
<td class="table-cell-padded">
<% if ci.item.usable? %>
- <%= link_to "[Use]", "#" %>
+ <%= button_to "Use", character_item_use_path(item_id: ci.item.id) %>
<% end %>
</td>
</tr>
diff --git a/config/routes.rb b/config/routes.rb
index 4eee2a0..69865ee 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -20,6 +20,7 @@ Rails.application.routes.draw do
post "/items/unequip/:slot", to: "items#unequip", as: :item_unequip
resources :items, only: [:index] do
post "/equip", to: "items#equip"
+ post "/use", to: "items#use"
end
resources :titles, only: [:index] do
post "/activate", to: "titles#activate"
diff --git a/data/items.yml b/data/items.yml
index 48e9b22..837702c 100644
--- a/data/items.yml
+++ b/data/items.yml
@@ -34,7 +34,7 @@ iron_dagger:
whatnot:
equip_slots:
- "mainhand"
- effects:
+ equip_effects:
- type: "stat"
gid: "accuracy"
modifier: 2
@@ -47,7 +47,7 @@ iron_short_sword:
whatnot:
equip_slots:
- "mainhand"
- effects:
+ equip_effects:
- type: "stat"
gid: "accuracy"
modifier: 3
@@ -64,7 +64,7 @@ iron_longsword:
skills:
- gid: "beastslay"
level: 3
- effects:
+ equip_effects:
- type: "stat"
gid: "accuracy"
modifier: 4
@@ -74,7 +74,11 @@ iron_longsword:
mending_salve:
name: "Mending salve"
description: "A healing mixture capable of closing wounds."
- usable: true
+ whatnot:
+ use_effects:
+ - type: "change_wounds"
+ value: -1
+ message: "You apply the salve to your skin."
wood:
name: "wood"
description: "A bit of wood."
diff --git a/db/migrate/20210523232805_remove_usable_from_items.rb b/db/migrate/20210523232805_remove_usable_from_items.rb
new file mode 100644
index 0000000..6e0dc61
--- /dev/null
+++ b/db/migrate/20210523232805_remove_usable_from_items.rb
@@ -0,0 +1,5 @@
+class RemoveUsableFromItems < ActiveRecord::Migration[6.1]
+ def change
+ remove_column :items, :usable
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 2568430..87f3473 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_23_151747) do
+ActiveRecord::Schema.define(version: 2021_05_23_232805) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -136,7 +136,6 @@ ActiveRecord::Schema.define(version: 2021_05_23_151747) do
t.string "gid"
t.string "name"
t.text "description"
- t.boolean "usable"
t.jsonb "whatnot"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
diff --git a/db/seeds.rb b/db/seeds.rb
index 5a6c696..a1459cc 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -27,10 +27,7 @@ end
load_data_file("data/items.yml").map do |gid, hash|
item = Item.find_or_create_by(gid: gid)
- item.assign_attributes(hash.except(:equip_slot, :usable))
- item.equip_slot = hash[:equip_slot]&.to_sym
- item.usable = hash[:usable] || false
- item.save
+ item.update(hash)
end
load_data_file("data/locations.yml").map do |gid, hash|
diff --git a/test/fixtures/items.yml b/test/fixtures/items.yml
index 2540b9e..6f27d73 100644
--- a/test/fixtures/items.yml
+++ b/test/fixtures/items.yml
@@ -5,13 +5,11 @@ one:
name: MyString
description: MyText
equip_slot: 1
- usable: false
- whatnot:
+ whatnot:
two:
gid: MyString
name: MyString
description: MyText
equip_slot: 1
- usable: false
- whatnot:
+ whatnot: