-
Notifications
You must be signed in to change notification settings - Fork 2
/
werwolf.py
1296 lines (1054 loc) · 43.8 KB
/
werwolf.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
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import ast
import random
import re
import secrets
from datetime import datetime
liste_tot_mit_aktion = ["Jaeger", "Hexe", "PLATZHALTER"]
liste_tot_ohne_aktion = ["Dorfbewohner", "Werwolf", "Seherin"]
def createDict():
"""
The createDict function creates a dictionary that assigns the number of players to each role.
It takes no arguments and returns a dictionary with the keys: Werwolf, Hexe, Seherin, Armor, Jaeger and Dorfbewohner.
The values are integers representing how many players have that role.
:return: A dictionary that assigns the number of players to each role
"""
with open("spieler_anzahl.txt", "r") as f:
spieleranzahl = f.read()
try:
spieleranzahl = int(spieleranzahl)
except ValueError:
spieleranzahl = 8 # auf 8 defaulten
with open("erzaehler_ist_zufaellig.txt", "r") as fl:
erzaehler_flag = int(fl.read())
werwolf = spieleranzahl // 4
hexe = spieleranzahl // 12
seherin = spieleranzahl // 12
armor = 1
if hexe == 0:
hexe = 1
if seherin == 0:
seherin = 1
jaeger = 1 if spieleranzahl >= 10 else 0
if erzaehler_flag == 0:
assign = (
{
"Werwolf": werwolf,
"Hexe": hexe,
"Armor": armor,
"Seherin": seherin,
"Jaeger": jaeger,
"Dorfbewohner": (
spieleranzahl - werwolf - seherin - hexe - jaeger - armor
),
}
if jaeger > 0
else {
"Werwolf": werwolf,
"Hexe": hexe,
"Armor": armor,
"Seherin": seherin,
"Dorfbewohner": (spieleranzahl - werwolf - seherin - hexe - armor),
}
)
elif erzaehler_flag == 1:
assign = (
{
"Erzaehler": 1,
"Werwolf": werwolf,
"Armor": armor,
"Hexe": hexe,
"Seherin": seherin,
"Jaeger": jaeger,
"Dorfbewohner": (
spieleranzahl - armor - werwolf - seherin - hexe - jaeger - 1
),
}
if jaeger > 0
else {
"Erzaehler": 1,
"Werwolf": werwolf,
"Armor": armor,
"Hexe": hexe,
"Seherin": seherin,
"Dorfbewohner": (spieleranzahl - werwolf - seherin - hexe - armor - 1),
}
)
with open("rollen_zuweisung.txt", "w+") as a:
a.write(str(assign))
def deduct():
"""
The deduct function is used to deduct a random key from the dictionary.
It is called by the main function and returns a value that is then assigned
to the variable 'rollen_zuweisung'. The function iterates through each key in
the dictionary, checks if it has been assigned yet, and assigns it if not. If all values are 0 or less,
then no keys remain in the dictionary and an empty string is returned.
:return: The index of a random key in the dictionary
"""
with open("rollen_zuweisung.txt", "r+") as a:
assign = a.read()
# print(assign)
assign = ast.literal_eval(assign)
keys = list(assign)
if sum(assign.values()) == 0:
return 0
def assignment():
"""
The assignment function is a helper function that is used to assign the
randomly generated number to the appropriate key in the dictionary. The while loop
continues until all values are assigned and there are no more keys left in the dictionary.
The for loop iterates through each key, checks if it has been assigned yet, and assigns it if not.
:return: The index of a random key in the dictionary
"""
while sum(assign.values()) >= 0:
num = random.randint(0, len(assign) - 1)
if assign[keys[num]] > 0:
assign[keys[num]] -= 1
if assign[keys[num]] == 0:
del assign[keys[num]]
return num
assignment()
ind = assignment()
print(assign)
with open("rollen_zuweisung.txt", "w+") as b:
b.write(str(assign))
result = keys[ind]
return result
def validiere_rolle(name: str, rolle: str) -> bool:
"""
The validiere_rolle function checks if the player is already in the log file.
If so, it returns True. Otherwise, it returns False.
:param name:str: Store the name of the player
:param rolle:str: Check if the name and role combination is already in the log file
:return: True if the player is already in the log file
"""
# create a string with the name and the role
wort = ("'" + name + " = " + rolle + "\n'").encode("unicode_escape").decode("utf-8")
with open("rollen_log.txt", "r") as file: # open the log file
players_vorhanden = str(file.readlines()) # read the log file
return wort in players_vorhanden
def validiere_rolle_original(name: str, rolle: str) -> bool:
"""
The validiere_rolle_original function checks if the given name and role are already in the log file.
:param name:str: Store the name of the player
:param rolle:str: Check if the role is already in the log file
:return: True if the name and role are in the log file
"""
# create a string with the name and the role
wort = ("'" + name + " = " + rolle + "\n'").encode("unicode_escape").decode("utf-8")
with open("rollen_original.txt", "r") as file: # open the log file
players_vorhanden = str(file.readlines()) # read the log file
return wort in players_vorhanden
def validiere_name(name: str) -> bool:
"""
The validiere_name function checks if the name of a player is already in use.
It returns True if the name is already used and False otherwise.
:param name:str: Set the name of the player
:return: True if the name is in the log file and false otherwise
"""
wort = ("'" + name + " = ").encode("unicode_escape").decode("utf-8")
with open("rollen_log.txt", "r") as file: # open the log file
players_vorhanden = str(file.readlines()) # read the log file
return wort in players_vorhanden
def hexe_verbraucht(flag: str):
"""
The hexe_verbraucht function removes the flag from the list of flags that
the hexe can use. The function takes one argument, a string containing either
a 't' or an 'h'. If it contains a 't', then it removes flag 2 from the list of
flags that she can use. If it contains an 'h', then it removes flag 1 from her
list of flags.
:param flag:str: Determine the action of the function
:return: The following:
"""
if "t" in flag or "T" in flag:
flag = str(2)
elif "h" in flag or "H" in flag:
flag = str(1)
if flag in {"1", "2"}:
with open("hexe_kann.txt", "r") as hexe_kann:
hexe_kann_text = hexe_kann.read()
hexe_kann_text = hexe_kann_text.replace(flag, "")
hexe_kann.close()
with open("hexe_kann.txt", "w") as hexe_kann_schreiben:
hexe_kann_schreiben.write(hexe_kann_text)
hexe_kann_schreiben.close()
else:
actions("")
# heilen --> 1
# toeten --> 2
def hexe_darf_toeten() -> bool:
"""
The hexe_darf_toeten function checks if the hexe can kill.
:returns: True if the hexe can kill, False otherwise.
:return: A boolean value
"""
with open("hexe_kann.txt", "r") as hexe_kann:
hexe_kann_text = hexe_kann.read()
if str(2) in hexe_kann_text:
hexe_kann.close()
return True
hexe_kann.close()
return False
def hexe_darf_heilen() -> bool:
"""
The hexe_darf_heilen function checks if the hexe can heal.
It does this by reading from a file called "hexe_kann.txt" which contains either a 1 or 0, depending on whether or not the hexe can heal.
:return: True if the hexe can heal
"""
with open("hexe_kann.txt", "r") as hexe_kann:
hexe_kann_text = hexe_kann.read()
if "1" in hexe_kann_text:
hexe_kann.close()
return True
hexe_kann.close()
return False
def armor_darf_auswaehlen() -> bool:
"""
The armor_darf_auswaehlen function checks if the armor has already selected the lovers.
If not, it returns True. Otherwise, it returns False.
:return: True if the armor can be selected and false otherwise
"""
with open("armor_kann.txt", "r") as armor_kann:
armor_kann_text = armor_kann.read()
if "1" in armor_kann_text:
armor_kann.close()
return True
armor_kann.close()
return False
def jaeger_darf_toeten() -> bool:
"""
The jaeger_darf_toeten function checks whether the Jaeger can kill
:returns: True if the Jaeger can kill, False otherwise.
:return: A boolean value
"""
with open("jaeger_kann.txt", "r") as jaeger_kann:
jaeger_kann_text = jaeger_kann.read()
if "1" in jaeger_kann_text:
jaeger_kann.close()
return True
jaeger_kann.close()
return False
def jaeger_fertig():
"""
The jaeger_fertig function is used to set the jaeger_kann.txt file to 0 and the jaeger can not kill somebody anymore
:return: The string "0"
"""
# jaeger_kann.txt mit 0 ersetzen
with open("jaeger_kann.txt", "w") as jaeger_kann:
jaeger_kann.write("0")
jaeger_kann.close()
def armor_fertig(player1: str, player2: str):
"""
The armor_fertig function adds a line to the verliebt.txt file and replaces the armor_kann.txt file with 0.
:param player1:str: Store the name of the first player
:param player2:str: Determine the name of the player that is going to be added to the list
:return: The following:
"""
if (
player1 == player2
or validiere_name(player1) is not True
or validiere_name(player2) is not True
):
return
with open("verliebt.txt", "r+") as verliebt:
verliebt_text = verliebt.read()
# wenn nicht in der Liste, dann hinzufügen.
if f"+{player1}+{player2}" + "\n" not in verliebt_text:
verliebt.seek(0)
verliebt.write(f"+{player1}+{player2}" + "\n")
verliebt.truncate()
verliebt.close()
# datei armor_kann.txt mit 0 ersetzen
with open("armor_kann.txt", "w") as armor_kann:
armor_kann.write("0")
armor_kann.close()
def verliebte_ausgeben() -> str:
"""
The verliebte_ausgeben function reads the verliebt.txt file and returns its content.
:return: The content of the verliebt.txt file
"""
with open("verliebt.txt", "r") as verliebt:
verliebt_text = verliebt.read()
if verliebt_text.count("+") == 2:
return (
"+"
+ verliebt_text.split("+")[1]
+ "+"
+ verliebt_text.split("+")[2]
+ "+"
)
def ist_verliebt(name: str) -> bool:
"""
The ist_verliebt function checks if the player is verliebt
:param name:str: Define the name of the player
:return: True if the player is verliebt, otherwise it returns false
"""
with open("verliebt.txt", "r") as verliebt:
verliebt_text = verliebt.read()
if f"+{name}" in verliebt_text:
verliebt.close()
return True
verliebt.close()
return False
def leere_dateien():
"""
The leere_dateien function is used to clear all the files that are used in the game.
It is called at the beginning of each new game.
:return: None
"""
with open("rollen_log.txt", "w+", encoding="UTF8") as f: # leere rollen_log.txt
f.write("*********************\n")
with open("abstimmung.txt", "r+", encoding="UTF8") as file:
file.truncate(0)
file.close()
with open("rollen_original.txt", "r+", encoding="UTF8") as file2:
file2.truncate(0)
file2.close()
with open("hat_gewaehlt.txt", "r+", encoding="UTF8") as file3:
file3.truncate(0)
file3.close()
with open("hexe_kann.txt", "w", encoding="UTF8") as file4:
file4.write(str(12))
file4.close()
with open("armor_kann.txt", "w", encoding="UTF8") as file5:
file5.write(str(1))
file5.close()
with open("verliebt.txt", "w", encoding="UTF8") as file6:
file6.write("")
file6.close()
with open("jaeger_kann.txt", "w", encoding="UTF8") as file7:
file7.write(str(1))
file7.close()
with open("logfile.txt", "w", encoding="UTF8") as file8:
file8.truncate(0)
file8.close()
with open("tokens.txt", "w", encoding="UTF8") as file9:
file9.truncate(0)
def momentane_rolle(player: str) -> str:
"""
The momentane_rolle function returns the current role of a player.
:param player:str: Get the name of the player whose role is to be returned
:return: The current role of the player
"""
lines = [f"+{line}" for line in open("rollen_log.txt")]
return next(
(
(line.split("=")[1].split("\n")[0]).replace(" ", "")
for line in lines
if f"+{player}" in line
),
f"Ein Fehler ist aufgetreten, die Rolle von {player} konnte nicht ermittelt werden.",
)
def fruehere_rolle(player: str) -> str:
"""
The fruehere_rolle function returns the previous role of a player, before dying.
:param player:str: Pass the name of the player to the function
:return: The previous role of the player, before dying
"""
lines = [f"+{line}" for line in open("rollen_original.txt")]
return next(
(
(line.split("=")[1].split("\n")[0]).replace(" ", "")
for line in lines
if f"+{player}" in line
),
f"Ein Fehler ist aufgetreten, die ursprüngliche Rolle von {player} konnte nicht ermittelt werden.",
)
def war_oder_ist_rolle(player: str, rolle: str) -> bool:
"""
The war_oder_ist_rolle function checks whether a player is currently or was previously in the given role.
It returns True if they are, False otherwise.
:param player:str: Specify the player whose role is to be checked
:param rolle:str: Check if the player is in the specified role
:return: True if the player is in the role or was in that role before
"""
return momentane_rolle(player) == rolle or fruehere_rolle(player) == rolle
def aktion_verfuegbar_ist_tot(player: str) -> bool:
"""
The aktion_verfuegbar_ist_tot function checks if the player can do an action.
It checks if the player is a witch, and if she can kill someone. If not, it checks
if the player is a hunter and he can kill someone.
:param player:str: Check if the player is a hunter or witch
:return: True if the player can do an action
"""
if war_oder_ist_rolle(player, "Hexe") is True:
return hexe_darf_toeten() is True
if war_oder_ist_rolle(player, "Jaeger") is True:
return jaeger_darf_toeten() is True
return False
def zufallszahl(minimum: int, maximum: int) -> int:
"""
The zufallszahl function returns a random integer between the specified minimum and maximum values.
The function takes two arguments, both integers: minimum and maximum.
It returns an integer.
:param minimum:int: Set the lowest possible number and the maximum:int parameter is used to set the highest possible number
:param maximum:int: Set the upper limit of the random number
:return: A random integer between the minimum and maximum value
"""
return random.randint(minimum, maximum)
def verliebte_toeten() -> str:
"""
The verliebte_toeten function takes the names of two players and writes them to a file.
The function then reads the file and checks if either player is in it. If so, it replaces their name with "Tot".
Finally, the function returns a string containing both names.
:return: The names of the two players who are dead
"""
log_liste = []
with open("verliebt.txt", "r") as verliebt:
read = verliebt.read()
player1 = (
str(read.split("+"))
.encode("utf-8")
.decode("utf-8")
.replace("\\n", "")
.replace("'", "")
.replace("[", "")
.replace("]", "")
.replace("'", "")
.replace(" ", "")
.split(",")[1]
)
player2 = (
str(read.split("+"))
.encode("utf-8")
.decode("utf-8")
.replace("\\n", "")
.replace("'", "")
.replace("[", "")
.replace("]", "")
.replace("'", "")
.replace(" ", "")
.split(",")[2]
)
verliebt.close()
with open("rollen_log.txt", "r") as rollen_log:
for line in rollen_log:
if line == f"{player1} = {momentane_rolle(player1)}" + "\n":
log_liste.append(f"+{player1} = Tot" + "\n")
elif line == f"{player2} = {momentane_rolle(player2)}" + "\n":
log_liste.append(f"+{player2} = Tot" + "\n")
else:
log_liste.append(line)
with open("rollen_log.txt", "w") as rollen_log_write:
for line in log_liste:
rollen_log_write.write((line).replace("+", ""))
rollen_log_write.close()
rollen_log.close()
return f"{player1} und {player2}"
def schreibe_zuletzt_gestorben(player: str) -> None:
"""
The schreibe_zuletzt_gestorben function writes the last dead player to the logfile
:param player:str: Write the name of the player who died last to a file
:return: None
"""
with open("letzter_tot.txt", "r+") as letzter_tot_w:
letzter_tot_w.write(player)
# Tötet den gewählten Spieler
def toete_spieler(player):
"""
The toete_spieler function takes a player as an argument and changes the role of that player to "Tot" in the rollen_log.txt file.
It also writes down when that player was killed.
:param player: Identify the player who is to be killed
:return: The following:
"""
player = str(player)
rolle = momentane_rolle(player)
list_for_the_log = []
statement = None # warum ist das nötig?
with open("rollen_log.txt", "r") as rollen_log:
for line in rollen_log:
if line == f"{player} = {rolle}" + "\n":
list_for_the_log.append(f"{player} = Tot" + "\n")
statement = f"{player} wurde getötet."
else:
if statement is None:
statement = f"Der Spieler {player} ist unbekannt."
list_for_the_log.append(line)
rollen_log.close()
with open("rollen_log.txt", "w+") as rollen_log_write:
for line in list_for_the_log:
rollen_log_write.write(line)
rollen_log_write.close()
schreibe_zuletzt_gestorben(player)
return statement
def log(debug: bool):
"""
The log function writes a string to the logfile.txt file, which is used by the
debug function to determine whether or not debug mode is on. If it's off, then
the logfile will be wiped clean so that way it doesn't interfere with any future
debugging efforts.
:param debug:bool: Decide whether or not to write a logfile
:return: A none object
"""
if not debug:
with open("logfile.txt", "w", encoding="UTF8") as logfile_schreiben:
logfile_schreiben.write("FALSE")
def in_log_schreiben(a: str):
"""
The in_log_schreiben function writes a string to the logfile.txt file.
It takes one argument, which is a string.
:param a:str: Pass the message to be logged
:return: The result of the function
"""
with open("logfile.txt", "r", encoding="UTF8") as logfile_lesen:
if "FALSE" in logfile_lesen.read():
logfile_lesen.close()
else:
with open("logfile.txt", "a", encoding="UTF8") as logfile:
now = datetime.now().strftime("%H:%M:%S")
logfile.write(str(now) + str(f" >> {a}") + "\n")
logfile.close()
def spieler_gestorben(player: str) -> str:
"""
The spieler_gestorben function performs actions if the player is dead.
:param player:str: Identify the player that is dead
:return: A string
"""
rolle = momentane_rolle(player)
if rolle == "Tot":
return "err"
if rolle in liste_tot_mit_aktion and aktion_verfuegbar_ist_tot(player) is True:
# TODO: #33 Aktionen für Hexe und Jaeger während des Todes
if rolle == "Hexe":
toete_spieler(player)
return "h" # hexe_aktion()
if rolle == "Jaeger":
toete_spieler(player)
return "j" # jaeger_aktion()
if ist_verliebt(player) is True:
verliebte_toeten()
return "v" # verliebte_sind_tot
if rolle in liste_tot_ohne_aktion:
toete_spieler(player)
return str(0) # keine Aktion, player ist jetzt tot
return "err"
return "err"
def spieler_ist_tot(player: str) -> bool:
"""
The spieler_ist_tot function checks if the player is dead
:param player:str: Identify the player
:return: A boolean value
"""
return momentane_rolle(player) == "Tot"
def name_richtig_schreiben(name: str) -> str:
"""
The name_richtig_schreiben function takes a string and returns the same string with all non-alphanumeric characters replaced by underscores.
:param name:str: Pass the name of the file to be renamed
:return: The name with the correct capitalization
"""
P = "USD"
O = "C"
N = "\xc3\x9c"
M = "\xc3\x96"
L = "\xc3\x84"
K = "\x0c"
J = "\x0b"
I = "-"
H = "a"
G = "U"
F = "I"
E = "E"
D = "O"
C = "A"
B = "_"
A = name
A = A.replace("/", B)
A = A.replace("=", I)
A = A.replace(":", B)
A = A.replace("*", B)
A = A.replace("<", B)
A = A.replace(">", B)
A = A.replace("?", B)
A = A.replace('"', B)
A = A.replace("\\", B)
A = A.replace("|", B)
A = A.replace(".", B)
A = A.replace(" ", B)
A = A.replace("\n", B)
A = A.replace("\t", B)
A = A.replace("\r", B)
A = A.replace(J, B)
A = A.replace(K, B)
A = A.replace("\x08", B)
A = A.replace("\x07", B)
A = A.replace("\\e", B)
A = A.replace("\x00", B)
A = A.replace(J, B)
A = A.replace(K, B)
A = A.replace("\x0e", B)
A = A.replace("\x0f", B)
A = A.replace("\x10", B)
A = A.replace("\x11", B)
A = A.replace("\x12", B)
A = A.replace("\x13", B)
A = A.replace("\x14", B)
A = A.replace("\x15", B)
A = A.replace("\x16", B)
A = A.replace("\x17", B)
A = A.replace("\x18", B)
A = A.replace("\x19", B)
A = A.replace("\x1a", B)
A = A.replace("\x1b", B)
A = A.replace("\x1c", B)
A = A.replace("\x1d", B)
A = A.replace("\x1e", B)
A = A.replace("\x1f", B)
A = A.replace("\xc3\xa4", "ae")
A = A.replace("\xc3\xb6", "oe")
A = A.replace("\xc3\xbc", "ue")
A = A.replace(L, "Ae")
A = A.replace(M, "Oe")
A = A.replace(N, "Ue")
A = A.replace("\xc3\x9f", "ss")
A = A.replace("\xc3\x80", C)
A = A.replace("\xc3\x81", C)
A = A.replace("\xc3\x82", C)
A = A.replace("\xc3\x83", C)
A = A.replace(L, C)
A = A.replace("\xc3\x85", C)
A = A.replace("\xc3\x86", C)
A = A.replace("\xc3\x87", O)
A = A.replace("\xc3\x88", E)
A = A.replace("\xc3\x89", E)
A = A.replace("\xc3\x8a", E)
A = A.replace("\xc3\x8b", E)
A = A.replace("\xc3\x8c", F)
A = A.replace("\xc3\x8d", F)
A = A.replace("\xc3\x8e", F)
A = A.replace("\xc3\x8f", F)
A = A.replace("\xc3\x91", "N")
A = A.replace("\xc3\x92", D)
A = A.replace("\xc3\x93", D)
A = A.replace("\xc3\x94", D)
A = A.replace("\xc3\x95", D)
A = A.replace(M, D)
A = A.replace("\xc3\x98", D)
A = A.replace("\xc3\x99", G)
A = A.replace("\xc3\x9a", G)
A = A.replace("\xc3\x9b", G)
A = A.replace(N, G)
A = A.replace("\xc3\x9d", "Y")
A = A.replace("\xc3\xa0", H)
A = A.replace("\xc3\xa1", H)
A = A.replace("\xc3\xa2", H)
A = A.replace("\xc3\xa3", H)
A = A.replace("@", "at")
A = A.replace("\xe2\x82\xac", "EURO")
A = A.replace("$", P)
A = A.replace("\xc2\xa3", "GBP")
A = A.replace("\xc2\xa5", "JPY")
A = A.replace("\xc2\xa4", P)
A = A.replace("\xc2\xa6", "B")
A = A.replace("\xc2\xa7", "S")
A = A.replace("\xc2\xa9", O)
A = A.replace("\xc2\xaa", C)
A = A.replace("\xc2\xab", "<<")
A = A.replace("\xc2\xac", "!")
A = A.replace("\xad", I)
A = A.replace("\xc2\xae", "R")
A = A.replace("\xc2\xaf", "^")
A = A.replace("\xc2\xb0", "o")
A = A.replace("\xc2\xb1", "+")
A = A.replace("\xc2\xb2", "2")
A = A.replace("\xc2\xb3", "3")
A = A.replace("\xc2\xb4", "'")
A = A.replace("\xc2\xb5", "u")
name = A
# use regex to replace all non-alphanumeric characters with _
name = re.sub(r"[^a-zA-Z0-9]", "_", name)
return name.capitalize()
def suche_spieler() -> bool:
"""
The suche_spieler function checks if the number of players is bigger than the number of lines in rollen_original.txt.
If this is true, then it returns True, else False.
:return: True if the number of players is greater than the number of roles
"""
with open("spieler_anzahl.txt", "r") as file:
for line in file:
if int(line) > len(open("rollen_original.txt", "r").readlines()):
return True
return False
def generiere_token(name: str, rolle: str) -> str:
"""
The generiere_token function generates a token for the user.
It checks if the role is valid and if it is not already in use.
If this condition is met, a new token will be generated and written to tokens.txt
:param name:str: Specify the name of the user
:param rolle:str: Check if the role is valid
:return: A token
"""
if validiere_rolle_original(name, rolle) and not ist_token_vorhandem(name, rolle):
token = secrets.token_hex(16)
# write the token and the name and the role to the file tokens.txt
with open("tokens.txt", "a", encoding="UTF8") as file:
file.write("+" + token + "+" + name + "+" + rolle + "+1+ \n")
return token
def validiere_token(token: str) -> bool:
"""
The validiere_token function checks if the token is in the file tokens.txt
and returns True if it is, False otherwise.
:param token:str: Check if the token is in the file
:return: True if the token is in the file, otherwise it returns false
"""
# read the file tokens.txt and check if the token is in the file
with open("tokens.txt", "r") as file:
return any("+" + token + "+" in line for line in file)
def name_und_rolle_aus_token(token: str):
"""
The name_und_rolle_aus_token function takes a token as input and returns the name and role of the user who has this token.
The function reads the file tokens.txt, checks if the given token is in this file and splits it at every + to return
the name and role of that user.
:param token:str: Check if the token is in the file tokens
:return: The name and the role of a token
"""
# read the file tokens.txt and check if the token is in the file
with open("tokens.txt", "r") as file:
for line in file:
if "+" + token + "+" in line:
# split the line at the + and return the name and the role
return line.split("+")[1], line.split("+")[2]
def loesche_token(token):
"""
The loesche_token function deletes a token from the file tokens.txt
:param token: Check if the token is in the file
:return: The name and the role of the token
"""
# read the file tokens.txt and check if the token is in the file
with open("tokens.txt", "r") as file:
for line in file:
if "+" + token + "+" in line:
# split the line at the + and return the name and the role
file.close()
with open("tokens.txt", "r") as file:
lines = file.readlines()
with open("tokens.txt", "w") as file:
for line in lines:
if "+" + token + "+" not in line:
file.write(line)
def rolle_aus_token(token: str):
"""
The rolle_aus_token function takes a token as an argument and returns the role of the user associated with that token.
The function reads from a file called tokens.txt, which contains all valid tokens and their corresponding roles.
:param token:str: Check if the token is in the file
:return: The role of the token
"""
# read the file tokens.txt and check if the token is in the file
with open("tokens.txt", "r") as file:
for line in file:
if "+" + token + "+" in line:
# split the line at the + and return the name and the role
return line.split("+")[3]
def name_aus_token(token: str):
"""
The name_aus_token function takes a token and returns the name of the person who has that token.
If no one has that token, it returns None.
:param token:str: Pass the token of a user to the function
:return: The name of the token if it is in tokens
"""
# read the file tokens.txt and check if the token is in the file
with open("tokens.txt", "r") as file:
for line in file:
if "+" + token + "+" in line:
# split the line at the + and return the name and the role
return line.split("+")[2]
def status_aus_token(token: str):
"""
The status_aus_token function checks if the token is in the file tokens.txt and returns
the status of the token if it is in the file.
:param token:str: Pass the token of the user that is checked
:return: The status of the token
"""
# read the file tokens.txt and check if the token is in the file
with open("tokens.txt", "r") as file:
for line in file:
if line != "\n" and "+" + token + "+" in line and line.count("+") == 5:
# split the line at the + and return the name and the role
return line.split("+")[4]
return "Fehler"
def token_aus_name_und_rolle(name: str, rolle: str) -> str:
"""
The token_aus_name_und_rolle function takes a name and a role as input. It checks if the given name and role are valid,
and returns the token of that player if it is. If not, it returns an error message.
:param name:str: Specify the name of the player
:param rolle:str: Check if the rolle is valid
:return: The token for the player name and role
"""
if validiere_rolle_original(name, rolle):
# read the file tokens.txt and check if the token is in the file
with open("tokens.txt", "r") as file:
for line in file:
if "+" + name + "+" + rolle + "+" in line:
# split the line at the + and return the name and the role
return line.split("+")[1]
else:
return "Spieler nicht gefunden"
return "Kein Token gefunden"
def ist_token_vorhandem(name, rolle):
"""
The ist_token_vorhandem function checks if the token is in the file tokens.txt
:param name: Check if the token is in the file
:param rolle: Check if the token is in the file
:return: True if the token is in the file, otherwise false
"""
# read the file tokens.txt and check if the token is in the file
with open("tokens.txt", "r") as file:
for line in file:
if "+" + name + "+" + rolle + "+" in line:
return True
return False
def setze_status(token: str, status: str):
"""
The setze_status function sets the status of a user to active or inactive.
It takes two arguments: token and status. The token is the unique identifier for each user,
and it is used to find the correct line in tokens.txt where their information is stored.
The status argument can be either "active" or "inactive". If it's set to active, then all lines containing that token will have their last column changed from an inactive string (e.g., 'inactive') to an active string (e.g., 'active'). If it's set to inactive, then all lines containing that token will have their