Spaces:
Paused
Paused
| import random | |
| import string | |
| from flask import Flask, request, jsonify, render_template | |
| 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 and player data | |
| sessions = {} | |
| players = {} | |
| def generate_session_id(): | |
| return ''.join(random.choices(string.digits, k=4)) | |
| def generate_player_id(): | |
| return ''.join(random.choices(string.ascii_uppercase + string.digits, k=6)) | |
| def create_game_session(): | |
| session_id = generate_session_id() | |
| while session_id in sessions: | |
| session_id = generate_session_id() | |
| sessions[session_id] = { | |
| 'players': [], | |
| 'words': {}, | |
| 'current_player': None, | |
| 'guesses': set(), | |
| 'incorrect_guesses': 0 | |
| } | |
| return session_id | |
| def join_game_session(session_id): | |
| if session_id not in sessions: | |
| return None | |
| player_id = generate_player_id() | |
| sessions[session_id]['players'].append(player_id) | |
| players[player_id] = {'session_id': session_id} | |
| return player_id | |
| def update_game_state(session_id, guess): | |
| session = sessions[session_id] | |
| current_player = session['current_player'] | |
| opponent = [p for p in session['players'] if p != current_player][0] | |
| guess = guess.lower() | |
| session['guesses'].add(guess) | |
| if guess not in session['words'][opponent]: | |
| session['incorrect_guesses'] += 1 | |
| if session['incorrect_guesses'] >= 6: | |
| return 'game_over' | |
| if all(letter in session['guesses'] for letter in session['words'][opponent]): | |
| return 'winner' | |
| session['current_player'] = opponent | |
| return 'continue' | |
| def check_win_condition(session_id): | |
| session = sessions[session_id] | |
| for player in session['players']: | |
| if all(letter in session['guesses'] for letter in session['words'][player]): | |
| return player | |
| return None | |
| def index(): | |
| return render_template('index.html') | |
| def play(): | |
| session_id = create_game_session() | |
| player_id = join_game_session(session_id) | |
| return jsonify({'session_id': session_id, 'player_id': player_id}) | |
| def join(session_id): | |
| player_id = join_game_session(session_id) | |
| if player_id: | |
| if len(sessions[session_id]['players']) == 2: | |
| sessions[session_id]['current_player'] = sessions[session_id]['players'][0] | |
| return jsonify({'player_id': player_id}) | |
| return jsonify({'error': 'Session not found or full'}), 404 | |
| def submit_word(): | |
| data = request.json | |
| session_id = data['session_id'] | |
| player_id = data['player_id'] | |
| word = data['word'].lower() | |
| if len(word) != 7: | |
| return jsonify({'error': 'Word must be 7 letters long'}), 400 | |
| sessions[session_id]['words'][player_id] = word | |
| if len(sessions[session_id]['words']) == 2: | |
| return jsonify({'status': 'game_started'}) | |
| return jsonify({'status': 'waiting_for_opponent'}) | |
| def guess(): | |
| data = request.json | |
| session_id = data['session_id'] | |
| player_id = data['player_id'] | |
| guess = data['guess'].lower() | |
| if player_id != sessions[session_id]['current_player']: | |
| return jsonify({'error': 'Not your turn'}), 400 | |
| result = update_game_state(session_id, guess) | |
| if result == 'game_over': | |
| winner = check_win_condition(session_id) | |
| return jsonify({'status': 'game_over', 'winner': winner}) | |
| elif result == 'winner': | |
| return jsonify({'status': 'game_over', 'winner': player_id}) | |
| return jsonify({'status': 'continue'}) | |
| def game_state(session_id): | |
| if session_id not in sessions: | |
| return jsonify({'error': 'Session not found'}), 404 | |
| session = sessions[session_id] | |
| return jsonify({ | |
| 'players': session['players'], | |
| 'current_player': session['current_player'], | |
| 'guesses': list(session['guesses']), | |
| 'incorrect_guesses': session['incorrect_guesses'], | |
| 'words': {player: ''.join(['_' if letter not in session['guesses'] else letter for letter in word]) | |
| for player, word in session['words'].items()} | |
| }) | |
| def cleanup_inactive_sessions(): | |
| current_time = time.time() | |
| for session_id in list(sessions.keys()): | |
| if current_time - sessions[session_id].get('last_activity', 0) > 60: | |
| 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) | |