summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Gay <david@davidgay.org>2021-05-20 18:39:20 -0400
committerDavid Gay <david@davidgay.org>2021-05-20 18:39:20 -0400
commitb0998224427594acda8e4e8419a945c20c062568 (patch)
tree8da46effc1122281f4349f4800a12fd5076352d5
parentc25a079be25cc0aadf85119d97d8e1b9395b15fb (diff)
Implement learned activities
-rw-r--r--app/models/activity.rb2
-rw-r--r--app/models/character.rb2
-rw-r--r--app/models/learned_activity.rb6
-rw-r--r--data/activities.yml4
-rw-r--r--db/migrate/20210520222335_create_learned_activities.rb12
-rw-r--r--db/schema.rb14
-rw-r--r--test/fixtures/learned_activities.yml9
-rw-r--r--test/models/learned_activity_test.rb7
8 files changed, 55 insertions, 1 deletions
diff --git a/app/models/activity.rb b/app/models/activity.rb
index b8eeed7..7272a3b 100644
--- a/app/models/activity.rb
+++ b/app/models/activity.rb
@@ -4,6 +4,8 @@ class Activity < ApplicationRecord
belongs_to :location, optional: true
validates :gid, :name, :description, presence: true
+ attribute :innate, :boolean, default: false
+
def cost_string
requirements = []
self.whatnot[:cost].each do |cost|
diff --git a/app/models/character.rb b/app/models/character.rb
index 507548e..41dc951 100644
--- a/app/models/character.rb
+++ b/app/models/character.rb
@@ -3,6 +3,7 @@ class Character < ApplicationRecord
belongs_to :activity, optional: true
has_one :hearth
has_many :character_items
+ has_many :learned_activities
has_many :items, through: :character_items
has_many :character_skills
has_many :chat_messages
@@ -66,6 +67,7 @@ class Character < ApplicationRecord
end
def can_do_activity?(activity)
+ return false unless activity.innate? || self.learned_activities.exists?(activity: activity)
activity.whatnot[:cost]&.each do |cost|
case cost[:type]
when "item"
diff --git a/app/models/learned_activity.rb b/app/models/learned_activity.rb
new file mode 100644
index 0000000..58a6e93
--- /dev/null
+++ b/app/models/learned_activity.rb
@@ -0,0 +1,6 @@
+class LearnedActivity < ApplicationRecord
+ belongs_to :character
+ belongs_to :activity
+
+ validates :character_id, uniqueness: { scope: :activity_id }
+end
diff --git a/data/activities.yml b/data/activities.yml
index 562e29a..01a4c88 100644
--- a/data/activities.yml
+++ b/data/activities.yml
@@ -1,6 +1,7 @@
construct_foundation_level1:
name: "Construct Foundation Level 1"
description: "Build a level 1 foundation."
+ innate: true
whatnot:
duration:
base: 60
@@ -18,6 +19,7 @@ construct_foundation_level1:
construct_forge_level1:
name: "Construct Forge Level 1"
description: "Build a level 1 forge."
+ innate: true
whatnot:
requirements:
- type: "hearth_amenity"
@@ -36,6 +38,7 @@ construct_forge_level1:
craft_pig_iron_ingot:
name: "Smelt Pig Iron Ingot"
description: "Smelt a pig iron ingot."
+ innate: true
whatnot:
duration:
base: 70
@@ -58,6 +61,7 @@ quarry_floret_mines:
name: "Quarry Floret Mines"
description: "Planequarry at the Floret Mines."
location: "floret_region"
+ innate: true
whatnot:
duration:
base: 70
diff --git a/db/migrate/20210520222335_create_learned_activities.rb b/db/migrate/20210520222335_create_learned_activities.rb
new file mode 100644
index 0000000..55d76f1
--- /dev/null
+++ b/db/migrate/20210520222335_create_learned_activities.rb
@@ -0,0 +1,12 @@
+class CreateLearnedActivities < ActiveRecord::Migration[6.1]
+ def change
+ create_table :learned_activities do |t|
+ t.references :character, null: false, foreign_key: true
+ t.references :activity, null: false, foreign_key: true
+
+ t.timestamps
+ end
+
+ add_column :activities, :innate, :boolean
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index ee68bb9..562eacf 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 2021_05_20_014637) do
+ActiveRecord::Schema.define(version: 2021_05_20_222335) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -23,6 +23,7 @@ ActiveRecord::Schema.define(version: 2021_05_20_014637) do
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.bigint "location_id"
+ t.boolean "innate"
t.index ["gid"], name: "index_activities_on_gid"
t.index ["location_id"], name: "index_activities_on_location_id"
end
@@ -118,6 +119,15 @@ ActiveRecord::Schema.define(version: 2021_05_20_014637) do
t.index ["gid"], name: "index_items_on_gid"
end
+ create_table "learned_activities", force: :cascade do |t|
+ t.bigint "character_id", null: false
+ t.bigint "activity_id", null: false
+ t.datetime "created_at", precision: 6, null: false
+ t.datetime "updated_at", precision: 6, null: false
+ t.index ["activity_id"], name: "index_learned_activities_on_activity_id"
+ t.index ["character_id"], name: "index_learned_activities_on_character_id"
+ end
+
create_table "locations", force: :cascade do |t|
t.string "gid"
t.string "name"
@@ -178,5 +188,7 @@ ActiveRecord::Schema.define(version: 2021_05_20_014637) do
add_foreign_key "chat_messages", "characters", column: "target_id"
add_foreign_key "chat_messages", "chat_rooms"
add_foreign_key "hearths", "characters"
+ add_foreign_key "learned_activities", "activities"
+ add_foreign_key "learned_activities", "characters"
add_foreign_key "users", "characters", column: "active_character_id"
end
diff --git a/test/fixtures/learned_activities.yml b/test/fixtures/learned_activities.yml
new file mode 100644
index 0000000..d6a4245
--- /dev/null
+++ b/test/fixtures/learned_activities.yml
@@ -0,0 +1,9 @@
+# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+ character: one
+ activity: one
+
+two:
+ character: two
+ activity: two
diff --git a/test/models/learned_activity_test.rb b/test/models/learned_activity_test.rb
new file mode 100644
index 0000000..ff0a9bb
--- /dev/null
+++ b/test/models/learned_activity_test.rb
@@ -0,0 +1,7 @@
+require "test_helper"
+
+class LearnedActivityTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end