diff options
author | David Gay <david@davidgay.org> | 2021-05-04 18:32:33 -0400 |
---|---|---|
committer | David Gay <david@davidgay.org> | 2021-05-04 18:32:33 -0400 |
commit | a32e05fea88fa4d9b9072f5a7e25189c99b16b9d (patch) | |
tree | 60b4f3cc3f77c91a0aa82c499d1c700d9ae7ab28 /app | |
parent | 8e2f4d6f10a18a662880c0d4c1ee79fdb6a8b3f2 (diff) |
Get XP awards working
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/game_controller.rb | 14 | ||||
-rw-r--r-- | app/models/character.rb | 6 | ||||
-rw-r--r-- | app/views/activities/_results.html.erb | 6 |
3 files changed, 23 insertions, 3 deletions
diff --git a/app/controllers/game_controller.rb b/app/controllers/game_controller.rb index 3516056..f5fd4a9 100644 --- a/app/controllers/game_controller.rb +++ b/app/controllers/game_controller.rb @@ -3,17 +3,29 @@ class GameController < ApplicationController @results = [] return unless current_char.activity_time_remaining <= 0 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 + result["table"].sort_by { |_, v| -v["score"] }.each do |item_gid, item_data| quantity = item_data["quantity"] || 1 if table_roll >= item_data["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), 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 }) + @results.push({ type: type, item: Item.find_by_gid(item_gid), quantity: quantity, + xp: xp_awards }) break end end diff --git a/app/models/character.rb b/app/models/character.rb index aa434ef..df4938d 100644 --- a/app/models/character.rb +++ b/app/models/character.rb @@ -13,12 +13,16 @@ class Character < ApplicationRecord def shift_item(gid, amount) CharacterItem.transaction do - item = self.character_items.find_or_initialize_by(item: Item.find_by_gid(gid)) + item = self.character_items.find_or_initialize_by(item: Item.find_by_gid(gid.to_s)) item.increment(:quantity, amount) item.save end end + def add_skill_xp(skill, amount) + CharacterSkill.find_by(skill: skill).increment!(:xp, amount) + end + def skill_level(gid) self.character_skills.find_by(skill: Skill.find_by_gid(gid.to_s)).level end diff --git a/app/views/activities/_results.html.erb b/app/views/activities/_results.html.erb index baa4ab0..ba2abe7 100644 --- a/app/views/activities/_results.html.erb +++ b/app/views/activities/_results.html.erb @@ -1,7 +1,11 @@ <div> <% results.each do |result| %> <% if result[:type] == "item" %> - <p>You got <%= result[:quantity] %> <%= result[:item].name %>.</p> + <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> <% end %> <% end %> </div> |