summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/controllers/characters/items_controller.rb5
-rw-r--r--app/errors/item_quantity_error.rb2
-rw-r--r--app/models/character.rb10
-rw-r--r--app/models/character_item.rb25
-rw-r--r--app/views/application/_navbar.html.erb11
-rw-r--r--app/views/characters/items/index.html.erb36
6 files changed, 82 insertions, 7 deletions
diff --git a/app/controllers/characters/items_controller.rb b/app/controllers/characters/items_controller.rb
new file mode 100644
index 0000000..e1a30f8
--- /dev/null
+++ b/app/controllers/characters/items_controller.rb
@@ -0,0 +1,5 @@
+class Characters::ItemsController < ApplicationController
+ def index
+ @character_items = Character.find(params[:character_id]).character_items
+ end
+end
diff --git a/app/errors/item_quantity_error.rb b/app/errors/item_quantity_error.rb
new file mode 100644
index 0000000..29576ca
--- /dev/null
+++ b/app/errors/item_quantity_error.rb
@@ -0,0 +1,2 @@
+class ItemQuantityError < StandardError
+end
diff --git a/app/models/character.rb b/app/models/character.rb
index 1c022ea..a158d88 100644
--- a/app/models/character.rb
+++ b/app/models/character.rb
@@ -1,5 +1,15 @@
class Character < ApplicationRecord
belongs_to :user
belongs_to :activity, optional: true
+ has_many :character_items
+ has_many :items, through: :character_items
validates :name, presence: true
+
+ def shift_item(gid, amount)
+ CharacterItem.transaction do
+ item = self.character_items.find_or_initialize_by(item: Item.find_by_gid(gid))
+ item.increment(:quantity, amount)
+ item.save
+ end
+ end
end
diff --git a/app/models/character_item.rb b/app/models/character_item.rb
new file mode 100644
index 0000000..b54f799
--- /dev/null
+++ b/app/models/character_item.rb
@@ -0,0 +1,25 @@
+class CharacterItem < ApplicationRecord
+ belongs_to :character
+ belongs_to :item
+ validates :quantity, presence: true
+
+ after_initialize do
+ if self.new_record?
+ self.quantity ||= 0
+ end
+ end
+
+ after_save :destroy_if_zero_quantity
+
+ scope :ordered_by_item_name, -> { includes(:item).order("items.name") }
+
+ private
+ def destroy_if_zero_quantity
+ if self.quantity == 0
+ destroy
+ elsif self.quantity < 0
+ # TODO: Can improve this (at the least, with reporting, later).
+ raise ItemQuantityError
+ end
+ end
+end
diff --git a/app/views/application/_navbar.html.erb b/app/views/application/_navbar.html.erb
index 5d3a2cb..85f4642 100644
--- a/app/views/application/_navbar.html.erb
+++ b/app/views/application/_navbar.html.erb
@@ -1,16 +1,13 @@
<ul class="py-2 px-2 col-span-12 text-display">
- <% if user_signed_in? %> <%# Will replace this with `current_character` or equivalent, eventually %>
+ <% if current_char %>
<li class="mr-6 inline">
- LinkA
+ Actions
</li>
<li class="mr-6 inline">
- LinkB
+ <%= link_to "Character", character_path(current_char) %>
</li>
<li class="mr-6 inline">
- LinkC
- </li>
- <li class="mr-6 inline">
- LinkD
+ <%= link_to "Inventory", character_items_path(current_char) %>
</li>
<% end %>
</ul>
diff --git a/app/views/characters/items/index.html.erb b/app/views/characters/items/index.html.erb
new file mode 100644
index 0000000..e313f7e
--- /dev/null
+++ b/app/views/characters/items/index.html.erb
@@ -0,0 +1,36 @@
+<h1 class="text-xl">Inventory</h1>
+
+<table class="table-auto">
+ <thead>
+ <tr>
+ <th class="table-header-padded">Amount</th>
+ <th class="table-header-padded">Item</th>
+ <th class="table-header-padded">Equip</th>
+ <th class="table-header-padded">Use</th>
+ <th class="table-header-padded">Destroy</th>
+ </tr>
+ </thead>
+ <tbody>
+ <% @character_items.ordered_by_item_name.each do |ci| %>
+ <tr>
+ <td class="table-cell-padded text-right"><%= ci.quantity %></td>
+ <td class="table-cell-padded"><%= ci.item.name %></td>
+ <td class="table-cell-padded">
+ <% if ci.item.equip_slot %>
+ <%= link_to "[Equip]", "#" %>
+ <% end %>
+ </td>
+ <td class="table-cell-padded">
+ <% if ci.item.usable? %>
+ <%= link_to "[Use]", "#" %>
+ <% end %>
+ </td>
+ <td class="table-cell-padded">
+ <%= link_to "[Destroy]", "#" %>
+ <%= link_to "[Destroy All]", "#" %>
+ </td>
+ </tr>
+ <% end %>
+ </tbody>
+</table>
+