Update grid_character.rb
- Adds code transforming incoming ring and awakening values into something the db understands
This commit is contained in:
parent
241393661c
commit
52225fe40b
1 changed files with 27 additions and 0 deletions
|
|
@ -15,6 +15,12 @@ class GridCharacter < ApplicationRecord
|
||||||
validate :validate_aetherial_mastery_value, on: :update
|
validate :validate_aetherial_mastery_value, on: :update
|
||||||
validate :over_mastery_attack_matches_hp, on: :update
|
validate :over_mastery_attack_matches_hp, on: :update
|
||||||
|
|
||||||
|
# Virtual attribute for the new rings structure
|
||||||
|
attr_accessor :new_rings
|
||||||
|
|
||||||
|
# Virtual attribute for the new awakening structure
|
||||||
|
attr_accessor :new_awakening
|
||||||
|
|
||||||
##### Amoeba configuration
|
##### Amoeba configuration
|
||||||
amoeba do
|
amoeba do
|
||||||
set ring1: { modifier: nil, strength: nil }
|
set ring1: { modifier: nil, strength: nil }
|
||||||
|
|
@ -25,6 +31,9 @@ class GridCharacter < ApplicationRecord
|
||||||
set perpetuity: false
|
set perpetuity: false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
before_validation :apply_new_rings, if: -> { new_rings.present? }
|
||||||
|
before_validation :apply_new_awakening, if: -> { new_awakening.present? }
|
||||||
|
|
||||||
# Add awakening before the model saves
|
# Add awakening before the model saves
|
||||||
before_save :add_awakening
|
before_save :add_awakening
|
||||||
|
|
||||||
|
|
@ -90,6 +99,24 @@ class GridCharacter < ApplicationRecord
|
||||||
self.awakening = Awakening.where(slug: 'character-balanced').sole
|
self.awakening = Awakening.where(slug: 'character-balanced').sole
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def apply_new_rings
|
||||||
|
# Expect new_rings to be an array of hashes, e.g.,
|
||||||
|
# [{"modifier" => "1", "strength" => "1500"}, {"modifier" => "2", "strength" => "750"}]
|
||||||
|
default_ring = { "modifier" => nil, "strength" => nil }
|
||||||
|
rings_array = Array(new_rings).map(&:to_h)
|
||||||
|
# Pad with defaults so there are exactly four rings
|
||||||
|
rings_array.fill(default_ring, rings_array.size...4)
|
||||||
|
self.ring1 = rings_array[0]
|
||||||
|
self.ring2 = rings_array[1]
|
||||||
|
self.ring3 = rings_array[2]
|
||||||
|
self.ring4 = rings_array[3]
|
||||||
|
end
|
||||||
|
|
||||||
|
def apply_new_awakening
|
||||||
|
self.awakening_id = new_awakening[:id]
|
||||||
|
self.awakening_level = new_awakening[:level].present? ? new_awakening[:level].to_i : 1
|
||||||
|
end
|
||||||
|
|
||||||
def check_value(property, type)
|
def check_value(property, type)
|
||||||
# Input format
|
# Input format
|
||||||
# { ring1: { atk: 300 } }
|
# { ring1: { atk: 300 } }
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue