Skip to content

Commit

Permalink
Merge pull request #17 from Thepunisher79/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
sadrasabouri authored Sep 14, 2020
2 parents 8a9b281 + 02e1185 commit ca72324
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 50 deletions.
47 changes: 22 additions & 25 deletions mafia.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from flask import Flask, render_template, url_for, request
from flask_httpauth import HTTPBasicAuth
from mafia_params import *
from player import Player

app = Flask(__name__)
auth = HTTPBasicAuth()
Expand All @@ -11,7 +12,7 @@
id = 0
nPlayers = 0
roles = []
ip2role_index_name = {}
ip2player = {}
nComments = 0
comments_ordered = []

Expand All @@ -24,24 +25,20 @@ def verify_password(username, password):
@app.route('/')
@auth.login_required
def index():
global id, ip2role_index_name
global id, ip2player
username = str(auth.current_user())
role = ""
image_name = ""
ip = str(request.remote_addr)

if ip in ip2role_index_name.keys():
return render_template("Player.html", player=ip2role_index_name[ip])
if ip in ip2player.keys():
return render_template("Player.html", player=ip2player[ip])
else:
if id > nPlayers:
return render_template("404.html", is_farsi=True)
role = roles[id]
ip2role_index_name[ip] = [role,
str(randrange(1, nRoles[role] + 1)),
username,
"alive",
False]
image_name = role + "_" + str(ip2role_index_name[ip][1])
image_name = role + "_" + str(randrange(1, nRoles[role] + 1))
ip2player[ip] = Player(ip, username, role, image_name)
print("*" * 20, "New Player","*" * 20)
toGod = ip + " : " + str(id) + " : " + username + " --> " + role
toGod += "/" + role2fa[role] #TODO: Just in Farsi Mode
Expand All @@ -63,43 +60,43 @@ def verify_password_god(username, password):
@app.route('/GOD')
@auth_GOD.login_required
def GOD_PAGE():
global ip2role_index_name, nComments, comments_ordered
global ip2player, nComments, comments_ordered
msg = ""
if request.args.get("Kill") is not None:
ip = request.args.get("Kill")
if ip in ip2role_index_name.keys():
if ip2role_index_name[ip][3] == "alive":
ip2role_index_name[ip][3] = "dead"
if ip in ip2player.keys():
if ip2player[ip].get_state() == "alive":
ip2player[ip].set_state("dead")
else:
ip2role_index_name[ip][3] = "alive"
ip2player[ip].set_state("alive")
else:
return render_template("404.html", is_farsi=True)
if request.args.get("Ban") is not None:
ip = request.args.get("Ban")
if ip in ip2role_index_name.keys():
if ip2role_index_name[ip][3] == "alive":
ip2role_index_name[ip][3] = "banned"
elif ip2role_index_name[ip][3] == "banned":
ip2role_index_name[ip][3] = "alive"
if ip in ip2player.keys():
if ip2player[ip].get_state() == "alive":
ip2player[ip].set_state("banned")
elif ip2player[ip].get_state() == "banned":
ip2player[ip].set_state("alive")
else:
return render_template("404.html", is_farsi=True)
if request.args.get("Comment") is not None:
ip = request.args.get("Comment")
if ip in ip2role_index_name.keys():
if ip2role_index_name[ip][4] == False:
if ip in ip2player.keys():
if ip2player[ip].get_comment() == False:
if nComments <= nPlayers // 3:
ip2role_index_name[ip][4] = True
ip2player[ip].set_comment(True)
nComments += 1
comments_ordered.append(ip)
else:
msg = "Error: Out of Comments."
else:
ip2role_index_name[ip][4] = False
ip2player[ip].set_comment(False)
nComments -= 1
comments_ordered.remove(ip)
else:
return render_template("404.html", is_farsi=True)
return render_template("GOD.html", ip2role_index_name=ip2role_index_name,
return render_template("GOD.html", ip2player=ip2player,
prompt_message=msg, roles={role:roles.count(role) for role in set(roles)},
comments=comments_ordered, role2team=role2team)

Expand Down
36 changes: 36 additions & 0 deletions player.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Player:
"""A player in mafia game"""
def __init__(self, ip, username, role, image_name):
self.ip = ip
self.username = username
self.state = "alive"
self.role = role
self.image_name = image_name
self.comment = False

def get_state(self):
return self.state

def get_ip(self):
return self.ip

def get_username(self):
return self.username

def get_role(self):
return self.role

def get_image_name(self):
return self.image_name

def get_comment(self):
return self.comment


def set_state(self, state):
if state in ["alive", "dead", "banned"]:
self.state = state

def set_comment(self, comment):
self.comment = comment

42 changes: 21 additions & 21 deletions templates/GOD.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,38 @@ <h4 class="table_header">Players</h4>
<th class="action_table" id="player_role">Player Role</th>
<th class="action_table" id="actions" colspan="3">Actions</th>
</tr>
{% for ip in ip2role_index_name.keys() %}
{% if ip2role_index_name[ip][3] == "alive" %}
<tr class="{{ ip2role_index_name[ip][3] }}">
<td class="action_table" id="is_comment">{% if ip2role_index_name[ip][4] == True%}+{% endif %}</td>
<td class="action_table" id="player_name">{{ ip2role_index_name[ip][2] }}</td>
<td class="action_table" id="player_role"><p class="{{ role2team[ip2role_index_name[ip][0]] }}">{{ ip2role_index_name[ip][0] }}</p></td>
{% for ip in ip2player.keys() %}
{% if ip2player[ip].get_state() == "alive" %}
<tr class="{{ ip2player[ip].get_state() }}">
<td class="action_table" id="is_comment">{% if ip2player[ip].get_comment() == True%}+{% endif %}</td>
<td class="action_table" id="player_name">{{ ip2player[ip].get_username() }}</td>
<td class="action_table" id="player_role"><p class="{{ role2team[ip2player[ip].get_role()] }}">{{ ip2player[ip].get_role() }}</p></td>
<td class="action_table"><a href="javascript:delay('/GOD?Ban={{ ip }}', 2000)" onclick="ban_sound_play()">Ban</a></td>
<td class="action_table"><a href="/GOD?Comment={{ ip }}">Comment</a></td>
<td class="action_table"><a href="javascript:delay('/GOD?Kill={{ ip }}', 1500)" onclick="kill_sound_play()">Kill</a></td>
</tr>
{% endif %}
{% endfor %}

{% for ip in ip2role_index_name.keys() %}
{% if ip2role_index_name[ip][3] == "banned" %}
<tr class="{{ ip2role_index_name[ip][3] }}">
<td class="action_table" id="is_comment">{% if ip2role_index_name[ip][4] == True%}+{% endif %}</td>
<td class="action_table" id="player_name">{{ ip2role_index_name[ip][2] }}</td>
<td class="action_table" id="player_role"><p class="{{ role2team[ip2role_index_name[ip][0]] }}">{{ ip2role_index_name[ip][0] }}</p></td>
{% for ip in ip2player.keys() %}
{% if ip2player[ip].get_state() == "banned" %}
<tr class="{{ ip2player[ip].get_state() }}">
<td class="action_table" id="is_comment">{% if ip2player[ip].get_comment() == True%}+{% endif %}</td>
<td class="action_table" id="player_name">{{ ip2player[ip].get_username() }}</td>
<td class="action_table" id="player_role"><p class="{{ role2team[ip2player[ip].get_role()] }}">{{ ip2player[ip].get_role() }}</p></td>
<td class="action_table"><a href="javascript:delay('/GOD?Ban={{ ip }}', 2000)" onclick="ban_sound_play()">Ban</a></td>
<td class="action_table"><a href="/GOD?Comment={{ ip }}">Comment</a></td>
<td class="action_table"><a href="javascript:delay('/GOD?Kill={{ ip }}', 1500)" onclick="kill_sound_play()">Kill</a></td>
</tr>
{% endif %}
{% endfor %}

{% for ip in ip2role_index_name.keys() %}
{% if ip2role_index_name[ip][3] == "dead" %}
<tr class="{{ ip2role_index_name[ip][3] }}">
<td class="action_table" id="is_comment">{% if ip2role_index_name[ip][4] == True%}+{% endif %}</td>
<td class="action_table" id="player_name">{{ ip2role_index_name[ip][2] }}</td>
<td class="action_table" id="player_role"><p class="{{ role2team[ip2role_index_name[ip][0]] }}">{{ ip2role_index_name[ip][0] }}</p></td>
{% for ip in ip2player.keys() %}
{% if ip2player[ip].get_state() == "dead" %}
<tr class="{{ ip2player[ip].get_state() }}">
<td class="action_table" id="is_comment">{% if ip2player[ip].get_comment() == True%}+{% endif %}</td>
<td class="action_table" id="player_name">{{ ip2player[ip].get_username() }}</td>
<td class="action_table" id="player_role"><p class="{{ role2team[ip2player[ip].get_role()] }}">{{ ip2player[ip].get_role() }}</p></td>
<td class="action_table"><a href="javascript:delay('/GOD?Ban={{ ip }}', 2000)" onclick="ban_sound_play()">Ban</a></td>
<td class="action_table"><a href="/GOD?Comment={{ ip }}">Comment</a></td>
<td class="action_table"><a href="javascript:delay('/GOD?Kill={{ ip }}', 1500)" onclick="kill_sound_play()">Kill</a></td>
Expand All @@ -68,10 +68,10 @@ <h4 class="table_header">Comments</h4>
<th class="action_table" id="player_role">Player Role</th>
</tr>
{% for ip in comments %}
<tr class="{{ ip2role_index_name[ip][3] }}">
<tr class="{{ ip2player[ip].get_state() }}">
<td class="action_table" id="is_comment">{{ comments.index(ip) + 1 }}</td>
<td class="action_table" id="player_name">{{ ip2role_index_name[ip][2] }}</td>
<td class="action_table" id="player_role"><p class="{{ role2team[ip2role_index_name[ip][0]] }}">{{ ip2role_index_name[ip][0] }}</p></td>
<td class="action_table" id="player_name">{{ ip2player[ip].get_username() }}</td>
<td class="action_table" id="player_role"><p class="{{ role2team[ip2player[ip].get_role()] }}">{{ ip2player[ip].get_role() }}</p></td>
</tr>
{% endfor %}
{% endblock %}
Expand Down
8 changes: 4 additions & 4 deletions templates/Player.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
{% block sub_table_1 %}
<tr>
<td class="role">
<h1>{{ player[2] }}</h1>
<h1>{{ player.get_username() }}</h1>
</td>
</tr>
<tr>
<td>
{% if player[3] == "alive" %}
{% if player.get_state() == "alive" %}
<img class="avatar" src="static/images/Warns/play.png" onclick="start()">
<div id="myProgress">
<div id="myBar">0</div>
</div>
{% endif %}
{% if player[3] == "dead" %}You are Killed!{% endif %}
{% if player[3] == "banned" %}You are Banned from speaking this turn!{% endif %}
{% if player.get_state() == "dead" %}You are Killed!{% endif %}
{% if player.get_state() == "banned" %}You are Banned from speaking this turn!{% endif %}
</td>
</tr>
{% endblock %}
Expand Down

0 comments on commit ca72324

Please sign in to comment.