|
from flask import Flask, render_template, jsonify, request |
|
import json |
|
import random |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
def load_coins(): |
|
with open('coins.json', 'r') as f: |
|
return json.load(f) |
|
|
|
|
|
def save_coins(coins): |
|
with open('coins.json', 'w') as f: |
|
json.dump(coins, f) |
|
|
|
@app.route('/') |
|
def index(): |
|
return render_template('index.html') |
|
|
|
@app.route('/api/coins') |
|
def get_coins(): |
|
return jsonify(load_coins()) |
|
|
|
@app.route('/api/flip', methods=['POST']) |
|
def flip_coin(): |
|
data = request.json |
|
coin_id = data['coinId'] |
|
coins = load_coins() |
|
coin = next((c for c in coins if c['id'] == coin_id), None) |
|
|
|
if coin and random.random() < coin['winrate']: |
|
return jsonify({'result': 'heads', 'value': coin['value']}) |
|
else: |
|
return jsonify({'result': 'tails', 'value': 0}) |
|
|
|
@app.route('/api/mint', methods=['POST']) |
|
def mint_coin(): |
|
coins = load_coins() |
|
new_id = max(coin['id'] for coin in coins) + 1 |
|
|
|
|
|
winrate = random.uniform(0.1, 0.9) |
|
value = random.uniform(0.1, 2.0) |
|
price = int(max(1, (winrate * value * 100) ** 1.5)) |
|
|
|
new_coin = { |
|
'id': new_id, |
|
'color': f'#{random.randint(0, 0xFFFFFF):06x}', |
|
'price': price, |
|
'value': round(value, 2), |
|
'winrate': round(winrate, 2) |
|
} |
|
|
|
coins.append(new_coin) |
|
save_coins(coins) |
|
return jsonify(new_coin) |
|
|
|
@app.route('/api/leaderboard', methods=['GET', 'POST']) |
|
def leaderboard(): |
|
leaderboard_file = 'leaderboard.json' |
|
|
|
if request.method == 'POST': |
|
data = request.json |
|
initials = data['initials'] |
|
score = data['score'] |
|
|
|
try: |
|
with open(leaderboard_file, 'r') as f: |
|
leaderboard = json.load(f) |
|
except FileNotFoundError: |
|
leaderboard = [] |
|
|
|
leaderboard.append({'initials': initials, 'score': score}) |
|
leaderboard.sort(key=lambda x: x['score'], reverse=True) |
|
leaderboard = leaderboard[:10] |
|
|
|
with open(leaderboard_file, 'w') as f: |
|
json.dump(leaderboard, f) |
|
|
|
return jsonify({'success': True}) |
|
else: |
|
try: |
|
with open(leaderboard_file, 'r') as f: |
|
leaderboard = json.load(f) |
|
except FileNotFoundError: |
|
leaderboard = [] |
|
|
|
return jsonify(leaderboard) |
|
|
|
if __name__ == '__main__': |
|
app.run(debug=True, host='0.0.0.0', port=7860) |