Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,19 @@
|
|
|
|
1 |
import random
|
2 |
import string
|
3 |
-
from flask import Flask, request, jsonify, render_template
|
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 |
-
def generate_player_id():
|
18 |
-
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
|
19 |
-
|
20 |
def create_game_session():
|
21 |
global waiting_session
|
22 |
session_id = generate_session_id()
|
@@ -25,10 +22,11 @@ def create_game_session():
|
|
25 |
sessions[session_id] = {
|
26 |
'players': [],
|
27 |
'words': {},
|
28 |
-
'
|
29 |
'guesses': set(),
|
30 |
'incorrect_guesses': 0,
|
31 |
-
'last_activity': time.time()
|
|
|
32 |
}
|
33 |
waiting_session = session_id
|
34 |
return session_id
|
@@ -37,19 +35,39 @@ def join_game_session():
|
|
37 |
global waiting_session
|
38 |
if waiting_session and waiting_session in sessions:
|
39 |
session_id = waiting_session
|
40 |
-
is_new_session = False
|
41 |
else:
|
42 |
session_id = create_game_session()
|
43 |
-
is_new_session = True
|
44 |
|
45 |
-
|
46 |
-
sessions[session_id]['players'].append(
|
47 |
sessions[session_id]['last_activity'] = time.time()
|
48 |
|
49 |
if len(sessions[session_id]['players']) == 2:
|
50 |
waiting_session = None
|
|
|
51 |
|
52 |
-
return session_id,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
@app.route('/')
|
55 |
def index():
|
@@ -57,18 +75,17 @@ def index():
|
|
57 |
|
58 |
@app.route('/play', methods=['POST'])
|
59 |
def play():
|
60 |
-
session_id,
|
61 |
-
status = 'waiting' if is_new_session else 'ready'
|
62 |
return jsonify({
|
63 |
'session_id': session_id,
|
64 |
-
'
|
65 |
-
'status':
|
66 |
})
|
67 |
|
68 |
@app.route('/check_status/<session_id>', methods=['GET'])
|
69 |
def check_status(session_id):
|
70 |
if session_id in sessions:
|
71 |
-
status = 'ready' if
|
72 |
return jsonify({'status': status})
|
73 |
return jsonify({'error': 'Session not found'}), 404
|
74 |
|
@@ -76,21 +93,66 @@ def check_status(session_id):
|
|
76 |
def submit_word():
|
77 |
data = request.json
|
78 |
session_id = data['session_id']
|
79 |
-
|
80 |
word = data['word'].lower()
|
81 |
|
82 |
if len(word) != 7:
|
83 |
return jsonify({'error': 'Word must be 7 letters long'}), 400
|
84 |
|
85 |
-
sessions[session_id]['words'][
|
86 |
sessions[session_id]['last_activity'] = time.time()
|
87 |
|
88 |
if len(sessions[session_id]['words']) == 2:
|
89 |
-
sessions[session_id]['
|
90 |
return jsonify({'status': 'game_started'})
|
91 |
return jsonify({'status': 'waiting_for_opponent'})
|
92 |
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
if __name__ == '__main__':
|
96 |
cleanup_inactive_sessions()
|
|
|
1 |
+
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 |
def create_game_session():
|
18 |
global waiting_session
|
19 |
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 |
+
'last_activity': time.time(),
|
29 |
+
'game_started': False
|
30 |
}
|
31 |
waiting_session = session_id
|
32 |
return session_id
|
|
|
35 |
global waiting_session
|
36 |
if waiting_session and waiting_session in sessions:
|
37 |
session_id = waiting_session
|
|
|
38 |
else:
|
39 |
session_id = create_game_session()
|
|
|
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 |
+
def update_game_state(session_id, guess):
|
52 |
+
session = sessions[session_id]
|
53 |
+
current_player_index = session['current_player_index']
|
54 |
+
opponent_index = 1 - current_player_index
|
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():
|
|
|
75 |
|
76 |
@app.route('/play', methods=['POST'])
|
77 |
def play():
|
78 |
+
session_id, player_index = join_game_session()
|
|
|
79 |
return jsonify({
|
80 |
'session_id': session_id,
|
81 |
+
'player_index': player_index,
|
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 |
|
|
|
93 |
def submit_word():
|
94 |
data = request.json
|
95 |
session_id = data['session_id']
|
96 |
+
player_index = data['player_index']
|
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'][player_index] = word
|
103 |
sessions[session_id]['last_activity'] = time.time()
|
104 |
|
105 |
if len(sessions[session_id]['words']) == 2:
|
106 |
+
sessions[session_id]['game_started'] = True
|
107 |
return jsonify({'status': 'game_started'})
|
108 |
return jsonify({'status': 'waiting_for_opponent'})
|
109 |
|
110 |
+
@app.route('/guess', methods=['POST'])
|
111 |
+
def guess():
|
112 |
+
data = request.json
|
113 |
+
session_id = data['session_id']
|
114 |
+
player_index = int(data['player_index'])
|
115 |
+
guess = data['guess'].lower()
|
116 |
+
|
117 |
+
if player_index != sessions[session_id]['current_player_index']:
|
118 |
+
return jsonify({'error': 'Not your turn'}), 400
|
119 |
+
|
120 |
+
result = update_game_state(session_id, guess)
|
121 |
+
|
122 |
+
if result == 'game_over':
|
123 |
+
winner = 1 - sessions[session_id]['current_player_index']
|
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>/<player_index>')
|
131 |
+
def game_state(session_id, player_index):
|
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 |
+
'current_player_index': session['current_player_index'],
|
141 |
+
'guesses': list(session['guesses']),
|
142 |
+
'incorrect_guesses': session['incorrect_guesses'],
|
143 |
+
'player_word': ''.join(['_' if letter not in session['guesses'] else letter for letter in session['words'].get(player_index, '')]),
|
144 |
+
'opponent_word': ''.join(['_' if letter not in session['guesses'] else letter for letter in session['words'].get(opponent_index, '')])
|
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()
|