From d64e5aab5c5e65fe1f8e285706b53888170e9a7e Mon Sep 17 00:00:00 2001 From: Jason Gessner Date: Sun, 13 Feb 2022 12:29:32 -0600 Subject: [PATCH 1/4] Move to codes as primary keys for the models. Update the importer to deal with all these changes. Remove legality models since we don't have a fleshed out design for formats and banlists yet. --- app/models/card.rb | 26 +++++-- app/models/card_set.rb | 13 ++++ app/models/card_set_type.rb | 9 +++ app/models/card_subtype.rb | 14 ++++ app/models/card_subtypes.rb | 6 -- app/models/card_type.rb | 6 +- app/models/cycle.rb | 10 +++ app/models/faction.rb | 7 +- app/models/legality.rb | 7 -- app/models/legality_type.rb | 5 -- app/models/nr_cycle.rb | 5 -- app/models/nr_set.rb | 6 -- app/models/nr_set_type.rb | 5 -- app/models/printing.rb | 11 ++- app/models/side.rb | 7 +- app/models/subtype.rb | 7 +- db/migrate/.keep | 0 db/schema.rb | 143 +++++++++++++++--------------------- lib/tasks/cards.rake | 128 ++++++++++++++++---------------- 19 files changed, 222 insertions(+), 193 deletions(-) create mode 100644 app/models/card_set.rb create mode 100644 app/models/card_set_type.rb create mode 100644 app/models/card_subtype.rb delete mode 100644 app/models/card_subtypes.rb create mode 100644 app/models/cycle.rb delete mode 100644 app/models/legality.rb delete mode 100644 app/models/legality_type.rb delete mode 100644 app/models/nr_cycle.rb delete mode 100644 app/models/nr_set.rb delete mode 100644 app/models/nr_set_type.rb create mode 100644 db/migrate/.keep diff --git a/app/models/card.rb b/app/models/card.rb index 56698860..7f9a492b 100644 --- a/app/models/card.rb +++ b/app/models/card.rb @@ -1,18 +1,30 @@ # frozen_string_literal: true class Card < ApplicationRecord - belongs_to :side - belongs_to :faction - belongs_to :card_type - has_and_belongs_to_many :subtype_relations, class_name: 'Subtype' - has_many :printings - has_many :legality + self.primary_key = :code + + belongs_to :side, + :primary_key => :code, + :foreign_key => :side_code + belongs_to :faction, + :primary_key => :code, + :foreign_key => :faction_code + belongs_to :card_type, + :primary_key => :code, + :foreign_key => :card_type_code + has_many :card_subtypes, + :primary_key => :code, + :foreign_key => :card_code + has_many :subtypes, :through => :card_subtypes + has_many :printings, + :primary_key => :code, + :foreign_key => :card_code validates :code, uniqueness: true validates :name, uniqueness: true def versions - printings.includes(nr_set: :nr_cycle).order(date_release: :desc) + printings.includes(card_set: :cycle).order(date_release: :desc) end def strength_selector diff --git a/app/models/card_set.rb b/app/models/card_set.rb new file mode 100644 index 00000000..0bfadbe8 --- /dev/null +++ b/app/models/card_set.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class CardSet < ApplicationRecord + self.primary_key = :code + + belongs_to :cycle, + optional: true, + :primary_key => :code, + :foreign_key => :cycle_code + belongs_to :card_set_type, + :primary_key => :code, + :foreign_key => :card_set_type_code +end diff --git a/app/models/card_set_type.rb b/app/models/card_set_type.rb new file mode 100644 index 00000000..d007bd5f --- /dev/null +++ b/app/models/card_set_type.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class CardSetType < ApplicationRecord + self.primary_key = :code + + has_many :card_sets, + :primary_key => :code, + :foreign_key => :card_set_type_code +end diff --git a/app/models/card_subtype.rb b/app/models/card_subtype.rb new file mode 100644 index 00000000..7b5e082a --- /dev/null +++ b/app/models/card_subtype.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class CardSubtype < ApplicationRecord + self.table_name = "cards_subtypes" + + belongs_to :cards, + :class_name => "Card", + :primary_key => :code, + :foreign_key => :card_code + belongs_to :subtypes, + :class_name => "Subtype", + :primary_key => :code, + :foreign_key => :subtype_code +end diff --git a/app/models/card_subtypes.rb b/app/models/card_subtypes.rb deleted file mode 100644 index 02374988..00000000 --- a/app/models/card_subtypes.rb +++ /dev/null @@ -1,6 +0,0 @@ -# frozen_string_literal: true - -class CardSubtypes < ApplicationRecord - belongs_to :cards - belongs_to :subtypes -end diff --git a/app/models/card_type.rb b/app/models/card_type.rb index 7e772a86..ec6b4cd3 100644 --- a/app/models/card_type.rb +++ b/app/models/card_type.rb @@ -1,5 +1,9 @@ # frozen_string_literal: true class CardType < ApplicationRecord - has_many :cards + self.primary_key = :code + + has_many :cards, + :primary_key => :code, + :foreign_key => :card_type_code end diff --git a/app/models/cycle.rb b/app/models/cycle.rb new file mode 100644 index 00000000..4c8cc3b4 --- /dev/null +++ b/app/models/cycle.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class Cycle < ApplicationRecord + self.primary_key = :code + + # TODO(plural): Add association path for cycles -> printings + has_many :card_sets, + :primary_key => :code, + :foreign_key => :cycle_code +end diff --git a/app/models/faction.rb b/app/models/faction.rb index 68aa2cb0..9db948ad 100644 --- a/app/models/faction.rb +++ b/app/models/faction.rb @@ -1,5 +1,10 @@ # frozen_string_literal: true +# TODO(plural): add a side code to faction. class Faction < ApplicationRecord - has_many :cards + self.primary_key = :code + + has_many :cards, + :primary_key => :code, + :foreign_key => :faction_code end diff --git a/app/models/legality.rb b/app/models/legality.rb deleted file mode 100644 index 29d4389b..00000000 --- a/app/models/legality.rb +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true - -class Legality < ApplicationRecord - belongs_to :legality_type - belongs_to :deck_format - belongs_to :card -end diff --git a/app/models/legality_type.rb b/app/models/legality_type.rb deleted file mode 100644 index f9d7e774..00000000 --- a/app/models/legality_type.rb +++ /dev/null @@ -1,5 +0,0 @@ -# frozen_string_literal: true - -class LegalityType < ApplicationRecord - has_many :legalities -end diff --git a/app/models/nr_cycle.rb b/app/models/nr_cycle.rb deleted file mode 100644 index c8907cd8..00000000 --- a/app/models/nr_cycle.rb +++ /dev/null @@ -1,5 +0,0 @@ -# frozen_string_literal: true - -class NrCycle < ApplicationRecord - has_many :nr_sets -end diff --git a/app/models/nr_set.rb b/app/models/nr_set.rb deleted file mode 100644 index 3657dd04..00000000 --- a/app/models/nr_set.rb +++ /dev/null @@ -1,6 +0,0 @@ -# frozen_string_literal: true - -class NrSet < ApplicationRecord - belongs_to :nr_cycle, optional: true - belongs_to :nr_set_type -end diff --git a/app/models/nr_set_type.rb b/app/models/nr_set_type.rb deleted file mode 100644 index 44d78fd5..00000000 --- a/app/models/nr_set_type.rb +++ /dev/null @@ -1,5 +0,0 @@ -# frozen_string_literal: true - -class NrSetType < ApplicationRecord - has_many :nr_sets -end diff --git a/app/models/printing.rb b/app/models/printing.rb index 3e54e6eb..ca7206ab 100644 --- a/app/models/printing.rb +++ b/app/models/printing.rb @@ -1,8 +1,15 @@ # frozen_string_literal: true class Printing < ApplicationRecord - belongs_to :card - belongs_to :nr_set + self.primary_key = :code + + belongs_to :card, + :primary_key => :code, + :foreign_key => :card_code + belongs_to :card_set, + :primary_key => :code, + :foreign_key => :card_set_code + # TODO(plural): Add an association to cycle. def format_flavor t = flavor || '' diff --git a/app/models/side.rb b/app/models/side.rb index e160c981..ccd2b6ef 100644 --- a/app/models/side.rb +++ b/app/models/side.rb @@ -1,5 +1,10 @@ # frozen_string_literal: true class Side < ApplicationRecord - has_many :cards + self.primary_key = :code + + # TODO(plural): Add an association for factions. + has_many :cards, + :primary_key => :code, + :foreign_key => :side_code end diff --git a/app/models/subtype.rb b/app/models/subtype.rb index bf65e88f..936862f7 100644 --- a/app/models/subtype.rb +++ b/app/models/subtype.rb @@ -1,5 +1,10 @@ # frozen_string_literal: true class Subtype < ApplicationRecord - has_and_belongs_to_many :cards + self.primary_key = :code + + has_many :card_subtypes, + :primary_key => :code, + :foreign_key => :subtype_code + has_many :cards, :through => :card_subtypes end diff --git a/db/migrate/.keep b/db/migrate/.keep new file mode 100644 index 00000000..e69de29b diff --git a/db/schema.rb b/db/schema.rb index f65d54e2..c2550625 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,121 +10,113 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20220114191001) do +ActiveRecord::Schema.define(version: 2022_02_13_014409) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" - create_table "card_types", force: :cascade do |t| + create_table "card_set_types", id: false, force: :cascade do |t| + t.text "code", null: false + t.text "name", null: false + t.text "description" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["code"], name: "index_card_set_types_on_code", unique: true + end + + create_table "card_sets", id: false, force: :cascade do |t| + t.text "code", null: false + t.text "name", null: false + t.date "date_release" + t.integer "size" + t.text "cycle_code" + t.text "card_set_type_code" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["card_set_type_code"], name: "index_card_sets_on_card_set_type_code" + t.index ["code"], name: "index_card_sets_on_code", unique: true + t.index ["cycle_code"], name: "index_card_sets_on_cycle_code" + end + + create_table "card_types", id: false, force: :cascade do |t| t.text "code", null: false t.text "name", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false - t.index ["code"], name: "index_card_types_on_code", unique: true + t.index ["code"], name: "index_non_id_card_type_pk", unique: true end - create_table "cards", force: :cascade do |t| + create_table "cards", id: false, force: :cascade do |t| + t.text "code", null: false + t.text "name", null: false t.integer "advancement_requirement" t.integer "agenda_points" t.integer "base_link" - t.text "code", null: false t.integer "cost" t.integer "deck_limit" - t.integer "faction_id" + t.text "faction_code" t.integer "influence_cost" t.integer "influence_limit" t.integer "memory_cost" t.integer "minimum_deck_size" - t.text "name", null: false - t.integer "side_id" + t.text "side_code" t.integer "strength" t.text "subtypes" t.text "text" t.integer "trash_cost" - t.integer "card_type_id" + t.text "card_type_code", null: false t.boolean "uniqueness" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false - t.index ["card_type_id"], name: "index_cards_on_card_type_id" + t.index ["card_type_code"], name: "index_cards_on_card_type_code" t.index ["code"], name: "index_cards_on_code", unique: true - t.index ["faction_id"], name: "index_cards_on_faction_id" - t.index ["side_id"], name: "index_cards_on_side_id" + t.index ["faction_code"], name: "index_cards_on_faction_code" + t.index ["side_code"], name: "index_cards_on_side_code" end create_table "cards_subtypes", id: false, force: :cascade do |t| - t.integer "card_id", null: false - t.integer "subtype_id", null: false - t.index ["subtype_id"], name: "index_cards_subtypes_on_subtype_id" + t.text "card_code", null: false + t.text "subtype_code", null: false + t.index ["card_code", "subtype_code"], name: "index_cards_subtypes_on_card_code_and_subtype_code" end - create_table "factions", force: :cascade do |t| - t.text "code", null: false - t.boolean "is_mini", null: false - t.text "name", null: false - t.datetime "created_at", precision: 6, null: false - t.datetime "updated_at", precision: 6, null: false - t.index ["code"], name: "index_factions_on_code", unique: true - end - - create_table "legality_types", force: :cascade do |t| - t.text "name", null: false - t.text "code", null: false - t.datetime "created_at", precision: 6, null: false - t.datetime "updated_at", precision: 6, null: false - t.index ["code"], name: "index_legality_types_on_code", unique: true - end - - create_table "nr_cycles", force: :cascade do |t| + create_table "cycles", id: false, force: :cascade do |t| t.text "code", null: false t.text "name", null: false t.text "description" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false - t.index ["code"], name: "index_nr_cycles_on_code", unique: true + t.index ["code"], name: "index_cycles_on_code", unique: true end - create_table "nr_set_types", force: :cascade do |t| - t.text "code", null: false - t.text "name", null: false - t.text "description" - t.datetime "created_at", precision: 6, null: false - t.datetime "updated_at", precision: 6, null: false - t.index ["code"], name: "index_nr_set_types_on_code", unique: true - end - - create_table "nr_sets", force: :cascade do |t| + create_table "factions", id: false, force: :cascade do |t| t.text "code", null: false + t.boolean "is_mini", null: false t.text "name", null: false - t.date "date_release" - t.integer "size" - t.integer "nr_cycle_id" - t.integer "nr_set_type_id" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false - t.index ["code"], name: "index_nr_sets_on_code", unique: true - t.index ["nr_cycle_id"], name: "index_nr_sets_on_nr_cycle_id" - t.index ["nr_set_type_id"], name: "index_nr_sets_on_nr_set_type_id" + t.index ["code"], name: "index_factions_on_code", unique: true end - create_table "printings", force: :cascade do |t| + create_table "printings", id: false, force: :cascade do |t| + t.text "code" + t.text "card_code" t.text "printed_text" t.boolean "printed_uniqueness" - t.text "code" t.text "flavor" t.text "illustrator" t.integer "position" t.integer "quantity" t.date "date_release" - t.integer "card_id" - t.integer "nr_set_id" + t.text "card_set_code" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false - t.index ["card_id"], name: "index_printings_on_card_id" + t.index ["card_code"], name: "index_printings_on_card_code" + t.index ["card_set_code"], name: "index_printings_on_card_set_code" t.index ["code"], name: "index_printings_on_code", unique: true - t.index ["nr_set_id"], name: "index_printings_on_nr_set_id" end - create_table "sides", force: :cascade do |t| + create_table "sides", id: false, force: :cascade do |t| t.text "code", null: false t.text "name", null: false t.datetime "created_at", precision: 6, null: false @@ -132,7 +124,7 @@ t.index ["code"], name: "index_sides_on_code", unique: true end - create_table "subtypes", force: :cascade do |t| + create_table "subtypes", id: false, force: :cascade do |t| t.text "code", null: false t.text "name", null: false t.datetime "created_at", precision: 6, null: false @@ -140,28 +132,13 @@ t.index ["code"], name: "index_subtypes_on_code", unique: true end -# create_table "users", force: :cascade do |t| -# t.string "email", default: "", null: false -# t.string "encrypted_password", default: "", null: false -# t.string "reset_password_token" -# t.datetime "reset_password_sent_at", precision: 6 -# t.datetime "remember_created_at", precision: 6 -# t.datetime "created_at", precision: 6, null: false -# t.datetime "updated_at", precision: 6, null: false -# t.string "name", null: false -# t.string "confirmation_token" -# t.datetime "confirmed_at", precision: 6 -# t.datetime "confirmation_sent_at", precision: 6 -# t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true -# t.index ["email"], name: "index_users_on_email", unique: true -# t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true -# end - - add_foreign_key "cards", "card_types" - add_foreign_key "cards", "factions" - add_foreign_key "cards", "sides" - add_foreign_key "nr_sets", "nr_cycles" - add_foreign_key "nr_sets", "nr_set_types" - add_foreign_key "printings", "cards" - add_foreign_key "printings", "nr_sets" + add_foreign_key "cards", "card_types", column: "card_type_code", primary_key: "code" + add_foreign_key "card_sets", "card_set_types", column: "card_set_type_code", primary_key: "code" + add_foreign_key "card_sets", "cycles", column: "cycle_code", primary_key: "code" + add_foreign_key "cards", "factions", column: "faction_code", primary_key: "code" + add_foreign_key "cards", "sides", column: "side_code", primary_key: "code" + add_foreign_key "cards_subtypes", "cards", column: "card_code", primary_key: "code" + add_foreign_key "cards_subtypes", "subtypes", column: "subtype_code", primary_key: "code" + add_foreign_key "printings", "cards", column: "card_code", primary_key: "code" + add_foreign_key "printings", "card_sets", column: "card_set_code", primary_key: "code" end diff --git a/lib/tasks/cards.rake b/lib/tasks/cards.rake index d728b0c6..4eb38dc6 100644 --- a/lib/tasks/cards.rake +++ b/lib/tasks/cards.rake @@ -152,13 +152,13 @@ namespace :cards do def import_card_subtypes subtypes = Subtype.all.index_by(&:code) cards = Card.all - card_id_to_subtype_id = [] + card_code_to_subtype_code = [] cards.each { |c| keywords_to_subtype_codes(c.subtypes).each { |k,v| - card_id_to_subtype_id << [c.id, subtypes[k].id] + card_code_to_subtype_code << [c.code, subtypes[k].code] } } - puts "Have to insert %d card -> subtype mappings." % card_id_to_subtype_id.length + puts "Have to insert %d card -> subtype mappings." % card_code_to_subtype_code.length # Use a transaction since we are deleting the mapping table. ActiveRecord::Base.transaction do puts 'Clear out existing card -> subtype mappings' @@ -168,13 +168,15 @@ namespace :cards do end num_assoc = 0 - card_id_to_subtype_id.each_slice(250) { |m| + card_code_to_subtype_code.each_slice(250) { |m| num_assoc += m.length puts ' %d card -> subtype associations' % num_assoc - sql = "INSERT INTO cards_subtypes (card_id, subtype_id) VALUES " + sql = "INSERT INTO cards_subtypes (card_code, subtype_code) VALUES " vals = [] m.each { |m| - vals << "(%d, %d)" % [m[0], m[1]] + # TODO(plural): use the associations object for this or ensure this is safe + puts m + vals << "('%s', '%s')" % [m[0], m[1]] } sql << vals.join(", ") unless ActiveRecord::Base.connection.execute(sql) @@ -193,7 +195,7 @@ namespace :cards do name: c['name'], } end - NrCycle.import cycles, on_duplicate_key_update: { conflict_target: [ :code ], columns: :all } + Cycle.import cycles, on_duplicate_key_update: { conflict_target: [ :code ], columns: :all } end def import_set_types @@ -208,30 +210,30 @@ namespace :cards do { code: 'expansion', name: 'Expansion' }, { code: 'promo', name: 'Promo' } ] - NrSetType.import set_types, on_duplicate_key_update: { conflict_target: [ :code ], columns: :all } + CardSetType.import set_types, on_duplicate_key_update: { conflict_target: [ :code ], columns: :all } end def import_sets(path) # TODO(plural): Get mappings into the JSON files. set_type_mapping = { - "terminal-directive-campaign" => 1, # campaign - "revised-core-set" => 2, # core - "system-gateway" => 2, # core - "system-core-2019" => 2, # core - "core-set" => 2, # core - "system-update-2021" => 2, # core - "reign-and-reverie" => 4, # deluxe - "data-and-destiny" => 4, # deluxe - "order-and-chaos" => 4, # deluxe - "creation-and-control" => 4, # deluxe - "honor-and-profit" => 4, # deluxe - "draft" => 5, # draft - "magnum-opus" => 6, # expansion - "magnum-opus-reprint" => 6, # expansion - "uprising-booster-pack" => 6, # expansion - "napd-multiplayer" => 7, # promo + "terminal-directive-campaign" => 'campaign', + "revised-core-set" => 'core', + "system-gateway" => 'core', + "system-core-2019" => 'core', + "core-set" => 'core', + "system-update-2021" => 'core', + "reign-and-reverie" => 'deluxe', + "data-and-destiny" => 'deluxe', + "order-and-chaos" => 'deluxe', + "creation-and-control" => 'deluxe', + "honor-and-profit" => 'deluxe', + "draft" => 'draft', + "magnum-opus" => 'expansion', + "magnum-opus-reprint" => 'expansion', + "uprising-booster-pack" => 'expansion', + "napd-multiplayer" => 'promo', } - cycles = NrCycle.all.index_by(&:code) + cycles = Cycle.all.index_by(&:code) sets = JSON.parse(File.read(path)) # TODO(plural): Get the updated code values in the JSON files, probably with a new name. sets.map! do |s| @@ -241,11 +243,11 @@ namespace :cards do # TODO(plural): Make this a proper date type, not a string. "date_release": s["date_release"], "size": s["size"], - "nr_cycle_id": cycles[s["cycle_code"]].id, - "nr_set_type_id": set_type_mapping.fetch(set_name_to_code(s["name"]), 3) + "cycle_code": cycles[s["cycle_code"]].code, + "card_set_type_code": set_type_mapping.fetch(set_name_to_code(s["name"]), "data_pack") } end - NrSet.import sets, on_duplicate_key_update: { conflict_target: [ :code ], columns: :all } + CardSet.import sets, on_duplicate_key_update: { conflict_target: [ :code ], columns: :all } end def import_printings(pack_cards_json, packs_path) @@ -255,12 +257,12 @@ namespace :cards do old_pack_code_to_set_code[r["code"]] = set_name_to_code(r["name"]) } raw_cards = Card.all.index_by(&:code) - nr_sets = NrSet.all.index_by(&:code) + sets = CardSet.all.index_by(&:code) new_printings = [] pack_cards_json.each { |set_card| card = raw_cards[stripped_title_to_card_code(set_card["stripped_title"])] - nr_set = nr_sets[old_pack_code_to_set_code[set_card["pack_code"]]] + set = sets[old_pack_code_to_set_code[set_card["pack_code"]]] new_printings << Printing.new( printed_text: card.text, @@ -270,9 +272,9 @@ namespace :cards do illustrator: set_card["illustrator"], position: set_card["position"], quantity: set_card["quantity"], - date_release: nr_set["date_release"], + date_release: set["date_release"], card: card, - nr_set: nr_set + card_set: set ) } @@ -285,41 +287,41 @@ namespace :cards do end task :import, [:json_dir] => [:environment] do |t, args| - args.with_defaults(:json_dir => '/netrunner-cards-json/') - puts 'Import card data...' - - puts 'Importing Sides...' - import_sides(args[:json_dir] + '/sides.json') - - puts 'Import factions...' - import_factions(args[:json_dir] + '/factions.json') - - puts 'Importing Types...' - import_types(args[:json_dir] + '/types.json') - - # The JSON from the files in packs/ are used by multiple methods. - pack_cards_json = load_pack_card_files(args[:json_dir] + '/pack/*.json') - - puts 'Importing Subtypes...,' - import_subtypes(pack_cards_json) - - puts 'Importing Cards...' - import_cards(pack_cards_json) - - puts 'Importing Cycles...' - import_cycles(args[:json_dir] + '/cycles.json') - - puts 'Importing Set Types...' - import_set_types() - - puts 'Importing Sets...' - import_sets(args[:json_dir] + '/packs.json') +# args.with_defaults(:json_dir => '/netrunner-cards-json/') +# puts 'Import card data...' +# +# puts 'Importing Sides...' +# import_sides(args[:json_dir] + '/sides.json') +# +# puts 'Import factions...' +# import_factions(args[:json_dir] + '/factions.json') +# +# puts 'Importing Types...' +# import_types(args[:json_dir] + '/types.json') +# +# # The JSON from the files in packs/ are used by multiple methods. +# pack_cards_json = load_pack_card_files(args[:json_dir] + '/pack/*.json') +# +# puts 'Importing Subtypes...,' +# import_subtypes(pack_cards_json) +# +# puts 'Importing Cards...' +# import_cards(pack_cards_json) +# +# puts 'Importing Cycles...' +# import_cycles(args[:json_dir] + '/cycles.json') +# +# puts 'Importing Card Set Types...' +# import_set_types() +# +# puts 'Importing Sets...' +# import_sets(args[:json_dir] + '/packs.json') puts 'Importing Subtypes for Cards...' import_card_subtypes() - puts('Importing Printings...') - import_printings(pack_cards_json, args[:json_dir] + '/packs.json') +# puts('Importing Printings...') +# import_printings(pack_cards_json, args[:json_dir] + '/packs.json') puts 'Done!' end From 854cfcbfb3ef85809d7259acd444344cdf573d6d Mon Sep 17 00:00:00 2001 From: Jason Gessner Date: Sun, 13 Feb 2022 17:35:56 -0600 Subject: [PATCH 2/4] Switch code fields to id fields to reduce extra config. --- app/models/card.rb | 41 +++------ app/models/card_cycle.rb | 6 ++ app/models/card_set.rb | 11 +-- app/models/card_set_type.rb | 6 +- app/models/card_subtype.rb | 8 +- app/models/card_type.rb | 6 +- app/models/cycle.rb | 10 -- app/models/faction.rb | 6 +- app/models/printing.rb | 10 +- app/models/side.rb | 6 +- app/models/subtype.rb | 6 +- db/schema.rb | 87 +++++++----------- lib/tasks/cards.rake | 179 ++++++++++++++++++------------------ 13 files changed, 156 insertions(+), 226 deletions(-) create mode 100644 app/models/card_cycle.rb delete mode 100644 app/models/cycle.rb diff --git a/app/models/card.rb b/app/models/card.rb index 7f9a492b..30df5eb9 100644 --- a/app/models/card.rb +++ b/app/models/card.rb @@ -1,26 +1,13 @@ # frozen_string_literal: true class Card < ApplicationRecord - self.primary_key = :code - - belongs_to :side, - :primary_key => :code, - :foreign_key => :side_code - belongs_to :faction, - :primary_key => :code, - :foreign_key => :faction_code - belongs_to :card_type, - :primary_key => :code, - :foreign_key => :card_type_code - has_many :card_subtypes, - :primary_key => :code, - :foreign_key => :card_code + belongs_to :side + belongs_to :faction + belongs_to :card_type + has_many :card_subtypes has_many :subtypes, :through => :card_subtypes - has_many :printings, - :primary_key => :code, - :foreign_key => :card_code + has_many :printings - validates :code, uniqueness: true validates :name, uniqueness: true def versions @@ -39,15 +26,15 @@ def strength_selector end end - def type_builder - c_type = "#{card_type.name}".dup - c_type << if subtypes.present? - ": #{subtypes}" - else - '' - end - c_type.freeze - end +# def type_builder +# c_type = "#{card_type.name}".dup +# c_type << if subtypes.present? +# ": #{subtypes}" +# else +# '' +# end +# c_type.freeze +# end def format_text t = text || ''.dup diff --git a/app/models/card_cycle.rb b/app/models/card_cycle.rb new file mode 100644 index 00000000..b5488fc4 --- /dev/null +++ b/app/models/card_cycle.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +class CardCycle < ApplicationRecord + has_many :card_sets + # TODO(plural): Add association path for cycles -> printings +end diff --git a/app/models/card_set.rb b/app/models/card_set.rb index 0bfadbe8..16c74e3d 100644 --- a/app/models/card_set.rb +++ b/app/models/card_set.rb @@ -1,13 +1,6 @@ # frozen_string_literal: true class CardSet < ApplicationRecord - self.primary_key = :code - - belongs_to :cycle, - optional: true, - :primary_key => :code, - :foreign_key => :cycle_code - belongs_to :card_set_type, - :primary_key => :code, - :foreign_key => :card_set_type_code + belongs_to :card_cycle + belongs_to :card_set_type end diff --git a/app/models/card_set_type.rb b/app/models/card_set_type.rb index d007bd5f..68b4c801 100644 --- a/app/models/card_set_type.rb +++ b/app/models/card_set_type.rb @@ -1,9 +1,5 @@ # frozen_string_literal: true class CardSetType < ApplicationRecord - self.primary_key = :code - - has_many :card_sets, - :primary_key => :code, - :foreign_key => :card_set_type_code + has_many :card_sets end diff --git a/app/models/card_subtype.rb b/app/models/card_subtype.rb index 7b5e082a..16057627 100644 --- a/app/models/card_subtype.rb +++ b/app/models/card_subtype.rb @@ -5,10 +5,10 @@ class CardSubtype < ApplicationRecord belongs_to :cards, :class_name => "Card", - :primary_key => :code, - :foreign_key => :card_code + :primary_key => :id, + :foreign_key => :card_id belongs_to :subtypes, :class_name => "Subtype", - :primary_key => :code, - :foreign_key => :subtype_code + :primary_key => :id, + :foreign_key => :subtype_id end diff --git a/app/models/card_type.rb b/app/models/card_type.rb index ec6b4cd3..7e772a86 100644 --- a/app/models/card_type.rb +++ b/app/models/card_type.rb @@ -1,9 +1,5 @@ # frozen_string_literal: true class CardType < ApplicationRecord - self.primary_key = :code - - has_many :cards, - :primary_key => :code, - :foreign_key => :card_type_code + has_many :cards end diff --git a/app/models/cycle.rb b/app/models/cycle.rb deleted file mode 100644 index 4c8cc3b4..00000000 --- a/app/models/cycle.rb +++ /dev/null @@ -1,10 +0,0 @@ -# frozen_string_literal: true - -class Cycle < ApplicationRecord - self.primary_key = :code - - # TODO(plural): Add association path for cycles -> printings - has_many :card_sets, - :primary_key => :code, - :foreign_key => :cycle_code -end diff --git a/app/models/faction.rb b/app/models/faction.rb index 9db948ad..7a2d75c8 100644 --- a/app/models/faction.rb +++ b/app/models/faction.rb @@ -2,9 +2,5 @@ # TODO(plural): add a side code to faction. class Faction < ApplicationRecord - self.primary_key = :code - - has_many :cards, - :primary_key => :code, - :foreign_key => :faction_code + has_many :cards end diff --git a/app/models/printing.rb b/app/models/printing.rb index ca7206ab..92e8c427 100644 --- a/app/models/printing.rb +++ b/app/models/printing.rb @@ -1,14 +1,8 @@ # frozen_string_literal: true class Printing < ApplicationRecord - self.primary_key = :code - - belongs_to :card, - :primary_key => :code, - :foreign_key => :card_code - belongs_to :card_set, - :primary_key => :code, - :foreign_key => :card_set_code + belongs_to :card + belongs_to :card_set # TODO(plural): Add an association to cycle. def format_flavor diff --git a/app/models/side.rb b/app/models/side.rb index ccd2b6ef..e0e667be 100644 --- a/app/models/side.rb +++ b/app/models/side.rb @@ -1,10 +1,6 @@ # frozen_string_literal: true class Side < ApplicationRecord - self.primary_key = :code - # TODO(plural): Add an association for factions. - has_many :cards, - :primary_key => :code, - :foreign_key => :side_code + has_many :cards end diff --git a/app/models/subtype.rb b/app/models/subtype.rb index 936862f7..1db5c159 100644 --- a/app/models/subtype.rb +++ b/app/models/subtype.rb @@ -1,10 +1,6 @@ # frozen_string_literal: true class Subtype < ApplicationRecord - self.primary_key = :code - - has_many :card_subtypes, - :primary_key => :code, - :foreign_key => :subtype_code + has_many :card_subtypes has_many :cards, :through => :card_subtypes end diff --git a/db/schema.rb b/db/schema.rb index c2550625..d5b07fb2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -15,92 +15,79 @@ # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" - create_table "card_set_types", id: false, force: :cascade do |t| - t.text "code", null: false + create_table "card_set_types", id: :string, force: :cascade do |t| t.text "name", null: false t.text "description" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false - t.index ["code"], name: "index_card_set_types_on_code", unique: true end - create_table "card_sets", id: false, force: :cascade do |t| - t.text "code", null: false + create_table "card_sets", id: :string, force: :cascade do |t| t.text "name", null: false t.date "date_release" t.integer "size" - t.text "cycle_code" - t.text "card_set_type_code" + t.text "card_cycle_id" + t.text "card_set_type_id" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false - t.index ["card_set_type_code"], name: "index_card_sets_on_card_set_type_code" - t.index ["code"], name: "index_card_sets_on_code", unique: true - t.index ["cycle_code"], name: "index_card_sets_on_cycle_code" end - create_table "card_types", id: false, force: :cascade do |t| - t.text "code", null: false + create_table "card_types", id: :string, force: :cascade do |t| t.text "name", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false - t.index ["code"], name: "index_non_id_card_type_pk", unique: true end - create_table "cards", id: false, force: :cascade do |t| - t.text "code", null: false + create_table "cards", id: :string, force: :cascade do |t| t.text "name", null: false + t.text "card_type_id", null: false + t.text "side_id", null: false + t.text "faction_id", null: false t.integer "advancement_requirement" t.integer "agenda_points" t.integer "base_link" t.integer "cost" t.integer "deck_limit" - t.text "faction_code" t.integer "influence_cost" t.integer "influence_limit" t.integer "memory_cost" t.integer "minimum_deck_size" - t.text "side_code" t.integer "strength" - t.text "subtypes" t.text "text" t.integer "trash_cost" - t.text "card_type_code", null: false t.boolean "uniqueness" + t.text "keywords" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false - t.index ["card_type_code"], name: "index_cards_on_card_type_code" - t.index ["code"], name: "index_cards_on_code", unique: true - t.index ["faction_code"], name: "index_cards_on_faction_code" - t.index ["side_code"], name: "index_cards_on_side_code" + t.index ["card_type_id"], name: "index_cards_on_card_type_id" + t.index ["faction_id"], name: "index_cards_on_faction_id" + t.index ["side_id"], name: "index_cards_on_side_id" + t.index ["name"], name: "index_cards_unique_name", unique: true end create_table "cards_subtypes", id: false, force: :cascade do |t| - t.text "card_code", null: false - t.text "subtype_code", null: false - t.index ["card_code", "subtype_code"], name: "index_cards_subtypes_on_card_code_and_subtype_code" + t.text "card_id", null: false + t.text "subtype_id", null: false + t.index ["card_id", "subtype_id"], name: "index_cards_subtypes_on_card_id_and_subtype_id" end - create_table "cycles", id: false, force: :cascade do |t| - t.text "code", null: false + create_table "card_cycles", id: :string, force: :cascade do |t| t.text "name", null: false t.text "description" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false - t.index ["code"], name: "index_cycles_on_code", unique: true end - create_table "factions", id: false, force: :cascade do |t| - t.text "code", null: false + create_table "factions", id: :string, force: :cascade do |t| t.boolean "is_mini", null: false t.text "name", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false - t.index ["code"], name: "index_factions_on_code", unique: true end - create_table "printings", id: false, force: :cascade do |t| - t.text "code" - t.text "card_code" + create_table "printings", id: :string, force: :cascade do |t| + t.text "card_id" + t.text "card_set_id" t.text "printed_text" t.boolean "printed_uniqueness" t.text "flavor" @@ -108,37 +95,29 @@ t.integer "position" t.integer "quantity" t.date "date_release" - t.text "card_set_code" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false - t.index ["card_code"], name: "index_printings_on_card_code" - t.index ["card_set_code"], name: "index_printings_on_card_set_code" - t.index ["code"], name: "index_printings_on_code", unique: true end - create_table "sides", id: false, force: :cascade do |t| - t.text "code", null: false + create_table "sides", id: :string, force: :cascade do |t| t.text "name", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false - t.index ["code"], name: "index_sides_on_code", unique: true end - create_table "subtypes", id: false, force: :cascade do |t| - t.text "code", null: false + create_table "subtypes", id: :string, force: :cascade do |t| t.text "name", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false - t.index ["code"], name: "index_subtypes_on_code", unique: true end - add_foreign_key "cards", "card_types", column: "card_type_code", primary_key: "code" - add_foreign_key "card_sets", "card_set_types", column: "card_set_type_code", primary_key: "code" - add_foreign_key "card_sets", "cycles", column: "cycle_code", primary_key: "code" - add_foreign_key "cards", "factions", column: "faction_code", primary_key: "code" - add_foreign_key "cards", "sides", column: "side_code", primary_key: "code" - add_foreign_key "cards_subtypes", "cards", column: "card_code", primary_key: "code" - add_foreign_key "cards_subtypes", "subtypes", column: "subtype_code", primary_key: "code" - add_foreign_key "printings", "cards", column: "card_code", primary_key: "code" - add_foreign_key "printings", "card_sets", column: "card_set_code", primary_key: "code" + add_foreign_key "card_sets", "card_cycles", column: "card_cycle_id", primary_key: "id" + add_foreign_key "card_sets", "card_set_types", column: "card_set_type_id", primary_key: "id" + add_foreign_key "cards_subtypes", "cards", column: "card_id", primary_key: "id" + add_foreign_key "cards_subtypes", "subtypes", column: "subtype_id", primary_key: "id" + add_foreign_key "cards", "card_types", column: "card_type_id", primary_key: "id" + add_foreign_key "cards", "factions", column: "faction_id", primary_key: "id" + add_foreign_key "cards", "sides", column: "side_id", primary_key: "id" + add_foreign_key "printings", "cards", column: "card_id", primary_key: "id" + add_foreign_key "printings", "card_sets", column: "card_set_id", primary_key: "id" end diff --git a/lib/tasks/cards.rake b/lib/tasks/cards.rake index 4eb38dc6..6c8eae7f 100644 --- a/lib/tasks/cards.rake +++ b/lib/tasks/cards.rake @@ -26,7 +26,7 @@ namespace :cards do keywords.each { |k| subtypes[subtype_name_to_code(k)] = k } - return subtypes + return subtypes end # Normalize set names by stripping apostrophes and replacing spaces with -. @@ -41,7 +41,7 @@ namespace :cards do .gsub(/[!:",\(\)\*]/, '') # Single quotes before or after a space and before a - are removed. # This normalized a word like Earth's to earths which reads better - # than earth-s + # than earth-s .gsub(/' /, ' ') .gsub(/ '/, ' ') .gsub(/'-/, '-') @@ -49,24 +49,30 @@ namespace :cards do .gsub('. ', ' ') # Trailing periods are removed. .gsub(/\.$/, '') - .gsub(/[\. '\/\.&;]/, '-') + .gsub(/[\. '\/\.&;]/, '-') end def import_sides(sides_path) sides = JSON.parse(File.read(sides_path)) - Side.import sides, on_duplicate_key_update: { conflict_target: [ :code ], columns: :all } + sides.map! do |s| + { + id: s['code'], + name: s['name'], + } + end + Side.import sides, on_duplicate_key_update: { conflict_target: [ :id ], columns: :all } end def import_factions(path) factions = JSON.parse(File.read(path)) factions.map! do |f| { - code: f['code'], + id: f['code'], name: f['name'], is_mini: f['is_mini'] } end - Faction.import factions, on_duplicate_key_update: { conflict_target: [ :code ], columns: :all } + Faction.import factions, on_duplicate_key_update: { conflict_target: [ :id ], columns: :all } end def import_types(path) @@ -74,11 +80,11 @@ namespace :cards do types = types.select {|t| t['is_subtype'] == false} types.map! do |t| { - code: t['code'], + id: t['code'], name: t['name'], } end - CardType.import types, on_duplicate_key_update: { conflict_target: [ :code ], columns: :all } + CardType.import types, on_duplicate_key_update: { conflict_target: [ :id ], columns: :all } end def import_subtypes(packs_json) @@ -92,19 +98,14 @@ namespace :cards do } subtypes = subtypes.to_a.map do |k, v| { - code: k, + id: k, name: v } - end - Subtype.import subtypes, on_duplicate_key_update: { conflict_target: [ :code ], columns: :all } + end + Subtype.import subtypes, on_duplicate_key_update: { conflict_target: [ :id ], columns: :all } end def import_cards(cards) - factions = Faction.all.index_by(&:code) - sides = Side.all.index_by(&:code) - types = CardType.all.index_by(&:code) - subtypes = Subtype.all.index_by(&:code) - new_cards = [] seen_cards = {} cards.each do |card| @@ -114,11 +115,15 @@ namespace :cards do next end seen_cards[card["title"]] = true + new_card = Card.new( + id: stripped_title_to_card_code(card["stripped_title"]), + card_type_id: card["type_code"], + side_id: card["side_code"], + faction_id: card["faction_code"], advancement_requirement: card["advancement_cost"], - agenda_points: card["agendaUpoints"], + agenda_points: card["agenda_points"], base_link: card["base_link"], - code: stripped_title_to_card_code(card["stripped_title"]), cost: card["cost"], deck_limit: card["deck_limit"], influence_cost: card["faction_cost"], @@ -127,14 +132,11 @@ namespace :cards do minimum_deck_size: card["minimum_deck_size"], name: card["title"], strength: card["strength"], - subtypes: card["keywords"], text: card["text"], trash_cost: card["trash_cost"], - uniqueness: card["uniqueness"] + uniqueness: card["uniqueness"], + keywords: card["keywords"], ) - new_card.faction = factions[card["faction_code"]] if card["faction_code"] - new_card.side = sides[card["side_code"]] if card["side_code"] - new_card.card_type = types[card["type_code"]] if card["type_code"] new_cards << new_card end @@ -143,22 +145,22 @@ namespace :cards do new_cards.each_slice(250) { |s| num_cards += s.length puts ' %d cards' % num_cards - Card.import s, on_duplicate_key_update: { conflict_target: [ :code ], columns: :all } + Card.import s, on_duplicate_key_update: { conflict_target: [ :id ], columns: :all } } end # We don't reload JSON files in here because we have already saved all the cards # with their subtypes fields and can parse from there. def import_card_subtypes - subtypes = Subtype.all.index_by(&:code) + # TODO(plural): Deal with caïssa type. + subtypes = Subtype.all.index_by(&:id) cards = Card.all - card_code_to_subtype_code = [] + card_id_to_subtype_id = [] cards.each { |c| - keywords_to_subtype_codes(c.subtypes).each { |k,v| - card_code_to_subtype_code << [c.code, subtypes[k].code] + keywords_to_subtype_codes(c.keywords).each { |k,v| + card_id_to_subtype_id << [c.id, subtypes[k].id] } } - puts "Have to insert %d card -> subtype mappings." % card_code_to_subtype_code.length # Use a transaction since we are deleting the mapping table. ActiveRecord::Base.transaction do puts 'Clear out existing card -> subtype mappings' @@ -168,14 +170,13 @@ namespace :cards do end num_assoc = 0 - card_code_to_subtype_code.each_slice(250) { |m| + card_id_to_subtype_id.each_slice(250) { |m| num_assoc += m.length puts ' %d card -> subtype associations' % num_assoc - sql = "INSERT INTO cards_subtypes (card_code, subtype_code) VALUES " + sql = "INSERT INTO cards_subtypes (card_id, subtype_id) VALUES " vals = [] m.each { |m| # TODO(plural): use the associations object for this or ensure this is safe - puts m vals << "('%s', '%s')" % [m[0], m[1]] } sql << vals.join(", ") @@ -191,26 +192,26 @@ namespace :cards do cycles = JSON.parse(File.read(path)) cycles.map! do |c| { - code: c['code'], + id: c['code'], name: c['name'], } end - Cycle.import cycles, on_duplicate_key_update: { conflict_target: [ :code ], columns: :all } + CardCycle.import cycles, on_duplicate_key_update: { conflict_target: [ :id ], columns: :all } end def import_set_types # TODO(plural): Make json files for set types. - subtypes = Subtype.all.index_by(&:code) + subtypes = Subtype.all set_types = [ - { code: 'campaign', name: 'Campaign' }, - { code: 'core', name: 'Core' }, - { code: 'data_pack', name: 'Data Pack' }, - { code: 'deluxe', name: 'Deluxe' }, - { code: 'draft', name: 'Draft' }, - { code: 'expansion', name: 'Expansion' }, - { code: 'promo', name: 'Promo' } + { id: 'campaign', name: 'Campaign' }, + { id: 'core', name: 'Core' }, + { id: 'data_pack', name: 'Data Pack' }, + { id: 'deluxe', name: 'Deluxe' }, + { id: 'draft', name: 'Draft' }, + { id: 'expansion', name: 'Expansion' }, + { id: 'promo', name: 'Promo' } ] - CardSetType.import set_types, on_duplicate_key_update: { conflict_target: [ :code ], columns: :all } + CardSetType.import set_types, on_duplicate_key_update: { conflict_target: [ :id ], columns: :all } end def import_sets(path) @@ -222,52 +223,52 @@ namespace :cards do "system-core-2019" => 'core', "core-set" => 'core', "system-update-2021" => 'core', - "reign-and-reverie" => 'deluxe', + "reign-and-reverie" => 'deluxe', "data-and-destiny" => 'deluxe', "order-and-chaos" => 'deluxe', "creation-and-control" => 'deluxe', "honor-and-profit" => 'deluxe', "draft" => 'draft', - "magnum-opus" => 'expansion', + "magnum-opus" => 'expansion', "magnum-opus-reprint" => 'expansion', "uprising-booster-pack" => 'expansion', "napd-multiplayer" => 'promo', } - cycles = Cycle.all.index_by(&:code) + cycles = CardCycle.all sets = JSON.parse(File.read(path)) # TODO(plural): Get the updated code values in the JSON files, probably with a new name. sets.map! do |s| { - "code": set_name_to_code(s["name"]), + "id": set_name_to_code(s["name"]), "name": s["name"], # TODO(plural): Make this a proper date type, not a string. "date_release": s["date_release"], - "size": s["size"], - "cycle_code": cycles[s["cycle_code"]].code, - "card_set_type_code": set_type_mapping.fetch(set_name_to_code(s["name"]), "data_pack") + "size": s["size"], + "card_cycle_id": s["cycle_code"], #cycles[s["cycle_code"]].id, + "card_set_type_id": set_type_mapping.fetch(set_name_to_code(s["name"]), "data_pack") } end - CardSet.import sets, on_duplicate_key_update: { conflict_target: [ :code ], columns: :all } + CardSet.import sets, on_duplicate_key_update: { conflict_target: [ :id ], columns: :all } end def import_printings(pack_cards_json, packs_path) raw_packs = JSON.parse(File.read(packs_path)) - old_pack_code_to_set_code = {} + old_pack_id_to_set_id = {} raw_packs.each{ |r| - old_pack_code_to_set_code[r["code"]] = set_name_to_code(r["name"]) + old_pack_id_to_set_id[r["code"]] = set_name_to_code(r["name"]) } - raw_cards = Card.all.index_by(&:code) - sets = CardSet.all.index_by(&:code) + raw_cards = Card.all.index_by(&:id) + sets = CardSet.all.index_by(&:id) new_printings = [] pack_cards_json.each { |set_card| card = raw_cards[stripped_title_to_card_code(set_card["stripped_title"])] - set = sets[old_pack_code_to_set_code[set_card["pack_code"]]] + set = sets[old_pack_id_to_set_id[set_card["pack_code"]]] new_printings << Printing.new( printed_text: card.text, printed_uniqueness: card.uniqueness, - code: set_card["code"], + id: set_card["code"], flavor: set_card["flavor"], illustrator: set_card["illustrator"], position: set_card["position"], @@ -282,46 +283,46 @@ namespace :cards do new_printings.each_slice(250) { |s| num_printings += s.length puts ' %d printings' % num_printings - Printing.import s, on_duplicate_key_update: { conflict_target: [ :code ], columns: :all } + Printing.import s, on_duplicate_key_update: { conflict_target: [ :id ], columns: :all } } end task :import, [:json_dir] => [:environment] do |t, args| -# args.with_defaults(:json_dir => '/netrunner-cards-json/') -# puts 'Import card data...' -# -# puts 'Importing Sides...' -# import_sides(args[:json_dir] + '/sides.json') -# -# puts 'Import factions...' -# import_factions(args[:json_dir] + '/factions.json') -# -# puts 'Importing Types...' -# import_types(args[:json_dir] + '/types.json') -# -# # The JSON from the files in packs/ are used by multiple methods. -# pack_cards_json = load_pack_card_files(args[:json_dir] + '/pack/*.json') -# -# puts 'Importing Subtypes...,' -# import_subtypes(pack_cards_json) -# -# puts 'Importing Cards...' -# import_cards(pack_cards_json) -# -# puts 'Importing Cycles...' -# import_cycles(args[:json_dir] + '/cycles.json') -# -# puts 'Importing Card Set Types...' -# import_set_types() -# -# puts 'Importing Sets...' -# import_sets(args[:json_dir] + '/packs.json') + args.with_defaults(:json_dir => '/netrunner-cards-json/') + puts 'Import card data...' + + # The JSON from the files in packs/ are used by multiple methods. + pack_cards_json = load_pack_card_files(args[:json_dir] + '/pack/*.json') + + puts 'Importing Sides...' + import_sides(args[:json_dir] + '/sides.json') + + puts 'Import factions...' + import_factions(args[:json_dir] + '/factions.json') + + puts 'Importing Cycles...' + import_cycles(args[:json_dir] + '/cycles.json') + + puts 'Importing Types...' + import_types(args[:json_dir] + '/types.json') + + puts 'Importing Subtypes...,' + import_subtypes(pack_cards_json) + + puts 'Importing Cards...' + import_cards(pack_cards_json) puts 'Importing Subtypes for Cards...' import_card_subtypes() -# puts('Importing Printings...') -# import_printings(pack_cards_json, args[:json_dir] + '/packs.json') + puts 'Importing Card Set Types...' + import_set_types() + + puts 'Importing Sets...' + import_sets(args[:json_dir] + '/packs.json') + + puts 'Importing Printings...' + import_printings(pack_cards_json, args[:json_dir] + '/packs.json') puts 'Done!' end From 39c07e92fb92277a0da14f299ad0768081a8aeb6 Mon Sep 17 00:00:00 2001 From: Jason Gessner Date: Sun, 13 Feb 2022 17:42:29 -0600 Subject: [PATCH 3/4] Uncomment method --- app/models/card.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/app/models/card.rb b/app/models/card.rb index 30df5eb9..8aa1211a 100644 --- a/app/models/card.rb +++ b/app/models/card.rb @@ -26,15 +26,15 @@ def strength_selector end end -# def type_builder -# c_type = "#{card_type.name}".dup -# c_type << if subtypes.present? -# ": #{subtypes}" -# else -# '' -# end -# c_type.freeze -# end + def type_builder + c_type = "#{card_type.name}".dup + c_type << if subtypes.present? + ": #{subtypes}" + else + '' + end + c_type.freeze + end def format_text t = text || ''.dup From f466c2cc68e36feea19762bee6188f6b547746bb Mon Sep 17 00:00:00 2001 From: Jason Gessner Date: Sun, 13 Feb 2022 18:52:19 -0600 Subject: [PATCH 4/4] Fix belongs_to for CardSubtype. --- app/models/card_subtype.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/models/card_subtype.rb b/app/models/card_subtype.rb index 16057627..0c15ff5a 100644 --- a/app/models/card_subtype.rb +++ b/app/models/card_subtype.rb @@ -3,12 +3,10 @@ class CardSubtype < ApplicationRecord self.table_name = "cards_subtypes" - belongs_to :cards, - :class_name => "Card", + belongs_to :card, :primary_key => :id, :foreign_key => :card_id - belongs_to :subtypes, - :class_name => "Subtype", + belongs_to :subtype, :primary_key => :id, :foreign_key => :subtype_id end