Renday commited on
Commit
9d88289
·
verified ·
1 Parent(s): 0e97091

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -52
app.py CHANGED
@@ -8,28 +8,38 @@ app = Flask(__name__, template_folder="templates")
8
  CORS(app, resources={r"/*": {"origins": "*"}})
9
  socketio = SocketIO(app, cors_allowed_origins="*", async_mode='eventlet')
10
 
11
- # --- SPIEL-KONFIGURATION ---
12
  SUITS = ['Herz', 'Karo', 'Pik', 'Kreuz']
13
  RANKS = {'7': 7, '8': 8, '9': 9, '10': 10, 'B': 10, 'D': 10, 'K': 10, 'A': 11}
14
 
15
  class GameState:
16
  def __init__(self):
17
- self.players = {}
18
- self.active_emails = []
19
- self.middle = []
20
- self.deck = []
21
- self.turn_idx = 0
22
- self.current_bet = 50
23
 
24
  def calculate_score(self, hand):
25
- if not hand: return 0
26
- return sum(c['val'] for c in hand)
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  def broadcast_state(self):
29
  for email in self.active_emails:
30
  if email in self.players:
31
- p = self.players[email]
32
- p['score'] = self.calculate_score(p.get('hand', []))
33
 
34
  state = {
35
  'players': [self.players[e] for e in self.active_emails if e in self.players],
@@ -43,12 +53,10 @@ class GameState:
43
  game = GameState()
44
 
45
  @app.route('/')
46
- def index():
47
- return render_template('index.html')
48
 
49
- @app.route('/login', methods=['POST', 'OPTIONS'])
50
  def login():
51
- if request.method == 'OPTIONS': return jsonify({}), 200
52
  data = request.json
53
  email = data.get('email')
54
  if email not in game.players:
@@ -71,12 +79,10 @@ def start_round(data):
71
  random.shuffle(game.deck)
72
  game.middle = [game.deck.pop() for _ in range(3)]
73
  for email in game.active_emails:
74
- if email in game.players:
75
- p = game.players[email]
76
- if p['coins'] >= game.current_bet:
77
- p['hand'] = [game.deck.pop() for _ in range(3)]
78
- p['score'] = game.calculate_score(p['hand'])
79
- p['coins'] -= int(game.current_bet)
80
  game.turn_idx = 0
81
  game.broadcast_state()
82
 
@@ -85,51 +91,29 @@ def handle_action(data):
85
  email = data.get('email')
86
  if not game.active_emails or game.active_emails[game.turn_idx] != email: return
87
  p = game.players.get(email)
88
- if not p: return
89
-
90
  if data['type'] == 'swap_one':
91
- h_idx, m_idx = data['h_idx'], data['m_idx']
92
- p['hand'][h_idx], game.middle[m_idx] = game.middle[m_idx], p['hand'][h_idx]
93
  elif data['type'] == 'swap_all':
94
  p['hand'], game.middle = game.middle, p['hand']
95
  elif data['type'] == 'pass':
96
  pass
97
  elif data['type'] == 'knock':
98
- max_score = -1
99
- for e in game.active_emails:
100
- s = game.calculate_score(game.players[e]['hand'])
101
- if s > max_score: max_score = s
102
- winners = [e for e in game.active_emails if game.calculate_score(game.players[e]['hand']) == max_score]
103
- total_pott = len(game.active_emails) * int(game.current_bet)
104
- share = total_pott // len(winners)
105
- for w_email in winners:
106
- game.players[w_email]['coins'] += share
107
- emit('game_over', {'winner': ", ".join(winners), 'score': max_score, 'pott': total_pott, 'is_split': len(winners) > 1}, broadcast=True)
108
  return
109
 
110
- p['score'] = game.calculate_score(p['hand'])
111
  game.turn_idx = (game.turn_idx + 1) % len(game.active_emails)
112
  game.broadcast_state()
113
 
114
  @socketio.on('admin/update_bet')
115
  def update_bet(data):
116
  game.current_bet = int(data.get('bet', 50))
117
- game.broadcast_state() # Broadcastet sofort den neuen Einsatz an alle
118
-
119
- @socketio.on('admin/reshuffle')
120
- def reshuffle(data):
121
- game.deck = [{'suit': s, 'rank': r, 'val': v} for s in SUITS for r, v in RANKS.items()]
122
- random.shuffle(game.deck)
123
- game.middle = [game.deck.pop() for _ in range(3)]
124
- for e in game.active_emails:
125
- if e in game.players: game.players[e]['hand'] = []
126
  game.broadcast_state()
127
 
128
- @socketio.on('admin/end_game')
129
- def end_game(data):
130
- game.active_emails = []
131
- game.middle = []
132
- emit('game_ended_by_admin', broadcast=True)
133
-
134
  if __name__ == '__main__':
135
- socketio.run(app, host='0.0.0.0', port=7860, debug=False)
 
8
  CORS(app, resources={r"/*": {"origins": "*"}})
9
  socketio = SocketIO(app, cors_allowed_origins="*", async_mode='eventlet')
10
 
11
+ # --- KONFIGURATION ---
12
  SUITS = ['Herz', 'Karo', 'Pik', 'Kreuz']
13
  RANKS = {'7': 7, '8': 8, '9': 9, '10': 10, 'B': 10, 'D': 10, 'K': 10, 'A': 11}
14
 
15
  class GameState:
16
  def __init__(self):
17
+ self.players = {}
18
+ self.active_emails = []
19
+ self.middle = []
20
+ self.deck = []
21
+ self.turn_idx = 0
22
+ self.current_bet = 50
23
 
24
  def calculate_score(self, hand):
25
+ """Offizielle 31-Regeln: Nur gleiche Farben addieren ODER Drillinge = 30.5"""
26
+ if not hand or len(hand) < 3: return 0
27
+
28
+ # Regel: Drillinge (3 gleiche Ränge) zählen 30.5
29
+ r = [c['rank'] for c in hand]
30
+ if r[0] == r[1] == r[2]:
31
+ return 30.5
32
+
33
+ # Regel: Höchste Summe einer Farbe
34
+ sums = {'Herz': 0, 'Karo': 0, 'Pik': 0, 'Kreuz': 0}
35
+ for c in hand:
36
+ sums[c['suit']] += c['val']
37
+ return max(sums.values())
38
 
39
  def broadcast_state(self):
40
  for email in self.active_emails:
41
  if email in self.players:
42
+ self.players[email]['score'] = self.calculate_score(self.players[email].get('hand', []))
 
43
 
44
  state = {
45
  'players': [self.players[e] for e in self.active_emails if e in self.players],
 
53
  game = GameState()
54
 
55
  @app.route('/')
56
+ def index(): return render_template('index.html')
 
57
 
58
+ @app.route('/login', methods=['POST'])
59
  def login():
 
60
  data = request.json
61
  email = data.get('email')
62
  if email not in game.players:
 
79
  random.shuffle(game.deck)
80
  game.middle = [game.deck.pop() for _ in range(3)]
81
  for email in game.active_emails:
82
+ p = game.players[email]
83
+ if p['coins'] >= game.current_bet:
84
+ p['hand'] = [game.deck.pop() for _ in range(3)]
85
+ p['coins'] -= int(game.current_bet)
 
 
86
  game.turn_idx = 0
87
  game.broadcast_state()
88
 
 
91
  email = data.get('email')
92
  if not game.active_emails or game.active_emails[game.turn_idx] != email: return
93
  p = game.players.get(email)
94
+
 
95
  if data['type'] == 'swap_one':
96
+ p['hand'][data['h_idx']], game.middle[data['m_idx']] = game.middle[data['m_idx']], p['hand'][data['h_idx']]
 
97
  elif data['type'] == 'swap_all':
98
  p['hand'], game.middle = game.middle, p['hand']
99
  elif data['type'] == 'pass':
100
  pass
101
  elif data['type'] == 'knock':
102
+ # Ermittlung aller Gewinner (Split Pott Support)
103
+ max_s = max(game.calculate_score(game.players[e]['hand']) for e in game.active_emails)
104
+ winners = [e for e in game.active_emails if game.calculate_score(game.players[e]['hand']) == max_s]
105
+ pott = len(game.active_emails) * int(game.current_bet)
106
+ for w in winners: game.players[w]['coins'] += pott // len(winners)
107
+ emit('game_over', {'winner': ", ".join(winners), 'score': max_s, 'pott': pott}, broadcast=True)
 
 
 
 
108
  return
109
 
 
110
  game.turn_idx = (game.turn_idx + 1) % len(game.active_emails)
111
  game.broadcast_state()
112
 
113
  @socketio.on('admin/update_bet')
114
  def update_bet(data):
115
  game.current_bet = int(data.get('bet', 50))
 
 
 
 
 
 
 
 
 
116
  game.broadcast_state()
117
 
 
 
 
 
 
 
118
  if __name__ == '__main__':
119
+ socketio.run(app, host='0.0.0.0', port=7860)