File size: 1,586 Bytes
9fca766
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""Deck viewer: what you're running, grouped and counted. Any key closes."""

from __future__ import annotations

from collections import Counter

from rich.table import Table
from rich.text import Text
from textual import events
from textual.app import ComposeResult
from textual.screen import ModalScreen
from textual.widgets import Static

from scrypt.engine.cards import Card


class DeckViewScreen(ModalScreen):
    CSS = """
    DeckViewScreen { align: center middle; }
    #deck-table { width: 60; max-height: 80%; padding: 1 2; background: $surface;
                  border: solid grey; }
    """

    def __init__(self, deck: list[Card], title: str = "your deck"):
        super().__init__()
        self.deck = deck
        self.title = title

    def compose(self) -> ComposeResult:
        counts = Counter(c.id for c in self.deck)
        specs = {c.id: c for c in self.deck}
        table = Table(title=f"{self.title}{len(self.deck)} cards", expand=True)
        table.add_column("card")
        table.add_column("#", justify="right")
        table.add_column("stats", justify="center")
        table.add_column("cost")
        table.add_column("sigils")
        for cid, n in sorted(counts.items(), key=lambda kv: (-specs[kv[0]].power, kv[0])):
            spec = specs[cid]
            table.add_row(
                spec.name, str(n), f"{spec.power}{spec.health}♥",
                str(spec.cost), " ".join(spec.sigils),
            )
        yield Static(table, id="deck-table")

    def on_key(self, event: events.Key) -> None:
        self.dismiss(None)