Fix mana filtering in search.
This commit is contained in:
@@ -28,7 +28,7 @@ class CardVaultDB:
|
|||||||
"TEXT, `hand` TEXT, `life` TEXT, `releaseDate` TEXT, `starter` TEXT, "
|
"TEXT, `hand` TEXT, `life` TEXT, `releaseDate` TEXT, `starter` TEXT, "
|
||||||
"`printings` TEXT, `originalText` TEXT, `originalType` TEXT, "
|
"`printings` TEXT, `originalText` TEXT, `originalType` TEXT, "
|
||||||
"`source` TEXT, `imageUrl` TEXT, `set` TEXT, `setName` TEXT, `id` TEXT, "
|
"`source` TEXT, `imageUrl` TEXT, `set` TEXT, `setName` TEXT, `id` TEXT, "
|
||||||
"`legalities` TEXT, `rulings` TEXT, `foreignNames` TEXT) ")
|
"`legalities` TEXT, `rulings` TEXT, `foreignNames` TEXT, `fcolor` TEXT) ")
|
||||||
con.execute("CREATE TABLE IF NOT EXISTS library ( multiverseid INT PRIMARY KEY, copies INT )")
|
con.execute("CREATE TABLE IF NOT EXISTS library ( multiverseid INT PRIMARY KEY, copies INT )")
|
||||||
con.execute("CREATE TABLE IF NOT EXISTS tags ( tag TEXT, multiverseid INT )")
|
con.execute("CREATE TABLE IF NOT EXISTS tags ( tag TEXT, multiverseid INT )")
|
||||||
con.execute("CREATE TABLE IF NOT EXISTS wants ( listName TEXT, multiverseid INT )")
|
con.execute("CREATE TABLE IF NOT EXISTS wants ( listName TEXT, multiverseid INT )")
|
||||||
@@ -41,7 +41,7 @@ class CardVaultDB:
|
|||||||
# Map card object to database tables
|
# Map card object to database tables
|
||||||
db_values = self.card_to_table_mapping(card)
|
db_values = self.card_to_table_mapping(card)
|
||||||
sql_string = "INSERT INTO `cards` VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," \
|
sql_string = "INSERT INTO `cards` VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," \
|
||||||
"?,?,?,?,?,?,?,?,?,?)"
|
"?,?,?,?,?,?,?,?,?,?,?)"
|
||||||
# Insert into database
|
# Insert into database
|
||||||
con.execute(sql_string, db_values)
|
con.execute(sql_string, db_values)
|
||||||
except sqlite3.OperationalError as err:
|
except sqlite3.OperationalError as err:
|
||||||
@@ -83,7 +83,7 @@ class CardVaultDB:
|
|||||||
try:
|
try:
|
||||||
with con:
|
with con:
|
||||||
sql_string = "INSERT INTO `cards` VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," \
|
sql_string = "INSERT INTO `cards` VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," \
|
||||||
"?,?,?,?,?,?,?,?,?,?)"
|
"?,?,?,?,?,?,?,?,?,?,?)"
|
||||||
con.executemany(sql_string, rows)
|
con.executemany(sql_string, rows)
|
||||||
except sqlite3.OperationalError as err:
|
except sqlite3.OperationalError as err:
|
||||||
util.log("Database Error", util.LogLevel.Error)
|
util.log("Database Error", util.LogLevel.Error)
|
||||||
@@ -235,6 +235,7 @@ class CardVaultDB:
|
|||||||
filer_type = filters["type"]
|
filer_type = filters["type"]
|
||||||
filter_set = filters["set"]
|
filter_set = filters["set"]
|
||||||
filter_mana = filters["mana"].split(',')
|
filter_mana = filters["mana"].split(',')
|
||||||
|
filter_mana.sort(key=lambda val: util.color_sort_order[val[0]])
|
||||||
|
|
||||||
sql = 'SELECT * FROM cards WHERE `name` LIKE ?'
|
sql = 'SELECT * FROM cards WHERE `name` LIKE ?'
|
||||||
parameters = ['%' + term + '%']
|
parameters = ['%' + term + '%']
|
||||||
@@ -248,9 +249,8 @@ class CardVaultDB:
|
|||||||
sql += ' AND `set` = ?'
|
sql += ' AND `set` = ?'
|
||||||
parameters.append(filter_set)
|
parameters.append(filter_set)
|
||||||
if len(filter_mana) != 0:
|
if len(filter_mana) != 0:
|
||||||
for color in filter_mana:
|
sql += ' AND `fcolor` = ?'
|
||||||
sql += ' AND `manaCost` LIKE ?'
|
parameters.append(self.filter_colors_list(filter_mana))
|
||||||
parameters.append('%' + color + '%')
|
|
||||||
sql += ' LIMIT ?'
|
sql += ' LIMIT ?'
|
||||||
parameters.append(list_size)
|
parameters.append(list_size)
|
||||||
|
|
||||||
@@ -299,7 +299,25 @@ class CardVaultDB:
|
|||||||
util.log(str(err), util.LogLevel.Error)
|
util.log(str(err), util.LogLevel.Error)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def card_to_table_mapping(card: Card):
|
def filter_colors_list(mana: list) -> str:
|
||||||
|
symbols = util.unique_list(mana)
|
||||||
|
output = [s for s in symbols if (s in util.card_colors.values())]
|
||||||
|
return "-".join(output)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def filter_colors(card) -> str:
|
||||||
|
"""Extracts the colors of a card for filtering."""
|
||||||
|
output = []
|
||||||
|
if card.colors is not None:
|
||||||
|
for color in card.colors:
|
||||||
|
output.append(util.card_colors[color])
|
||||||
|
else:
|
||||||
|
output.append("C")
|
||||||
|
# TODO extrafilter_colorsct symbols from card text
|
||||||
|
|
||||||
|
return "-".join(output)
|
||||||
|
|
||||||
|
def card_to_table_mapping(self, card: Card):
|
||||||
"""Return the database representation of a card object"""
|
"""Return the database representation of a card object"""
|
||||||
return (str(card.name), str(card.layout), str(card.mana_cost), card.cmc, str(card.colors), str(card.names),
|
return (str(card.name), str(card.layout), str(card.mana_cost), card.cmc, str(card.colors), str(card.names),
|
||||||
str(card.type), str(card.supertypes), str(card.subtypes), str(card.types), str(card.rarity),
|
str(card.type), str(card.supertypes), str(card.subtypes), str(card.types), str(card.rarity),
|
||||||
@@ -312,7 +330,7 @@ class CardVaultDB:
|
|||||||
str(card.original_text),
|
str(card.original_text),
|
||||||
str(card.original_type), str(card.source), str(card.image_url), str(card.set), str(card.set_name),
|
str(card.original_type), str(card.source), str(card.image_url), str(card.set), str(card.set_name),
|
||||||
str(card.id),
|
str(card.id),
|
||||||
str(card.legalities), str(card.rulings), str(card.foreign_names))
|
str(card.legalities), str(card.rulings), str(card.foreign_names), self.filter_colors(card))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def table_to_card_mapping(row: sqlite3.Row):
|
def table_to_card_mapping(row: sqlite3.Row):
|
||||||
|
|||||||
@@ -251,8 +251,42 @@
|
|||||||
<property name="margin_end">10</property>
|
<property name="margin_end">10</property>
|
||||||
<property name="margin_top">6</property>
|
<property name="margin_top">6</property>
|
||||||
<property name="margin_bottom">6</property>
|
<property name="margin_bottom">6</property>
|
||||||
<property name="orientation">vertical</property>
|
|
||||||
<property name="spacing">2</property>
|
<property name="spacing">2</property>
|
||||||
|
<child>
|
||||||
|
<object class="GtkSpinner" id="statusbar_spinner">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">False</property>
|
||||||
|
<property name="fill">True</property>
|
||||||
|
<property name="position">1</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkLabel">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="label" translatable="yes">Online</property>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">False</property>
|
||||||
|
<property name="fill">True</property>
|
||||||
|
<property name="position">2</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
|
<child>
|
||||||
|
<object class="GtkImage">
|
||||||
|
<property name="visible">True</property>
|
||||||
|
<property name="can_focus">False</property>
|
||||||
|
<property name="icon_name">network-idle</property>
|
||||||
|
</object>
|
||||||
|
<packing>
|
||||||
|
<property name="expand">False</property>
|
||||||
|
<property name="fill">True</property>
|
||||||
|
<property name="position">3</property>
|
||||||
|
</packing>
|
||||||
|
</child>
|
||||||
</object>
|
</object>
|
||||||
<packing>
|
<packing>
|
||||||
<property name="expand">False</property>
|
<property name="expand">False</property>
|
||||||
|
|||||||
@@ -81,6 +81,22 @@ legality_colors = {
|
|||||||
"Legal": "#62B62F"
|
"Legal": "#62B62F"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
card_colors = {
|
||||||
|
'White': 'W',
|
||||||
|
'Blue': 'U',
|
||||||
|
'Black': 'B',
|
||||||
|
'Red': 'R',
|
||||||
|
'Green': 'G'
|
||||||
|
}
|
||||||
|
|
||||||
|
color_sort_order = {
|
||||||
|
'W': 0,
|
||||||
|
'U': 1,
|
||||||
|
'B': 2,
|
||||||
|
'R': 3,
|
||||||
|
'G': 4
|
||||||
|
}
|
||||||
|
|
||||||
rarity_dict = {
|
rarity_dict = {
|
||||||
"special": 0,
|
"special": 0,
|
||||||
"common": 1,
|
"common": 1,
|
||||||
@@ -367,6 +383,12 @@ def create_mana_icons(icons: dict, mana_string: str) -> GdkPixbuf:
|
|||||||
return pixbuf
|
return pixbuf
|
||||||
|
|
||||||
|
|
||||||
|
def unique_list(seq):
|
||||||
|
seen = set()
|
||||||
|
seen_add = seen.add
|
||||||
|
return [x for x in seq if not (x in seen or seen_add(x))]
|
||||||
|
|
||||||
|
|
||||||
def sizeof_fmt(num, suffix='B'):
|
def sizeof_fmt(num, suffix='B'):
|
||||||
for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
|
for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
|
||||||
if abs(num) < 1024.0:
|
if abs(num) < 1024.0:
|
||||||
|
|||||||
Reference in New Issue
Block a user