-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
363 lines (306 loc) · 10.5 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
from flask import Flask, jsonify, request
from flask_cors import CORS
from model.game import Game
from controller.gui_controller import GUIController
from firebaseDB.db_facade import database
from time import sleep
app = Flask(__name__)
CORS(app)
game = Game(8, "", "")
controller = GUIController(game)
db = database()
ai_Flag = False
database_Flag = False
@app.route('/')
def main_page():
"""
Serves the main page of the application.
Returns:
str: A simple string indicating the user has reached the main page.
"""
return "Main Page"
@app.route('/hello')
def hello():
"""
A simple endpoint for testing that the service is operational.
Returns:
str: A greeting string 'Hello, World!'
"""
return 'Hello, World!'
# Returns the size of the board
@app.route('/board-size')
def get_board_size():
"""
Retrieves the current size of the game board.
Returns:
JSON: A JSON object containing the size of the board.
"""
size = game.get_board_size()
return jsonify({'size': size})
# Returns the player's current score
@app.route('/scores')
def get_scores():
"""
Retrieves the current scores for both players in the game.
Returns:
tuple: A tuple containing the scores for player 1 and player 2.
"""
p1 = game.get_player_score(game.player1)
p2 = game.get_player_score(game.player2)
return p1, p2
# Swaps player's turn, returns the new current player
@app.route('/pass-turn')
def pass_turn():
"""
Passes the turn to the next player and returns the current player after the turn is passed.
Returns:
str: The string representation of the current player after passing the turn.
"""
controller.pass_turn()
return get_current_player()
# Returns the player whose currently making a move
@app.route('/current-player')
def get_current_player():
"""
Retrieves the current player of the game.
Returns:
str: The string representation of the current player.
"""
return controller.player_to_str(game.current_player)
@app.route('/board')
def get_board_state():
"""
Retrieves the entire state of the game board.
Returns:
list: A nested list representing the state of each cell on the game board.
"""
size = game.get_board_size()
board = [[controller.get_cell(row, col) for col in range(size)] for row in range(size)]
# return jsonify({'board': board})
return board
# Calls controller to execute a move
@app.route('/execute-move/<row>/<col>')
def execute_move(row, col):
"""
Executes a move on the board at the specified row and column indices.
Parameters:
row (str): The row index where the move is to be made.
col (str): The column index where the move is to be made.
Returns:
str: A message indicating that the move has been executed and which player made the move.
"""
row, col = int(row), int(col)
current = game.get_current_player()
print("database flag:", database_Flag)
print(db.get_username_current_player(game.current_player.id))
if database_Flag:
if game.current_player.id == db.loginPlayerID:
game.deserialize_game_state(db.get_game_state(db.gameID))
controller.execute_move(row, col)
print("updating database")
db.update_game_state(db.gameID, game.serialize_game_state())
game.deserialize_game_state(db.get_game_state(db.gameID))
else:
controller.execute_move(row, col)
return f'Move Executed {row} {col} by {controller.player_to_str(current)}'
# Triggers the AI to execute a move
@app.route('/trigger-ai')
def trigger_AI():
"""
Triggers the AI to execute a move if enabled.
Returns:
str: A message indicating the result of the AI's action or stating that the AI is disabled.
"""
row, col = controller.get_AI_move()
if row == -1 or col == -1:
return f"AI Disabled {controller.player_to_str(game.current_player)}'s turn"
current = game.get_current_player()
sleep(1)
controller.execute_move(row, col)
return f"AI Move Executed {row} {col} by {controller.player_to_str(current)}"
# Returns True if the AI feature is enabled
@app.route('/ai-status')
def get_ai_status():
"""
Checks if the AI feature is enabled in the game.
Returns:
bool: True if AI is enabled, False otherwise.
"""
return controller.aiEnabled
# Toggles AI Status
@app.route('/toggle-ai')
def toggle_ai_status():
"""
Toggles the AI feature on or off and resets the game to its initial state.
Returns:
str: A message indicating the AI status has been updated.
"""
controller.toggle_ai_status()
controller.reset_game()
return 'AI Status Updated'
# Restarts the game
@app.route('/reset')
def reset_game():
"""
Resets the game to its initial state.
Returns:
str: A message indicating the game has been reset.
"""
controller.reset_game()
return "Game Reset"
@app.route('/default-settings')
def default_settings():
"""
Resets the game to default settings, disabling AI and any database flags.
Returns:
str: A simple OK message indicating successful resetting to default settings.
"""
global database_Flag
if controller.aiEnabled:
controller.toggle_ai_status()
database_Flag = False
return 'OK'
@app.route('/message')
def get_message():
"""
Generates a message based on the current game state, including game over conditions.
Returns:
str: A message reflecting the current state or outcome of the game.
"""
if game.game_over():
if controller.get_winner():
winner = controller.player_to_str(controller.get_winner())
winner_id = controller.get_winner().id
loser_id = controller.get_loser().id
db.update_ratings_win(winner_id, loser_id, game.get_player_score(winner))
return f'{winner} wins!' if winner else "Draw."
else:
db.update_ratings_draw(game.player1.id, game.player2.id)
db.delete_game(db.gameID)
if controller.is_AI_making_move():
return "AI is making a move..."
return f"{get_current_player()}'s turn"
# Returns the current state of the game
@app.route('/game-state')
def get_game_state():
"""
Retrieves the complete current state of the game, including player scores, the current player, and board state.
Returns:
JSON: A JSON object containing comprehensive game state information.
"""
p1, p2 = get_scores()
return jsonify({
'currentPlayer': controller.player_to_str(game.get_current_player()),
'scores': {"player1": p1, "player2": p2},
'message': get_message(),
'aiStatus': get_ai_status(),
'board': get_board_state()
})
# Logs in user based on the entered username and password
@app.route('/login/<username>/<password>')
def login(username, password):
"""
Attempts to log in a user with the provided username and password.
Parameters:
username (str): The username of the user attempting to log in.
password (str): The password of the user attempting to log in.
Returns:
JSON: A JSON object indicating the result of the login attempt and any associated messages.
"""
loginResult = db.login_user(username, password)
user_message = ''
if loginResult:
user_message = "Login Successful"
else:
user_message = "Login Unsuccessful"
print(user_message)
print(loginResult)
return jsonify({'auth': loginResult, 'message': user_message})
# Registers user in the database based on the entered username and password
@app.route('/register/<username>/<password>')
def register(username, password):
"""
Registers a new user with the provided username and password.
Parameters:
username (str): The username for the new user.
password (str): The password for the new user.
Returns:
JSON: A JSON object indicating the result of the registration attempt and any associated messages.
"""
registerResult = db.create_user(username,password)
user_message = ''
if registerResult:
user_message = "Registration Successful"
else:
user_message = "User already exists"
print(user_message)
print(registerResult)
return jsonify({'regAuth': registerResult, 'message': user_message})
# Returns a list of all registered users
@app.route('/users')
def get_users():
"""
Retrieves a list of all registered users.
Returns:
JSON: A JSON object containing a list of registered users.
"""
users = db.list_current_users()
return jsonify({'users': users})
# Starts a game with one human player and the AI
@app.route('/play-ai')
def playAI():
"""
Starts a game against the AI.
Returns:
None
"""
if not controller.aiEnabled:
controller.toggle_ai_status()
return
# Starts a game with two human players
@app.route('/play-user/<username>')
def playUser(username):
"""
Initiates a game against another human player identified by username.
Parameters:
username (str): The username of the opponent player.
Returns:
str: A simple OK message indicating that the game setup is complete.
"""
global database_Flag
database_Flag = True
if controller.aiEnabled == True:
controller.aiEnabled = False
db.set_opponent(username)
if not db.check_rating_exists(db.loginPlayerID):
db.create_rating(db.loginPlayerID)
if not db.check_rating_exists(db.OpponentPlayerID):
db.create_rating(db.OpponentPlayerID)
p = db.check_game_exists(db.loginPlayerID, db.OpponentPlayerID)
q = db.check_game_exists(db.OpponentPlayerID, db.loginPlayerID)
if p:
print("loading game 1")
game_state = db.get_game_state(p)
game.deserialize_game_state(game_state)
db.gameID = p
game.player1.id = db.loginPlayerID
game.player2.id = db.OpponentPlayerID
elif q:
print("loading game 2")
game_state = db.get_game_state(q)
db.gameID = q
game.deserialize_game_state(game_state)
game.player2.id = db.loginPlayerID
game.player1.id = db.OpponentPlayerID
else:
print("creating game")
db.create_game(game.serialize_game_state())
game.player1.id = db.loginPlayerID
game.player2.id = db.OpponentPlayerID
print("player1ID: ", game.player1.id)
print("player2ID: ", game.player2.id)
print("currentPlayerID: ", game.current_player.id)
print(database_Flag)
return "ok"
if __name__ == '__main__':
app.run(debug=True)