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