Spaces:
Paused
Paused
from flask import Flask, request, jsonify, render_template | |
import random | |
import string | |
import time | |
from threading import Timer | |
app = Flask(__name__) | |
app.config['SECRET_KEY'] = 'your-secret-key' # Replace with a real secret key | |
# In-memory storage for game sessions | |
sessions = {} | |
waiting_session = None | |
def generate_session_id(): | |
return ''.join(random.choices(string.digits, k=4)) | |
def create_game_session(): | |
global waiting_session | |
session_id = generate_session_id() | |
while session_id in sessions: | |
session_id = generate_session_id() | |
sessions[session_id] = { | |
'players': [], | |
'words': {}, | |
'current_player_index': 0, | |
'guesses': set(), | |
'incorrect_guesses': 0, | |
'last_activity': time.time(), | |
'game_started': False | |
} | |
waiting_session = session_id | |
return session_id | |
def join_game_session(): | |
global waiting_session | |
if waiting_session and waiting_session in sessions: | |
session_id = waiting_session | |
else: | |
session_id = create_game_session() | |
player_index = len(sessions[session_id]['players']) | |
sessions[session_id]['players'].append(player_index) | |
sessions[session_id]['last_activity'] = time.time() | |
if len(sessions[session_id]['players']) == 2: | |
waiting_session = None | |
sessions[session_id]['game_started'] = True | |
return session_id, player_index | |
def update_game_state(session_id, guess): | |
session = sessions[session_id] | |
current_player_index = session['current_player_index'] | |
opponent_index = 1 - current_player_index | |
guess = guess.lower() | |
session['guesses'].add(guess) | |
if guess not in session['words'][opponent_index]: | |
session['incorrect_guesses'] += 1 | |
if session['incorrect_guesses'] >= 6: | |
return 'game_over' | |
if all(letter in session['guesses'] for letter in session['words'][opponent_index]): | |
return 'winner' | |
session['current_player_index'] = opponent_index | |
session['last_activity'] = time.time() | |
return 'continue' | |
def index(): | |
return render_template('index.html') | |
def play(): | |
session_id, player_index = join_game_session() | |
return jsonify({ | |
'session_id': session_id, | |
'player_index': player_index, | |
'status': 'waiting' if len(sessions[session_id]['players']) == 1 else 'ready' | |
}) | |
def check_status(session_id): | |
if session_id in sessions: | |
status = 'ready' if sessions[session_id]['game_started'] else 'waiting' | |
return jsonify({'status': status}) | |
return jsonify({'error': 'Session not found'}), 404 | |
def submit_word(): | |
data = request.json | |
session_id = data['session_id'] | |
player_index = data['player_index'] | |
word = data['word'].lower() | |
if len(word) != 7: | |
return jsonify({'error': 'Word must be 7 letters long'}), 400 | |
sessions[session_id]['words'][player_index] = word | |
sessions[session_id]['last_activity'] = time.time() | |
if len(sessions[session_id]['words']) == 2: | |
sessions[session_id]['game_started'] = True | |
return jsonify({'status': 'game_started'}) | |
return jsonify({'status': 'waiting_for_opponent'}) | |
def guess(): | |
data = request.json | |
session_id = data['session_id'] | |
player_index = int(data['player_index']) | |
guess = data['guess'].lower() | |
if player_index != sessions[session_id]['current_player_index']: | |
return jsonify({'error': 'Not your turn'}), 400 | |
result = update_game_state(session_id, guess) | |
if result == 'game_over': | |
winner = 1 - sessions[session_id]['current_player_index'] | |
return jsonify({'status': 'game_over', 'winner': winner}) | |
elif result == 'winner': | |
return jsonify({'status': 'game_over', 'winner': player_index}) | |
return jsonify({'status': 'continue'}) | |
def game_state(session_id, player_index): | |
if session_id not in sessions: | |
return jsonify({'error': 'Session not found'}), 404 | |
session = sessions[session_id] | |
player_index = int(player_index) | |
opponent_index = 1 - player_index | |
return jsonify({ | |
'current_player_index': session['current_player_index'], | |
'guesses': list(session['guesses']), | |
'incorrect_guesses': session['incorrect_guesses'], | |
'player_word': ''.join(['_' if letter not in session['guesses'] else letter for letter in session['words'].get(player_index, '')]), | |
'opponent_word': ''.join(['_' if letter not in session['guesses'] else letter for letter in session['words'].get(opponent_index, '')]) | |
}) | |
def cleanup_inactive_sessions(): | |
global waiting_session | |
current_time = time.time() | |
for session_id in list(sessions.keys()): | |
if current_time - sessions[session_id]['last_activity'] > 60: | |
if session_id == waiting_session: | |
waiting_session = None | |
del sessions[session_id] | |
Timer(60, cleanup_inactive_sessions).start() | |
if __name__ == '__main__': | |
cleanup_inactive_sessions() | |
app.run(host='0.0.0.0', port=7860) |