Skip to content

Commit 7f2f6e2

Browse files
committed
add : initial states added.
1 parent d60ba85 commit 7f2f6e2

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

mafia.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,51 @@
11
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 *
35

46
app = Flask(__name__)
7+
id = 0
8+
nPlayers = 0
9+
roles = []
10+
ip2role = {}
11+
ip2image = {}
512

613
@app.route('/')
714
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)
833
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,
1137
is_farsi=True)
1238

1339

1440
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"
1644
usage += "Usage: python3 mafia number_of_players[int]\n"
1745
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"
1949
print(usage)
2050
exit()
2151

@@ -24,6 +54,11 @@ def help_me():
2454
if len(argv) < 2 or argv[1] in ['--help', 'help', '-h']:
2555
help_me()
2656
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)
2762
app.run(host="0.0.0.0", \
2863
port=5000, \
2964
debug=True)

0 commit comments

Comments
 (0)