Renday commited on
Commit
4717f77
·
verified ·
1 Parent(s): 35f705f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -17
app.py CHANGED
@@ -1,42 +1,108 @@
1
  import eventlet
2
- eventlet.monkey_patch() # UNBEDINGT ZEILE 1
3
 
4
- from flask import Flask, render_template, request, jsonify
5
- from flask_socketio import SocketIO, emit
6
  import random
7
  import logging
 
 
8
 
9
- # Logging aktivieren, damit wir Fehler im HF-Log sehen
10
  logging.basicConfig(level=logging.INFO)
11
 
12
  app = Flask(__name__)
13
- app.config['SECRET_KEY'] = 'poker-secret-789'
14
- # async_mode='eventlet' ist die Rettung für den Internal Server Error
15
  socketio = SocketIO(app, cors_allowed_origins="*", async_mode='eventlet')
16
 
17
- # --- SPIEL-LOGIK (Minimal-Setup) ---
18
- game_state = {"players": [], "middle": [], "turn_idx": 0}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  @app.route('/')
21
  def index():
22
- try:
23
- return render_template('index.html')
24
- except Exception as e:
25
- return str(e), 500
26
 
27
  @app.route('/login', methods=['POST'])
28
  def login():
29
  data = request.json
30
  email = data.get('email', '').strip()
31
- is_admin = len(game_state["players"]) == 0
32
- player = {"email": email, "coins": 1000, "hand": [], "score": 0, "is_admin": is_admin}
33
- game_state["players"].append(player)
 
 
 
 
 
 
 
34
  return jsonify({"status": "ok", "stats": {"is_admin": is_admin}})
35
 
36
  @socketio.on('join_game')
37
- def handle_join(data):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  emit('update_table', game_state, broadcast=True)
39
 
40
  if __name__ == '__main__':
41
- # WICHTIG: Port 7860 ist Pflicht auf Hugging Face
42
  socketio.run(app, host='0.0.0.0', port=7860, debug=False)
 
1
  import eventlet
2
+ eventlet.monkey_patch()
3
 
 
 
4
  import random
5
  import logging
6
+ from flask import Flask, render_template, request, jsonify
7
+ from flask_socketio import SocketIO, emit
8
 
9
+ # Logging für Hugging Face Logs
10
  logging.basicConfig(level=logging.INFO)
11
 
12
  app = Flask(__name__)
13
+ app.config['SECRET_KEY'] = 'poker-31-secret-key'
 
14
  socketio = SocketIO(app, cors_allowed_origins="*", async_mode='eventlet')
15
 
16
+ # --- SPIELZUSTAND ---
17
+ game_state = {
18
+ "players": [],
19
+ "middle": [],
20
+ "deck": [],
21
+ "turn_idx": 0,
22
+ "current_bet": 50,
23
+ "game_active": False
24
+ }
25
+
26
+ def create_deck():
27
+ suits = ['Herz', 'Karo', 'Pik', 'Kreuz']
28
+ ranks = ['7', '8', '9', '10', 'B', 'D', 'K', 'A']
29
+ return [{"suit": s, "rank": r} for s in suits for r in ranks]
30
+
31
+ def calculate_31_score(hand):
32
+ if not hand or len(hand) < 3: return 0
33
+ values = {'7':7, '8':8, '9':9, '10':10, 'B':10, 'D':10, 'K':10, 'A':11}
34
+ # Drei Gleiche (z.B. drei 7er) = 30.5
35
+ if hand[0]['rank'] == hand[1]['rank'] == hand[2]['rank']:
36
+ return 30.5
37
+ # Summe pro Farbe
38
+ scores = {'Herz': 0, 'Karo': 0, 'Pik': 0, 'Kreuz': 0}
39
+ for card in hand:
40
+ scores[card['suit']] += values[card['rank']]
41
+ return max(scores.values())
42
 
43
  @app.route('/')
44
  def index():
45
+ return render_template('index.html')
 
 
 
46
 
47
  @app.route('/login', methods=['POST'])
48
  def login():
49
  data = request.json
50
  email = data.get('email', '').strip()
51
+ if not email: return jsonify({"error": "No email"}), 400
52
+
53
+ # Erster User ist Admin
54
+ is_admin = len(game_state["players"]) == 0 or email.startswith('admin')
55
+
56
+ player = next((p for p in game_state["players"] if p["email"] == email), None)
57
+ if not player:
58
+ player = {"email": email, "coins": 1000, "hand": [], "score": 0, "is_admin": is_admin}
59
+ game_state["players"].append(player)
60
+
61
  return jsonify({"status": "ok", "stats": {"is_admin": is_admin}})
62
 
63
  @socketio.on('join_game')
64
+ def on_join(data):
65
+ emit('update_table', game_state, broadcast=True)
66
+
67
+ @socketio.on('admin/set_bet')
68
+ def on_set_bet(data):
69
+ game_state["current_bet"] = int(data.get('bet', 50))
70
+
71
+ @socketio.on('start_round')
72
+ def on_start(data):
73
+ deck = create_deck()
74
+ random.shuffle(deck)
75
+ for p in game_state["players"]:
76
+ p["hand"] = [deck.pop() for _ in range(3)]
77
+ p["score"] = calculate_31_score(p["hand"])
78
+ game_state["middle"] = [deck.pop() for _ in range(3)]
79
+ game_state["deck"] = deck
80
+ game_state["turn_idx"] = 0
81
+ game_state["game_active"] = True
82
+ emit('update_table', game_state, broadcast=True)
83
+
84
+ @socketio.on('player_action')
85
+ def on_action(data):
86
+ email = data.get('email')
87
+ action_type = data.get('type')
88
+
89
+ if len(game_state["players"]) == 0: return
90
+ current_player = game_state["players"][game_state["turn_idx"]]
91
+
92
+ if current_player["email"] != email: return
93
+
94
+ if action_type == 'swap_one':
95
+ h_idx, m_idx = int(data.get('h_idx')), int(data.get('m_idx'))
96
+ current_player["hand"][h_idx], game_state["middle"][m_idx] = \
97
+ game_state["middle"][m_idx], current_player["hand"][h_idx]
98
+ elif action_type == 'swap_all':
99
+ current_player["hand"], game_state["middle"] = \
100
+ game_state["middle"], current_player["hand"]
101
+
102
+ current_player["score"] = calculate_31_score(current_player["hand"])
103
+ game_state["turn_idx"] = (game_state["turn_idx"] + 1) % len(game_state["players"])
104
  emit('update_table', game_state, broadcast=True)
105
 
106
  if __name__ == '__main__':
107
+ print("Server startet auf Port 7860...")
108
  socketio.run(app, host='0.0.0.0', port=7860, debug=False)