Implement reroll

1-reproducible-board
Felix Pankratz 5 months ago
parent a90adbb165
commit 3cded45cfd

@ -2,10 +2,12 @@
from textual.app import App, ComposeResult from textual.app import App, ComposeResult
from textual.widgets import Header, Footer, Input, Label, Static, Button from textual.widgets import Header, Footer, Input, Label, Static, Button
from textual.widget import Widget
from textual.message import Message from textual.message import Message
from textual.color import Color from textual.color import Color
from textual.containers import Horizontal from textual.containers import Horizontal
from textual.validation import Number from textual.validation import Number
from textual.reactive import reactive
from asyncio import sleep from asyncio import sleep
@ -16,6 +18,7 @@ class BingoField(Static):
"""A Bingo field widget.""" """A Bingo field widget."""
cursor_x, cursor_y = 2, 2 cursor_x, cursor_y = 2, 2
text = reactive("temp")
class Selected(Message): class Selected(Message):
"""Send message to the board containing clicked field info""" """Send message to the board containing clicked field info"""
@ -25,10 +28,10 @@ class BingoField(Static):
super().__init__() super().__init__()
def __init__(self, num, text: str) -> None: def __init__(self, num, text: str) -> None:
self.text = text
self.num = num self.num = num
self.selected = False self.selected = False
super().__init__() super().__init__()
self.text = text
def on_mount(self) -> None: def on_mount(self) -> None:
self.styles.content_align = ("center", "middle") self.styles.content_align = ("center", "middle")
@ -72,28 +75,37 @@ class BingoDisplay(Static):
"""Create child widgets for the app.""" """Create child widgets for the app."""
self.board = BingoBoard() self.board = BingoBoard()
yield self.board yield self.board
input_field = Input( self.input_field = Input(
str(self.board.seed), str(self.board.seed),
type='integer', type='integer',
placeholder='UNIX timestamp', placeholder='UNIX timestamp',
max_length=10, max_length=10,
classes='seed_input', classes='seed_input',
validators=[ validators=[
Number(minimum=1000000000, maximum = int(datetime.now().timestamp())) Number(minimum=1000000000, maximum = 2000000000)
] ]
) )
input_field.border_title = 'Seed' self.input_field.border_title = 'Seed'
yield Horizontal( yield Horizontal(
input_field, self.input_field,
Button('re-roll', classes='roll_btn'), Button('re-roll', classes='roll_btn'),
classes='bottom_line' classes='bottom_line'
) )
class BingoBoard(Static): def on_button_pressed(self, event: Button.Pressed) -> None:
""""""
# reroll
self.board.roll_board(int(datetime.now().timestamp()))
self.input_field.value = str(self.board.seed)
class BingoBoard(Widget):
fieldstate = [False for _ in range(25)] fields = reactive([], recompose = True)
fields = [ def __init__(self) -> None:
self.fieldstate = [False for _ in range(25)]
super().__init__()
self.fields = [
'Datenelch', 'Datenelch',
'6 Stunden Schlaf', '6 Stunden Schlaf',
'Tschunk getrunken', 'Tschunk getrunken',
@ -121,10 +133,21 @@ class BingoBoard(Static):
'Gulasch gegessen' 'Gulasch gegessen'
] ]
# get rng seed from current time self.roll_board(int(datetime.now().timestamp()))
seed = int(datetime.now().timestamp())
def roll_board(self, seed):
print('rolling board.')
self.seed = seed
random.seed(seed) random.seed(seed)
random.shuffle(fields) self.fields = random.sample(self.fields, len(self.fields))
print('board after shuf:')
print(self.fields)
def watch_fields(self, new_state) -> None:
print('watch_fields: called')
self.fieldstate = [False for _ in range(25)]
for idx, field in enumerate(self.query(BingoField)):
field.text = new_state[idx]
def compose(self) -> ComposeResult: def compose(self) -> ComposeResult:
"""Create child widgets for the app.""" """Create child widgets for the app."""

Loading…
Cancel
Save