Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -2,108 +2,70 @@ from flask import Flask, request, jsonify, render_template
|
|
2 |
import random
|
3 |
import string
|
4 |
import time
|
5 |
-
from threading import Timer
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
-
app.config['SECRET_KEY'] = 'your-secret-key' # Replace with a real secret key
|
9 |
|
10 |
# In-memory storage for game sessions
|
11 |
sessions = {}
|
12 |
-
waiting_session = None
|
13 |
|
14 |
def generate_session_id():
|
15 |
return ''.join(random.choices(string.digits, k=4))
|
16 |
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
19 |
session_id = generate_session_id()
|
20 |
while session_id in sessions:
|
21 |
session_id = generate_session_id()
|
22 |
sessions[session_id] = {
|
23 |
-
'players':
|
24 |
'words': {},
|
25 |
-
'current_player_index': 0,
|
26 |
'guesses': set(),
|
27 |
'incorrect_guesses': 0,
|
28 |
-
'
|
29 |
-
'
|
30 |
}
|
31 |
-
|
32 |
-
return session_id
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
if
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
player_index = len(sessions[session_id]['players'])
|
42 |
-
sessions[session_id]['players'].append(player_index)
|
43 |
sessions[session_id]['last_activity'] = time.time()
|
44 |
-
|
45 |
-
if len(sessions[session_id]['players']) == 2:
|
46 |
-
waiting_session = None
|
47 |
-
sessions[session_id]['game_started'] = True
|
48 |
-
|
49 |
-
return session_id, player_index
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
guess = guess.lower()
|
57 |
-
session['guesses'].add(guess)
|
58 |
-
|
59 |
-
if guess not in session['words'][opponent_index]:
|
60 |
-
session['incorrect_guesses'] += 1
|
61 |
-
|
62 |
-
if session['incorrect_guesses'] >= 6:
|
63 |
-
return 'game_over'
|
64 |
-
|
65 |
-
if all(letter in session['guesses'] for letter in session['words'][opponent_index]):
|
66 |
-
return 'winner'
|
67 |
-
|
68 |
-
session['current_player_index'] = opponent_index
|
69 |
-
session['last_activity'] = time.time()
|
70 |
-
return 'continue'
|
71 |
-
|
72 |
-
@app.route('/')
|
73 |
-
def index():
|
74 |
-
return render_template('index.html')
|
75 |
-
|
76 |
-
@app.route('/play', methods=['POST'])
|
77 |
-
def play():
|
78 |
-
session_id, player_index = join_game_session()
|
79 |
return jsonify({
|
80 |
-
'
|
81 |
-
'
|
82 |
-
'status': 'waiting' if len(sessions[session_id]['players']) == 1 else 'ready'
|
83 |
})
|
84 |
|
85 |
-
@app.route('/check_status/<session_id>', methods=['GET'])
|
86 |
-
def check_status(session_id):
|
87 |
-
if session_id in sessions:
|
88 |
-
status = 'ready' if sessions[session_id]['game_started'] else 'waiting'
|
89 |
-
return jsonify({'status': status})
|
90 |
-
return jsonify({'error': 'Session not found'}), 404
|
91 |
-
|
92 |
@app.route('/submit_word', methods=['POST'])
|
93 |
def submit_word():
|
94 |
data = request.json
|
95 |
session_id = data['session_id']
|
96 |
-
|
97 |
word = data['word'].lower()
|
98 |
|
|
|
|
|
99 |
if len(word) != 7:
|
100 |
return jsonify({'error': 'Word must be 7 letters long'}), 400
|
101 |
|
102 |
-
sessions[session_id]['words'][
|
103 |
sessions[session_id]['last_activity'] = time.time()
|
104 |
|
105 |
if len(sessions[session_id]['words']) == 2:
|
106 |
-
sessions[session_id]['
|
107 |
return jsonify({'status': 'game_started'})
|
108 |
return jsonify({'status': 'waiting_for_opponent'})
|
109 |
|
@@ -111,49 +73,45 @@ def submit_word():
|
|
111 |
def guess():
|
112 |
data = request.json
|
113 |
session_id = data['session_id']
|
114 |
-
|
115 |
guess = data['guess'].lower()
|
116 |
|
117 |
-
if
|
|
|
|
|
118 |
return jsonify({'error': 'Not your turn'}), 400
|
119 |
|
120 |
-
|
|
|
|
|
|
|
121 |
|
122 |
-
if
|
123 |
-
|
124 |
-
return jsonify({'status': 'game_over', 'winner': winner})
|
125 |
-
elif result == 'winner':
|
126 |
-
return jsonify({'status': 'game_over', 'winner': player_index})
|
127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
return jsonify({'status': 'continue'})
|
129 |
|
130 |
-
@app.route('/game_state/<session_id
|
131 |
-
def game_state(session_id
|
132 |
if session_id not in sessions:
|
133 |
return jsonify({'error': 'Session not found'}), 404
|
134 |
|
135 |
session = sessions[session_id]
|
136 |
-
player_index = int(player_index)
|
137 |
-
opponent_index = 1 - player_index
|
138 |
-
|
139 |
return jsonify({
|
140 |
-
'
|
141 |
'guesses': list(session['guesses']),
|
142 |
'incorrect_guesses': session['incorrect_guesses'],
|
143 |
-
'
|
144 |
-
|
145 |
})
|
146 |
|
147 |
-
def cleanup_inactive_sessions():
|
148 |
-
global waiting_session
|
149 |
-
current_time = time.time()
|
150 |
-
for session_id in list(sessions.keys()):
|
151 |
-
if current_time - sessions[session_id]['last_activity'] > 60:
|
152 |
-
if session_id == waiting_session:
|
153 |
-
waiting_session = None
|
154 |
-
del sessions[session_id]
|
155 |
-
Timer(60, cleanup_inactive_sessions).start()
|
156 |
-
|
157 |
if __name__ == '__main__':
|
158 |
-
cleanup_inactive_sessions()
|
159 |
app.run(host='0.0.0.0', port=7860)
|
|
|
2 |
import random
|
3 |
import string
|
4 |
import time
|
|
|
5 |
|
6 |
app = Flask(__name__)
|
|
|
7 |
|
8 |
# In-memory storage for game sessions
|
9 |
sessions = {}
|
|
|
10 |
|
11 |
def generate_session_id():
|
12 |
return ''.join(random.choices(string.digits, k=4))
|
13 |
|
14 |
+
@app.route('/')
|
15 |
+
def index():
|
16 |
+
return render_template('index.html')
|
17 |
+
|
18 |
+
@app.route('/play', methods=['POST'])
|
19 |
+
def play():
|
20 |
session_id = generate_session_id()
|
21 |
while session_id in sessions:
|
22 |
session_id = generate_session_id()
|
23 |
sessions[session_id] = {
|
24 |
+
'players': 1,
|
25 |
'words': {},
|
|
|
26 |
'guesses': set(),
|
27 |
'incorrect_guesses': 0,
|
28 |
+
'current_player': None,
|
29 |
+
'last_activity': time.time()
|
30 |
}
|
31 |
+
return jsonify({'session_id': session_id})
|
|
|
32 |
|
33 |
+
@app.route('/join/<session_id>', methods=['POST'])
|
34 |
+
def join(session_id):
|
35 |
+
if session_id not in sessions:
|
36 |
+
return jsonify({'error': 'Session not found'}), 404
|
37 |
+
if sessions[session_id]['players'] == 2:
|
38 |
+
return jsonify({'error': 'Session is full'}), 400
|
39 |
+
sessions[session_id]['players'] += 1
|
|
|
|
|
40 |
sessions[session_id]['last_activity'] = time.time()
|
41 |
+
return jsonify({'status': 'joined', 'players': sessions[session_id]['players']})
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
@app.route('/status/<session_id>', methods=['GET'])
|
44 |
+
def status(session_id):
|
45 |
+
if session_id not in sessions:
|
46 |
+
return jsonify({'error': 'Session not found'}), 404
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
return jsonify({
|
48 |
+
'players': sessions[session_id]['players'],
|
49 |
+
'status': 'ready' if sessions[session_id]['players'] == 2 else 'waiting'
|
|
|
50 |
})
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
@app.route('/submit_word', methods=['POST'])
|
53 |
def submit_word():
|
54 |
data = request.json
|
55 |
session_id = data['session_id']
|
56 |
+
player = data['player']
|
57 |
word = data['word'].lower()
|
58 |
|
59 |
+
if session_id not in sessions:
|
60 |
+
return jsonify({'error': 'Session not found'}), 404
|
61 |
if len(word) != 7:
|
62 |
return jsonify({'error': 'Word must be 7 letters long'}), 400
|
63 |
|
64 |
+
sessions[session_id]['words'][player] = word
|
65 |
sessions[session_id]['last_activity'] = time.time()
|
66 |
|
67 |
if len(sessions[session_id]['words']) == 2:
|
68 |
+
sessions[session_id]['current_player'] = 'player1'
|
69 |
return jsonify({'status': 'game_started'})
|
70 |
return jsonify({'status': 'waiting_for_opponent'})
|
71 |
|
|
|
73 |
def guess():
|
74 |
data = request.json
|
75 |
session_id = data['session_id']
|
76 |
+
player = data['player']
|
77 |
guess = data['guess'].lower()
|
78 |
|
79 |
+
if session_id not in sessions:
|
80 |
+
return jsonify({'error': 'Session not found'}), 404
|
81 |
+
if player != sessions[session_id]['current_player']:
|
82 |
return jsonify({'error': 'Not your turn'}), 400
|
83 |
|
84 |
+
session = sessions[session_id]
|
85 |
+
opponent = 'player2' if player == 'player1' else 'player1'
|
86 |
+
|
87 |
+
session['guesses'].add(guess)
|
88 |
|
89 |
+
if guess not in session['words'][opponent]:
|
90 |
+
session['incorrect_guesses'] += 1
|
|
|
|
|
|
|
91 |
|
92 |
+
if session['incorrect_guesses'] >= 6:
|
93 |
+
return jsonify({'status': 'game_over', 'winner': opponent})
|
94 |
+
|
95 |
+
if all(letter in session['guesses'] for letter in session['words'][opponent]):
|
96 |
+
return jsonify({'status': 'game_over', 'winner': player})
|
97 |
+
|
98 |
+
session['current_player'] = opponent
|
99 |
+
session['last_activity'] = time.time()
|
100 |
return jsonify({'status': 'continue'})
|
101 |
|
102 |
+
@app.route('/game_state/<session_id>')
|
103 |
+
def game_state(session_id):
|
104 |
if session_id not in sessions:
|
105 |
return jsonify({'error': 'Session not found'}), 404
|
106 |
|
107 |
session = sessions[session_id]
|
|
|
|
|
|
|
108 |
return jsonify({
|
109 |
+
'current_player': session['current_player'],
|
110 |
'guesses': list(session['guesses']),
|
111 |
'incorrect_guesses': session['incorrect_guesses'],
|
112 |
+
'words': {player: ''.join(['_' if letter not in session['guesses'] else letter for letter in word])
|
113 |
+
for player, word in session['words'].items()}
|
114 |
})
|
115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
if __name__ == '__main__':
|
|
|
117 |
app.run(host='0.0.0.0', port=7860)
|