Files
ordinaceprojekt/SběrDatRůzné/DailyStr8ts/vykresli_puzzle.py
T
Vladimir Buzalka c9903646f1 notebookvb
2026-05-08 13:30:47 +02:00

95 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Vykreslí Str8ts puzzle do PDF z dat v MySQL tabulce puzzles.
Formát: 2 puzzle nad sebou na A4, velikost 11×11 cm.
"""
import json
import sys
from pathlib import Path
sys.stdout.reconfigure(encoding="utf-8")
sys.path.insert(0, str(Path(__file__).parent.parent.parent / "Knihovny"))
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import cm
from reportlab.pdfgen.canvas import Canvas
from mysql_db import connect_mysql
OUTPUT = Path(__file__).parent / "test_grid2.pdf"
GRID = 9
BOARD_CM = 11
BOARD = BOARD_CM * cm
CELL = BOARD / GRID
def draw_str8ts(c: Canvas, x0: float, y0: float, puzzle: str, bw: str, title: str = ""):
font_size = CELL * 0.55
if title:
c.setFont("Helvetica-Bold", 12)
c.drawString(x0, y0 + 5, title)
for idx in range(81):
row, col = divmod(idx, 9)
cell_x = x0 + col * CELL
cell_y = y0 - (row + 1) * CELL
cx = cell_x + CELL / 2
cy = cell_y + CELL * 0.3
is_black = bw[idx] == "1"
ch = puzzle[idx]
if is_black:
c.setFillColor(colors.black)
c.rect(cell_x, cell_y, CELL, CELL, fill=1, stroke=0)
if ch in "123456789":
c.setFillColor(colors.yellow if is_black else colors.black)
c.setFont("Helvetica-Bold", font_size)
c.drawCentredString(cx, cy, ch)
c.setFillColor(colors.black)
for i in range(GRID + 1):
c.setLineWidth(0.8)
c.line(x0, y0 - i * CELL, x0 + BOARD, y0 - i * CELL)
c.line(x0 + i * CELL, y0, x0 + i * CELL, y0 - BOARD)
def main():
conn = connect_mysql(database="puzzle")
cur = conn.cursor()
cur.execute(
"SELECT difficulty, puzzle, extra FROM puzzles "
"WHERE game_type='str8ts' AND puzzle_date='2026-05-08' "
"ORDER BY FIELD(difficulty, 'easy', 'medium', 'hard')"
)
rows = cur.fetchall()
cur.close()
conn.close()
if not rows:
print("Žádná data pro dnešek.")
return
page_w, page_h = A4
x0 = (page_w - BOARD) / 2
c = Canvas(str(OUTPUT), pagesize=A4)
# 2 puzzle na stránku
for i in range(0, len(rows), 2):
page_rows = rows[i:i + 2]
for j, (difficulty, puzzle, extra) in enumerate(page_rows):
bw = json.loads(extra)["bw"]
y0 = page_h - 2 * cm - j * (BOARD + 3 * cm)
draw_str8ts(c, x0, y0, puzzle, bw, difficulty.capitalize())
c.showPage()
c.save()
print(f"PDF uloženo: {OUTPUT}")
if __name__ == "__main__":
main()