dungeon_game / app.py
broadfield-dev's picture
Update app.py
99c898d verified
raw
history blame
1.7 kB
from flask import Flask, render_template, session
from flask_socketio import SocketIO, emit
from game import Game
import uuid
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your-secret-key' # Replace with a secure key
socketio = SocketIO(app)
# Store game instances per client session
games = {}
@app.route('/')
def index():
session['sid'] = str(uuid.uuid4()) # Unique session ID for each client
return render_template('index.html')
@socketio.on('connect')
def handle_connect():
sid = session.get('sid')
games[sid] = Game() # Create a new game instance
emit('update_game', games[sid].get_game_state())
emit('message', 'Connected to the dungeon! Use arrow keys to move.')
@socketio.on('move')
def handle_move(direction):
sid = session.get('sid')
if sid in games:
game = games[sid]
game.move_player(direction)
emit('update_game', game.get_game_state())
# Combat or item messages are emitted within move_player if applicable
@socketio.on('use_item')
def handle_use_item(item_type):
sid = session.get('sid')
if sid in games:
game = games[sid]
if item_type == 'potion' and 'potion' in game.inventory:
game.inventory.remove('potion')
game.player_health = min(game.player_health + 5, 20) # Max health 20
emit('message', 'Used a health potion! Health restored.')
emit('update_game', game.get_game_state())
@socketio.on('disconnect')
def handle_disconnect():
sid = session.get('sid')
if sid in games:
del games[sid] # Clean up game instance
if __name__ == '__main__':
socketio.run(app, host='0.0.0.0', port=7860, allow_unsafe_werkzeug=True)