Sergidev commited on
Commit
320f5d6
·
verified ·
1 Parent(s): aafbb78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -43
app.py CHANGED
@@ -1,71 +1,89 @@
1
- from flask import Flask, request, jsonify, render_template
2
  import random
3
  import string
 
 
4
  import time
5
 
6
  app = Flask(__name__)
7
 
8
- # In-memory storage for game sessions
9
  sessions = {}
 
10
 
11
  def generate_session_id():
12
  return ''.join(random.choices(string.digits, k=4))
13
 
14
- @app.route('/')
15
- def index():
16
- return render_template('index.html')
17
 
18
- @app.route('/play', methods=['POST'])
19
- def play():
20
- session_id = generate_session_id()
21
- while session_id in sessions:
 
 
22
  session_id = generate_session_id()
23
- sessions[session_id] = {
24
- 'players': 1,
25
- 'words': {},
26
- 'guesses': set(),
27
- 'incorrect_guesses': 0,
28
- 'current_player': None,
29
- 'last_activity': time.time()
30
- }
31
- return jsonify({'session_id': session_id})
 
 
 
 
 
32
 
33
- @app.route('/join/<session_id>', methods=['POST'])
34
- def join(session_id):
35
  if session_id not in sessions:
36
- return jsonify({'error': 'Session not found'}), 404
37
- if sessions[session_id]['players'] == 2:
38
- return jsonify({'error': 'Session is full'}), 400
39
- sessions[session_id]['players'] += 1
40
  sessions[session_id]['last_activity'] = time.time()
41
- return jsonify({'status': 'joined', 'players': sessions[session_id]['players']})
42
 
43
- @app.route('/status/<session_id>', methods=['GET'])
44
- def status(session_id):
45
- if session_id not in sessions:
46
- return jsonify({'error': 'Session not found'}), 404
 
 
 
 
47
  return jsonify({
48
- 'players': sessions[session_id]['players'],
49
- 'status': 'ready' if sessions[session_id]['players'] == 2 else 'waiting'
 
50
  })
51
 
 
 
 
 
 
 
 
 
 
52
  @app.route('/submit_word', methods=['POST'])
53
  def submit_word():
54
  data = request.json
55
  session_id = data['session_id']
56
- player = data['player']
57
  word = data['word'].lower()
58
 
59
- if session_id not in sessions:
60
- return jsonify({'error': 'Session not found'}), 404
61
  if len(word) != 7:
62
  return jsonify({'error': 'Word must be 7 letters long'}), 400
63
 
64
- sessions[session_id]['words'][player] = word
65
  sessions[session_id]['last_activity'] = time.time()
66
 
67
  if len(sessions[session_id]['words']) == 2:
68
- sessions[session_id]['current_player'] = 'player1'
69
  return jsonify({'status': 'game_started'})
70
  return jsonify({'status': 'waiting_for_opponent'})
71
 
@@ -73,19 +91,16 @@ def submit_word():
73
  def guess():
74
  data = request.json
75
  session_id = data['session_id']
76
- player = data['player']
77
  guess = data['guess'].lower()
78
 
79
- if session_id not in sessions:
80
- return jsonify({'error': 'Session not found'}), 404
81
- if player != sessions[session_id]['current_player']:
82
  return jsonify({'error': 'Not your turn'}), 400
83
 
84
  session = sessions[session_id]
85
- opponent = 'player2' if player == 'player1' else 'player1'
86
-
87
  session['guesses'].add(guess)
88
 
 
89
  if guess not in session['words'][opponent]:
90
  session['incorrect_guesses'] += 1
91
 
@@ -93,10 +108,11 @@ def guess():
93
  return jsonify({'status': 'game_over', 'winner': opponent})
94
 
95
  if all(letter in session['guesses'] for letter in session['words'][opponent]):
96
- return jsonify({'status': 'game_over', 'winner': player})
97
 
98
  session['current_player'] = opponent
99
  session['last_activity'] = time.time()
 
100
  return jsonify({'status': 'continue'})
101
 
102
  @app.route('/game_state/<session_id>')
@@ -106,6 +122,7 @@ def game_state(session_id):
106
 
107
  session = sessions[session_id]
108
  return jsonify({
 
109
  'current_player': session['current_player'],
110
  'guesses': list(session['guesses']),
111
  'incorrect_guesses': session['incorrect_guesses'],
@@ -113,5 +130,16 @@ def game_state(session_id):
113
  for player, word in session['words'].items()}
114
  })
115
 
 
 
 
 
 
 
 
 
 
 
116
  if __name__ == '__main__':
 
117
  app.run(host='0.0.0.0', port=7860)
 
 
1
  import random
2
  import string
3
+ from flask import Flask, request, jsonify, render_template
4
+ from threading import Timer
5
  import time
6
 
7
  app = Flask(__name__)
8
 
9
+ # In-memory storage for game sessions and player data
10
  sessions = {}
11
+ waiting_session = None
12
 
13
  def generate_session_id():
14
  return ''.join(random.choices(string.digits, k=4))
15
 
16
+ def generate_player_id():
17
+ return ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
 
18
 
19
+ def create_game_session():
20
+ global waiting_session
21
+ if waiting_session:
22
+ session_id = waiting_session
23
+ waiting_session = None
24
+ else:
25
  session_id = generate_session_id()
26
+ while session_id in sessions:
27
+ session_id = generate_session_id()
28
+ waiting_session = session_id
29
+
30
+ if session_id not in sessions:
31
+ sessions[session_id] = {
32
+ 'players': [],
33
+ 'words': {},
34
+ 'current_player': None,
35
+ 'guesses': set(),
36
+ 'incorrect_guesses': 0,
37
+ 'last_activity': time.time()
38
+ }
39
+ return session_id
40
 
41
+ def join_game_session(session_id):
 
42
  if session_id not in sessions:
43
+ return None
44
+ player_id = generate_player_id()
45
+ sessions[session_id]['players'].append(player_id)
 
46
  sessions[session_id]['last_activity'] = time.time()
47
+ return player_id
48
 
49
+ @app.route('/')
50
+ def index():
51
+ return render_template('index.html')
52
+
53
+ @app.route('/play', methods=['POST'])
54
+ def play():
55
+ session_id = create_game_session()
56
+ player_id = join_game_session(session_id)
57
  return jsonify({
58
+ 'session_id': session_id,
59
+ 'player_id': player_id,
60
+ 'waiting': len(sessions[session_id]['players']) == 1
61
  })
62
 
63
+ @app.route('/check_status/<session_id>', methods=['GET'])
64
+ def check_status(session_id):
65
+ if session_id in sessions:
66
+ return jsonify({
67
+ 'players': len(sessions[session_id]['players']),
68
+ 'waiting': len(sessions[session_id]['players']) == 1
69
+ })
70
+ return jsonify({'error': 'Session not found'}), 404
71
+
72
  @app.route('/submit_word', methods=['POST'])
73
  def submit_word():
74
  data = request.json
75
  session_id = data['session_id']
76
+ player_id = data['player_id']
77
  word = data['word'].lower()
78
 
 
 
79
  if len(word) != 7:
80
  return jsonify({'error': 'Word must be 7 letters long'}), 400
81
 
82
+ sessions[session_id]['words'][player_id] = word
83
  sessions[session_id]['last_activity'] = time.time()
84
 
85
  if len(sessions[session_id]['words']) == 2:
86
+ sessions[session_id]['current_player'] = sessions[session_id]['players'][0]
87
  return jsonify({'status': 'game_started'})
88
  return jsonify({'status': 'waiting_for_opponent'})
89
 
 
91
  def guess():
92
  data = request.json
93
  session_id = data['session_id']
94
+ player_id = data['player_id']
95
  guess = data['guess'].lower()
96
 
97
+ if player_id != sessions[session_id]['current_player']:
 
 
98
  return jsonify({'error': 'Not your turn'}), 400
99
 
100
  session = sessions[session_id]
 
 
101
  session['guesses'].add(guess)
102
 
103
+ opponent = [p for p in session['players'] if p != player_id][0]
104
  if guess not in session['words'][opponent]:
105
  session['incorrect_guesses'] += 1
106
 
 
108
  return jsonify({'status': 'game_over', 'winner': opponent})
109
 
110
  if all(letter in session['guesses'] for letter in session['words'][opponent]):
111
+ return jsonify({'status': 'game_over', 'winner': player_id})
112
 
113
  session['current_player'] = opponent
114
  session['last_activity'] = time.time()
115
+
116
  return jsonify({'status': 'continue'})
117
 
118
  @app.route('/game_state/<session_id>')
 
122
 
123
  session = sessions[session_id]
124
  return jsonify({
125
+ 'players': session['players'],
126
  'current_player': session['current_player'],
127
  'guesses': list(session['guesses']),
128
  'incorrect_guesses': session['incorrect_guesses'],
 
130
  for player, word in session['words'].items()}
131
  })
132
 
133
+ def cleanup_inactive_sessions():
134
+ current_time = time.time()
135
+ global waiting_session
136
+ for session_id in list(sessions.keys()):
137
+ if current_time - sessions[session_id]['last_activity'] > 60:
138
+ if session_id == waiting_session:
139
+ waiting_session = None
140
+ del sessions[session_id]
141
+ Timer(60, cleanup_inactive_sessions).start()
142
+
143
  if __name__ == '__main__':
144
+ cleanup_inactive_sessions()
145
  app.run(host='0.0.0.0', port=7860)