summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Gay <david@davidgay.org>2021-05-19 19:04:13 -0400
committerDavid Gay <david@davidgay.org>2021-05-19 19:04:13 -0400
commit045867220ea68b912f74b74a1476ac08bd19dbfd (patch)
tree302e9740a54ad0b9147a6995db8ebd3409e2b9b3
parent9fec79398a34d26be1042e35cae429b88f8b96d0 (diff)
More work on getting amenity construction working
-rw-r--r--app/controllers/game_controller.rb68
-rw-r--r--app/models/character.rb19
-rw-r--r--app/views/activities/_results.html.erb7
3 files changed, 61 insertions, 33 deletions
diff --git a/app/controllers/game_controller.rb b/app/controllers/game_controller.rb
index 147b692..1b88cb8 100644
--- a/app/controllers/game_controller.rb
+++ b/app/controllers/game_controller.rb
@@ -2,45 +2,57 @@ class GameController < ApplicationController
def finish_activity
@results = []
return unless current_char.activity_time_remaining <= 0
- return unless current_char.can_do_activity?(current_char.activity) # TODO: Add error message
+ activity = current_char.activity
+
+ return unless current_char.can_do_activity?(activity) # TODO: Add error message
current_char.update(activity_started_at: Time.now)
- current_char.activity.whatnot[:results].each do |result|
- type = result[:type]
- case type
- when "item"
- next if rand > result[:chance]
- table_roll = rand
+ Character.transaction do
+ current_char.pay_cost_for(activity)
+
+ activity.whatnot[:results].each do |result|
+ type = result[:type]
+ case type
+ when "item"
+ next if rand > result[:chance]
+ table_roll = rand
- result[:table].sort_by { |_, v| -v[:score] }.each do |item_gid, item_data|
- quantity = item_data[:quantity] || 1
+ result[:table].sort_by { |_, v| -v[:score] }.each do |item_gid, item_data|
+ quantity = item_data[:quantity] || 1
- score = item_data[:score]
- if result[:table_scaling]
- result[:table_scaling][:skills]&.each do |skill_gid, scale_value|
- score = score**(1 + (scale_value * current_char.skill_level(skill_gid)))
+ score = item_data[:score]
+ if result[:table_scaling]
+ result[:table_scaling][:skills]&.each do |skill_gid, scale_value|
+ score = score**(1 + (scale_value * current_char.skill_level(skill_gid)))
+ end
end
- end
- if table_roll >= score
- item = Item.find_by_gid(item_gid)
- xp_awards = {}
- if item.whatnot && item.whatnot.key?(:xp_value)
- xp_awards = item.whatnot[:xp_value]
- .map { |gid, amount| { skill: Skill.find_by_gid(gid.to_s), amount: amount } }
- xp_awards.each do |award|
- current_char.add_skill_xp(award[:skill], award[:amount])
+ if table_roll >= score
+ item = Item.find_by_gid(item_gid)
+ xp_awards = {}
+ if item.whatnot && item.whatnot.key?(:xp_value)
+ xp_awards = item.whatnot[:xp_value]
+ .map { |gid, amount| { skill: Skill.find_by_gid(gid.to_s), amount: amount } }
+ xp_awards.each do |award|
+ current_char.add_skill_xp(award[:skill], award[:amount])
+ end
end
+ current_char.shift_item(item_gid, quantity)
+ @results.push({ type: type, item: Item.find_by_gid(item_gid), quantity: quantity,
+ xp: xp_awards })
+ break
end
- current_char.shift_item(item_gid, quantity)
- @results.push({ type: type, item: Item.find_by_gid(item_gid), quantity: quantity,
- xp: xp_awards })
- break
end
+ when "hearth_amenity"
+ bhi = BuiltHearthAmenity.find_or_initialize_by(hearth_amenity: HearthAmenity.find_by_gid(result[:gid]))
+ bhi.update(level: result[:level])
+ @results.push({ type: type, hearth_amenity: bhi.hearth_amenity })
+ else
+ raise "Invalid result type (#{type})" # TODO: Improve this.
end
- else
- raise "Invalid result type (#{type})" # TODO: Improve this.
end
end
+ rescue ItemQuantityError
+ @results.replace({ type: "error", message: "You don't have enough items to complete this activity." })
end
end
diff --git a/app/models/character.rb b/app/models/character.rb
index a55230e..1d24f93 100644
--- a/app/models/character.rb
+++ b/app/models/character.rb
@@ -13,11 +13,22 @@ class Character < ApplicationRecord
after_create :create_skills
after_create { Hearth.create(character: self) }
- def shift_item(gid, amount)
+ def shift_item(item, amount)
+ item = Item.find_by_gid(item) if item.is_a? String
CharacterItem.transaction do
- item = self.character_items.find_or_initialize_by(item: Item.find_by_gid(gid.to_s))
- item.increment(:quantity, amount)
- item.save
+ ci = self.character_items.find_or_initialize_by(item: item)
+ ci.increment(:quantity, amount)
+ ci.save
+ end
+ end
+
+ def pay_cost_for(activity)
+ CharacterItem.transaction do
+ if activity.whatnot[:cost]
+ activity.whatnot[:cost][:items]&.each do |item_gid, quantity|
+ self.shift_item(item_gid, -quantity)
+ end
+ end
end
end
diff --git a/app/views/activities/_results.html.erb b/app/views/activities/_results.html.erb
index ba2abe7..42f22a3 100644
--- a/app/views/activities/_results.html.erb
+++ b/app/views/activities/_results.html.erb
@@ -1,11 +1,16 @@
<div>
<% results.each do |result| %>
- <% if result[:type] == "item" %>
+ <% case result[:type] %>
+ <% when "item" %>
<p>You got <%= result[:quantity] %> <%= result[:item].name %>
<% if result[:xp].any? %>
(<%= result[:xp].map { |award| "#{award[:amount]} xp #{award[:skill].name}" }.join(", ") %>)
<% end %>
</p>
+ <% when "hearth_amenity" %>
+ <p>You constructed <%= result[:hearth_amenity].name %>.</p>
+ <% when "error" %>
+ <p><%= result[:message] %></p>
<% end %>
<% end %>
</div>