Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
__pycache__/
*.pyc
*.pyo
*.pyd
.Python
env/
venv/
.git
.gitignore
.DS_Store
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,4 @@ dmypy.json

# Pyre type checker
.pyre/
.lh
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's pretty harmless just in the .gitignore, but what is this for? I think this can be removed.

21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM python:3.12-slim


# Upgrade pip and system packages to reduce vulnerabilities
RUN apt-get update && \
apt-get upgrade -y && \
apt-get install -y --no-install-recommends gcc build-essential && \
python3 -m pip install --upgrade pip && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

WORKDIR /app

COPY requirements.txt ./
RUN python3 -m pip install --no-cache-dir -r requirements.txt

COPY . .
Comment on lines +14 to +17
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just use this last COPY you don't need both. It may be easier for development to save the cached layer for later, but it doesn't need to be that way on the repo.


EXPOSE 5000

CMD ["python3", "web.py"]
6 changes: 3 additions & 3 deletions clonenames.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Board(object):
"""docstring for Board"""
def __init__(self, wordlist: str = u'codenames') -> None:
# self.wordlist = wordlist
with open(u'wordlists/{wordlist}'.format(wordlist = wordlist), u'r') as text_file:
with open(u'wordlists/{wordlist}'.format(wordlist=wordlist), u'r') as text_file:
self.source = [line for line in text_file.read().split(u'\n') if line != u'']

def load_settings(self, teams: int = 2, size: int = 25) -> bool:
Expand Down Expand Up @@ -48,8 +48,8 @@ def check_size(self, size: int) -> None:
self.length = int(self.size ** 0.5)

def load_words(self) -> None:
from time import clock
random.seed(clock())
from time import perf_counter
random.seed(perf_counter())

self.COLORS = [u'red', u'blue', u'green', u'yellow']
random.shuffle(self.COLORS)
Expand Down
10 changes: 10 additions & 0 deletions docker-compose.yml
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole webapp doesn't have any external services, so I don't see a need for it to have a docker compose. I appreciate the Dockerfile though!

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "3.12"

services:
clonenames:
build: .
ports:
- "5000:5000"
environment:
- PYTHONUNBUFFERED=1
restart: unless-stopped
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dnspython==1.16.0
eventlet==0.30.1
Flask==1.1.2
Flask-SocketIO==5.0.1
greenlet==1.0.0
greenlet
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This dependency (and many others) is out of date, but I don't want to unpin them.

itsdangerous==1.1.0
Jinja2==2.11.3
MarkupSafe==1.1.1
Expand Down
135 changes: 71 additions & 64 deletions web.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,116 +21,123 @@
games = dict()


@app.route(u'/')
@app.route("/")
def home_page():
return render_template(u'index.html')
return render_template("index.html")


@app.route(u'/start', methods = [u'GET', u'POST'])
@app.route("/start", methods=["GET", "POST"])
def start_page():
if request.method == u'POST':
game = clonenames.Board(request.form.get(u'words'))
if request.method == "POST":
game = clonenames.Board(request.form.get("words"))

success = game.load_settings(
teams = int(request.form.get(u'number')),
size = int(request.form.get(u'size')))
teams=int(request.form.get("number")), size=int(request.form.get("size"))
)

if success:
room = generate_room_code()

games[room] = game

return redirect(url_for(u'game_page', room = room))
return redirect(url_for("game_page", room=room))

else:
return render_template(
u'start.html',
words = clonenames.wordlists,
alert = u'The word list selected must be played on a smaller game board... Sorry!')
"start.html",
words=clonenames.wordlists,
alert="The word list selected must be played on a smaller game board... Sorry!",
)

elif request.method == u'GET':
return render_template(u'start.html', words = clonenames.wordlists)
elif request.method == "GET":
return render_template("start.html", words=clonenames.wordlists)


@app.route(u'/game', methods = [u'GET', u'POST'])
@app.route("/game", methods=["GET", "POST"])
def game_page():
if request.args.get(u'room'):
room = request.args.get(u'room')
if request.args.get("room"):
room = request.args.get("room")
return render_template(
u'game.html',
show_input = False,
room = room,
host = True,
words = games[room].table(),
start = games[room].order[0],
remnants = games[room].remnants)
"game.html",
show_input=False,
room=room,
host=True,
words=games[room].table(),
start=games[room].order[0],
remnants=games[room].remnants,
)

else:
try:
if request.method == u'GET':
return render_template(
u'game.html',
show_input = True)
if request.method == "GET":
return render_template("game.html", show_input=True)

elif request.method == u'POST':
room = request.form.get(u'room', False).upper()
elif request.method == "POST":
room = request.form.get("room", False).upper()
if check_room_code(room):
return render_template(
u'game.html',
show_input = False,
room = room,
host = request.form.get(u'host', False) == u'on',
words = games[room].table(),
start = games[room].order[0],
remnants = games[room].remnants)
"game.html",
show_input=False,
room=room,
host=request.form.get("host", False) == "on",
words=games[room].table(),
start=games[room].order[0],
remnants=games[room].remnants,
)

else:
return render_template(
u'game.html',
show_input = True,
alert = u'The room code you entered does not exist. Please try again!')
"game.html",
show_input=True,
alert="The room code you entered does not exist. Please try again!",
)

except AttributeError:
return redirect(url_for(u'home_page'))
return redirect(url_for("home_page"))


# @app.route(u'/statistics')
# def stats_page():
# return render_template(u'statistics.html',
# rooms = [games[game].statistics() for game in games])


@socketio.on(u'join')
@socketio.on("join")
def join(data):
join_room(data[u'room'])
join_room(data["room"])


@socketio.on(u'clicked')
@socketio.on("clicked")
def handle_host_click(json):
response = games[json[u'room']].get(json[u'id'])
socketio.emit(u'revealed', {
u'text': u'Host clicked on {word}'.format(
word = response[u'word']),
u'id': u'#{id}'.format(id = json[u'id']),
u'remnant': response[u'remnant'],
u'class': u'btn-{team}'.format(
team = response[u'team'])}, room = json['room'])


@socketio.on(u'ended_turn')
response = games[json["room"]].get(json["id"])
socketio.emit(
"revealed",
{
"text": "Host clicked on {word}".format(word=response["word"]),
"id": "#{id}".format(id=json["id"]),
"remnant": response["remnant"],
"class": "btn-{team}".format(team=response["team"]),
},
room=json["room"],
)


@socketio.on("ended_turn")
def handle_end_turn(json):
team = games[json[u'room']].advance_turn()
team = games[json["room"]].advance_turn()

alert = u'alert {team}-start alert-start'.format(team = team)
button = u'btn btn-{team} float-right'.format(team = team)
alert = "alert {team}-start alert-start".format(team=team)
button = "btn btn-{team} float-right".format(team=team)

socketio.emit(u'change_turn', {
u'alert': alert,
u'text': team,
u'button': button}, room = json[u'room'])
socketio.emit(
"change_turn",
{"alert": alert, "text": team, "button": button},
room=json["room"],
)


def generate_room_code():
letters = u''.join(random.sample(list(u'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 5))
letters = "".join(random.sample(list("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 5))
while letters in games.keys():
return generate_room_code()

Expand All @@ -141,5 +148,5 @@ def check_room_code(code):
return code in games.keys()


if __name__ == '__main__':
socketio.run(app, debug=True, host=u'0.0.0.0')
if __name__ == "__main__":
socketio.run(app, debug=False, host="0.0.0.0")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
socketio.run(app, debug=False, host="0.0.0.0")
socketio.run(app, debug=True, host="0.0.0.0")

This should all probably be reading in parameters from a config file or something, but let's keep the original default value.