2021-08-03 00:22:00 +00:00
|
|
|
#!/usr/bin/env python3
|
2021-08-04 16:36:23 +00:00
|
|
|
import argparse
|
2021-09-03 02:57:34 +00:00
|
|
|
import io
|
2021-08-04 16:36:23 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2021-09-03 02:57:34 +00:00
|
|
|
import sys
|
|
|
|
import zipfile
|
2021-08-03 00:22:00 +00:00
|
|
|
|
2021-08-09 05:01:25 +00:00
|
|
|
import fftcg
|
2021-08-04 22:17:47 +00:00
|
|
|
|
2021-08-23 14:00:17 +00:00
|
|
|
# constants
|
|
|
|
OUT_DIR_NAME = "out" # name of output directory
|
|
|
|
|
2018-11-02 22:13:17 +00:00
|
|
|
|
2021-08-23 12:57:05 +00:00
|
|
|
def opus_decks(args: argparse.Namespace) -> list[fftcg.TTSDeck]:
|
2021-09-01 18:14:56 +00:00
|
|
|
# load the current carddb
|
|
|
|
carddb = fftcg.CardDB()
|
|
|
|
carddb.load()
|
2021-08-23 12:57:05 +00:00
|
|
|
|
2021-09-01 18:14:56 +00:00
|
|
|
decks: list[fftcg.TTSDeck] = []
|
2021-08-23 16:25:25 +00:00
|
|
|
for opus_id in args.opus_ids:
|
|
|
|
# import an opus
|
|
|
|
opus = fftcg.Opus(opus_id, args.language)
|
|
|
|
book = fftcg.Book(opus, args.language, args.num_requests)
|
|
|
|
book.save()
|
|
|
|
|
|
|
|
carddb.update(opus)
|
|
|
|
decks.extend(opus.elemental_decks)
|
2021-08-23 12:57:05 +00:00
|
|
|
|
2021-09-02 01:06:12 +00:00
|
|
|
carddb.upload_prompt()
|
|
|
|
|
2021-08-23 12:57:05 +00:00
|
|
|
# create elemental decks for opus
|
2021-08-23 16:25:25 +00:00
|
|
|
return decks
|
2021-08-23 12:57:05 +00:00
|
|
|
|
|
|
|
|
2021-08-24 15:32:51 +00:00
|
|
|
def ffdecks_decks(args: argparse.Namespace) -> list[fftcg.TTSDeck]:
|
2021-08-23 12:57:05 +00:00
|
|
|
# load the current carddb
|
2021-09-01 18:14:56 +00:00
|
|
|
carddb = fftcg.CardDB()
|
2021-08-23 12:57:05 +00:00
|
|
|
carddb.load()
|
|
|
|
|
2021-08-24 15:32:51 +00:00
|
|
|
decks: list[fftcg.TTSDeck] = []
|
|
|
|
for deck_id in args.deck_ids:
|
|
|
|
decks.append(fftcg.TTSDeck.from_ffdecks_deck(deck_id))
|
|
|
|
|
2021-08-23 12:57:05 +00:00
|
|
|
# import a deck
|
2021-08-24 15:32:51 +00:00
|
|
|
return decks
|
2021-08-23 12:57:05 +00:00
|
|
|
|
|
|
|
|
2021-08-09 02:03:24 +00:00
|
|
|
def main() -> None:
|
2021-08-04 22:17:47 +00:00
|
|
|
# set up CLI
|
2021-08-23 12:57:05 +00:00
|
|
|
|
|
|
|
# main parser
|
2021-08-04 16:36:23 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
2021-08-23 12:57:05 +00:00
|
|
|
prog="fftcgtool",
|
2021-08-09 04:18:56 +00:00
|
|
|
description="Imports FFTCG cards for TT-Sim.",
|
|
|
|
)
|
2018-11-02 22:13:17 +00:00
|
|
|
|
2021-08-24 15:29:12 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"-v", "--verbose",
|
|
|
|
help="increase output verbosity",
|
|
|
|
action="count",
|
|
|
|
)
|
|
|
|
|
2021-08-23 16:16:56 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"-l", "--language",
|
|
|
|
type=fftcg.Language,
|
|
|
|
default="en",
|
2021-09-03 00:45:32 +00:00
|
|
|
metavar="LANG",
|
|
|
|
help="language for imported objects",
|
2021-08-23 16:16:56 +00:00
|
|
|
)
|
|
|
|
|
2021-09-03 02:57:34 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"-s", "--stdout",
|
|
|
|
action="store_true",
|
|
|
|
help="print the deck files in a zip archive to stdout, skip creating JSONs on disk",
|
|
|
|
)
|
|
|
|
|
2021-08-23 12:57:05 +00:00
|
|
|
subparsers = parser.add_subparsers(
|
|
|
|
description="Import either an Opus to extend the mod, or import a deck to play right away.",
|
|
|
|
dest="subcommand",
|
|
|
|
help="valid subcommands",
|
|
|
|
required=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
# "opus" subcommand
|
2021-09-03 00:45:32 +00:00
|
|
|
opuses_parser = subparsers.add_parser(
|
|
|
|
"opuses",
|
|
|
|
description="Imports Opuses from the square API and creates its elemental decks as JSON files.",
|
2021-08-23 12:57:05 +00:00
|
|
|
)
|
|
|
|
|
2021-09-03 00:45:32 +00:00
|
|
|
opuses_parser.set_defaults(
|
2021-08-23 12:57:05 +00:00
|
|
|
func=opus_decks
|
|
|
|
)
|
|
|
|
|
2021-09-03 00:45:32 +00:00
|
|
|
opuses_parser.add_argument(
|
2021-08-23 16:25:25 +00:00
|
|
|
"opus_ids",
|
2021-08-23 12:57:05 +00:00
|
|
|
type=str,
|
2021-08-23 16:25:25 +00:00
|
|
|
nargs="+",
|
2021-08-04 22:54:15 +00:00
|
|
|
metavar="Opus_ID",
|
2021-08-23 16:25:25 +00:00
|
|
|
help="the Opuses to import",
|
2021-08-09 04:18:56 +00:00
|
|
|
)
|
2021-08-04 16:36:23 +00:00
|
|
|
|
2021-09-03 00:45:32 +00:00
|
|
|
opuses_parser.add_argument(
|
2021-08-23 12:57:05 +00:00
|
|
|
"-n", "--num_requests",
|
2021-08-04 16:36:23 +00:00
|
|
|
type=int,
|
|
|
|
default=20,
|
|
|
|
metavar="COUNT",
|
2021-08-09 04:18:56 +00:00
|
|
|
help="maximum number of concurrent requests",
|
|
|
|
)
|
2021-08-04 16:36:23 +00:00
|
|
|
|
2021-09-03 00:45:32 +00:00
|
|
|
# "ffdecks" subcommand
|
|
|
|
ffdecks_parser = subparsers.add_parser(
|
|
|
|
"ffdecks",
|
|
|
|
description="Imports Decks from the ffdecks.com API and creates it as a JSON file.",
|
2021-08-23 12:57:05 +00:00
|
|
|
)
|
|
|
|
|
2021-09-03 00:45:32 +00:00
|
|
|
ffdecks_parser.set_defaults(
|
2021-08-24 15:32:51 +00:00
|
|
|
func=ffdecks_decks
|
2021-08-23 12:57:05 +00:00
|
|
|
)
|
|
|
|
|
2021-09-03 00:45:32 +00:00
|
|
|
ffdecks_parser.add_argument(
|
2021-08-24 15:32:51 +00:00
|
|
|
"deck_ids",
|
2021-08-23 12:57:05 +00:00
|
|
|
type=str,
|
2021-08-24 15:32:51 +00:00
|
|
|
nargs="+",
|
2021-08-23 12:57:05 +00:00
|
|
|
metavar="Deck_ID",
|
2021-08-24 15:32:51 +00:00
|
|
|
help="the Decks to import",
|
2021-08-23 12:57:05 +00:00
|
|
|
)
|
2021-08-04 16:36:23 +00:00
|
|
|
|
2021-08-24 15:29:12 +00:00
|
|
|
# parse arguments
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-08-04 22:17:47 +00:00
|
|
|
# set up logging
|
2021-08-24 15:29:12 +00:00
|
|
|
if args.verbose is None:
|
|
|
|
args.verbose = logging.WARN
|
|
|
|
elif args.verbose == 1:
|
|
|
|
args.verbose = logging.INFO
|
|
|
|
else:
|
|
|
|
args.verbose = logging.DEBUG
|
|
|
|
|
2021-08-23 12:57:05 +00:00
|
|
|
logging.basicConfig(
|
2021-08-24 15:29:12 +00:00
|
|
|
level=args.verbose,
|
2021-08-23 12:57:05 +00:00
|
|
|
format="%(levelname)s: %(processName)s %(message)s",
|
|
|
|
)
|
2021-08-04 16:36:23 +00:00
|
|
|
|
2021-08-24 15:29:12 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logger.debug(f"{args = }")
|
|
|
|
|
2021-08-04 16:36:23 +00:00
|
|
|
# output directory
|
2021-08-23 14:00:17 +00:00
|
|
|
if not os.path.exists(OUT_DIR_NAME):
|
|
|
|
os.mkdir(OUT_DIR_NAME)
|
|
|
|
|
|
|
|
os.chdir(OUT_DIR_NAME)
|
2021-08-04 16:36:23 +00:00
|
|
|
|
2021-08-23 12:57:05 +00:00
|
|
|
# call function based on args
|
2021-09-03 02:57:34 +00:00
|
|
|
decks = args.func(args)
|
|
|
|
|
|
|
|
# decide what to do with the decks
|
|
|
|
if args.stdout:
|
|
|
|
# create file-like buffer
|
|
|
|
with io.BytesIO() as buffer:
|
|
|
|
# create zip file in buffer
|
|
|
|
with zipfile.ZipFile(buffer, "w") as zip_file:
|
|
|
|
for deck in decks:
|
|
|
|
zip_file.writestr(deck.file_name, deck.get_json(args.language))
|
|
|
|
|
2021-09-03 03:46:56 +00:00
|
|
|
# write buffer to stdout
|
2021-09-03 02:57:34 +00:00
|
|
|
buffer.seek(0)
|
|
|
|
with open(sys.stdout.fileno(), "wb", closefd=False, buffering=0) as stdout:
|
|
|
|
stdout.write(buffer.read())
|
|
|
|
|
|
|
|
else:
|
|
|
|
for deck in decks:
|
|
|
|
deck.save(args.language)
|
2021-08-04 22:17:47 +00:00
|
|
|
|
2021-09-03 02:57:34 +00:00
|
|
|
# bye
|
|
|
|
print("Done. Put the generated JSON files in your 'Saved Objects' Folder.")
|
|
|
|
print("Thanks for using fftcgtool!")
|
2021-08-04 01:39:19 +00:00
|
|
|
|
2018-11-02 22:13:17 +00:00
|
|
|
|
2021-08-09 04:18:56 +00:00
|
|
|
if __name__ == "__main__":
|
2021-08-03 21:41:25 +00:00
|
|
|
main()
|