Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ from flask_cors import CORS
|
|
| 7 |
app = Flask(__name__, template_folder="templates")
|
| 8 |
CORS(app, resources={r"/*": {"origins": "*"}})
|
| 9 |
|
| 10 |
-
#
|
| 11 |
socketio = SocketIO(app, cors_allowed_origins="*", async_mode='eventlet')
|
| 12 |
|
| 13 |
# --- SPIEL-KONFIGURATION ---
|
|
@@ -16,21 +16,20 @@ RANKS = {'7': 7, '8': 8, '9': 9, '10': 10, 'B': 10, 'D': 10, 'K': 10, 'A': 11}
|
|
| 16 |
|
| 17 |
class GameState:
|
| 18 |
def __init__(self):
|
| 19 |
-
self.players = {} #
|
| 20 |
-
self.active_emails = [] #
|
| 21 |
-
self.middle = [] #
|
| 22 |
-
self.deck = [] #
|
| 23 |
-
self.turn_idx = 0 # Wer ist
|
| 24 |
-
self.current_bet = 50 #
|
| 25 |
|
| 26 |
def calculate_score(self, hand):
|
| 27 |
-
"""Berechnet die Summe der Kartenwerte
|
| 28 |
if not hand: return 0
|
| 29 |
return sum(c['val'] for c in hand)
|
| 30 |
|
| 31 |
def broadcast_state(self):
|
| 32 |
-
"""Sendet den
|
| 33 |
-
# Scores vor dem Senden aktualisieren
|
| 34 |
for email in self.active_emails:
|
| 35 |
if email in self.players:
|
| 36 |
p = self.players[email]
|
|
@@ -46,7 +45,7 @@ class GameState:
|
|
| 46 |
|
| 47 |
game = GameState()
|
| 48 |
|
| 49 |
-
# ---
|
| 50 |
|
| 51 |
@app.route('/')
|
| 52 |
def index():
|
|
@@ -64,11 +63,11 @@ def login():
|
|
| 64 |
'coins': 1000,
|
| 65 |
'hand': [],
|
| 66 |
'score': 0,
|
| 67 |
-
'is_admin': len(game.players) == 0
|
| 68 |
}
|
| 69 |
return jsonify({'stats': game.players[email]})
|
| 70 |
|
| 71 |
-
# --- SOCKET
|
| 72 |
|
| 73 |
@socketio.on('join_game')
|
| 74 |
def handle_join(data):
|
|
@@ -80,14 +79,10 @@ def handle_join(data):
|
|
| 80 |
@socketio.on('start_round')
|
| 81 |
def start_round(data):
|
| 82 |
"""Startet die Runde, zieht Einsatz ab und teilt Karten aus."""
|
| 83 |
-
# Neues Deck mischen
|
| 84 |
game.deck = [{'suit': s, 'rank': r, 'val': v} for s in SUITS for r, v in RANKS.items()]
|
| 85 |
random.shuffle(game.deck)
|
| 86 |
-
|
| 87 |
-
# Mitte füllen
|
| 88 |
game.middle = [game.deck.pop() for _ in range(3)]
|
| 89 |
|
| 90 |
-
# Spieler versorgen und Einsatz abziehen
|
| 91 |
for email in game.active_emails:
|
| 92 |
if email in game.players:
|
| 93 |
p = game.players[email]
|
|
@@ -115,68 +110,58 @@ def handle_action(data):
|
|
| 115 |
elif data['type'] == 'swap_all':
|
| 116 |
p['hand'], game.middle = game.middle, p['hand']
|
| 117 |
|
| 118 |
-
|
| 119 |
-
#
|
| 120 |
max_score = -1
|
|
|
|
| 121 |
for e in game.active_emails:
|
| 122 |
-
|
| 123 |
-
if
|
| 124 |
-
max_score = score
|
| 125 |
|
| 126 |
-
# 2. Alle
|
| 127 |
winners = [e for e in game.active_emails if game.calculate_score(game.players[e]['hand']) == max_score]
|
| 128 |
|
| 129 |
-
# 3. Pott
|
| 130 |
total_pott = len(game.active_emails) * int(game.current_bet)
|
| 131 |
-
share = total_pott // len(winners)
|
| 132 |
|
| 133 |
for w_email in winners:
|
| 134 |
game.players[w_email]['coins'] += share
|
| 135 |
|
| 136 |
-
# 4. Ergebnis-Text für das Frontend
|
| 137 |
-
winner_names = ", ".join(winners)
|
| 138 |
emit('game_over', {
|
| 139 |
-
'winner':
|
| 140 |
'score': max_score,
|
| 141 |
'pott': total_pott,
|
| 142 |
'is_split': len(winners) > 1
|
| 143 |
}, broadcast=True)
|
| 144 |
return
|
| 145 |
|
| 146 |
-
# Zug-Management
|
| 147 |
p['score'] = game.calculate_score(p['hand'])
|
| 148 |
game.turn_idx = (game.turn_idx + 1) % len(game.active_emails)
|
| 149 |
game.broadcast_state()
|
| 150 |
|
| 151 |
-
# --- ADMIN
|
| 152 |
|
| 153 |
@socketio.on('admin/update_bet')
|
| 154 |
def update_bet(data):
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
if new_bet is not None:
|
| 158 |
-
game.current_bet = int(new_bet)
|
| 159 |
-
emit('bet_updated', {'new_bet': game.current_bet}, broadcast=True)
|
| 160 |
|
| 161 |
@socketio.on('admin/reshuffle')
|
| 162 |
def reshuffle(data):
|
| 163 |
-
"""Mischt Karten neu, ohne die Spielerliste zu ändern."""
|
| 164 |
game.deck = [{'suit': s, 'rank': r, 'val': v} for s in SUITS for r, v in RANKS.items()]
|
| 165 |
random.shuffle(game.deck)
|
| 166 |
game.middle = [game.deck.pop() for _ in range(3)]
|
| 167 |
for e in game.active_emails:
|
| 168 |
-
if e in game.players:
|
| 169 |
-
game.players[e]['hand'] = []
|
| 170 |
emit('cards_reshuffled', broadcast=True)
|
| 171 |
game.broadcast_state()
|
| 172 |
|
| 173 |
@socketio.on('admin/end_game')
|
| 174 |
def end_game(data):
|
| 175 |
-
"""Beendet die gesamte Session."""
|
| 176 |
game.active_emails = []
|
| 177 |
game.middle = []
|
| 178 |
emit('game_ended_by_admin', broadcast=True)
|
| 179 |
|
| 180 |
if __name__ == '__main__':
|
| 181 |
-
# Port 7860 ist ideal für Hugging Face Spaces
|
| 182 |
socketio.run(app, host='0.0.0.0', port=7860, debug=False)
|
|
|
|
| 7 |
app = Flask(__name__, template_folder="templates")
|
| 8 |
CORS(app, resources={r"/*": {"origins": "*"}})
|
| 9 |
|
| 10 |
+
# Eventlet für stabile Web-Sockets auf Servern wie Hugging Face
|
| 11 |
socketio = SocketIO(app, cors_allowed_origins="*", async_mode='eventlet')
|
| 12 |
|
| 13 |
# --- SPIEL-KONFIGURATION ---
|
|
|
|
| 16 |
|
| 17 |
class GameState:
|
| 18 |
def __init__(self):
|
| 19 |
+
self.players = {} # email -> {coins, hand, score, is_admin}
|
| 20 |
+
self.active_emails = [] # Spieler in der aktuellen Runde
|
| 21 |
+
self.middle = [] # Karten in der Mitte
|
| 22 |
+
self.deck = [] # Nachziehstapel
|
| 23 |
+
self.turn_idx = 0 # Wer ist am Zug?
|
| 24 |
+
self.current_bet = 50 # Aktueller Einsatz
|
| 25 |
|
| 26 |
def calculate_score(self, hand):
|
| 27 |
+
"""Berechnet die Summe der Kartenwerte."""
|
| 28 |
if not hand: return 0
|
| 29 |
return sum(c['val'] for c in hand)
|
| 30 |
|
| 31 |
def broadcast_state(self):
|
| 32 |
+
"""Sendet den Tisch-Status an alle."""
|
|
|
|
| 33 |
for email in self.active_emails:
|
| 34 |
if email in self.players:
|
| 35 |
p = self.players[email]
|
|
|
|
| 45 |
|
| 46 |
game = GameState()
|
| 47 |
|
| 48 |
+
# --- ROUTES ---
|
| 49 |
|
| 50 |
@app.route('/')
|
| 51 |
def index():
|
|
|
|
| 63 |
'coins': 1000,
|
| 64 |
'hand': [],
|
| 65 |
'score': 0,
|
| 66 |
+
'is_admin': len(game.players) == 0
|
| 67 |
}
|
| 68 |
return jsonify({'stats': game.players[email]})
|
| 69 |
|
| 70 |
+
# --- SOCKET EVENTS ---
|
| 71 |
|
| 72 |
@socketio.on('join_game')
|
| 73 |
def handle_join(data):
|
|
|
|
| 79 |
@socketio.on('start_round')
|
| 80 |
def start_round(data):
|
| 81 |
"""Startet die Runde, zieht Einsatz ab und teilt Karten aus."""
|
|
|
|
| 82 |
game.deck = [{'suit': s, 'rank': r, 'val': v} for s in SUITS for r, v in RANKS.items()]
|
| 83 |
random.shuffle(game.deck)
|
|
|
|
|
|
|
| 84 |
game.middle = [game.deck.pop() for _ in range(3)]
|
| 85 |
|
|
|
|
| 86 |
for email in game.active_emails:
|
| 87 |
if email in game.players:
|
| 88 |
p = game.players[email]
|
|
|
|
| 110 |
elif data['type'] == 'swap_all':
|
| 111 |
p['hand'], game.middle = game.middle, p['hand']
|
| 112 |
|
| 113 |
+
elif data['type'] == 'knock':
|
| 114 |
+
# --- FAIRE GEWINNERMITTLUNG (SPLIT POTT) ---
|
| 115 |
max_score = -1
|
| 116 |
+
# 1. Höchsten Wert finden
|
| 117 |
for e in game.active_emails:
|
| 118 |
+
s = game.calculate_score(game.players[e]['hand'])
|
| 119 |
+
if s > max_score: max_score = s
|
|
|
|
| 120 |
|
| 121 |
+
# 2. Alle Gewinner mit diesem Wert finden
|
| 122 |
winners = [e for e in game.active_emails if game.calculate_score(game.players[e]['hand']) == max_score]
|
| 123 |
|
| 124 |
+
# 3. Pott teilen
|
| 125 |
total_pott = len(game.active_emails) * int(game.current_bet)
|
| 126 |
+
share = total_pott // len(winners)
|
| 127 |
|
| 128 |
for w_email in winners:
|
| 129 |
game.players[w_email]['coins'] += share
|
| 130 |
|
|
|
|
|
|
|
| 131 |
emit('game_over', {
|
| 132 |
+
'winner': ", ".join(winners),
|
| 133 |
'score': max_score,
|
| 134 |
'pott': total_pott,
|
| 135 |
'is_split': len(winners) > 1
|
| 136 |
}, broadcast=True)
|
| 137 |
return
|
| 138 |
|
|
|
|
| 139 |
p['score'] = game.calculate_score(p['hand'])
|
| 140 |
game.turn_idx = (game.turn_idx + 1) % len(game.active_emails)
|
| 141 |
game.broadcast_state()
|
| 142 |
|
| 143 |
+
# --- ADMIN STEUERUNG ---
|
| 144 |
|
| 145 |
@socketio.on('admin/update_bet')
|
| 146 |
def update_bet(data):
|
| 147 |
+
game.current_bet = int(data.get('bet', 50))
|
| 148 |
+
emit('bet_updated', {'new_bet': game.current_bet}, broadcast=True)
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
@socketio.on('admin/reshuffle')
|
| 151 |
def reshuffle(data):
|
|
|
|
| 152 |
game.deck = [{'suit': s, 'rank': r, 'val': v} for s in SUITS for r, v in RANKS.items()]
|
| 153 |
random.shuffle(game.deck)
|
| 154 |
game.middle = [game.deck.pop() for _ in range(3)]
|
| 155 |
for e in game.active_emails:
|
| 156 |
+
if e in game.players: game.players[e]['hand'] = []
|
|
|
|
| 157 |
emit('cards_reshuffled', broadcast=True)
|
| 158 |
game.broadcast_state()
|
| 159 |
|
| 160 |
@socketio.on('admin/end_game')
|
| 161 |
def end_game(data):
|
|
|
|
| 162 |
game.active_emails = []
|
| 163 |
game.middle = []
|
| 164 |
emit('game_ended_by_admin', broadcast=True)
|
| 165 |
|
| 166 |
if __name__ == '__main__':
|
|
|
|
| 167 |
socketio.run(app, host='0.0.0.0', port=7860, debug=False)
|