Spaces:
Paused
Paused
File size: 3,894 Bytes
70a9ad8 7ef7190 ce1dd56 7ef7190 70a9ad8 7ef7190 aafbb78 7ef7190 aafbb78 7ef7190 ce1dd56 aafbb78 7ef7190 aafbb78 7ef7190 aafbb78 ce1dd56 aafbb78 70a9ad8 aafbb78 ce1dd56 aafbb78 ce1dd56 7ef7190 aafbb78 7ef7190 ce1dd56 aafbb78 7ef7190 ce1dd56 aafbb78 ce1dd56 7ef7190 aafbb78 7ef7190 70a9ad8 aafbb78 70a9ad8 aafbb78 70a9ad8 aafbb78 70a9ad8 aafbb78 70a9ad8 aafbb78 70a9ad8 aafbb78 70a9ad8 aafbb78 70a9ad8 aafbb78 70a9ad8 7ef7190 ce1dd56 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
from flask import Flask, request, jsonify, render_template
import random
import string
import time
app = Flask(__name__)
# In-memory storage for game sessions
sessions = {}
def generate_session_id():
return ''.join(random.choices(string.digits, k=4))
@app.route('/')
def index():
return render_template('index.html')
@app.route('/play', methods=['POST'])
def play():
session_id = generate_session_id()
while session_id in sessions:
session_id = generate_session_id()
sessions[session_id] = {
'players': 1,
'words': {},
'guesses': set(),
'incorrect_guesses': 0,
'current_player': None,
'last_activity': time.time()
}
return jsonify({'session_id': session_id})
@app.route('/join/<session_id>', methods=['POST'])
def join(session_id):
if session_id not in sessions:
return jsonify({'error': 'Session not found'}), 404
if sessions[session_id]['players'] == 2:
return jsonify({'error': 'Session is full'}), 400
sessions[session_id]['players'] += 1
sessions[session_id]['last_activity'] = time.time()
return jsonify({'status': 'joined', 'players': sessions[session_id]['players']})
@app.route('/status/<session_id>', methods=['GET'])
def status(session_id):
if session_id not in sessions:
return jsonify({'error': 'Session not found'}), 404
return jsonify({
'players': sessions[session_id]['players'],
'status': 'ready' if sessions[session_id]['players'] == 2 else 'waiting'
})
@app.route('/submit_word', methods=['POST'])
def submit_word():
data = request.json
session_id = data['session_id']
player = data['player']
word = data['word'].lower()
if session_id not in sessions:
return jsonify({'error': 'Session not found'}), 404
if len(word) != 7:
return jsonify({'error': 'Word must be 7 letters long'}), 400
sessions[session_id]['words'][player] = word
sessions[session_id]['last_activity'] = time.time()
if len(sessions[session_id]['words']) == 2:
sessions[session_id]['current_player'] = 'player1'
return jsonify({'status': 'game_started'})
return jsonify({'status': 'waiting_for_opponent'})
@app.route('/guess', methods=['POST'])
def guess():
data = request.json
session_id = data['session_id']
player = data['player']
guess = data['guess'].lower()
if session_id not in sessions:
return jsonify({'error': 'Session not found'}), 404
if player != sessions[session_id]['current_player']:
return jsonify({'error': 'Not your turn'}), 400
session = sessions[session_id]
opponent = 'player2' if player == 'player1' else 'player1'
session['guesses'].add(guess)
if guess not in session['words'][opponent]:
session['incorrect_guesses'] += 1
if session['incorrect_guesses'] >= 6:
return jsonify({'status': 'game_over', 'winner': opponent})
if all(letter in session['guesses'] for letter in session['words'][opponent]):
return jsonify({'status': 'game_over', 'winner': player})
session['current_player'] = opponent
session['last_activity'] = time.time()
return jsonify({'status': 'continue'})
@app.route('/game_state/<session_id>')
def game_state(session_id):
if session_id not in sessions:
return jsonify({'error': 'Session not found'}), 404
session = sessions[session_id]
return jsonify({
'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()}
})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860) |