-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonanywhere_app.py
More file actions
77 lines (64 loc) · 2.14 KB
/
pythonanywhere_app.py
File metadata and controls
77 lines (64 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from flask import Flask, render_template, send_from_directory, redirect, jsonify, request
import os
import logging
app = Flask(__name__)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/game')
def game():
return redirect('/')
@app.route('/play')
def play_direct():
return render_template('play.html')
@app.route('/assets/<path:filename>')
def assets(filename):
try:
return send_from_directory('assets', filename)
except Exception as e:
logger.error(f"Error serving asset {filename}: {e}")
return "", 404
@app.route('/static/<path:filename>')
def static_files(filename):
try:
return send_from_directory('static', filename)
except Exception as e:
logger.error(f"Error serving static file {filename}: {e}")
return "", 404
@app.route('/api/highscore', methods=['GET'])
def get_highscore():
try:
with open('highscore.txt', 'r') as f:
score = int(f.read().strip())
return jsonify({'highscore': score})
except:
return jsonify({'highscore': 0})
@app.route('/api/highscore', methods=['POST'])
def update_highscore():
try:
data = request.get_json()
new_score = int(data.get('score', 0))
try:
with open('highscore.txt', 'r') as f:
current_score = int(f.read().strip())
except:
current_score = 0
if new_score > current_score:
with open('highscore.txt', 'w') as f:
f.write(str(new_score))
return jsonify({'success': True, 'highscore': new_score})
else:
return jsonify({'success': False, 'message': 'Score not high enough'})
except Exception as e:
logger.error(f"Error updating high score: {e}")
return jsonify({'success': False, 'message': str(e)}), 400
@app.route('/health')
def health_check():
return jsonify({'status': 'healthy'})
@app.errorhandler(404)
def page_not_found(e):
return render_template('index.html'), 200
if __name__ == '__main__':
app.run(debug=True)