/
hello.py
274 lines (229 loc) · 8.47 KB
/
hello.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import os
from flask import Flask, jsonify, Response, json
from flask_basicauth import BasicAuth
from pymongo import MongoClient
from DAOs.gamesDAO import GameDAO
from DAOs.killsDAO import KillDAO
from DAOs.playersDAO import PlayerDAO
from DAOs.userDAO import UserDAO
from Models.kills import *
from Models.players import *
from Models.user import *
from Service.GameService import *
#hello.py serves as a config file that routes all HTTP traffic to appropriate functions/methods
app = Flask(__name__)
#app configs for BasicAuth.local accepted user/pass only set at registration, which is NOt protected
app.config['BASIC_AUTH_USERNAME'] = 'specialkeythatnoonewilleverknow'
app.config['BASIC_AUTH_PASSWORD'] = 'specialerpasswordisawesome'
basic_auth = BasicAuth(app)
#DAO initialization for easy access
playerDAO = PlayerDAO()
usersDAO = UserDAO()
killsDAO = KillDAO()
gameDAO = GameDAO()
@app.route('/')
def index():
return "Welcome to Ronak's Mafia Game Web Service. Please register/login."
#==============ROUTING FOR GameService METHODS===========#
@app.route('/playersNearTo/<userID>/<int:radius>', methods=['GET', 'POST'])
@basic_auth.required
def getNearbyPlayers(userID, radius):
conf = playersNearTo(userID, radius)
if conf == False:
return "false"
elif len(conf) == 0:
return "false"
else:
nearby_list = conf
jsoned_list = {"response": nearby_list}
return jsonify(jsoned_list)
@app.route('/startGame/<userID>/<int:freq>', methods=['GET', 'POST'])
@basic_auth.required
def newGame(userID, freq):
conf = startGame(userID, freq)
if conf == True:
return "New game started successfully"
else:
return "New game not created: a game is already in progress"
@app.route('/restartGame/<userID>/<int:new_freq>', methods=['GET', 'POST'])
@basic_auth.required
def restartCurrentGame(userID, new_freq):
conf = restartGame(userID, new_freq)
if conf == True:
return "Current game restarted successfully. Player configuration preserved."
else:
return "Could not restart game, $s does not have admin privaledges" % userID
@app.route('/concludeGame', methods=['GET', 'POST'])
@basic_auth.required
def concludeCurGame():
conf = concludeGame()
return conf
@app.route('/getAllAlivePlayers', methods=['GET', 'POST'])
@basic_auth.required
def getAlivePlayers():
alive_player_list = getAllAlivePlayers()
conf = {"response": alive_player_list}
return jsonify(conf)
@app.route('/getAllVotablePlayers', methods=['GET', 'POST'])
@basic_auth.required
def getAllVotablePlayers():
votable_player_list = getVotablePlayers()
return Response(json.dumps(votable_player_list), mimetype='application/json')
@app.route('/placeVote/<voter_userID>/<votee_userID>', methods=['GET', 'POST'])
@basic_auth.required
def voteForPlayer(voter_userID, votee_userID):
conf = placeVote(voter_userID, votee_userID)
if conf == True:
return "Vote cast successfully"
else:
return "Vote not cast. %s not a Townsperson" % voter_userID
@app.route('/killPlayer/<killer_userID>/<victim_userID>', methods=['GET', 'POST'])
@basic_auth.required
def kill(killer_userID, victim_userID):
conf = killPlayer(killer_userID, victim_userID)
if conf == True:
return "%s killed by %s successfully" % (victim_userID, killer_userID)
else:
return "Error: %s not a Werewolf" % killer_userID
@app.route('/getCurrentLocation/<userID>', methods=['GET', 'POST'])
@basic_auth.required
def getLocationOf(userID):
location_tuple = getCurrentLocation(userID)
location_json = {'latitude': location_tuple[0],
'longitude': location_tuple[1]
}
return jsonify(location_json)
@app.route('/getHighscore', methods=['GET', 'POST'])
@basic_auth.required
def getHighScore():
conf = getHighscorePlayer()
resp = jsonify(conf)
return resp
@app.route('/initGame', methods=['GET', 'POST'])
@basic_auth.required
def initGame():
conf = initializeGame()
if conf == True:
return "true"
else:
return "false"
@app.route('/getKillCount', methods=['GET', 'POST'])
@basic_auth.required
def getKillCount():
kill_count = getNumberOfKills()
return "%i" % kill_count
#========================================================#
#==============ROUTING FOR gamesDAO METHODS==============#
@app.route('/dayNightSwitch', methods=['GET', 'POST'])
@basic_auth.required
def switchGameDayNightState():
return gameDAO.switchDayNight()
@app.route('/getCurrentGame', methods=['GET', 'POST'])
@basic_auth.required
def getCurrentGame():
conf = gameDAO.getGame()
if conf == None:
return "No games"
else:
return jsonify(conf)
@app.route('/isGameActive', methods=['GET', 'POST'])
@basic_auth.required
def getGameActivity():
conf = gamesDAO.getGameActivity()
if conf == True:
return "true"
else:
return "false"
@app.route('/getTimeState', methods=['GET', 'POST'])
@basic_auth.required
def getCurTimeState():
conf = gamesDAO.currentTimeState()
if conf == None:
return "false"
else:
return conf
#========================================================#
#==============ROUTING FOR killDAO METHODS===============#
@app.route('/getAllKills', methods=['GET', 'POST'])
@basic_auth.required
def getTotalKills():
kill_list = killsDAO.getAllKills()
return Response(json.dumps(kill_list), mimetype='application/json')
#========================================================#
#==============ROUTING FOR playerDAO METHODS===============#
@app.route('/updatePlayer/<userID>/<field>/<value>', methods=['GET', 'POST'])
@basic_auth.required
def updateSpecificPlayerParam(userID, field, value):
return playerDAO.updatePlayer(userID, field, value)
@app.route('/createPlayer/<userID>/<latitude>/<longitude>/<alignment>', methods=['GET', 'POST'])
@basic_auth.required
def createPlayer(userID, latitude, longitude, alignment):
return playerDAO.setPlayer(userID, float(latitude), float(longitude), alignment)
@app.route('/getAllPlayers', methods=['GET', 'POST'])
@basic_auth.required
def getAllPlayers():
player_list = playerDAO.getAllPlayers()
jsoned_list = {"response": player_list}
return jsonify(jsoned_list)
@app.route('/getSpecificValue/<userID>/<field>', methods=['GET', 'POST'])
@basic_auth.required
def getSpecificValue(userID, field):
conf = playerDAO.checkValue(userID, field)
if conf == True:
return "true"
else:
return "false"
@app.route('/updateLocation/<userID>/<latitude>/<longitude>', methods=['GET', 'POST'])
@basic_auth.required
def updateLoc(userID, latitude, longitude):
conf = playerDAO.updateLocation(userID, float(latitude), float(longitude))
if conf == True:
return "true"
else:
return "false"
@app.route('/doesPlayerExist/<userID>', methods = ['GET', 'POST'])
@basic_auth.required
def doesPlayerExist(userID):
conf = playerDAO.getPlayer(userID)
if conf == False:
return "false"
else:
return "true"
@app.route('/getPlayer/<userID>', methods = ['GET', 'POST'])
@basic_auth.required
def getSpecificPlayer(userID):
conf = playerDAO.getPlayer(userID)
if conf == False:
return "false"
else:
return jsonify(conf)
#==========================================================#
#==============ROUTING FOR userDAO METHODS===============#
@app.route('/register/<userID>/<password>', methods=['GET', 'POST'])
def regUser(userID, password):
#app.config['BASIC_AUTH_USERNAME'] = userID
#app.config['BASIC_AUTH_PASSWORD'] = password
return usersDAO.registerUser(userID, password)
@app.route('/login/<userID>/<password>', methods=['GET', 'POST'])
def logInUser(userID, password):
conf = usersDAO.loginUser(userID, password)
if(conf == True):
#app.config['BASIC_AUTH_USERNAME'] = userID
#app.config['BASIC_AUTH_PASSWORD'] = password
return "Logged in successfully"
else:
return "Login unsuccessful"
@app.route('/logout/<userID>', methods=['GET', 'POST'])
@basic_auth.required
def logOutUser(userID):
conf = usersDAO.logoutUser(userID)
if conf == True:
return "%s logged out successfully" % userID
#========================================================#
#========SUPER DUPER VOLATILE URL ROUTING THAT SHOULDN'T BE USED OUTSIDE OF TESTING======#
@app.route('/deathToDatabase', methods=['GET', 'POST'])
@basic_auth.required
def deathToDB():
wipeDatabase()
if __name__ == "__main__":
app.run(debug=True)