summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Gay <david@davidgay.org>2021-06-13 21:29:52 -0400
committerDavid Gay <david@davidgay.org>2021-06-13 21:29:52 -0400
commite78a3513632954cb53fc8c806158c1fc98357173 (patch)
treea8d7ac13bb4db9ef244bec57416fc08ef4345d6d
parent18c2378d06d5f9323e50eead92b3cf7dc61917b5 (diff)
ItemInfixes
-rw-r--r--app/controllers/characters/rankings_controller.rb2
-rw-r--r--app/models/character.rb6
-rw-r--r--app/models/item_infix.rb13
-rw-r--r--db/migrate/20210614004827_create_item_infixes.rb11
-rw-r--r--test/fixtures/item_infixes.yml11
-rw-r--r--test/models/item_infix_test.rb7
6 files changed, 49 insertions, 1 deletions
diff --git a/app/controllers/characters/rankings_controller.rb b/app/controllers/characters/rankings_controller.rb
index bbae9fc..429b487 100644
--- a/app/controllers/characters/rankings_controller.rb
+++ b/app/controllers/characters/rankings_controller.rb
@@ -6,4 +6,4 @@ class Characters::RankingsController < ApplicationController
def index
@character = Character.find(params[:character_id])
end
-end \ No newline at end of file
+end
diff --git a/app/models/character.rb b/app/models/character.rb
index d386002..03bd510 100644
--- a/app/models/character.rb
+++ b/app/models/character.rb
@@ -9,6 +9,7 @@ class Character < ApplicationRecord
has_many :character_items
has_many :learned_activities
has_many :items, through: :character_items
+ has_many :item_infixes
has_many :character_skills
has_many :conditions, through: :states
has_many :states
@@ -182,6 +183,11 @@ class Character < ApplicationRecord
end
end
+ def max_infixes(skill)
+ skill = Skill.find_by_gid(skill) if skill.is_a? String
+ 1 + (skill_level(skill) / 20).floor
+ end
+
def add_skill_xp(skill, amount)
skill = Skill.find_by_gid(skill) if skill.is_a? String
Character.transaction do
diff --git a/app/models/item_infix.rb b/app/models/item_infix.rb
new file mode 100644
index 0000000..cc99ee7
--- /dev/null
+++ b/app/models/item_infix.rb
@@ -0,0 +1,13 @@
+class ItemInfix < ApplicationRecord
+ belongs_to :character
+ belongs_to :item
+ belongs_to :skill
+
+ before_create :check_max_infixes
+
+ private
+ def check_max_infixes
+ current_infixes = character.item_infixes.where(skill: skill)
+ raise :abort if current_infixes.count >= character.max_infixes(skill)
+ end
+end
diff --git a/db/migrate/20210614004827_create_item_infixes.rb b/db/migrate/20210614004827_create_item_infixes.rb
new file mode 100644
index 0000000..5b49892
--- /dev/null
+++ b/db/migrate/20210614004827_create_item_infixes.rb
@@ -0,0 +1,11 @@
+class CreateItemInfixes < ActiveRecord::Migration[6.1]
+ def change
+ create_table :item_infixes do |t|
+ t.references :character, null: false, foreign_key: true
+ t.references :item, null: false, foreign_key: true
+ t.references :skill, null: false, foreign_key: true
+
+ t.timestamps
+ end
+ end
+end
diff --git a/test/fixtures/item_infixes.yml b/test/fixtures/item_infixes.yml
new file mode 100644
index 0000000..535831c
--- /dev/null
+++ b/test/fixtures/item_infixes.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+ character: one
+ item: one
+ skill: one
+
+two:
+ character: two
+ item: two
+ skill: two
diff --git a/test/models/item_infix_test.rb b/test/models/item_infix_test.rb
new file mode 100644
index 0000000..a6f63a3
--- /dev/null
+++ b/test/models/item_infix_test.rb
@@ -0,0 +1,7 @@
+require "test_helper"
+
+class ItemInfixTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end