1
1
from sys import argv
2
- from flask import Flask , render_template , url_for
2
+ from random import randrange , shuffle
3
+ from flask import Flask , render_template , url_for , request
4
+ from mafia_params import *
3
5
4
6
app = Flask (__name__ )
7
+ id = 0
8
+ nPlayers = 0
9
+ roles = []
10
+ ip2role = {}
11
+ ip2image = {}
5
12
6
13
@app .route ('/' )
7
14
def index ():
15
+ global id , roles , ip2role , ip2image , nPlayers
16
+ role = ""
17
+ image_name = ""
18
+ ip = str (request .remote_addr )
19
+
20
+ if ip in ip2role .keys ():
21
+ role = ip2role [ip ]
22
+ image_name = ip2image [ip ]
23
+ else :
24
+ if id > nPlayers :
25
+ return "Numbers of players out of range!" #TODO:well defined Error Page
26
+ role = roles [id ]
27
+ image_name = role + "_" + str (randrange (1 , nRoles [role ] + 1 ))
28
+ id += 1
29
+ ip2role [ip ] = role
30
+ ip2image [ip ] = image_name
31
+ print ("*" * 20 , "New Player" ,"*" * 20 )
32
+ print (ip + " : " + str (id ) + " --> " + role )
8
33
return render_template ("index.html" ,
9
- image_name = "Don" ,
10
- role_name = "Don" ,
34
+ image_name = image_name ,
35
+ role_name = role ,
36
+ player_id = id - 1 ,
11
37
is_farsi = True )
12
38
13
39
14
40
def help_me ():
15
- usage = "mafia - Web Server Application For Mafia Game Playing On Local Network \n \n "
41
+ usage = "-" * 70 + "\n "
42
+ usage += "mafia - Web Server Application For Mafia Game Playing On Local Network \n \n "
43
+ usage += "-" * 70 + "\n "
16
44
usage += "Usage: python3 mafia number_of_players[int]\n "
17
45
usage += "ex: python3 mafia 5\n "
18
- usage += "this will tell mafia.py that you want a game for 5 people."
46
+ usage += "this will tell mafia.py that you want a game for 5 people.\n \n "
47
+ usage += "If you've seen a bug here (or any idea that can help us) feel free to open an issue\n "
48
+ usage += "here at : https://github.com/sadrasabouri/mafia/issues"
19
49
print (usage )
20
50
exit ()
21
51
@@ -24,6 +54,11 @@ def help_me():
24
54
if len (argv ) < 2 or argv [1 ] in ['--help' , 'help' , '-h' ]:
25
55
help_me ()
26
56
nPlayers = int (argv [1 ])
57
+ if nPlayers > len (ordered_roles ):
58
+ print ("Too many players, mafia doesn't support a game with" , nPlayers , "player." )
59
+ help_me ()
60
+ roles = ordered_roles [:nPlayers ]
61
+ shuffle (roles )
27
62
app .run (host = "0.0.0.0" , \
28
63
port = 5000 , \
29
64
debug = True )
0 commit comments