/
tools.py
210 lines (144 loc) · 5.45 KB
/
tools.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import room
import playa
import random
import threading
import time
import colors
original_deck = playa.original_deck
power_deck = playa.power_deck
power_dictionary = playa.power_dictionary
def display_cards(player: playa.playa):
print(f"\n-- Remember your cards : ")
print(f"{colors.purple}-- 1 : {show_card(player.cards[0])}, 2 : {show_card(player.cards[1])}{colors.ENDC}", end='')
time.sleep(4)
for i in range(50):
print('\b', end='')
print(f"{colors.purple}-- That's all the time you get, kid.{colors.ENDC}")
def shuffle(deck: list):
new_deck = deck.copy()
random.shuffle(new_deck)
return new_deck
def pop_cards(deck: list, count: int):
cards = []
for i in range(count):
cards.append(deck.pop(0))
return cards, deck
def list_indices(deck: list):
indices = []
for card in deck:
indices.append(original_deck.index(card))
return indices
def init_game():
current_deck = shuffle(original_deck)
my_room = room.room()
my_room.publisher(current_status=0, players=[''], player_status='', stack_status=[''],
deck_status=list_indices(current_deck))
time.sleep(1)
refresh = threading.Thread(target=my_room.refresher)
refresh.start()
print("- room initialising -\n")
return join_room(my_room=my_room, through_init=True)
def join_room(my_room=None, through_init=False):
start = time.time()
player = input("Enter your name : ")
if not through_init:
my_room = room.room(join=True)
refresh = threading.Thread(target=my_room.refresher)
refresh.start()
player_cards, new_deck = pop_cards(deck=my_room.deck, count=5)
my_player = playa.playa(name=player.lower(), cards=player_cards)
print("\n- adding you to the ROOM -")
my_room.add_player(player=my_player, deck=new_deck)
print(f"JOINED ROOM in : {time.time() - start}s")
time.sleep(1)
display_cards(player=my_player)
return my_room, my_player
def show_card(card):
if card == '-':
return '-'
else:
return original_deck[int(card)]
def get_other_player(my_room: room.room, name: str):
return my_room.all_players[name]
def compute_score(my_room: room.room):
scores_dictionary = {}
for player in my_room.all_players.values():
player, score = player_score(player=player)
scores_dictionary[player] = score
return scores_dictionary
def player_score(player: playa.playa, indexes: list = None):
player_sum = 0
cards = []
if indexes is not None:
for index in room.remove_blanks(indexes):
cards.append(show_card(int(index)-1))
else:
for card in room.remove_blanks(player.cards):
cards.append(show_card(int(card)))
if len(cards) == 0:
return player, 0
for card in cards:
if card == 'HK' or card == 'DK':
continue
elif card[-1] in power_dictionary:
player_sum += power_dictionary[card[-1]]
else:
player_sum += int(card[2:])
return player, player_sum
def execute_burn(my_room: room.room, my_player: playa.playa, indexes: list):
stack = []
for index in indexes:
card = my_player.cards[int(index)-1]
stack.append(card)
my_player.cards_index.remove(int(index))
for index in range(len(my_player.cards)):
if index+1 not in my_player.cards_index:
my_player.cards[int(index)] = '_'
my_room.stack.extend(sorted(stack, reverse=True))
print(f"\nAnd that's a {colors.red}BURN !{colors.ENDC}")
stage_changes(my_room=my_room, player=my_player, what="burn", whose=my_player.name)
def lol_burn(my_room: room.room, my_player: playa.playa):
my_player.cards.append(my_room.deck.pop(0))
if max(my_player.cards_index) > 6:
my_player.cards_index.append(max(my_player.cards_index) + 1)
else:
my_player.cards_index.append(6)
print(f"\nMISMATCHED !\nInstead of your cards, {colors.red}YOU JUST GOT BURNED.{colors.ENDC} Here's an extra card for your troubles.")
stage_changes(my_room=my_room, player=my_player, what="burn", whose=my_player.name)
def stage_changes(my_room: room.room, player: playa.playa, what: str, whose: str, which: str = None):
other_player = None
player.CHANGED = True
player.WHAT = what
player.WHOSE = whose
if player.name != whose:
other_player = get_other_player(my_room=my_room, name=whose)
other_player.CHANGED = True
other_player.WHAT = what
other_player.WHOSE = whose
if which is not None and ',' in which:
player.WHICH = which.split(',')[0]
other_player.WHICH = which.split(',')[1]
elif which is not None:
if other_player is not None:
other_player.WHICH = which
else:
player.WHICH = which
# def power_function(my_room: room.room):
# new_player_list = my_room.players[-1:] + my_room.players[:-1]
# my_room.players = new_player_list
def invoke_cameo(my_room: room.room, my_player: playa.playa):
my_room.cameo_invoked = my_player.name
my_player.CHANGED = True
my_player.WHAT = "Cameo"
def execute_cameo(my_room: room.room):
scores_dictionary = compute_score(my_room=my_room)
min_score = 100
winner = None
for player, score in scores_dictionary.items():
player.WHICH = score
if score < min_score:
min_score = score
winner = player
my_room.winner = winner
my_room.print_result()
my_room.kill_me()