Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, session
|
2 |
+
from flask_socketio import SocketIO, emit
|
3 |
+
from game import Game
|
4 |
+
import uuid
|
5 |
+
|
6 |
+
app = Flask(__name__)
|
7 |
+
app.config['SECRET_KEY'] = 'your-secret-key' # Replace with a secure key
|
8 |
+
socketio = SocketIO(app)
|
9 |
+
|
10 |
+
# Store game instances per client session
|
11 |
+
games = {}
|
12 |
+
|
13 |
+
@app.route('/')
|
14 |
+
def index():
|
15 |
+
session['sid'] = str(uuid.uuid4()) # Unique session ID for each client
|
16 |
+
return render_template('index.html')
|
17 |
+
|
18 |
+
@socketio.on('connect')
|
19 |
+
def handle_connect():
|
20 |
+
sid = session.get('sid')
|
21 |
+
games[sid] = Game() # Create a new game instance
|
22 |
+
emit('update_game', games[sid].get_game_state())
|
23 |
+
emit('message', 'Connected to the dungeon! Use arrow keys to move.')
|
24 |
+
|
25 |
+
@socketio.on('move')
|
26 |
+
def handle_move(direction):
|
27 |
+
sid = session.get('sid')
|
28 |
+
if sid in games:
|
29 |
+
game = games[sid]
|
30 |
+
game.move_player(direction)
|
31 |
+
emit('update_game', game.get_game_state())
|
32 |
+
# Combat or item messages are emitted within move_player if applicable
|
33 |
+
|
34 |
+
@socketio.on('use_item')
|
35 |
+
def handle_use_item(item_type):
|
36 |
+
sid = session.get('sid')
|
37 |
+
if sid in games:
|
38 |
+
game = games[sid]
|
39 |
+
if item_type == 'potion' and 'potion' in game.inventory:
|
40 |
+
game.inventory.remove('potion')
|
41 |
+
game.player_health = min(game.player_health + 5, 20) # Max health 20
|
42 |
+
emit('message', 'Used a health potion! Health restored.')
|
43 |
+
emit('update_game', game.get_game_state())
|
44 |
+
|
45 |
+
@socketio.on('disconnect')
|
46 |
+
def handle_disconnect():
|
47 |
+
sid = session.get('sid')
|
48 |
+
if sid in games:
|
49 |
+
del games[sid] # Clean up game instance
|
50 |
+
|
51 |
+
if __name__ == '__main__':
|
52 |
+
socketio.run(app, host='0.0.0.0', port=7860)
|