Renday commited on
Commit
3faed8f
·
verified ·
1 Parent(s): 1c65297

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -13
app.py CHANGED
@@ -1,10 +1,14 @@
1
  import random
2
- from flask import Flask, request, jsonify
 
3
  from flask_socketio import SocketIO, emit
4
  from flask_cors import CORS
5
 
6
- app = Flask(__name__)
7
- CORS(app) # Erlaubt den Zugriff von deiner externen Webseite
 
 
 
8
  socketio = SocketIO(app, cors_allowed_origins="*", async_mode='eventlet')
9
 
10
  # --- SPIELLOGIK ---
@@ -13,7 +17,7 @@ RANKS = {'7': 7, '8': 8, '9': 9, '10': 10, 'B': 10, 'D': 10, 'K': 10, 'A': 11}
13
 
14
  class GameState:
15
  def __init__(self):
16
- self.players = {} # email -> data
17
  self.middle = []
18
  self.deck = []
19
  self.turn_idx = 0
@@ -21,6 +25,7 @@ class GameState:
21
  self.current_bet = 50
22
 
23
  def calculate_score(self, hand):
 
24
  scores = {'Herz': 0, 'Karo': 0, 'Pik': 0, 'Kreuz': 0}
25
  for c in hand: scores[c['suit']] += c['val']
26
  ranks = [c['rank'] for c in hand]
@@ -29,7 +34,7 @@ class GameState:
29
 
30
  def broadcast_state(self):
31
  state = {
32
- 'players': [self.players[e] for e in self.active_emails],
33
  'middle': self.middle,
34
  'turn_idx': self.turn_idx
35
  }
@@ -37,9 +42,21 @@ class GameState:
37
 
38
  game = GameState()
39
 
40
- @app.route('/login', methods=['POST'])
 
 
 
 
 
 
41
  def login():
 
 
 
42
  data = request.json
 
 
 
43
  email = data.get('email')
44
  if email not in game.players:
45
  game.players[email] = {
@@ -48,6 +65,7 @@ def login():
48
  }
49
  return jsonify({'stats': game.players[email]})
50
 
 
51
  @socketio.on('join_game')
52
  def handle_join(data):
53
  email = data.get('email')
@@ -61,10 +79,11 @@ def start_round(data):
61
  random.shuffle(game.deck)
62
  game.middle = [game.deck.pop() for _ in range(3)]
63
  for email in game.active_emails:
64
- p = game.players[email]
65
- p['hand'] = [game.deck.pop() for _ in range(3)]
66
- p['score'] = game.calculate_score(p['hand'])
67
- p['coins'] -= int(game.current_bet)
 
68
  game.turn_idx = 0
69
  game.broadcast_state()
70
 
@@ -73,9 +92,12 @@ def handle_action(data):
73
  email = data.get('email')
74
  if not game.active_emails or game.active_emails[game.turn_idx] != email: return
75
 
76
- p = game.players[email]
 
 
77
  if data['type'] == 'swap_one':
78
- p['hand'][data['h_idx']], game.middle[data['m_idx']] = game.middle[data['m_idx']], p['hand'][data['h_idx']]
 
79
  elif data['type'] == 'swap_all':
80
  p['hand'], game.middle = game.middle, p['hand']
81
  elif data['type'] == 'knock':
@@ -91,4 +113,5 @@ def set_bet(data):
91
  game.current_bet = data.get('bet', 50)
92
 
93
  if __name__ == '__main__':
94
- socketio.run(app, host='0.0.0.0', port=7860)
 
 
1
  import random
2
+ import os
3
+ from flask import Flask, request, jsonify, render_template
4
  from flask_socketio import SocketIO, emit
5
  from flask_cors import CORS
6
 
7
+ # 1. WICHTIG: template_folder explizit angeben, falls HF die Struktur nicht erkennt
8
+ app = Flask(__name__, template_folder="templates")
9
+ # 2. WICHTIG: CORS muss extrem offen sein für externe Webseiten
10
+ CORS(app, resources={r"/*": {"origins": "*"}})
11
+
12
  socketio = SocketIO(app, cors_allowed_origins="*", async_mode='eventlet')
13
 
14
  # --- SPIELLOGIK ---
 
17
 
18
  class GameState:
19
  def __init__(self):
20
+ self.players = {}
21
  self.middle = []
22
  self.deck = []
23
  self.turn_idx = 0
 
25
  self.current_bet = 50
26
 
27
  def calculate_score(self, hand):
28
+ if not hand: return 0
29
  scores = {'Herz': 0, 'Karo': 0, 'Pik': 0, 'Kreuz': 0}
30
  for c in hand: scores[c['suit']] += c['val']
31
  ranks = [c['rank'] for c in hand]
 
34
 
35
  def broadcast_state(self):
36
  state = {
37
+ 'players': [self.players[e] for e in self.active_emails if e in self.players],
38
  'middle': self.middle,
39
  'turn_idx': self.turn_idx
40
  }
 
42
 
43
  game = GameState()
44
 
45
+ # --- FEHLENDE ROUTE HINZUGEFÜGT ---
46
+ @app.route('/')
47
+ def index():
48
+ # Diese Route verhindert den 404 Fehler beim Aufruf der HF-URL
49
+ return render_template('index.html')
50
+
51
+ @app.route('/login', methods=['POST', 'OPTIONS'])
52
  def login():
53
+ if request.method == 'OPTIONS':
54
+ return jsonify({}), 200
55
+
56
  data = request.json
57
+ if not data or 'email' not in data:
58
+ return jsonify({'error': 'No email'}), 400
59
+
60
  email = data.get('email')
61
  if email not in game.players:
62
  game.players[email] = {
 
65
  }
66
  return jsonify({'stats': game.players[email]})
67
 
68
+ # --- SOCKET EVENTS ---
69
  @socketio.on('join_game')
70
  def handle_join(data):
71
  email = data.get('email')
 
79
  random.shuffle(game.deck)
80
  game.middle = [game.deck.pop() for _ in range(3)]
81
  for email in game.active_emails:
82
+ if email in game.players:
83
+ p = game.players[email]
84
+ p['hand'] = [game.deck.pop() for _ in range(3)]
85
+ p['score'] = game.calculate_score(p['hand'])
86
+ p['coins'] -= int(game.current_bet)
87
  game.turn_idx = 0
88
  game.broadcast_state()
89
 
 
92
  email = data.get('email')
93
  if not game.active_emails or game.active_emails[game.turn_idx] != email: return
94
 
95
+ p = game.players.get(email)
96
+ if not p: return
97
+
98
  if data['type'] == 'swap_one':
99
+ h_idx, m_idx = data['h_idx'], data['m_idx']
100
+ p['hand'][h_idx], game.middle[m_idx] = game.middle[m_idx], p['hand'][h_idx]
101
  elif data['type'] == 'swap_all':
102
  p['hand'], game.middle = game.middle, p['hand']
103
  elif data['type'] == 'knock':
 
113
  game.current_bet = data.get('bet', 50)
114
 
115
  if __name__ == '__main__':
116
+ # HF braucht Port 7860
117
+ socketio.run(app, host='0.0.0.0', port=7860, debug=False)