Skip to content

Commit

Permalink
動くようになった
Browse files Browse the repository at this point in the history
  • Loading branch information
poslogithub committed Jun 11, 2022
1 parent fc0ba7a commit b3d67aa
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 55 deletions.
4 changes: 2 additions & 2 deletions script/generate_set_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ def generate_set_map(loc, cards, enums, set_name):

abilities_raw = card["abilities"]
for ability in abilities_raw:
aid = ability["abilityId"]
textid = ability["textId"]
aid = ability["Id"]
textid = ability["TextId"]
text = loc_map[textid].encode("ascii", errors="ignore").decode()
abilities.append(aid)
all_abilities[aid] = text
Expand Down
29 changes: 16 additions & 13 deletions source/mtga/set_data/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
from mtga.models.card_set import Pool
try:
from mtga.set_data import dynamic
print("import dynamic complete.")
dynamic_sets = []
all_mtga_abilities = {}
#try:
from mtga.set_data import dynamic
print("import dynamic complete.")
dynamic_sets = []
all_mtga_abilities = {}

for cardset, ability_map in dynamic.dynamic_set_tuples:
for ability_key in ability_map.keys():
all_mtga_abilities[ability_key] = ability_map[ability_key]
dynamic_sets.append(cardset)
for cardset, ability_map in dynamic.dynamic_set_tuples:
for ability_key in ability_map.keys():
all_mtga_abilities[ability_key] = ability_map[ability_key]
dynamic_sets.append(cardset)

all_mtga_cards = Pool.from_sets("mtga_cards",
sets=[*dynamic_sets],
abilities=all_mtga_abilities)
all_mtga_cards = Pool.from_sets("mtga_cards",
sets=[*dynamic_sets],
abilities=all_mtga_abilities)

except:
"""
except Exception as e:
print(e.args)
print("WARNING! Could not dynamically generate card sets. Do you have Arena installed?")
from mtga.set_data import xln, dom, rix, m19, ana, grn, rna, war, m20, eld, akh, arenasup, bfz, mi, roe, rtr
Expand All @@ -32,3 +34,4 @@
arenasup.ArenaSup, bfz.BattleForZendikar, mi.Mirage, roe.RiseOfEldrazi,
rtr.ReturnToRavnica],
abilities=all_mtga_abilities)
"""
57 changes: 17 additions & 40 deletions source/mtga/set_data/dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,26 @@
This file adapted from generate_set_map.py """
import json
import locale
import os
import re
import sys
from pathlib import Path
from mtga.models.card import Card
from mtga.models.card_set import Set
from sqlite2json import sqlite2json


def _get_data_location_hardcoded():
root = os.environ.get(
"ProgramFiles",
r"C:\Program Files"
)
return os.path.join(root, "Wizards of the Coast", "MTGA", "MTGA_Data", "Downloads", "Data")
return os.path.join(root, "Wizards of the Coast", "MTGA", "MTGA_Data", "Downloads", "Raw")


COLOR_ID_MAP = {1: "W", 2: "U", 3: "B", 4: "R", 5: "G"}
RARITY_ID_MAP = {0: "Token", 1: "Basic", 2: "Common", 3: "Uncommon", 4: "Rare", 5: "Mythic Rare"}
ISO_CODE_MAP = {
"English": "en-US",
"French": "fr-FR",
"Italian": "it-IT",
"German": "de-DE",
"Spanish": "es-ES",
"Japanese": "ja-JP",
"Portuguese": "pt-BR",
"Russian": "ru-RU",
"Korean": "ko-KR",
}


dynamic_set_tuples = []

Expand All @@ -49,7 +39,7 @@ def get_data_location():
def get_darwin_data_location():
return os.path.join(
os.path.expanduser("~"),
"Library/Application Support/com.wizards.mtga/Downloads/Data",
"Library/Application Support/com.wizards.mtga/Downloads/Raw",
)

def get_win_data_location():
Expand All @@ -58,7 +48,7 @@ def get_win_data_location():
registry_connection = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
reg_path = r"SOFTWARE\Wizards of the Coast\MTGArena"
registry_key = OpenKey(registry_connection, reg_path)
data_location = QueryValueEx(registry_key, "Path")[0] + r"MTGA_Data\Downloads\Data"
data_location = QueryValueEx(registry_key, "Path")[0] + r"MTGA_Data\Downloads\Raw"
print("Found data @ ")
print(data_location)
except:
Expand All @@ -72,7 +62,7 @@ def del_ruby(s):

data_location = get_data_location()

json_filepaths = {"enums": "", "cards": "", "abilities": "", "loc": ""}
json_filepaths = {"cards": "", "abilities": "", "CardDatabase": ""}

# A newer file SHOULD be the preference; alpha sort of hashes may be out of order
# Otherwise it will be necessary to find which is really used
Expand All @@ -91,11 +81,7 @@ def del_ruby(s):
with open(json_filepaths["cards"], "r", encoding="utf-8") as card_in:
cards = json.load(card_in)

with open(json_filepaths["loc"], "r", encoding="utf-8") as loc_in:
loc = json.load(loc_in)

with open(json_filepaths["enums"], "r", encoding="utf-8") as enums_in:
enums = json.load(enums_in)
loc, enums = sqlite2json(json_filepaths["CardDatabase"])

listed_cardsets = list(set([card["set"] for card in cards]))

Expand All @@ -104,20 +90,7 @@ def del_ruby(s):
set_name_class_cased = re.sub('[^0-9a-zA-Z_]', '', set_name)
all_abilities = {}

try:
iso_code = ISO_CODE_MAP.get(locale.getlocale()[0].split("_")[0])
except:
iso_code = ISO_CODE_MAP.get("English")
loc_map = {}
try:
lang = list(filter(lambda x: x["isoCode"] == iso_code, loc))[0]
except:
lang = loc[0]
for obj in lang["keys"]:
# if obj["id"] in loc_map.keys():
# print("WARNING: overwriting id {} = {} with {}".format(obj["id"], loc_map[obj["id"]], obj["text"]))
loc_map[obj["id"]] = obj["text"]
loc_map = {obj["id"]: obj["text"] for obj in lang["keys"]}
loc_map = {obj["id"]: obj["text"] for obj in loc}
enum_map = {obj["name"]: {inner_obj["id"]: inner_obj["text"] for inner_obj in obj["values"]} for obj in enums}
set_cards = [card for card in cards if card["set"].upper() == set_name.upper()]
assert set_cards, "No cards found in set {}. Double check your nomenclature, and ensure the input files contain your set!"
Expand Down Expand Up @@ -224,9 +197,11 @@ def del_ruby(s):
abilities_raw = []
for ability in abilities_raw:
aid = ability["Id"]
textid = ability["TextId"]
textid = ability.get("TextId")
text = ""
try:
text = loc_map[textid].encode("ascii", errors="ignore").decode()
if textid:
text = loc_map[textid].encode("ascii", errors="ignore").decode()
except:
# TODO: there are multiple loc files now?? something weird is up. I don't really feel like trying to
# figure this out right now though.
Expand All @@ -240,9 +215,11 @@ def del_ruby(s):
hidden_abilities_raw = []
for ability in hidden_abilities_raw:
aid = ability["Id"]
textid = ability["TextId"]
textid = ability.get("TextId")
text = ""
try:
text = loc_map[textid].encode("ascii", errors="ignore").decode()
if textid:
text = loc_map[textid].encode("ascii", errors="ignore").decode()
except:
# TODO: there are multiple loc files now?? something weird is up. I don't really feel like trying to
# figure this out right now though.
Expand All @@ -260,6 +237,6 @@ def del_ruby(s):
except Exception:
print("hit an error on {} / {} / {}".format(card["grpid"], loc_map[card["titleId"]],
card["collectorNumber"]))
# raise
raise
card_set_obj = Set(set_name_class_cased, cards=set_card_objs)
dynamic_set_tuples.append((card_set_obj, all_abilities))
89 changes: 89 additions & 0 deletions source/mtga/set_data/sqlite2json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import sqlite3
from locale import getlocale

class Table():
LOCALIZATIONS = 'Localizations'
ENUMS = 'Enums'

class Row():
LOC_ID = 'LocId'
FORMATTED = 'Formatted'
TYPE = 'Type'

class Key():
ID = 'id'
TEXT = 'text'
NAME = 'name'
VALUES = 'values'

ISO_CODE = {
'English': 'enUS',
'Portuguese': 'ptBR',
'French': 'frFR',
'Italian': 'itIT',
'German': 'deDE',
'Spanish': 'esES',
'Castilian': 'esES',
'Russian': 'ruRU',
'Japanese': 'jaJP',
'Korean': 'koKR'
}

ENUMS_TYPES = [
'CardType',
'Color',
'CounterType',
'FailureReason',
'MatchState',
'Phase',
'ResultCode',
'Step',
'SubType',
'SuperType'
]

def sqlite2json(sqlite_filepath):
loc = []
enums = []
lang = getlocale()[0].split("_")[0]
iso_code = ISO_CODE.get(lang) if ISO_CODE.get(lang) else ISO_CODE['English']

con = sqlite3.connect(sqlite_filepath)
cur = con.cursor()

records = cur.execute("select "+Row.LOC_ID+", "+iso_code.replace('_', '')+" from "+Table.LOCALIZATIONS+" where "+Row.FORMATTED+" = 1")
for record in records:
loc.append(
{
Key.ID: record[0],
Key.TEXT: record[1]
}
)

for type in ENUMS_TYPES:
enums.append(
{
Key.NAME: type,
Key.VALUES: []
}
)
records = cur.execute("select * from "+Table.ENUMS+" where "+Row.TYPE+ " = '"+type+"'")
for record in records:
enums[-1][Key.VALUES].append(
{
Key.ID: record[1],
Key.TEXT: record[2]
}
)

return loc, enums

if __name__ == "__main__":
from json import dump
loc, enums = sqlite2json(r'C:\Program Files\Wizards of the Coast\MTGA\MTGA_Data\Downloads\Raw\Raw_CardDatabase_3fc09d06a61a10dcabcc28369ea62b2b.mtga')

with open("loc.json", "w", encoding='utf-8') as f:
dump(loc, f, indent=2, ensure_ascii=False)

with open("enums.json", "w", encoding='utf-8') as f:
dump(enums, f, indent=2, ensure_ascii=False)

0 comments on commit b3d67aa

Please sign in to comment.