-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflaskConnector.py
78 lines (59 loc) · 2.53 KB
/
flaskConnector.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
from flask import Flask, request, jsonify
from flask_cors import CORS
import main # Import your game logic
app = Flask(__name__)
CORS(app)
# Store chat messages (or game state) temporarily
messages = []
# Initial button list (before game starts)
button_list = [{"label": "Start Game", "action": "start_game", "description": "Start the game session."},
{"label": "Add Player", "action": "add_player", "description": "Add a player to the game."}]
@app.route('/RomanticRoyale.html')
def home():
with open("main.html") as f:
html = f.read()
return html
@app.route("/send_game_update", methods=["POST"])
def send_game_update():
"""Receive data from the game and update the front-end."""
data = request.json
game_message:str = data.get("message", "")
# Save the game message to be displayed in the front-end
messages.append({"sender": "You", "message": game_message})
return jsonify({"response": "Game update received"})
@app.route("/send_response_update", methods=["POST"])
def send_response_update():
"""Receive data from the game and update the front-end."""
data = request.json
game_message:str = data.get("message", "")
# Save the game message to be displayed in the front-end
messages.append({"sender": "Your Date", "message": game_message})
return jsonify({"response": "Game update received"})
@app.route("/get_messages", methods=["GET"])
def get_messages():
"""Fetch chat messages to send to the front-end."""
return jsonify({"messages": messages})
@app.route("/get_buttons", methods=["GET"])
def get_buttons():
"""Fetch available buttons to send to the front-end."""
return jsonify({"buttons": button_list})
@app.route("/button_click", methods=["POST"])
def button_click():
"""Receive the button click action and forward it to the game."""
data = request.json
button_action = data.get("action", "")
# Handle the button click (send action to game)
game_response = main.handle_button_action(button_action)
# Optionally, send the updated response to the front-end
#messages.append({"sender": "You", "message": game_response})
return jsonify({"response": game_response})
# Endpoint to update buttons when game state changes
@app.route("/update_buttons", methods=["POST"])
def update_buttons():
"""Update the button list after the game starts."""
global button_list
data = request.json
button_list = data.get("buttons", [])
return jsonify({"response": "Button list updated"})
if __name__ == "__main__":
app.run(debug=True)