-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
1392 lines (1079 loc) · 47.5 KB
/
app.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 datetime
from datetime import datetime
from inspect import currentframe
from inspect import getframeinfo
import trace
import sentry_sdk
from flask import app, escape
from flask import Flask
from flask import redirect
from flask import render_template
from flask import request
from sentry_sdk import set_user
from sentry_sdk.integrations.flask import FlaskIntegration
import importlib.util
import sys
import flask
spec = importlib.util.spec_from_file_location("werwolf", "werwolf.py")
werwolf = importlib.util.module_from_spec(spec)
sys.modules["werwolf"] = werwolf
spec.loader.exec_module(werwolf)
sentry_sdk.init(
dsn="https://78fe9de58a5847ada071bf5f62f9c214@o1363527.ingest.sentry.io/6678492",
integrations=[
FlaskIntegration(),
],
traces_sample_rate=1.0,
)
werwolf.log(debug=False)
app = flask.Flask(__name__)
# index page
@app.route("/", methods=["GET"]) # Homepage
def index():
"""
The index function is the main page of the application. It is called when a user navigates to
the root directory of our web application. The function returns an HTML template that contains
a list of links to other pages within our web application.
:return: The index page of the application.
"""
werwolf.in_log_schreiben("index geöffnet")
return render_template(
"index.html", spieler_suche=bool(werwolf.suche_spieler())
) # Render index.html
# einstellungen
@app.route("/einstellungen", methods=["GET"]) # Einstellungen
def einstellungen():
"""
The einstellungen function is used to open the einstellungen page.
:return: The einstellungen
"""
werwolf.in_log_schreiben("einstellungen geöffnet")
return render_template("einstellungen.html") # Render einstellungen.html
# wie viele Spieler sollen vorhanden sein?
# Spieleranzahl
@app.route("/einstellungen/spieleranzahl", methods=["POST"])
def setPlayerNumber(): # set the number of players
"""
The setPlayerNumber function is called when the user clicks on the button "Spieleranzahl setzen" in einstellungen.html.
It takes a number of players from the form and checks if it is an integer between 8 and 18, otherwise it sets it to 8.
If the checkbox "Erzähler ist zufällig" is checked, erzaehler_flag = 1 else 0.
:return: The number of players
"""
# get the number of players from the form
spieleranzahl = request.form.get("num")
try:
# eingabe ist wirklich ein integer
spieleranzahl_int = int(spieleranzahl)
if (
spieleranzahl_int < 8 or spieleranzahl_int > 18
): # Spieleranzahl ist zwischen 8 und 18
spieleranzahl = 8 # auf 8 defaulten
except ValueError:
spieleranzahl = 8 # auf 8 defaulten
# speichern der spieleranzahl in einer textdatei
with open("spieler_anzahl.txt", "w+") as file:
file.write(str(spieleranzahl))
erzaehler_flag = 1 if bool(request.form.get("cbx")) else 0
# speichern des erzaehler_flag in einer textdatei
with open("erzaehler_ist_zufaellig.txt", "w+") as flag:
# speichern des erzaehler_flag in einer textdatei
flag.write(str(erzaehler_flag))
werwolf.createDict() # create the dictionary with the names of the players
with open("rollen_log.txt", "w+") as f: # leere rollen_log.txt
f.write("*********************\n")
werwolf.in_log_schreiben(f"Spieleranzahl: auf {spieleranzahl} gesetzt")
# render einstellungen_gespeichert.html
return render_template(
"einstellungen_gespeichert.html", spieleranzahl_var=spieleranzahl
)
# namenseingabe spieler
@app.route("/spieler", methods=["POST"]) # Spieler
def get_data(): # get the data from the form
"""
The get_data function gets the data from the form. If it is a POST request,
it gets the name from the form and checks if it is already in use. If so,
it renders an error page with a link to go back to index.html.
:return: The name and the operator from the form
"""
if request.method != "POST":
return render_template("fehler.html"), 500
name = request.form.get("name") # get the name from the form
name = werwolf.name_richtig_schreiben(name) # clean the name
with open("rollen_log.txt") as players_log: # open the log file
players_log = players_log.read() # read the log file
if werwolf.validiere_name(name) is True:
# if the name is already in the log file
# name doppelt ausgeben
return render_template("name_doppelt.html", name=name)
with open("spieler_anzahl.txt") as file:
num = file.read() # read the file
operator = werwolf.deduct() # get the operator
try: # try to get the operator
if operator == 0: # if the operator is 0
code = "code" # set the code to code
# render spiel_beginnt.html
return render_template("spiel_beginnt.html", code=code)
# append the name to the log file
with open("rollen_log.txt", "a") as names:
# write the name and the operator to the log file
names.write(f"{name} = {operator}")
# names.write(f'{date}: {name} = {operator}')
# write a new line to the log file
names.write("\n")
names.close()
# append the name to the log file
with open("rollen_original.txt", "a") as names:
# write the name and the operator to the log file
names.write(f"{name} = {operator}")
# names.write(f'{date}: {name} = {operator}')
# write a new line to the log file
names.write("\n")
# credits to @joschicraft
set_user({"username": f"{name} = {str(operator)}"})
token = werwolf.generiere_token(name, operator)
werwolf.in_log_schreiben(f"Neuer Spieler {name} hat die Rolle {operator}")
# render rollen_zuweisung.html
return render_template(
"rollen_zuweisung.html",
players=num,
name=name,
operator=operator,
token=token,
)
except Exception as e:
# render neu_laden.html
return render_template("neu_laden.html")
# Pfad des Erzählers, momentan für debugzwecke auf einem ungeschützten pfad
@app.route("/erzaehler", methods=["GET"]) # Erzähler
def erzaehler():
"""
The erzaehler function opens the log file and renders it to erzaehler.html
:return: The erzaehler
"""
try:
with open("rollen_log.txt") as players_log: # open the log file
players_log = players_log.readlines() # read the log file
# render erzaehler.html
werwolf.in_log_schreiben("Erzähler geöffnet")
return render_template("erzaehler.html", names=players_log)
except Exception as e:
return str(404) # return 404 if the file is not found
# Neues Spiel
# reset der rollen_log.txt
@app.route("/erzaehler/reset", methods=["POST"])
def reset():
"""
The reset function is called when the user presses the reset button. It resets all files and starts a new game.
:return: The reset
"""
if (
request.method == "POST" and request.form["reset_button"] == "Neues Spiel"
): # wenn neues spiel gewuenscht
werwolf.leere_dateien() # leere die dateien
werwolf.in_log_schreiben("Neues Spiel gestartet")
# zurück zur einstellungen
return render_template("einstellungen.html")
return render_template("fehler.html"), 500
@app.route("/<name>/<rolle>/toeten/<name_kill>") # kill a player
def kill_player(name, rolle, name_kill):
"""
The kill_player function is called when a player is killed.
It takes the name of the player who was killed and their role as arguments.
If the person who was killed is a Werwolf, it will kill them and return
the template for dead players. If they are not a Werwolf, it will return an error page.
:param name: Identify the player that is killed
:param rolle: Determine which role the player has
:param name_kill: Get the name of the player that is killed
:return: The html template "dashboards/status/tot
"""
auswahl = name_kill
if rolle in ("Hexe", "Jaeger"):
if (
rolle == "Hexe"
and werwolf.hexe_darf_toeten() is True
and werwolf.validiere_rolle(name, rolle) is True
):
werwolf.toete_spieler(auswahl)
werwolf.hexe_verbraucht("toeten")
werwolf.in_log_schreiben(f"Die Hexe ({name}) hat {name_kill} getötet")
return render_template(
"Dashboards/Dash_Hexe.html", name=name, rolle=rolle, name_kill=name_kill
)
if rolle == "Jaeger":
if werwolf.jaeger_darf_toeten() is True:
werwolf.toete_spieler(auswahl)
werwolf.jaeger_fertig()
return render_template(
"Dashboards/status/tot.html", name=name, todesgrund=""
)
return render_template(
"Dashboards/status/tot.html", name=name, todesgrund=""
)
return render_template("fehler.html"), 500
return render_template("fehler.html"), 500
@app.route("/<name>/Armor_aktion/<player1>/<player2>") # player auswahl
def armor_player(player1, player2, name):
"""
The armor_player function is used to protect the player from being killed by the werewolf.
The function checks if a player is allowed to use this ability and if so, it will set the
armor_used variable in Werwolf Class to True. If not, it will return an error message.
:param player1: Determine the player who is currently playing
:param player2: Determine the player who is going to be protected by the armor
:param name: Check if the player is a werewolf or not
:return: The html code of the page that is shown when the armor player wants to use their ability
"""
rolle = "Armor"
if (
werwolf.validiere_rolle(name, rolle) is True
and werwolf.armor_darf_auswaehlen() is True
):
werwolf.armor_fertig(player1, player2)
return render_template("Dashboards/status/aktion_warten.html")
if (
werwolf.armor_darf_auswaehlen() is False
and werwolf.validiere_rolle(name, rolle) is True
):
return render_template("Dashboards/status/aktion_warten.html")
if werwolf.validiere_rolle(name, rolle) is False:
# print the error
print("Spieler oder Rolle falsch!")
# render the url_system.html
return render_template("url_system.html", name=name, rolle=rolle)
return render_template("fehler.html"), 500
@app.route("/<name>/<rolle>/warten_auf_aktions_ende")
def aktion_warten(name, rolle):
"""
The aktion_warten function is used to render the template for the aktion_warten page.
It takes two arguments, name and rolle. If name is in werwolf.rolle and rolle is a valid role,
then it will return a rendered template of aktion_warten.
:param name: Identify the player
:param rolle: Determine the role of the player
:return: The template for the warten page
"""
if werwolf.validiere_rolle(name, rolle) is True:
return render_template("Dashboards/status/aktion_warten.html")
return render_template("fehler.html"), 500
# Übersicht der Spieler
@app.route("/uebersicht/<ist_unschuldig>") # Übersicht
def overview_all(ist_unschuldig): # Übersicht
"""
The overview_all function renders the overview_innocent.html or overview_guilty.html template, depending on the value of ist_unschuldig.
:param ist_unschuldig: Distinguish between the innocent and guilty overview
:return: The overview_innocent
"""
try:
# ist_unschuldig ist wirklich ein integer
ist_unschuldig = int(ist_unschuldig)
if ist_unschuldig == 1: # wenn ist_unschuldig = 1
with open("rollen_original.txt") as players_log: # open the log file
players_log = players_log.readlines() # read the log file
# render overview_innocent.html
return render_template("overview_innocent.html", names=players_log)
if ist_unschuldig == 0: # wenn ist_unschuldig = 0
with open("rollen_oriinal.txt") as players_log: # open the log file
players_log = players_log.readlines() # read the log file
# render overview_guilty.html
return render_template("overview_guilty.html", names=players_log)
return render_template("fehler.html"), 500 # render fehler.html
except (ValueError, TypeError, NameError):
return render_template("fehler.html"), 500 # render fehler.html
# Rollen Dashboards
@app.route("/<name>/<rolle>/Dashboard") # Dashboard
def Dashboard(name, rolle): # Dashboard
"""
The Dashboard function is called when the user wants to see the Dashboard of Dorfbewohner.
It renders Dash_rolle.html and passes all variables to it.
:param name: Get the name of the player
:param rolle: Determine which dashboard is shown
:return: The dash_rolle
"""
# create a string with the name and the role
with open("rollen_log.txt", "r", encoding="UTF8") as file: # open the log file
players_vorhanden = file.read() # read the log file
rolleAusLog = players_vorhanden.split(" = ") # split the log file into a list
rolleAusLog = rolleAusLog[1]
if rolleAusLog == "Tot":
return render_template("tot.html", name=name) # render tot.html
# if the name and the role are in the log file
if werwolf.validiere_rolle(name, rolle) is True:
try: # try to get the role
with open("rollen_log.txt") as players_log: # open the log file
players_log = players_log.readlines() # read the log file
nurNamen = [] # create a list with the names
try:
for line in players_log: # for every line in the log file
if "*" not in line:
line = line.split(" = ") # split the line at the =
# set the role to the second part of the line
auswahlRolle = line[1]
# if the role is not Tot or the role is not the Erzähler
if auswahlRolle not in ("Tot", "Erzaehler"):
name_line = line[0]
# append the name to the list
nurNamen.append(name_line)
except IOError:
print(
"[Debug] Fehler beim Auslesen des rollen_logs in app.py line "
+ str(getframeinfo(currentframe()).lineno - 1)
) # print the error
# render Dash_rolle.html
werwolf.in_log_schreiben(
"Dorfbewohner Dashboard für "
+ name
+ " mit Rolle "
+ rolle
+ " angezeigt"
)
return render_template(
"Dashboards/Dash_Dorfbewohner.html",
name=name,
rolle=rolle,
names=players_log,
nurNamen=nurNamen,
)
except Exception as e:
return render_template("fehler.html"), 500 # render fehler.html
else:
# print the error
print("Spieler oder Rolle falsch!")
# render url_system.html
return render_template("url_system.html", name=name, rolle=rolle)
@app.route("/<name>/<rolle>/Dashboard_sp")
def spezielles_Dashboard(name, rolle):
"""
The spezielles_Dashboard function is called when a player opens his Dashboard.
It takes two arguments: name and rolle. The function checks if the role is valid, then renders the spezielles Dashboard for that role.
:param name: Get the name of the player who wants to see his dashboard
:param rolle: Determine which dashboard is shown
:return: The dashboard of the role
"""
if rolle == "Tot":
werwolf.setze_status_fuer_name(name, "0")
return render_template("fehler.html"), 500
# create a string with the name and the role
with open("rollen_log.txt", "r", encoding="UTF8") as file: # open the log file
players_vorhanden = file.read() # read the log file
rolleAusLog = players_vorhanden.split(" = ") # split the log file into a list
rolleAusLog = rolleAusLog[1]
if rolleAusLog == "Tot":
werwolf.setze_status_fuer_name(name, "0") # render tot.html
# if the name and the role are in the log file
if werwolf.validiere_rolle(name, rolle) is True:
nurNamen = [] # create a list with the names
if rolle == "Hexe":
print("Hexe")
with open("hexe_kann.txt", "r", encoding="UTF8") as file:
hexe_kann = file.read()
hexe_kann = str(hexe_kann)
file.close()
with open("rollen_log.txt") as players_log: # open the log file
players_log = players_log.readlines() # read the log file
for line in players_log: # for every line in the log file
if "*" not in line and "Tot" not in line and "Erzaehler" not in line:
line = line.split(" = ") # split the line at the =
name_line = line[0]
# set the role to the second part of the line
nurNamen.append(name_line) # append the name to the list
if rolle == "Hexe":
with open("letzter_tot.txt", "r", encoding="UTF8") as file:
letzter_tot = file.read()
werwolf.in_log_schreiben(
f"Hexe Dashboard für {name} mit Rolle {rolle} angezeigt"
)
werwolf.setze_status_fuer_name(name, "2")
# render Dash_rolle.html
return render_template(
f"Dashboards/Dash_{rolle}.html",
name=name,
rolle=rolle,
names=players_log,
nurNamen=nurNamen,
hexe_kann=hexe_kann,
letzter_tot=letzter_tot,
)
if rolle == "Armor":
werwolf.in_log_schreiben(
f"Armor Dashboard für {name} mit Rolle {rolle} angezeigt"
)
werwolf.setze_status_fuer_rolle("Armor", "2")
return render_template(
f"Dashboards/Dash_{rolle}.html",
name=name,
rolle=rolle,
names=players_log,
nurNamen=nurNamen,
armor_kann=werwolf.armor_darf_auswaehlen(),
)
# render Dash_rolle.html
werwolf.in_log_schreiben(
"Dashboard der Rolle "
+ rolle
+ " für "
+ name
+ " mit Rolle "
+ rolle
+ " angezeigt"
)
werwolf.setze_status_fuer_name(name, "2")
return render_template(
f"Dashboards/Dash_{rolle}.html",
name=name,
rolle=rolle,
names=players_log,
nurNamen=nurNamen,
)
@app.route("/<name>/<rolle>/spiel_ende")
def spiel_ende(name, rolle):
"""
The spiel_ende function is called when the game is over. It checks if Werwolf has won or lost and returns a
template with the result.
:param name: Identify the player
:param rolle: Determine which template to display
:return: The following:
"""
with open("rollen_original.txt", "r", encoding="UTF8") as file:
players_vorhanden = file.read()
file.close()
if werwolf.validiere_rolle_original(name, rolle) is True:
if (
"Werwolf" in players_vorhanden
and "Dorfbewohner" in players_vorhanden
or "Hexe" in players_vorhanden
and "Werwolf" in players_vorhanden
or "Seherin" in players_vorhanden
and "Werwolf" in players_vorhanden
or "Jaeger" in players_vorhanden
and "Werwolf" in players_vorhanden
or "Armor" in players_vorhanden
and "Werwolf" in players_vorhanden
):
werwolf.in_log_schreiben(
"Spiel noch nicht zuende für "
+ name
+ " mit Rolle "
+ rolle
+ " angezeigt"
)
return f"Hallo {escape(name)}, das Spiel ist noch nicht beendet!"
print("Spiel ist beendet!")
if rolle == "Werwolf":
if "Werwolf" in players_vorhanden:
werwolf.in_log_schreiben(
"Spiel beendet für "
+ name
+ " mit Rolle "
+ rolle
+ " angezeigt"
)
return render_template(
"gewonnen.html", name=name, rolle=rolle, unschuldig=0
)
werwolf.in_log_schreiben(
f"Spiel beendet für {name} mit Rolle {rolle} angezeigt"
)
return render_template(
"verloren.html", name=name, rolle=rolle, unschuldig=0
)
if "Werwolf" in players_vorhanden:
werwolf.in_log_schreiben(
f"Spiel beendet für {name} mit Rolle {rolle} angezeigt"
)
return render_template(
"verloren.html", name=name, rolle=rolle, unschuldig=1
)
werwolf.in_log_schreiben(
f"Spiel beendet für {name} mit Rolle {rolle} angezeigt"
)
return render_template(
"gewonnen.html", name=name, rolle=rolle, unschuldig=1
)
return render_template("fehler.html"), 500
@app.route("/waehlen/<name>/<rolle>/<auswahl>")
def wahl(name, rolle, auswahl):
"""
The wahl function is called when a user has selected a role and wishes to vote for another player.
It takes the name of the player, their role and an option from the dropdown menu as arguments.
If this is valid it will write that information into hat_gewaehlt.txt which is used by die_wahl() to check if someone has already voted.
:param name: Identify the player
:param rolle: Determine which role the player has
:param auswahl: Store the users input
:return: The following:
"""
if rolle == "Tot":
return render_template("warten.html")
wort2 = f"{name} : "
if werwolf.validiere_rolle(name, rolle) is not True:
return render_template("fehler.html"), 500
with open("hat_gewaehlt.txt", "r+") as text:
contents = text.read()
if wort2 in contents:
werwolf.in_log_schreiben(
f"Wahl schon getätigt für {name} mit Rolle {rolle} angezeigt"
)
return render_template("wahl_doppelt.html")
text.write(f"{name} : " + "\n")
text.close()
werwolf.in_log_schreiben(
f"Wahl getätigt für {name} mit Rolle {rolle} angezeigt, auswahl: {auswahl}"
)
with open("abstimmung.txt", "a") as abstimmung:
abstimmung.write(f"{auswahl}" + "\n")
abstimmung.close()
return render_template("Dashboards/status/warten.html")
# schlafen function
@app.route("/<name>/<rolle>/schlafen") # route for the sleep function
def schlafen(name, rolle): # function for the sleep function
"""
The schlafen function is used to sleep the player.
The function takes two parameters: name and rolle.
If the string is in the log file, render schlafen.html.
:param name: Get the name of the player
:param rolle: Determine the role of the player
:return: The sleep
"""
if rolle == "Tot":
return render_template("tot.html", name=name)
# if the string is in the log file
if werwolf.validiere_rolle(name, rolle) is True:
try:
with open("rollen_log.txt") as players_log: # open the log file
players_log = players_log.readlines() # read the log file
# render the sleep.html
werwolf.in_log_schreiben(f"Schlafen für {name} mit Rolle {rolle} angezeigt")
werwolf.setze_status_fuer_name(name, "1")
return render_template(
"Dashboards/status/schlafen.html",
name=name,
rolle=rolle,
names=players_log,
)
except (FileNotFoundError, IOError, PermissionError):
# render the fehler.html
return render_template("fehler.html"), 500
else:
# print the error
print("Spieler oder Rolle falsch!")
# render the url_system.html
return render_template("url_system.html", name=name, rolle=rolle)
# warten funktion
@app.route("/warten") # route for the wait function
def warten(): # function for the wait function
"""
The warten function is used to wait for all players to vote.
It checks if all players have voted and then shows the results of the voting.
:return: The template warten
"""
i = 0 # set i to 0
try:
with open("rollen_log.txt", "r", encoding="UTF8") as text:
for line in text:
if "Tot" not in line and "Erzaehler" not in line and "*" not in line:
i = i + 1
text.close()
with open("abstimmung.txt", "r", encoding="UTF8") as text:
# empty lines are not counted
anzahl_stimmen = sum(bool(line.rstrip()) for line in text)
text.close()
print(anzahl_stimmen)
print(i)
if i == anzahl_stimmen:
print("Alle Spieler haben gewaehlt")
werwolf.in_log_schreiben("Alle Spieler haben gewaehlt")
count = 0
name_tot = ""
maxCount = 0
words = []
file = open("abstimmung.txt", "r", encoding="UTF8")
for line in file:
string = line.lower().replace(",", "").replace(".", "").split(" ")
words.extend(iter(string))
for i, item in enumerate(words):
count = 1
for j in range(i + 1, len(words)):
if item == words[j]:
count = count + 1
if count > maxCount:
maxCount = count
name_tot = item
with open("rollen_log.txt", "r+") as fileTot:
counter_tot = 0
file_list = list(fileTot)
# print(file_list)
name_tot = name_tot.strip("\n")
name_tot = name_tot.replace("\n", "")
while counter_tot < len(file_list):
print(f"Name Tot: {name_tot} =")
if name_tot in file_list[counter_tot]:
dffd = file_list[counter_tot].split(" = ")
new_line = dffd[0] + " = Tot \n"
# print(new_line)
file_list[counter_tot] = new_line
# print(file_list)
counter_tot += 1
fileTot.close()
with open("rollen_log.txt", "w", encoding="UTF8") as fileFinal:
fileFinal.writelines(file_list)
fileFinal.close()
werwolf.schreibe_zuletzt_gestorben(name_tot)
werwolf.in_log_schreiben(f"Ergebnis angezeigt für {name_tot}")
return render_template("Dashboards/status/ergebnis.html", name_tot=name_tot)
return render_template("Dashboards/status/warten.html")
except (FileNotFoundError, IOError, PermissionError):
return render_template("fehler.html"), 500 # render the fehler.html
# tot function
# route for the death function
@app.route("/<name>/<rolle>/<todesgrund>/tot")
def tot(name, rolle, todesgrund): # function for the death function
"""
The tot function is used to render the status page of a player who has been killed.
It takes three arguments: name, rolle and todesgrund.
name is the name of the player who was killed.
rolle is either Werwolf or Dorfbewohner depending on what role they had in-game.
todesgrund can be "Werwolf", "Abstimung" or "Hexe". It's used for different death reasons.
:param name: Get the name of the player
:param rolle: Determine the role of the player
:param todesgrund: Set the death reason
:return: The death page
"""
# if the string is in the log file
if werwolf.validiere_rolle(name, rolle) is True:
try: # try to get the role
with open(
"rollen_log.txt", "r", encoding="UTF8"
) as players_log: # open the log file
players_log = players_log.readlines() # read the log file
if todesgrund in (
"Werwolf",
"werwolf",
): # if the death reason is a werewolf
# set the death reason to a werewolf
todesgrund = "Du wurdest von einem Werwolf getötet"
# if the death reason is a abstimulation
elif todesgrund in ("Abstimung", "abstimmung"):
# set the death reason to a abstimulation
todesgrund = "Du wurdest in Folge einer Abstimmung getötet"
elif todesgrund == "Hexe": # if the death reason is a witch
todesgrund = (
"Du wurdest von der Hexe getötet" # set the death reason to a witch
)
else:
todesgrund = (
"Du wurdest getötet" # set the death reason to a normal death
)
# rendert die Seite zum Status Tot
werwolf.in_log_schreiben(f"Tot für {name} mit Rolle {rolle} angezeigt")
werwolf.setze_status_fuer_name(name, "0")
return render_template(
"Dashboards/status/tot.html",
name=name,
todesgrund=todesgrund,
)
except (FileNotFoundError, IOError, PermissionError):
# rendert die Seite zum Status Fehler
return render_template("fehler.html"), 500
else:
# print the error
print("Spieler oder Rolle falsch!")
# render the url_system.html
return render_template("url_system.html", name=name, rolle=rolle)
# kick function
@app.route("/<name>/<rolle>/kick/") # route for the kick function
def rausschmeissen(name, rolle): # function for the kick function
"""
The rausschmeissen function is used to kick a player from the game.
It takes two arguments: name and rolle.
If the function is called with valid arguments, it will remove the player from
the game and write an entry in log_file.
:param name: Render the kick
:param rolle: Specify the role of the player that is kicked
:return: The kick function
"""
if werwolf.validiere_rolle(name, rolle) is True:
print("Spieler vorhanden") # print the string
try:
with open(
"rollen_log.txt", "r", encoding="UTF8"
) as players_log: # open the log file
players_log = players_log.readlines() # read the log file
# render the rausschmeissen.html
werwolf.toete_spieler(name)
werwolf.in_log_schreiben(
f"Spieler {name} rausgeschmissen, er hatt die Rolle {rolle}"
)
return render_template(
"rausschmeissen.html", name=name, rolle=rolle, names=players_log
)
except IOError as e:
# render the fehler.html
return render_template("fehler.html"), 500
else:
# print the error
print("Spieler oder Rolle falsch!")
# render the url_system.html
return render_template("url_system.html", name=name, rolle=rolle)
# wahlbalken
@app.route("/wahlbalken/") # route for the wahlbalken function
def wahlbalken():
"""
The wahlbalken function renders the wahlbalken.html page, which is used to select a player for the current round.
:return: The wahlbalken
"""
with open("rollen_log.txt", encoding="UTF8") as players_log: # open the log file
players_log = players_log.readlines() # read the log file
nurNamen = [] # create a list for the names
try:
for line in players_log: # for every line in the log file
if "*" not in line:
line = line.split(" = ") # split the line at the =
auswahlRolle = line[1] # get the role
# if the role is not dead or the narrator
if auswahlRolle not in ("Tot", "Erzaehler"):
name = line[0] # get the name
nurNamen.append(name) # append the name to the list
# render the wahlbalken.html
return render_template("wahlbalken.html", names=nurNamen)
except Exception as e:
return render_template("fehler.html"), 500 # render the fehler.html
@app.route("/wahlstatus") # route for the wahlstatus function
def wahl_stats():
"""
The wahl_stats function counts the number of times a name appears in the abstimmung.txt file and returns
the name with the most votes. It also writes this name to wahl_zuletzt_gestorben.txt.
:return: The most common name in the text
"""
anzahl = 0
name_tot = ""
maxCount = 0
words = []
file = open("abstimmung.txt", "r", encoding="UTF8")
for line in file:
string = line.lower().replace(",", "").replace(".", "").split(" ")
words.extend(iter(string))
for i, item in enumerate(words):
anzahl = 1
for j in range(i + 1, len(words)):
if item == words[j]:
anzahl = anzahl + 1
if anzahl > maxCount:
maxCount = anzahl
name_tot = item
werwolf.schreibe_zuletzt_gestorben(name_tot)
return render_template("wahlstatus.html", name_tot=name_tot)
@app.route("/test")
def test():
return render_template("game.html", nurNamen=werwolf.nurNamen())
@app.route("/sehen/<name>/<rolle>/<auswahl>")
def sehen(name, rolle, auswahl):
"""
The sehen function allows the Seherin to see the role of a player.
The function takes three arguments: name, rolle and auswahl.
name is the name of the seherin, rolle is her role and auswahl is
the player she wants to check.
:param name: Identify the player
:param rolle: Check if the player is a werewolf or not
:param auswahl: Select the role you want to see
:return: The role of the player that was chosen
"""
if werwolf.validiere_rolle(name, rolle) is True:
with open(
"rollen_log.txt", encoding="UTF8"
) as players_log: # open the log file
players_log = players_log.readlines() # read the log file
for line in players_log:
if auswahl in line:
ergebnis = line
ergebnis = ergebnis.replace("=", "hat die Rolle")
werwolf.in_log_schreiben(
(
"Seherin "
+ name
+ "hat die Rolle von "
+ auswahl
+ " gesehen "
+ ergebnis.replace(f"{name} hat die Rolle", "")
)
)
return render_template(
"Dashboards/status/sehen.html", ergebnis=ergebnis
)
return render_template("fehler.html"), 500