ParthCodes commited on
Commit
f8de470
·
1 Parent(s): b85cffc

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +80 -7
main.py CHANGED
@@ -1,16 +1,89 @@
1
- from flask import Flask, request
 
 
2
  from flask_cors import CORS
3
 
4
-
5
  app = Flask(__name__)
6
-
7
  CORS(app)
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
 
10
 
11
- @app.route('/', methods=['GET'])
12
- def main():
13
- return {'message': 'Hello, World'}
14
 
15
  if __name__ == '__main__':
16
- app.run()
 
1
+ from flask import Flask, request, jsonify
2
+ import random
3
+ import string
4
  from flask_cors import CORS
5
 
 
6
  app = Flask(__name__)
 
7
  CORS(app)
8
 
9
+ # Store room information in memory (replace with a database in production).
10
+ rooms = {}
11
+
12
+ started_rooms = {}
13
+
14
+ def generate_room_code():
15
+ # Generate a random 6-character room code.
16
+ return ''.join(random.choices(string.ascii_uppercase, k=6))
17
+
18
+ @app.route('/create-room', methods=['POST'])
19
+ def create_room():
20
+ room_code = generate_room_code()
21
+ rooms[room_code] = {'players': [], 'started': False}
22
+
23
+ player_name = request.json.get('name')
24
+ if not player_name:
25
+ return jsonify({'error': 'Player name is required'})
26
+
27
+ # Add the player to the room.
28
+ player_id = len(rooms[room_code]['players']) + 1
29
+ rooms[room_code]['players'].append({'id': player_id, 'name': player_name})
30
+
31
+ return jsonify({'room_code': room_code})
32
+
33
+ @app.route('/join-room/<room_code>', methods=['POST', 'GET'])
34
+ def join_room(room_code):
35
+ if request.method == 'POST':
36
+ if room_code not in rooms:
37
+ return jsonify({'error': 'Room not found'}), 404
38
+
39
+ if len(rooms[room_code]['players']) >= 2:
40
+ return jsonify({'error': 'Room is full'})
41
+
42
+ player_name = request.json.get('name')
43
+ if not player_name:
44
+ return jsonify({'error': 'Player name is required'})
45
+
46
+ # Add the player to the room.
47
+ player_id = len(rooms[room_code]['players']) + 1
48
+ rooms[room_code]['players'].append({'id': player_id, 'name': player_name})
49
+
50
+ return jsonify({'player_id': player_id, 'players': rooms[room_code]['players']})
51
+
52
+ if request.method == 'GET':
53
+ if room_code not in rooms:
54
+ return jsonify({'error': 'Room not found'}), 404
55
+
56
+ ready = False
57
+
58
+ if len(rooms[room_code]['players']) == 2:
59
+ ready = True
60
+
61
+ return jsonify({'players': rooms[room_code]['players'], 'ready': ready, 'started': rooms[room_code]['started']})
62
+
63
+
64
+ @app.route('/start-room/<room_code>', methods=['POST'])
65
+ def start_room(room_code):
66
+ if room_code not in rooms:
67
+ return jsonify({'error': 'Room not found'}), 404
68
+
69
+ rooms[room_code]['started'] = True
70
+
71
+ return jsonify({'success': True})
72
+
73
+
74
+
75
+ @app.route('/leave-room/<room_code>/<int:player_id>', methods=['POST'])
76
+ def leave_room(room_code, player_id):
77
+ if room_code not in rooms:
78
+ return jsonify({'error': 'Room not found'}), 404
79
+
80
+ rooms[room_code]['players'] = [player for player in rooms[room_code]['players'] if player['id'] != player_id]
81
+
82
+ if not rooms[room_code]['players']:
83
+ del rooms[room_code]
84
 
85
+ return jsonify({'message': 'Player has left the room'})
86
 
 
 
 
87
 
88
  if __name__ == '__main__':
89
+ app.run(debug=True)