Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/models/card_face.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

# Model for Card Faces - flip cards, cards with multiple versions, etc.
class CardFace < ApplicationRecord
belongs_to :card
has_many :card_face_card_subtypes
has_many :card_subtypes, through: :card_face_card_subtypes
end
12 changes: 12 additions & 0 deletions app/models/card_face_card_subtype.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

# Join model connecting CardFace objects to CardSubtype objects.
class CardFaceCardSubtype < ApplicationRecord
self.table_name = 'card_faces_card_subtypes'

belongs_to :card_face,
primary_key: %i[card_id face_index],
query_constraints: %i[card_id face_index]
belongs_to :card_subtype,
primary_key: :id
end
7 changes: 7 additions & 0 deletions db/migrate/20241230053539_add_layout_id_to_cards.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddLayoutIdToCards < ActiveRecord::Migration[7.1] # rubocop:disable Style/Documentation
def change
add_column :cards, :layout_id, :string, default: 'normal', null: false
end
end
17 changes: 17 additions & 0 deletions db/migrate/20241230055434_create_card_faces.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

class CreateCardFaces < ActiveRecord::Migration[7.1] # rubocop:disable Style/Documentation
def change
create_table :card_faces, primary_key: %i[card_id face_index] do |t|
t.string :card_id, null: false
t.integer :face_index, null: false
t.text :base_link
t.text :display_subtypes
t.text :stripped_text
t.text :stripped_title
t.text :text
t.text :title
t.timestamps
end
end
end
11 changes: 11 additions & 0 deletions db/migrate/20241230055711_create_card_faces_card_subtypes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class CreateCardFacesCardSubtypes < ActiveRecord::Migration[7.1] # rubocop:disable Style/Documentation
def change
create_table :card_faces_card_subtypes, id: false do |t|
t.string :card_id, null: false
t.integer :face_index, null: false
t.string :card_subtype_id
end
end
end
318 changes: 169 additions & 149 deletions db/schema.rb

Large diffs are not rendered by default.

79 changes: 76 additions & 3 deletions lib/tasks/cards.rake
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ namespace :cards do
is_unique: card['is_unique'],
display_subtypes: flatten_subtypes(subtypes, card['subtypes']),
attribution: card['attribution'],
designed_by: card['designed_by']
designed_by: card['designed_by'],
layout_id: card.key?('layout_id') ? card['layout_id'] : 'normal'
)
if card.key?('cost')
new_card.cost = (card['cost'].nil? ? -1 : card['cost'])
Expand Down Expand Up @@ -209,6 +210,75 @@ namespace :cards do
end
end

def import_card_faces(cards)
# Use a transaction since we are deleting the card_faces and card_faces_card_subtypes tables completely.
ActiveRecord::Base.transaction do
puts ' Clear out existing card face -> card subtype mappings'
unless ActiveRecord::Base.connection.delete('DELETE FROM card_faces_card_subtypes')
puts 'Hit an error while deleting card face -> card subtype mappings. rolling back.'
raise ActiveRecord::Rollback
end

puts ' Clear out existing card faces'
unless ActiveRecord::Base.connection.delete('DELETE FROM card_faces')
puts 'Hit an error while deleting card faces. rolling back.'
raise ActiveRecord::Rollback
end

subtypes = CardSubtype.all.index_by(&:id)

cards.each do |card|
# Only generate faces for cards with multiple faces
next if !card.key?('layout_id') || card['layout_id'].nil? || card['layout_id'] == 'normal'

# The first face of a card is just the main Card object and we do not make a CardFace for it.
# The rest of the faces are generated from the explicitly-defined faces of the card.
# Missing attributes are assumed to be unchanged.
i = 0
card['faces'].each do |face|
i += 1
face_subtypes = face.key?('subtypes') ? face['subtypes'] : card['subtypes']
# There aren't enough cards with multiple faces to worry about optimizing inserts for them.
new_face = CardFace.new(
card_id: card['id'],
face_index: i
)
new_face.base_link = face['base_link'] if face.key?('base_link')
display_subtypes = []
if face.key?('subtypes')
face['subtypes'].each do |s|
unless subtypes.key?(s)
puts "subtype #{s} is invalid for card face for #{new_face.card_id}"
raise ActiveRecord::Rollback
end
display_subtypes << subtypes[s].name
end
end
new_face.display_subtypes = display_subtypes.join(' - ') if face.key?('subtypes')
new_face.stripped_text = face['stripped_text'] if face.key?('stripped_text')
new_face.stripped_title = face['stripped_title'] if face.key?('stripped_title')
new_face.text = face['text'] if face.key?('text')
new_face.title = face['title'] if face.key?('title')

new_face.save

next if face_subtypes.nil?

face_subtypes.each do |s|
puts "Adding subtype #{s} to face #{i} of card #{new_face.card_id}"
cfcs = CardFaceCardSubtype.new(
card_id: new_face.card_id,
face_index: i,
card_subtype_id: s
)
puts cfcs.inspect
cfcs.save
end
end
end
end
end

# This assumes that cards and card subtypes have already been loaded.
def import_printing_subtypes
printing_id_to_card_subtype_id = []
Expand Down Expand Up @@ -381,13 +451,13 @@ namespace :cards do
def import_illustrators
# Use a transaction since we are deleting the illustrator and mapping tables.
ActiveRecord::Base.transaction do
puts 'Clear out existing illustrator -> printing mappings'
puts ' Clear out existing illustrator -> printing mappings'
unless ActiveRecord::Base.connection.delete('DELETE FROM illustrators_printings')
puts 'Hit an error while deleting illustrator -> printing mappings. rolling back.'
raise ActiveRecord::Rollback
end

puts 'Clear out existing illustrators'
puts ' Clear out existing illustrators'
unless ActiveRecord::Base.connection.delete('DELETE FROM illustrators')
puts 'Hit an error while deleting illustrators. rolling back.'
raise ActiveRecord::Rollback
Expand Down Expand Up @@ -821,6 +891,9 @@ namespace :cards do
puts 'Importing Subtypes for Cards...'
import_card_subtypes(cards_json)

puts 'Importing Card Faces...'
import_card_faces(cards_json)

puts 'Importing Printings...'
import_printings(printings_json)

Expand Down
Loading