-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
104 lines (81 loc) · 2.93 KB
/
server.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
from datetime import timedelta
from flask import Flask, render_template, request, redirect, session, url_for
import random
from flask_socketio import SocketIO, join_room, leave_room, disconnect
import logging
from classes.game import Game
# from engineio.payload import Payload
# configs
# Payload.max_decode_packets = 50
app = Flask(__name__)
app.config['SECRET_KEY'] = 'vlkjnflajhbfl1232#'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_USE_SIGNER"] = True
# logging.getLogger('werkzeug').disabled = True # disabling logs
# app.logger.disabled = True
socketio = SocketIO(app, cors_allowed_origins='*')
my_game = Game("", "", socketio)
next_guest_num = 100
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=7)
@app.before_request
def before_request():
""" Sets up the session for a guest """
global next_guest_num
print(session)
print('username' not in session)
if 'username' not in session:
session['username'] = "Guest" + str(next_guest_num)
session.permanent = True
next_guest_num += 1
@app.route('/', methods=['POST', 'GET'])
def index():
print("from index: " + session['username'])
return render_template('index.html')
@socketio.on('request_current_username', namespace='/index')
def print_current_username():
print(session['username'])
@socketio.on('change_username', namespace='/index')
def change_username(json):
username = json.get('username', session['username'])
print(f"got username change request: {session['username']} to {username}")
session.pop('username', None)
session['username'] = username
socketio.emit('username_changed', {'username': username}, namespace='/index')
print(session['username'])
print(session)
@app.route('/game', methods=['POST', 'GET'])
def game():
print("from game: " + session['username'])
return render_template('game.html')
@socketio.on('connect_to_game', namespace='/game')
def connect_to_game():
global my_game
if my_game:
my_game.send_current_boards(request.sid)
@socketio.on('new_chat_message', namespace='/game')
def new_chat_message(json):
global my_game
if my_game:
my_game.process_chat_message(request.sid, json.get('message', ""))
@socketio.on('request_reset', namespace='/game')
def game_reset():
global my_game
my_game.reset()
my_game = Game(my_game.p1_sid, my_game.p2_sid, socketio)
@socketio.on('become_a_player', namespace='/game')
def become_a_player(json):
global my_game
player = json.get('player', 1)
if my_game:
my_game.add_player(player, request.sid)
@socketio.on('chose_column', namespace='/game')
def got_number(json):
global my_game
col = json.get('column', 1)
board = json.get('board', 1)
print(f"got: {col}")
if my_game:
my_game.add_dice(board, col, request.sid)
if __name__ == '__main__':
socketio.run(app, debug=True, host="0.0.0.0", port=80)