Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,14 +10,50 @@ app.config['SECRET_KEY'] = 'your-secret-key' # Replace with a secure key
|
|
10 |
# Configure SocketIO with eventlet for WebSocket support
|
11 |
socketio = SocketIO(app,
|
12 |
cors_allowed_origins="https://broadfield-dev-dungeon-game.hf.space", # Your Spaces URL
|
13 |
-
async_mode='eventlet')
|
14 |
|
15 |
# Store game instances per client session
|
16 |
games = {}
|
17 |
|
18 |
-
# ... (rest of your
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
if __name__ == '__main__':
|
21 |
-
# Use port 7860 for Hugging Face Spaces or environment variable
|
22 |
port = int(os.environ.get('PORT', 7860))
|
23 |
socketio.run(app, debug=True, host='0.0.0.0', port=port)
|
|
|
10 |
# Configure SocketIO with eventlet for WebSocket support
|
11 |
socketio = SocketIO(app,
|
12 |
cors_allowed_origins="https://broadfield-dev-dungeon-game.hf.space", # Your Spaces URL
|
13 |
+
async_mode='eventlet')
|
14 |
|
15 |
# Store game instances per client session
|
16 |
games = {}
|
17 |
|
18 |
+
# ... (rest of your routes and SocketIO handlers remain the same)
|
19 |
+
|
20 |
+
@app.route('/')
|
21 |
+
def index():
|
22 |
+
session['sid'] = str(uuid.uuid4()) # Unique session ID for each client
|
23 |
+
return render_template('index.html')
|
24 |
+
|
25 |
+
@socketio.on('connect')
|
26 |
+
def handle_connect(auth=None):
|
27 |
+
sid = session.get('sid')
|
28 |
+
games[sid] = Game()
|
29 |
+
emit('update_game', games[sid].get_game_state())
|
30 |
+
emit('message', 'Connected to the dungeon! Use arrow keys to move.')
|
31 |
+
|
32 |
+
@socketio.on('move')
|
33 |
+
def handle_move(direction):
|
34 |
+
sid = session.get('sid')
|
35 |
+
if sid in games:
|
36 |
+
game = games[sid]
|
37 |
+
game.move_player(direction)
|
38 |
+
emit('update_game', game.get_game_state())
|
39 |
+
|
40 |
+
@socketio.on('use_item')
|
41 |
+
def handle_use_item(item_type):
|
42 |
+
sid = session.get('sid')
|
43 |
+
if sid in games:
|
44 |
+
game = games[sid]
|
45 |
+
if item_type == 'potion' and 'potion' in game.inventory:
|
46 |
+
game.inventory.remove('potion')
|
47 |
+
game.player_health = min(game.player_health + 5, 20)
|
48 |
+
emit('message', 'Used a health potion! Health restored.')
|
49 |
+
emit('update_game', game.get_game_state())
|
50 |
+
|
51 |
+
@socketio.on('disconnect')
|
52 |
+
def handle_disconnect():
|
53 |
+
sid = session.get('sid')
|
54 |
+
if sid in games:
|
55 |
+
del games[sid]
|
56 |
|
57 |
if __name__ == '__main__':
|
|
|
58 |
port = int(os.environ.get('PORT', 7860))
|
59 |
socketio.run(app, debug=True, host='0.0.0.0', port=port)
|