Renday commited on
Commit
8bd9ab1
·
verified ·
1 Parent(s): 46714dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -27
app.py CHANGED
@@ -6,10 +6,10 @@ from flask import Flask, render_template, request, jsonify
6
  from flask_socketio import SocketIO, emit
7
 
8
  app = Flask(__name__)
9
- app.config['SECRET_KEY'] = 'casino-31-secret'
10
  socketio = SocketIO(app, cors_allowed_origins="*", async_mode='eventlet')
11
 
12
- # --- GLOBALER SPIELZUSTAND ---
13
  game_state = {
14
  "players": [],
15
  "middle": [],
@@ -24,6 +24,21 @@ def create_deck():
24
  ranks = ['7', '8', '9', '10', 'B', 'D', 'K', 'A']
25
  return [{"suit": s, "rank": r} for s in suits for r in ranks]
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  @app.route('/')
28
  def index():
29
  return render_template('index.html')
@@ -34,7 +49,7 @@ def login():
34
  email = data.get('email', '').strip()
35
  if not email: return jsonify({"error": "No email"}), 400
36
 
37
- # Admin Logik: Erster User ist Admin
38
  is_admin = len(game_state["players"]) == 0 or email.startswith('admin')
39
 
40
  player = next((p for p in game_state["players"] if p["email"] == email), None)
@@ -56,52 +71,34 @@ def on_set_bet(data):
56
  def on_start(data):
57
  deck = create_deck()
58
  random.shuffle(deck)
59
-
60
- # Karten an alle Spieler verteilen
61
  for p in game_state["players"]:
62
  p["hand"] = [deck.pop() for _ in range(3)]
63
- p["score"] = 0 # Hier könnte man eine Score-Berechnungs-Funktion einfügen
64
-
65
  game_state["middle"] = [deck.pop() for _ in range(3)]
66
  game_state["deck"] = deck
67
  game_state["turn_idx"] = 0
68
  game_state["game_active"] = True
69
-
70
  emit('update_table', game_state, broadcast=True)
71
 
72
  @socketio.on('player_action')
73
  def on_action(data):
74
  email = data.get('email')
75
  action_type = data.get('type')
76
-
77
- # Prüfen, ob der Spieler dran ist
78
  current_player = game_state["players"][game_state["turn_idx"]]
79
- if current_player["email"] != email:
80
- return # Nicht dein Zug!
81
 
82
- # 1. EINE KARTE TAUSCHEN
83
  if action_type == 'swap_one':
84
- h_idx = data.get('h_idx')
85
- m_idx = data.get('m_idx')
86
- # Karten kreuzen
87
  current_player["hand"][h_idx], game_state["middle"][m_idx] = \
88
  game_state["middle"][m_idx], current_player["hand"][h_idx]
89
-
90
- # 2. ALLE KARTEN TAUSCHEN
91
  elif action_type == 'swap_all':
92
  current_player["hand"], game_state["middle"] = \
93
  game_state["middle"], current_player["hand"]
94
 
95
- # 3. KLOPFEN
96
- elif action_type == 'knock':
97
- # Logik für Rundenende könnte hier stehen
98
- pass
99
-
100
- # Zug beenden und zum nächsten Spieler springen
101
  game_state["turn_idx"] = (game_state["turn_idx"] + 1) % len(game_state["players"])
102
-
103
- # Update an ALLE senden
104
  emit('update_table', game_state, broadcast=True)
105
 
106
  if __name__ == '__main__':
107
- socketio.run(app, host='0.0.0.0', port=7860, debug=False)
 
6
  from flask_socketio import SocketIO, emit
7
 
8
  app = Flask(__name__)
9
+ app.config['SECRET_KEY'] = 'casino-31-key'
10
  socketio = SocketIO(app, cors_allowed_origins="*", async_mode='eventlet')
11
 
12
+ # --- SPIELZUSTAND ---
13
  game_state = {
14
  "players": [],
15
  "middle": [],
 
24
  ranks = ['7', '8', '9', '10', 'B', 'D', 'K', 'A']
25
  return [{"suit": s, "rank": r} for s in suits for r in ranks]
26
 
27
+ def calculate_31_score(hand):
28
+ if not hand or len(hand) < 3: return 0
29
+ values = {'7':7, '8':8, '9':9, '10':10, 'B':10, 'D':10, 'K':10, 'A':11}
30
+
31
+ # 1. Check: Drei Gleiche (z.B. drei 7er = 30.5)
32
+ if hand[0]['rank'] == hand[1]['rank'] == hand[2]['rank']:
33
+ return 30.5
34
+
35
+ # 2. Check: Summe pro Farbe
36
+ scores = {'Herz': 0, 'Karo': 0, 'Pik': 0, 'Kreuz': 0}
37
+ for card in hand:
38
+ scores[card['suit']] += values[card['rank']]
39
+
40
+ return max(scores.values())
41
+
42
  @app.route('/')
43
  def index():
44
  return render_template('index.html')
 
49
  email = data.get('email', '').strip()
50
  if not email: return jsonify({"error": "No email"}), 400
51
 
52
+ # Erster ist Admin
53
  is_admin = len(game_state["players"]) == 0 or email.startswith('admin')
54
 
55
  player = next((p for p in game_state["players"] if p["email"] == email), None)
 
71
  def on_start(data):
72
  deck = create_deck()
73
  random.shuffle(deck)
 
 
74
  for p in game_state["players"]:
75
  p["hand"] = [deck.pop() for _ in range(3)]
76
+ p["score"] = calculate_31_score(p["hand"])
 
77
  game_state["middle"] = [deck.pop() for _ in range(3)]
78
  game_state["deck"] = deck
79
  game_state["turn_idx"] = 0
80
  game_state["game_active"] = True
 
81
  emit('update_table', game_state, broadcast=True)
82
 
83
  @socketio.on('player_action')
84
  def on_action(data):
85
  email = data.get('email')
86
  action_type = data.get('type')
 
 
87
  current_player = game_state["players"][game_state["turn_idx"]]
88
+
89
+ if current_player["email"] != email: return
90
 
 
91
  if action_type == 'swap_one':
92
+ h_idx, m_idx = int(data.get('h_idx')), int(data.get('m_idx'))
 
 
93
  current_player["hand"][h_idx], game_state["middle"][m_idx] = \
94
  game_state["middle"][m_idx], current_player["hand"][h_idx]
 
 
95
  elif action_type == 'swap_all':
96
  current_player["hand"], game_state["middle"] = \
97
  game_state["middle"], current_player["hand"]
98
 
99
+ current_player["score"] = calculate_31_score(current_player["hand"])
 
 
 
 
 
100
  game_state["turn_idx"] = (game_state["turn_idx"] + 1) % len(game_state["players"])
 
 
101
  emit('update_table', game_state, broadcast=True)
102
 
103
  if __name__ == '__main__':
104
+ socketio.run(app, host='0.0.0.0', port=7860)