broadfield-dev commited on
Commit
ca24c9d
·
verified ·
1 Parent(s): 9880cea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -42
app.py CHANGED
@@ -7,53 +7,17 @@ import os
7
  app = Flask(__name__)
8
  app.config['SECRET_KEY'] = 'your-secret-key' # Replace with a secure key
9
 
10
- # Configure SocketIO with CORS for all origins and threading mode
11
- socketio = SocketIO(app,
12
- cors_allowed_origins="https://broadfield-dev-dungeon-game.hf.space", # Specify your Spaces URL
13
- async_mode='threading') # Use threading instead of eventlet
14
 
15
  # Store game instances per client session
16
  games = {}
17
 
18
- @app.route('/')
19
- def index():
20
- session['sid'] = str(uuid.uuid4()) # Unique session ID for each client
21
- return render_template('index.html')
22
-
23
- @socketio.on('connect')
24
- def handle_connect(auth=None): # Add auth parameter (can be None if not used)
25
- sid = session.get('sid')
26
- games[sid] = Game() # Create a new game instance
27
- emit('update_game', games[sid].get_game_state())
28
- emit('message', 'Connected to the dungeon! Use arrow keys to move.')
29
-
30
- @socketio.on('move')
31
- def handle_move(direction):
32
- sid = session.get('sid')
33
- if sid in games:
34
- game = games[sid]
35
- game.move_player(direction)
36
- emit('update_game', game.get_game_state())
37
- # Combat or item messages are emitted within move_player if applicable
38
-
39
- @socketio.on('use_item')
40
- def handle_use_item(item_type):
41
- sid = session.get('sid')
42
- if sid in games:
43
- game = games[sid]
44
- if item_type == 'potion' and 'potion' in game.inventory:
45
- game.inventory.remove('potion')
46
- game.player_health = min(game.player_health + 5, 20) # Max health 20
47
- emit('message', 'Used a health potion! Health restored.')
48
- emit('update_game', game.get_game_state())
49
-
50
- @socketio.on('disconnect')
51
- def handle_disconnect():
52
- sid = session.get('sid')
53
- if sid in games:
54
- del games[sid] # Clean up game instance
55
 
56
  if __name__ == '__main__':
57
  # Use port 7860 for Hugging Face Spaces or environment variable
58
  port = int(os.environ.get('PORT', 7860))
59
- socketio.run(app, debug=True, host='0.0.0.0', port=port, allow_unsafe_werkzeug=True)
 
7
  app = Flask(__name__)
8
  app.config['SECRET_KEY'] = 'your-secret-key' # Replace with a secure key
9
 
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') # Switch to eventlet
14
 
15
  # Store game instances per client session
16
  games = {}
17
 
18
+ # ... (rest of your app.py code remains the same)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)