forked from NJIT-CS490-SP21/project2-as3627
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
246 lines (188 loc) · 6.98 KB
/
app.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
'''The backend, written in python'''
import os
from flask import Flask, send_from_directory, json
from flask_socketio import SocketIO
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv()) # This is to load your env variables from .env
APP = Flask(__name__, static_folder='./build/static')
# Point SQLAlchemy to your Heroku database
APP.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('DATABASE_URL')
# Gets rid of a warning
APP.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
DB = SQLAlchemy(APP)
# IMPORTANT: This must be AFTER creating db variable to prevent
# circular import issues
import models
DB.create_all()
CORS = CORS(APP, resources={r"/*": {"origins": "*"}})
SOCKETIO = SocketIO(APP,
cors_allowed_origins="*",
json=json,
manage_session=False)
@APP.route('/', defaults={"filename": "index.html"})
@APP.route('/<path:filename>')
def index(filename):
'''Used for index'''
return send_from_directory('./build', filename)
def print_players():
'''Function used to output the current rankings of players.'''
player_names = DB.session.query(models.Player.username).order_by(
models.Player.points.desc())
names = []
for i in player_names:
names.append(str(i))
new_names = format_list(names)
return new_names
def print_points():
'''Function used to output a list of rankings'''
player_points = DB.session.query(models.Player.points).order_by(
models.Player.points.desc())
pts = []
for i in player_points:
pts.append(str(i))
new_points = format_list(pts)
return new_points
def format_list(old):
'''Properly formats the list we send back'''
new_list = []
for value in enumerate(old):
temp = value[1]
temp = temp.replace("(", "")
temp = temp.replace(")", "")
temp = temp.replace("'", "")
temp = temp.replace(",", "")
new_list.append(temp)
return new_list
def add_player(name):
'''Function to add username to DB'''
all_players = models.Player.query.all()
users = []
for player in all_players:
users.append(player.username)
if name not in users:
new_user = models.Player(username=name, points=100)
DB.session.add(new_user)
DB.session.commit()
return True
return False
def update_points(name, status):
'''Used to update the point totals for a specifc user'''
user = DB.session.query(models.Player).filter_by(username=name).first()
if status == 'win':
setattr(user, 'points', user.points + 1)
DB.session.commit()
user = models.Player.query.filter_by(username=name).first()
print("Winner's Points")
print(user.points)
if status == 'loss':
setattr(user, 'points', user.points - 1)
DB.session.commit()
user = models.Player.query.filter_by(username=name).first()
print("Loser's Points")
print(user.points)
@SOCKETIO.on('connect')
def on_connect():
'''When a client connects from this Socket connection, this function is run'''
print('User connected!')
@SOCKETIO.on('disconnect')
def on_disconnect():
'''When a client disconnects from this Socket connection, this function is run'''
print('User disconnected!')
@SOCKETIO.on('move')
def on_move(data):
'''When a client emits the event 'move' to the server, this function is run'''
print(str(data))
SOCKETIO.emit('move', data, broadcast=True, include_self=False)
# Have a players and Spectators array in the back end.
PLAYERS = []
SPECTATORS = []
@SOCKETIO.on('login')
def on_login(data):
'''When a client logs in'''
if data['name'] in PLAYERS:
SOCKETIO.emit('deny', data, broadcast=True, include_self=True)
elif data['name'] in SPECTATORS:
SOCKETIO.emit('deny', data, broadcast=True, include_self=True)
else:
SOCKETIO.emit('confirm', data, broadcast=True, include_self=True)
var = add_player(data['name'])
if var:
print("New user has been added")
else:
print("Returning user has not been added")
if len(PLAYERS) < 2:
PLAYERS.append(data['name'])
else:
SPECTATORS.append(data['name'])
data['players'] = PLAYERS
data['spectators'] = SPECTATORS
data['ready'] = False
data['rank_names'] = print_players()
data['rank_points'] = print_points()
if len(PLAYERS) == 2:
data['ready'] = True
SOCKETIO.emit('login', data, broadcast=True, include_self=True)
# Variable to store the current winner
CURRENT_WINNER = ""
@SOCKETIO.on('winner')
def on_winner(data):
''' This function will get a winner and update the DB accordingly '''
global CURRENT_WINNER
print(str(data))
if CURRENT_WINNER == "":
CURRENT_WINNER = data["name"]
update_points(CURRENT_WINNER, 'win')
print("CURRENT WINNER IS: " + CURRENT_WINNER)
data['rank_names'] = print_players()
data['rank_points'] = print_points()
SOCKETIO.emit('update', data, broadcast=True, include_self=True)
# Variable to store current loser
CURRENT_LOSER = ""
@SOCKETIO.on('loser')
def on_loser(
data): # This function will get a winner and update the DB accordingly
''' When the player who lost sends a message to the client '''
global CURRENT_LOSER
print(str(data))
if CURRENT_LOSER == "":
CURRENT_LOSER = data["name"]
update_points(CURRENT_LOSER, 'loss')
print("CURRENT LOSER IS: " + CURRENT_LOSER)
data['rank_names'] = print_players()
data['rank_points'] = print_points()
SOCKETIO.emit('update', data, broadcast=True, include_self=True)
# Use an array to hold the players who hit restart.
RESTART = []
@SOCKETIO.on('restart')
def on_restart(
data): # data is whatever arg you pass in your emit call on client
''' When the reset event is recieved '''
global CURRENT_WINNER
global CURRENT_LOSER
print(str(data))
if data['username'] not in RESTART:
if data['username'] in PLAYERS:
RESTART.append(data['username'])
print(RESTART)
if len(RESTART) == 2:
SOCKETIO.emit('confirm_restart',
data,
broadcast=True,
include_self=True)
CURRENT_WINNER = ""
CURRENT_LOSER = ""
print("AFTER RESTART, CURRENT WINNER IS: " + CURRENT_WINNER)
print("AND CURRENT LOSER IS: " + CURRENT_LOSER)
RESTART.clear()
elif data['username'] in PLAYERS:
SOCKETIO.emit('restart', data, broadcast=True, include_self=True)
# Note we need to add this line so we can import app in the python shell
if __name__ == "__main__":
# Note that we don't call app.run anymore. We call socketio.run with app arg
SOCKETIO.run(
APP,
host=os.getenv('IP', '0.0.0.0'),
port=8081 if os.getenv('C9_PORT') else int(os.getenv('PORT', 8081)),
)