From 82fa2b72be613713d42b451e0d1801904b4580e8 Mon Sep 17 00:00:00 2001 From: David Gay Date: Wed, 5 May 2021 18:39:27 -0400 Subject: Symbolize whatnot keys --- app/controllers/game_controller.rb | 18 +++++++++--------- app/models/activity.rb | 2 ++ app/models/character.rb | 8 ++++---- app/models/concerns/has_whatnot.rb | 9 +++++++++ app/models/item.rb | 2 ++ app/models/location.rb | 2 ++ app/models/skill.rb | 2 ++ 7 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 app/models/concerns/has_whatnot.rb diff --git a/app/controllers/game_controller.rb b/app/controllers/game_controller.rb index f5fd4a9..4e55924 100644 --- a/app/controllers/game_controller.rb +++ b/app/controllers/game_controller.rb @@ -4,21 +4,21 @@ class GameController < ApplicationController 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"] + current_char.activity.whatnot[:results].each do |result| + type = result[:type] case type when "item" - next if rand > result["chance"] + 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"] + 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 } } + 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 diff --git a/app/models/activity.rb b/app/models/activity.rb index be5b573..fb99e1d 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -1,4 +1,6 @@ class Activity < ApplicationRecord + include HasWhatnot + belongs_to :location validates :gid, :name, :description, presence: true end diff --git a/app/models/character.rb b/app/models/character.rb index df4938d..7df48e8 100644 --- a/app/models/character.rb +++ b/app/models/character.rb @@ -29,12 +29,12 @@ class Character < ApplicationRecord def activity_time_remaining return nil unless self.activity - duration_data = self.activity.whatnot["duration"] - duration = duration_data["base"] - duration_data["scaling"].each do |skill, scaling_amount| + duration_data = self.activity.whatnot[:duration] + duration = duration_data[:base] + duration_data[:scaling].each do |skill, scaling_amount| duration -= self.skill_level(skill) * scaling_amount end - duration = [duration, duration_data["minimum"]].max + duration = [duration, duration_data[:minimum]].max duration - (Time.now - self.activity_started_at) end diff --git a/app/models/concerns/has_whatnot.rb b/app/models/concerns/has_whatnot.rb new file mode 100644 index 0000000..a4dcfa0 --- /dev/null +++ b/app/models/concerns/has_whatnot.rb @@ -0,0 +1,9 @@ +module HasWhatnot + extend ActiveSupport::Concern + + included do + def whatnot + @whatnot ||= super.is_a?(Hash) ? super.deep_symbolize_keys! : nil + end + end +end diff --git a/app/models/item.rb b/app/models/item.rb index 71482f5..d42460b 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -1,4 +1,6 @@ class Item < ApplicationRecord + include HasWhatnot + enum equip_slot: [:mainhand, :offhand, :head, :neck, :back, :torso, :grip, :left_ring, :right_ring, :waist, :legs, :feet, :curio] validates :gid, :name, :description, presence: true diff --git a/app/models/location.rb b/app/models/location.rb index e35e5e7..b7c5bf5 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -1,4 +1,6 @@ class Location < ApplicationRecord + include HasWhatnot + has_many :activities validates :gid, :name, presence: true end diff --git a/app/models/skill.rb b/app/models/skill.rb index b77165b..f97bb39 100644 --- a/app/models/skill.rb +++ b/app/models/skill.rb @@ -1,3 +1,5 @@ class Skill < ApplicationRecord + include HasWhatnot + validates :gid, :name, :description, presence: true end -- cgit v1.2.3