1
0
Fork 0
mirror of https://github.com/ldericher/fftcgtool synced 2025-01-15 15:02:59 +00:00
fftcgtool/fftcg/opus.py

140 lines
4.3 KiB
Python
Raw Normal View History

2021-08-04 16:36:23 +00:00
import logging
from dataclasses import replace
2021-09-03 05:34:12 +00:00
from typing import Callable, Iterator
2021-08-04 16:36:23 +00:00
2021-08-20 22:16:38 +00:00
import requests
2021-08-04 01:39:19 +00:00
import roman
2021-08-03 17:45:35 +00:00
2021-08-23 14:55:41 +00:00
from .card import Card
from .carddb import CardDB
2021-08-09 05:01:25 +00:00
from .cards import Cards
from .language import Language, API_LANGS
2021-08-17 15:37:28 +00:00
from .ttsdeck import TTSDeck
2021-08-02 23:55:12 +00:00
2021-08-03 17:45:35 +00:00
2021-08-03 21:41:25 +00:00
class Opus(Cards):
__SQUARE_API_URL = "https://fftcg.square-enix-games.com/en/get-cards"
2021-08-23 16:16:56 +00:00
def __init__(self, opus_id: str, language: Language):
2021-08-04 16:36:23 +00:00
logger = logging.getLogger(__name__)
self.__language = language
2021-08-04 16:36:23 +00:00
2021-08-20 22:16:38 +00:00
params: dict[str, any]
2021-08-04 22:40:32 +00:00
if opus_id.isnumeric():
name = f"Opus {opus_id} ({self.__language.short})"
2021-08-04 22:40:32 +00:00
self.__number = opus_id
2021-08-18 11:49:34 +00:00
params = {"set": [f"Opus {roman.toRoman(int(opus_id)).upper()}"]}
2021-08-04 16:36:23 +00:00
elif opus_id == "chaos":
name = f"Boss Deck Chaos ({self.__language.short})"
self.__number = "B"
2021-08-23 16:16:56 +00:00
params = {"set": ["Boss Deck Chaos"]}
2021-08-04 16:36:23 +00:00
elif opus_id == "promo":
name = f"Promo ({self.__language.short})"
self.__number = "PR"
2021-08-09 12:03:45 +00:00
params = {"rarity": ["pr"]}
2021-08-04 16:36:23 +00:00
else:
2021-08-18 11:49:34 +00:00
name = "?"
2021-08-09 12:03:45 +00:00
self.__number = "?"
self.__filename = "?"
params = {"set": "?"}
2021-08-02 23:55:12 +00:00
2021-08-20 22:16:38 +00:00
# required params:
# text
# supported params:
# [str] text, language, code, multicard="○"|"", ex_burst="○"|"", special="《S》"|""
# [array] type, element, cost, rarity, power, category_1, set
# [int] exactmatch=0|1
if "text" not in params:
params["text"] = ""
# get cards from square api
req = requests.post(Opus.__SQUARE_API_URL, json=params)
carddb = CardDB()
cards = [
Card.from_square_api_data(card_data)
2021-08-20 22:16:38 +00:00
for card_data in req.json()["cards"]
]
2021-08-04 01:39:19 +00:00
# remove reprints
super().__init__(name, [
card
for card in cards
if card.code.opus == self.__number
])
2021-08-04 16:36:23 +00:00
# sort cards by opus, then serial
self.sort(key=lambda x: x.code.serial)
self.sort(key=lambda x: x.code.opus)
2021-08-04 16:36:23 +00:00
for card in self:
if card.code in carddb:
for lang in API_LANGS:
card[lang] = replace(card[lang], face=carddb[card.code][lang].face)
logger.debug(f"imported card {card}")
@property
2021-08-09 02:03:24 +00:00
def number(self) -> str:
return self.__number
2021-08-09 12:03:45 +00:00
2021-08-17 15:37:28 +00:00
@property
2021-09-03 05:34:12 +00:00
def elemental_decks(self) -> Iterator[TTSDeck]:
2021-08-23 16:16:56 +00:00
if self.number in ["PR", "B"]:
2021-08-18 11:49:34 +00:00
return [TTSDeck(
2021-09-03 00:46:04 +00:00
codes=[
2021-08-18 11:49:34 +00:00
card.code
for card in self
],
2021-09-03 00:46:04 +00:00
name=f"{self.name}",
description=f"All {self.name} Cards",
face_down=False,
2021-08-18 11:49:34 +00:00
)]
2021-08-17 15:37:28 +00:00
else:
2021-09-03 05:34:12 +00:00
def element_filter(element: str) -> Callable[[Card], list[str]]:
2021-08-17 15:37:28 +00:00
return lambda card: card.elements == [element]
# simple cases: create lambdas for base elemental decks
base_elements = ["Fire", "Ice", "Wind", "Earth", "Lightning", "Water"]
2021-08-20 22:16:38 +00:00
filters = {
elem: element_filter(elem)
for elem in base_elements
}
2021-08-17 15:37:28 +00:00
2021-08-18 11:49:34 +00:00
filters |= {
2021-08-17 15:37:28 +00:00
# light/darkness elemental deck
2021-08-18 15:38:58 +00:00
"Light-Darkness": lambda card: card.elements == ["Light"] or card.elements == ["Darkness"],
2021-08-17 15:37:28 +00:00
# multi element deck
2021-08-18 11:49:34 +00:00
"Multi": lambda card: len(card.elements) > 1,
}
2021-09-02 00:54:42 +00:00
# sort cards by element, then alphabetically
cards = list(self)
cards.sort(key=lambda x: x[self.__language].name)
cards.sort(key=lambda x: "Multi" if len(x.elements) > 1 else x.elements[0])
2021-09-03 05:34:12 +00:00
# generate decks
decks = (
TTSDeck(
codes=[
card.code
for card in cards
if f(card)
],
name=f"{self.name} {elem}",
description=f"All {self.name} Cards with {elem} element in alphabetical order",
face_down=False,
) for elem, f in filters.items()
)
# Ignore empty decks
return (
deck
for deck in decks
if deck
)