-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdurak.py
More file actions
749 lines (597 loc) · 22 KB
/
Copy pathdurak.py
File metadata and controls
749 lines (597 loc) · 22 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
import json
import os, keyboard
import threading
import time
import random
import sys
import requests
import ast
HOST = "127.0.0.1"
PORT = 4433
selector = "ᐞ"
selected_counter = 2
require_update = True
you_win = False
you_lose = False
post_requested = False
sync_locked = False
dont_add = False
dont_deal = False
taking = False
taking_accepted = False
debug = False
trump = ["Ace Spades"]
def clear():
if os.name == 'nt':
os.system("cls")
else:
os.system("clear")
def shuffle_cards(deck):
cards = list(deck.keys())
random.shuffle(cards)
shuffled_deck = {}
for card in cards:
shuffled_deck[card] = deck[card]
return shuffled_deck
def deal_cards(num_cards):
try:
global playing_deck
global trump
global dont_deal
if dont_deal:
return []
if debug:
with open("log.txt", "a") as f:
f.write(str(len(playing_deck)) + "\n")
if num_cards > len(list(playing_deck.items())):
playing_deck[trump] = playing_deck_preserved[trump]
trump = ""
dont_deal = True
if num_cards > len(list(playing_deck.items())):
num_cards = len(list(playing_deck.items()))
dealt_cards = []
for _ in range(0, num_cards):
card = list(playing_deck.items())[0][0]
dealt_cards.append(card)
#print(card)
#with open("cards.txt", "a") as f:
#f.write(card + "\n")
del playing_deck[card]
return dealt_cards
except Exception as e:
#print("deal_cards: " + str(e))
return []
def move_left():
global selected_counter
global require_update
if selected_counter > min_cards:
selected_counter -= 1
require_update = True
def move_right():
global selected_counter
global require_update
if selected_counter < max_cards:
selected_counter += 1
require_update = True
def can_play(card):
global trump_suit
global require_update
global dont_add
card_rank = card.split(" ")[0]
card_suit = card.split(" ")[1]
if your_turn:
if table_cards_enemy == [] and table_cards_your == [] and table_cards_your_defended == [] and table_cards_enemy_defended == []:
return True
for card in table_cards_your:
if card.split(" ")[0] == card_rank:
return True
for card in table_cards_enemy:
if card.split(" ")[0] == card_rank:
return True
for card in table_cards_your_defended:
if card.split(" ")[0] == card_rank:
return True
for card in table_cards_enemy_defended:
if card.split(" ")[0] == card_rank:
return True
else:
if table_cards_enemy == []:
require_update = True
return False
for counter in range(0, len(table_cards_enemy)):
if table_cards_enemy[counter] not in table_cards_enemy_defended:
card_enemy = table_cards_enemy[counter]
break
enemy_card_suit = card_enemy.split(" ")[1]
if card_suit == enemy_card_suit or card_suit == trump_suit:
defend_weight = playing_deck_preserved.get(card).get("weight")
attack_weight = playing_deck_preserved.get(card_enemy).get("weight")
if defend_weight > attack_weight:
table_cards_enemy_defended.append(card_enemy)
table_cards_your_defended.append(card)
table_cards_enemy.remove(card_enemy)
dont_add = True
return True
return False
def action():
global post_requested
global your_cards
global enemy_cards
global sync_locked
global your_turn
global taking
global taking_accepted
global require_update
sync_locked = True
if debug:
with open("log.txt", "at") as f:
f.write(str(table_cards_enemy) + "\n")
f.write(str(table_cards_your) + "\n")
f.write(str(table_cards_enemy_defended) + "\n")
f.write(str(table_cards_your_defended) + "\n")
if your_turn:
if taking:
taking_accepted = True
post_requested = True
sync_locked = False
require_update = True
return
if table_cards_enemy + table_cards_your == []:
flush()
enemy_cards_amount = len(enemy_cards)
your_cards_amount = len(your_cards)
if your_turn:
if your_cards_amount < 6:
your_cards.extend(deal_cards(6 - your_cards_amount))
if enemy_cards_amount < 6:
enemy_cards.extend(deal_cards(6 - enemy_cards_amount))
else:
if enemy_cards_amount < 6:
enemy_cards.extend(deal_cards(6 - enemy_cards_amount))
if your_cards_amount < 6:
your_cards.extend(deal_cards(6 - your_cards_amount))
your_turn = not your_turn
else:
if table_cards_enemy + table_cards_your + table_cards_enemy_defended + table_cards_your_defended != []:
taking = True
if not taking_accepted:
post_requested = True
sync_locked = False
require_update = True
return
taking = False
taking_accepted = False
grab()
enemy_cards_amount = len(enemy_cards)
your_cards_amount = len(your_cards)
if your_turn:
if your_cards_amount < 6:
your_cards.extend(deal_cards(6 - your_cards_amount))
if enemy_cards_amount < 6:
enemy_cards.extend(deal_cards(6 - enemy_cards_amount))
else:
if enemy_cards_amount < 6:
enemy_cards.extend(deal_cards(6 - enemy_cards_amount))
if your_cards_amount < 6:
your_cards.extend(deal_cards(6 - your_cards_amount))
post_requested = True
sync_locked = False
# except Exception as e:
# print(e)
# post_requested = True
# sync_locked = False
#raise Exception("Test")
def weight(card):
return playing_deck_preserved.get(card).get("weight")
def sort(cards: list):
spades = []
diamonds = []
clubs = []
hearts = []
for card in cards:
card_suit = card.split(" ")[1]
#print(card_suit)
if card_suit == "Spades":
spades.append(card)
if card_suit == "Diamonds":
diamonds.append(card)
if card_suit == "Clubs":
clubs.append(card)
if card_suit == "Hearts":
hearts.append(card)
spades.sort(key=weight)
diamonds.sort(key=weight)
clubs.sort(key=weight)
hearts.sort(key=weight)
cards = spades + diamonds + clubs + hearts
return cards
def grab():
global table_cards_enemy
global table_cards_your
global table_cards_your_defended
global table_cards_enemy_defended
global your_cards
global require_update
global your_turn
your_cards.extend(table_cards_enemy)
your_cards.extend(table_cards_your)
your_cards.extend(table_cards_your_defended)
your_cards.extend(table_cards_enemy_defended)
with open("log.txt", "at") as f:
f.write("Grabbed!")
table_cards_enemy = []
table_cards_your = []
table_cards_your_defended = []
table_cards_enemy_defended = []
require_update = True
def flush():
global table_cards_enemy
global table_cards_your
global table_cards_your_defended
global table_cards_enemy_defended
global your_cards
global enemy_cards
global require_update
global your_turn
table_cards_enemy = []
table_cards_your = []
table_cards_your_defended = []
table_cards_enemy_defended = []
if debug:
with open("log.txt", "at") as f:
f.write("your_turn = " + str(your_turn) + "\n")
require_update = True
def play(cards):
global require_update
global your_cards
global max_cards
global selected_counter
global post_requested
global sync_locked
global dont_add
sync_locked = True
if len(cards) == 0:
sync_locked = False
return
card = cards[selected_counter - 1]
if not can_play(card):
sync_locked = False
return
if selected_counter == 1:
pass
#selected_counter += 1
else:
selected_counter -= 1
your_cards.remove(card)
max_cards = len(your_cards)
if not dont_add:
table_cards_your.append(card)
else:
dont_add = False
sync_locked = False
require_update = True
post_requested = True
def set_as_trump(deck, suit):
for card in deck:
if suit in card:
deck[card]["weight"] += 9
return deck
def get_first_turn():
your_trumps = [999]
enemy_trumps = [999]
for card in your_cards:
weight = playing_deck_preserved.get(card).get("weight")
if weight >= 15:
your_trumps.append(weight)
for card in enemy_cards:
weight = playing_deck_preserved.get(card).get("weight")
if weight >= 15:
enemy_trumps.append(weight)
if min(enemy_trumps) == min(your_trumps):
return random.choice([True, False])
if min(enemy_trumps) < min(your_trumps):
return False
return True
cards_dict = {
'Six Spades': {'unicode': '🂦', 'weight': 6},
'Seven Spades': {'unicode': '🂧', 'weight': 7},
'Eight Spades': {'unicode': '🂨', 'weight': 8},
'Nine Spades': {'unicode': '🂩', 'weight': 9},
'Ten Spades': {'unicode': '🂪', 'weight': 10},
'Jack Spades': {'unicode': '🂫', 'weight': 11},
'Queen Spades': {'unicode': '🂭', 'weight': 12},
'King Spades': {'unicode': '🂮', 'weight': 13},
'Ace Spades': {'unicode': '🂡', 'weight': 14},
'Six Hearts': {'unicode': '🂶', 'weight': 6},
'Seven Hearts': {'unicode': '🂷', 'weight': 7},
'Eight Hearts': {'unicode': '🂸', 'weight': 8},
'Nine Hearts': {'unicode': '🂹', 'weight': 9},
'Ten Hearts': {'unicode': '🂺', 'weight': 10},
'Jack Hearts': {'unicode': '🂻', 'weight': 11},
'Queen Hearts': {'unicode': '🂽', 'weight': 12},
'King Hearts': {'unicode': '🂾', 'weight': 13},
'Ace Hearts': {'unicode': '🂱', 'weight': 14},
'Six Diamonds': {'unicode': '🃆', 'weight': 6},
'Seven Diamonds': {'unicode': '🃇', 'weight': 7},
'Eight Diamonds': {'unicode': '🃈', 'weight': 8},
'Nine Diamonds': {'unicode': '🃉', 'weight': 9},
'Ten Diamonds': {'unicode': '🃊', 'weight': 10},
'Jack Diamonds': {'unicode': '🃋', 'weight': 11},
'Queen Diamonds': {'unicode': '🃍', 'weight': 12},
'King Diamonds': {'unicode': '🃎', 'weight': 13},
'Ace Diamonds': {'unicode': '🃁', 'weight': 14},
'Six Clubs': {'unicode': '🃖', 'weight': 6},
'Seven Clubs': {'unicode': '🃗', 'weight': 7},
'Eight Clubs': {'unicode': '🃘', 'weight': 8},
'Nine Clubs': {'unicode': '🃙', 'weight': 9},
'Ten Clubs': {'unicode': '🃚', 'weight': 10},
'Jack Clubs': {'unicode': '🃛', 'weight': 11},
'Queen Clubs': {'unicode': '🃝', 'weight': 12},
'King Clubs': {'unicode': '🃞', 'weight': 13},
'Ace Clubs': {'unicode': '🃑', 'weight': 14}
}
playing_deck = shuffle_cards(cards_dict)
playing_deck_preserved = dict(playing_deck)
your_cards = deal_cards(6)
enemy_cards = deal_cards(6)
min_cards = 1
max_cards = len(your_cards)
table_cards_your = []
table_cards_enemy = []
table_cards_your_defended = []
table_cards_enemy_defended = []
trump = deal_cards(1)[0]
trump_suit = trump.split(" ")[1]
playing_deck_preserved = set_as_trump(playing_deck_preserved, trump_suit)
your_turn = get_first_turn()
# keyboard.add_hotkey("enter", lambda: play(your_cards))
# keyboard.add_hotkey("f", lambda: action())
# keyboard.add_hotkey('left', lambda: move_left())
# keyboard.add_hotkey('right', lambda: move_right())
#keyboard.add_hotkey("g", lambda: get_card())
running = True
def async_sync(game_code):
global save
global running
global playing_deck, playing_deck_preserved, your_cards, enemy_cards, table_cards_your, table_cards_enemy, table_cards_your_defended, table_cards_enemy_defended, trump, trump_suit, your_turn, taking, taking_accepted
global require_update
global post_requested
while running:
if sync_locked:
if debug:
with open("log.txt", "at") as f:
f.write("Sync locked!" + "\n")
time.sleep(0.1)
continue
if post_requested:
if debug:
with open("log.txt", "at") as f:
f.write("Post requested!" + "\n")
if connection_type == "create":
save = {
'playing_deck': playing_deck,
'playing_deck_preserved': playing_deck_preserved,
'your_cards': your_cards,
'enemy_cards': enemy_cards,
'table_cards_your': table_cards_your,
'table_cards_enemy': table_cards_enemy,
'table_cards_your_defended': table_cards_your_defended,
'table_cards_enemy_defended': table_cards_enemy_defended,
'trump': trump,
'trump_suit': trump_suit,
'your_turn': your_turn,
'taking': taking,
'taking_accepted': taking_accepted
}
else:
save = {
'playing_deck': playing_deck,
'playing_deck_preserved': playing_deck_preserved,
'your_cards': enemy_cards,
'enemy_cards': your_cards,
'table_cards_your': table_cards_enemy,
'table_cards_enemy': table_cards_your,
'table_cards_your_defended': table_cards_enemy_defended,
'table_cards_enemy_defended': table_cards_your_defended,
'trump': trump,
'trump_suit': trump_suit,
'your_turn': not your_turn,
'taking': taking,
'taking_accepted': taking_accepted
}
params = {
'game_code': game_code,
'game_data': save
}
req = requests.post(url=f"http://{HOST}:{PORT}/push", json=params).text + "\n"
if debug:
with open("log.txt", "at") as f:
f.write(req)
post_requested = False
continue
if debug:
with open("log.txt", "at") as f:
f.write("Working!" + "\n")
try:
new_save = json.loads(requests.get(url=f"http://{HOST}:{PORT}/get", params={"game_code": game_code}).text)
except ConnectionError:
if debug:
with open("log.txt", "at") as f:
f.write("Closed!" + "\n")
clear()
print("[ERROR] Lost connection! Closing...")
time.sleep(3)
sys.exit()
if save != new_save:
trump = new_save["trump"]
trump_suit = new_save["trump_suit"]
playing_deck = new_save["playing_deck"]
playing_deck_preserved = new_save["playing_deck_preserved"]
taking = new_save["taking"]
taking_accepted = new_save["taking_accepted"]
if connection_type == "create":
your_cards = new_save["your_cards"]
enemy_cards = new_save["enemy_cards"]
table_cards_your = new_save["table_cards_your"]
table_cards_enemy = new_save["table_cards_enemy"]
table_cards_your_defended = new_save["table_cards_your_defended"]
table_cards_enemy_defended = new_save["table_cards_enemy_defended"]
your_turn = new_save["your_turn"]
else:
your_cards = new_save["enemy_cards"]
enemy_cards = new_save["your_cards"]
table_cards_your_defended = new_save["table_cards_enemy_defended"]
table_cards_enemy_defended = new_save["table_cards_your_defended"]
table_cards_your = new_save["table_cards_enemy"]
table_cards_enemy = new_save["table_cards_your"]
your_turn = not new_save["your_turn"]
save = new_save
require_update = True
time.sleep(3)
def post():
if connection_type == "create":
save = {
'playing_deck': playing_deck,
'playing_deck_preserved': playing_deck_preserved,
'your_cards': your_cards,
'enemy_cards': enemy_cards,
'table_cards_your': table_cards_your,
'table_cards_enemy': table_cards_enemy,
'table_cards_enemy_defended': table_cards_enemy_defended,
'table_cards_your_defended': table_cards_your_defended,
'trump': trump,
'trump_suit': trump_suit,
'your_turn': your_turn,
'taking': taking,
'taking_accepted': taking_accepted
}
else:
save = {
'playing_deck': playing_deck,
'playing_deck_preserved': playing_deck_preserved,
'your_cards': enemy_cards,
'enemy_cards': your_cards,
'table_cards_your': table_cards_enemy,
'table_cards_enemy': table_cards_your,
'table_cards_enemy_defended': table_cards_your_defended,
'table_cards_your_defended': table_cards_enemy_defended,
'trump': trump,
'trump_suit': trump_suit,
'your_turn': not your_turn,
'taking': taking,
'taking_accepted': taking_accepted
}
params = {
'game_code': game_code,
'game_data': save
}
req = requests.post(url=f"http://{HOST}:{PORT}/push", json=params).text + "\n"
if debug:
with open("log.txt", "at") as f:
f.write(req)
def Update():
global max_cards
global your_cards
your_cards = sort(your_cards)
#print(your_cards)
if not debug:
clear()
max_cards = len(your_cards)
if you_win:
print("You WIN!")
input()
sys.exit()
if you_lose:
print("You LOSE!")
input()
sys.exit()
for card in enemy_cards:
#print(cards_dict.get(card).get("unicode"), end="")
print("🂠", end="")
if taking and your_turn:
print(" ⋄", end="")
print("\n", end="")
for card in table_cards_enemy_defended + table_cards_enemy:
print(cards_dict.get(card).get("unicode"), end="")
if trump != "":
print(" ", cards_dict.get(trump).get("unicode"), end="")
if playing_deck != {}:
print("🂠", end="")
print("\n", end="")
for card in table_cards_your_defended + table_cards_your:
print(cards_dict.get(card).get("unicode"), end="")
print("\n", end="")
for card in your_cards:
print(cards_dict.get(card).get("unicode"), end="")
if your_turn:
print(" ☜", end="")
if taking and not your_turn:
print(" ⋄", end="")
print(f"\n{selector:>{selected_counter}}")
connection_type = input("Connection type: ")
if not debug:
keyboard.add_hotkey("enter", lambda: play(your_cards))
keyboard.add_hotkey("f", lambda: action())
keyboard.add_hotkey('left', lambda: move_left())
keyboard.add_hotkey('right', lambda: move_right())
if connection_type == "create":
clear()
if debug:
keyboard.add_hotkey("enter", lambda: play(your_cards))
keyboard.add_hotkey("f", lambda: action())
keyboard.add_hotkey('left', lambda: move_left())
keyboard.add_hotkey('right', lambda: move_right())
game_code = requests.get(url=f"http://{HOST}:{PORT}/create_new_game").text
post()
save = json.loads(requests.get(url=f"http://{HOST}:{PORT}/get", params={"game_code": game_code}).text)
input(f"Room code is {game_code}.\nPress ENTER when your friend connects. ")
#post_requested = True
else:
if debug:
keyboard.add_hotkey("shift+enter", lambda: play(your_cards))
keyboard.add_hotkey("shift+f", lambda: action())
keyboard.add_hotkey('shift+left', lambda: move_left())
keyboard.add_hotkey('shift+right', lambda: move_right())
game_code = input("Game code (XXXXXX): ")
save = json.loads(requests.get(url=f"http://{HOST}:{PORT}/get", params={"game_code": game_code}).text)
playing_deck = save["playing_deck"]
playing_deck_preserved = save["playing_deck_preserved"]
your_cards = save["enemy_cards"]
enemy_cards = save["your_cards"]
table_cards_your = save["table_cards_enemy"]
table_cards_enemy = save["table_cards_your"]
table_cards_your_defended = save["table_cards_enemy_defended"]
table_cards_enemy_defended = save["table_cards_your_defended"]
trump = save["trump"]
trump_suit = save["trump_suit"]
your_turn = not save["your_turn"]
taking = save["taking"]
taking_accepted = save["taking_accepted"]
# Create two threads
thread1 = threading.Thread(target=async_sync, args=(game_code,))
# Start the threads
thread1.start()
#time.sleep(1)
while True:
if taking and taking_accepted and not your_turn:
action()
try:
if require_update:
Update()
require_update = False
if your_cards == [] and playing_deck == {}:
clear()
print("You WIN!")
time.sleep(10)
running = False
break
if enemy_cards == [] and playing_deck == {}:
clear()
print("You LOSE!")
time.sleep(10)
running = False
break
time.sleep(0.1)
except KeyboardInterrupt:
running = False
break