-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_cells.py
More file actions
51 lines (42 loc) · 1.74 KB
/
Copy pathservice_cells.py
File metadata and controls
51 lines (42 loc) · 1.74 KB
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
49
50
51
from __future__ import annotations
from backend.rules.base import AutomatonRule
from backend.simulation.models import SimulationStateData
def validate_state_value(rule: AutomatonRule, state: int) -> None:
if not rule.is_valid_state(state):
raise ValueError(f"State '{state}' is invalid for rule '{rule.name}'.")
def validate_state_values(rule: AutomatonRule, states: list[int]) -> None:
for state in states:
validate_state_value(rule, state)
def set_cells_by_id(state: SimulationStateData, cells: list[tuple[str, int]]) -> dict[str, int]:
previous_states = {
cell_id: state.board.state_for(cell_id)
for cell_id, _ in cells
if state.topology.has_cell(cell_id)
}
for cell_id, next_state in cells:
if state.topology.has_cell(cell_id):
state.board.set_state_for(cell_id, int(next_state))
return {
cell_id: state.board.state_for(cell_id)
for cell_id, previous_state in previous_states.items()
if state.board.state_for(cell_id) != previous_state
}
def toggle_cells_by_id(state: SimulationStateData, cell_ids: list[str]) -> dict[str, int]:
previous_states = {
cell_id: state.board.state_for(cell_id)
for cell_id in cell_ids
if state.topology.has_cell(cell_id)
}
for cell_id in cell_ids:
if not state.topology.has_cell(cell_id):
continue
current_state = state.board.state_for(cell_id)
state.board.set_state_for(
cell_id,
0 if current_state else state.rule.default_paint_state,
)
return {
cell_id: state.board.state_for(cell_id)
for cell_id, previous_state in previous_states.items()
if state.board.state_for(cell_id) != previous_state
}