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
4 changes: 4 additions & 0 deletions app/models/printing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ class Printing < ApplicationRecord
has_one :side, :through => :card
has_many :illustrator_printings
has_many :illustrators, :through => :illustrator_printings

has_many :unified_restrictions, primary_key: :card_id, foreign_key: :card_id
has_many :card_pool_cards, primary_key: :card_id, foreign_key: :card_id
has_many :card_pools, :through => :card_pool_cards
end
12 changes: 11 additions & 1 deletion app/resources/api/v3/public/printing_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,17 @@ class Api::V3::Public::PrintingResource < JSONAPI::Resource
filter :is_unique, apply: ->(records, value, _options){
records.joins(:card).where('cards.is_unique= ?', value)
}

filter :search, apply: ->(records, value, _options) {
query_builder = PrintingSearchQueryBuilder.new(value[0])
if query_builder.parse_error.nil?
records.left_joins(query_builder.left_joins)
.where(query_builder.where, *query_builder.where_values)
else
raise JSONAPI::Exceptions::BadRequest.new(
'Invalid search query: [%s] / %s' % [value[0], query_builder.parse_error])
end
}

# Images will return a nested map for different types of images.
# 'nrdb_classic' represents the JPEGs used for classic netrunnerdb.com.
# We will likely add other formats like png and webp, as well as various sizes,
Expand Down
5 changes: 0 additions & 5 deletions lib/card_search_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ class CardSearchParser < Parslet::Parser
str('advancement_cost') |
str('agenda_points') |
str('base_link') |
str('card_cycle') |
str('card_pool') |
str('card_set') |
str('card_subtype') |
str('card_type') |
str('cost') |
str('eternal_points') |
str('faction') |
str('flavor_text') |
str('format') |
str('global_penalty') |
str('illustrator') |
Expand All @@ -38,8 +35,6 @@ class CardSearchParser < Parslet::Parser
str('is_restricted') |
str('is_unique') |
str('memory_usage') |
str('quantity_in_card_set') |
str('release_date') |
str('restriction_id') |
str('side') |
str('strength') |
Expand Down
53 changes: 21 additions & 32 deletions lib/card_search_query_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,6 @@ class CardSearchQueryBuilder
}
@@term_to_field_map = {
# format should implicitly use the currently active card pool and restriction lists unless another is specified.
# 'format' => '',
# 'restriction' => '',

# printing? or minimum release date from printing for the card? Add release date to the card? 'r' => 'release_date',
# printing 'a' => 'flavor',
# printing 'c' => 'card_cycle_id',
# printing 'card_cycle' => 'card_cycle_id'',
# printing 'card_set' => 'card_set_id'',
# printing 'e' => 'card_set_id',
# printing 'i' => 'illustrator',
# printing 'quantity_in_card_set' => ''',
# printing 'release_date' => ''',
# printing flavor 'flavor_text' => ''',
# printing illustrator 'illustrator' => ''',
# printing quantity 'y' => ''',

'_' => 'cards.stripped_title',
'advancement_cost' => 'cards.advancement_requirement',
'agenda_points' => 'cards.agenda_points',
Expand All @@ -88,6 +72,7 @@ class CardSearchQueryBuilder
'eternal_points' => 'unified_restrictions.eternal_points',
'f' => 'cards.faction_id',
'faction' => 'cards.faction_id',
'format' => 'unified_restrictions.format_id',
'g' => 'cards.advancement_requirement',
'global_penalty' => 'unified_restrictions.global_penalty',
'h' => 'cards.trash_cost',
Expand Down Expand Up @@ -115,11 +100,23 @@ class CardSearchQueryBuilder
'x' => 'cards.stripped_text',
}

@@term_to_left_join_map = {
'card_pool' => :card_pool_cards,
'card_subtype' => :card_subtypes,
'eternal_points' => :unified_restrictions,
'global_penalty' => :unified_restrictions,
'is_banned' => :unified_restrictions,
'is_restricted' => :unified_restrictions,
'restriction_id' => :unified_restrictions,
's' => :card_subtypes,
'universal_faction_cost' => :unified_restrictions,
}

def initialize(query)
@query = query
@parse_error = nil
@parse_tree = nil
@left_joins = []
@left_joins = Set.new
@where = ''
@where_values = []
begin
Expand Down Expand Up @@ -152,9 +149,6 @@ def initialize(query)
@parse_error = 'Invalid boolean operator "%s"' % match_type
return
end
if ['is_banned', 'is_restricted'].include?(keyword)
@left_joins << :unified_restrictions
end
constraints << '%s %s ?' % [@@term_to_field_map[keyword], operator]
where << value
elsif @@numeric_keywords.include?(keyword)
Expand All @@ -163,10 +157,7 @@ def initialize(query)
return
end
operator = ''
if ['eternal_points', 'global_penalty', 'universal_faction_cost'].include?(keyword)
@left_joins << :unified_restrictions
end
if @@numeric_operators.include?(match_type)
if @@numeric_operators.include?(match_type)
operator = @@numeric_operators[match_type]
else
@parse_error = 'Invalid numeric operator "%s"' % match_type
Expand All @@ -184,15 +175,13 @@ def initialize(query)
@parse_error = 'Invalid string operator "%s"' % match_type
return
end
if ['s', 'card_subtype'].include?(keyword)
@left_joins << :card_subtypes
elsif keyword == 'card_pool'
@left_joins << :card_pool_cards
end
constraints << 'lower(%s) %s ?' % [@@term_to_field_map[keyword], operator]
constraints << 'lower(%s) %s ?' % [@@term_to_field_map[keyword], operator]
where << '%%%s%%' % value
end
end
if @@term_to_left_join_map.include?(keyword)
@left_joins << @@term_to_left_join_map[keyword]
end
end

# bare/quoted words in the query are automatically mapped to stripped_title
if f.include?(:string)
Expand All @@ -216,6 +205,6 @@ def where_values
return @where_values
end
def left_joins
return @left_joins
return @left_joins.to_a
end
end
60 changes: 60 additions & 0 deletions lib/printing_search_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require 'parslet'

# TODO(plural): Add support for | in : and ! operators .
class PrintingSearchParser < Parslet::Parser
rule(:spaces) { match('\s').repeat(1) }
rule(:spaces?) { spaces.maybe }
rule(:bare_string) {
match('[!\w-]').repeat(1).as(:string)
}
rule(:quoted_string) {
str('"') >> (
str('"').absent? >> any
).repeat.as(:string) >> str('"')
}
rule(:string) {
spaces? >> (bare_string | quoted_string) >> spaces?
}
# Note that while this list should generally be kept sorted, an entry that is a prefix of
# a later entry will clobber the later entries and throw an error parsing text with the later entries.
rule(:keyword) {
str('advancement_cost') |
str('agenda_points') |
str('base_link') |
str('card_cycle') |
str('card_pool') |
str('card_set') |
str('card_subtype') |
str('card_type') |
str('cost') |
str('eternal_points') |
str('faction') |
str('flavor') |
str('format') |
str('global_penalty') |
str('illustrator') |
str('influence_cost') |
str('is_banned') |
str('is_restricted') |
str('is_unique') |
str('memory_usage') |
str('quantity') |
str('release_date') |
str('restriction_id') |
str('side') |
str('strength') |
str('text') |
str('title') |
str('trash_cost') |
str('universal_faction_cost') |
# Single letter 'short codes'
match('[_abcdefghilmnoprstuvxyz]')
}
rule(:match_type) { str('<=') | str('>=') | match('[:!<>]') }
rule(:operator) { keyword >> match_type}
rule(:search_term) { keyword.as(:keyword) >> match_type.as(:match_type) >> (string).as(:value) }
rule(:query) {
(spaces? >> (search_term.as(:search_term) | string) >> spaces?).repeat.as(:fragments)
}
root :query
end
Loading