-
Notifications
You must be signed in to change notification settings - Fork 11
/
interface.py
109 lines (100 loc) · 3.81 KB
/
interface.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
class InterfaceState:
def __init__(self, change_callback):
self.dealt_cards = []
self.robot = None
self.program = [None, None, None, None, None]
self.blocked_cards = []
self.power_down = False
self.selection_confirmed = False
self.cursor_index = 0 # 0-4 number of positon
self.timer = None
# Assign the function that should be called within some InterfaceState methods,
# eg. after choosing or returning cards on hand,
# not on change of the purely visual elements of interface, like moving the cursor.
# Assigned function is be called after every robot-related interface state change.
self.change_callback = change_callback
def __repr__(self):
return f"InterfaceState \
Cards: {self.dealt_cards}, \
My Cards: {self.program}, \
Power Down: {self.power_down,}, \
Robot: {self.robot}"
def as_dict(self):
"""
Return dictionary about state of client_interface."
"""
return {
"interface_data": {
"program": self.program,
"power_down": self.power_down,
"confirmed": self.selection_confirmed,
}
}
def select_card(self, dealt_card_index):
"""
Select a card from the dealt cards and put on
a place where selector is (in the robot program)
and selector cursor moves to the next free place.
"""
if not self.selection_confirmed:
if dealt_card_index >= len(self.dealt_cards):
return
if dealt_card_index not in self.program:
self.program[self.cursor_index] = dealt_card_index
self.change_callback()
# After selecting a card move the cursor to the right
self.cursor_index_plus()
def return_card(self):
"""
Return one selected card from your program back to the dealt cards.
"""
if not self.selection_confirmed:
self.program[self.cursor_index] = None
self.change_callback()
def return_cards(self):
"""
Return all cards of your program back to the dealt cards.
"""
if not self.selection_confirmed:
for card in range(len(self.program)):
self.program[card] = None
self.cursor_index = 0
self.change_callback()
def cursor_index_plus(self):
"""
Change selecting cursor position to the next one.
"""
if not self.selection_confirmed:
max_cursor_index = len(self.program) - 1
if self.cursor_index < max_cursor_index:
self.cursor_index += 1
def cursor_index_minus(self):
"""
Change selecting cursor position to the previous one.
"""
if not self.selection_confirmed:
if self.cursor_index > 0:
self.cursor_index -= 1
def switch_power_down(self):
"""
Switch power down status between True and False.
When it is True the Robot doesn't play this round.
"""
if not self.selection_confirmed:
if not self.power_down:
self.power_down = True
else:
self.power_down = False
self.change_callback()
def confirm_selection(self):
"""
When Power Down is switched on or all cards are chosen,
it is possible to confirm selection.
Additionally when Power Down is switched on,
the cards from hand are returned.
"""
if None not in self.program or self.power_down:
if self.power_down:
self.return_cards()
self.selection_confirmed = True
self.change_callback()