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

60 lines
1.8 KiB
Python
Raw Normal View History

2021-09-03 07:29:40 +00:00
import itertools
from typing import Iterator, Generator, Iterable
2021-09-03 05:34:12 +00:00
from PIL import Image
2021-08-17 15:37:28 +00:00
from .grid import Grid
# constants
GRID = Grid((10, 7)) # default in TTsim: 10 columns, 7 rows
RESOLUTION = Grid((429, 600)) # default in TTsim: 480x670 pixels per card
2021-08-23 14:00:17 +00:00
DECKS_DIR_NAME = "decks" # name of decks directory
IMAGES_DIR_NAME = "images" # name of images directory
2021-09-03 05:11:46 +00:00
CARDDB_FILE_NAME = "carddb.zip" # name of card db file
2021-08-18 11:49:34 +00:00
# card back URL (image by Aurik)
CARD_BACK_URL = "http://cloud-3.steamusercontent.com/ugc/948455238665576576/85063172B8C340602E8D6C783A457122F53F7843/"
2021-08-17 15:37:28 +00:00
# functions
2021-09-03 05:34:12 +00:00
def encircle_symbol(symbol: str, negative: bool) -> str:
2021-08-17 15:37:28 +00:00
symbol = symbol[0].upper()
base_symbols: tuple[str, str] = "", ""
if symbol.isalpha():
if negative:
base_symbols = "A", "🅐"
else:
base_symbols = "A", ""
elif symbol == "0":
if negative:
base_symbols = "0", "🄌"
else:
base_symbols = "0", ""
elif symbol.isnumeric():
if negative:
base_symbols = "1", ""
else:
base_symbols = "1", ""
symbol_num = ord(symbol) - ord(base_symbols[0])
return chr(ord(base_symbols[1]) + symbol_num)
2021-09-03 07:29:40 +00:00
def chunks(chunk_size: int, whole: Iterable) -> Generator[Iterator, None, None]:
it = iter(whole)
# get chunk
while chunk := itertools.islice(it, chunk_size):
# stop if no first element was found
try:
first_el = next(chunk)
except StopIteration:
return
# reattach first element
yield itertools.chain((first_el,), chunk)
def grid_paste(page: Image.Image, index: int, card: Image.Image) -> None:
w, h = card.size
position = (index % GRID.x) * w, (index // GRID.x) * h
page.paste(card, position)