File size: 5,438 Bytes
5b97c22
058bc84
0f05b68
5b97c22
 
 
 
 
 
 
 
 
 
0f05b68
5b97c22
0f05b68
 
 
 
 
 
 
 
 
 
 
 
 
5b97c22
 
 
 
0f05b68
058bc84
5b97c22
 
 
0f05b68
5b97c22
 
0f05b68
 
 
5b97c22
058bc84
 
 
 
 
 
 
 
 
 
 
 
 
 
0f05b68
058bc84
 
5b97c22
 
 
 
0f05b68
5b97c22
 
 
 
0f05b68
 
 
 
 
 
 
 
 
5b97c22
 
 
 
 
058bc84
0f05b68
 
5b97c22
0f05b68
5b97c22
0f05b68
 
058bc84
0f05b68
 
5b97c22
 
 
 
058bc84
0f05b68
 
 
 
 
 
 
 
 
 
 
 
 
 
5b97c22
058bc84
5b97c22
 
 
 
058bc84
 
5b97c22
 
058bc84
 
 
 
0f05b68
058bc84
5b97c22
0f05b68
058bc84
 
0f05b68
058bc84
 
0f05b68
058bc84
 
 
5b97c22
 
0f05b68
5b97c22
 
058bc84
5b97c22
0f05b68
 
058bc84
 
5b97c22
 
0f05b68
 
 
 
 
 
5b97c22
 
0f05b68
 
 
 
 
 
 
 
 
 
 
5b97c22
0f05b68
 
 
 
 
 
 
5b97c22
 
 
0f05b68
 
 
 
 
 
 
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from flask import Flask, render_template, request, jsonify
from flask_socketio import SocketIO, emit, join_room
from flask_cors import CORS
import os
import requests
import json
import uuid
from datetime import datetime
from dotenv import load_dotenv
import logging
from werkzeug.utils import secure_filename
import random

# Initialize Flask with CORS
app = Flask(__name__)
CORS(app)
app.config['SECRET_KEY'] = os.urandom(24)
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024

# Configure Socket.IO for Hugging Face Spaces
socketio = SocketIO(app,
    cors_allowed_origins="*",
    async_mode='eventlet',
    logger=True,
    engineio_logger=True
)

# Load environment variables
load_dotenv()
MISTRAL_API_KEY = os.getenv('MISTRAL_API_KEY')
ELEVENLABS_API_KEY = os.getenv('ELEVENLABS_API_KEY')

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class GameState:
    """Manages all active game sessions with WebSocket room support"""
    
    def __init__(self):
        self.games = {}
        self.cleanup_interval = 3600

    def create_game(self):
        game_id = str(uuid.uuid4())
        self.games[game_id] = {
            'players': [],
            'current_phase': 'setup',
            'recordings': {},
            'impostor': None,
            'votes': {},
            'question': None,
            'impostor_answer': None,
            'modified_recording': None,
            'round_number': 1,
            'start_time': datetime.now().isoformat(),
            'completed_rounds': [],
            'score': {'impostor_wins': 0, 'player_wins': 0},
            'socket_room': f'game_{game_id}'
        }
        return game_id

    def cleanup_inactive_games(self):
        current_time = datetime.now()
        for game_id, game in list(self.games.items()):
            if (current_time - datetime.fromisoformat(game['start_time'])).total_seconds() > 7200:
                del self.games[game_id]

game_state = GameState()

# WebSocket Handlers
@socketio.on('connect')
def handle_connect():
    logger.info('Client connected: %s', request.sid)

@socketio.on('disconnect')
def handle_disconnect():
    logger.info('Client disconnected: %s', request.sid)

@socketio.on('create_game')
def handle_create_game():
    try:
        game_id = game_state.create_game()
        emit('game_created', {
            'status': 'success',
            'gameId': game_id,
            'message': 'Game created successfully'
        })
        logger.info('Created game: %s', game_id)
    except Exception as e:
        logger.error('Game creation failed: %s', str(e))
        emit('game_created', {
            'status': 'error',
            'error': 'Game creation failed',
            'details': str(e)
        })

@socketio.on('join_game')
def handle_join_game(data):
    try:
        game_id = data.get('game_id')
        player_name = data.get('player_name')
        
        if not game_id or not player_name:
            raise ValueError('Missing game ID or player name')
            
        if game_id not in game_state.games:
            raise KeyError('Game not found')
            
        game = game_state.games[game_id]
        
        if len(game['players']) >= 5:
            raise ValueError('Game is full')

        player_id = len(game['players']) + 1
        new_player = {
            'id': player_id,
            'name': player_name,
            'socket_id': request.sid
        }
        game['players'].append(new_player)
        join_room(game['socket_room'])
        
        emit('player_joined', {
            'status': 'success',
            'player': new_player
        }, room=game['socket_room'])
        
        logger.info('Player %s joined game %s', player_name, game_id)
        
    except Exception as e:
        logger.error('Join game error: %s', str(e))
        emit('join_failed', {
            'status': 'error',
            'error': str(e)
        })

# REST API Endpoints
@app.route('/')
def home():
    return render_template('index.html')

@app.route('/api/start_game', methods=['POST'])
def start_game():
    try:
        data = request.get_json()
        game_id = data.get('game_id')
        
        if not game_id or game_id not in game_state.games:
            return jsonify({'status': 'error', 'error': 'Invalid game ID'}), 400
            
        game = game_state.games[game_id]
        game['current_phase'] = 'recording'
        
        socketio.emit('round_start', {
            'phase': 'recording',
            'duration': 30
        }, room=game['socket_room'])
        
        return jsonify({'status': 'success', 'message': 'Game started'})
        
    except Exception as e:
        logger.error('Start game error: %s', str(e))
        return jsonify({'status': 'error', 'error': str(e)}), 500

# Voice Processing Functions
async def generate_question():
    # Implementation remains same as before
    pass  

async def generate_impostor_answer(question):
    # Implementation remains same as before
    pass

async def clone_voice(audio_file):
    # Implementation remains same as before
    pass

async def generate_cloned_speech(voice_id, text):
    # Implementation remains same as before
    pass

if __name__ == '__main__':
    os.makedirs('temp', exist_ok=True)
    socketio.run(app,
        host='0.0.0.0',
        port=7860,
        debug=True,
        allow_unsafe_werkzeug=True,
        use_reloader=False
    )