|
from flask import Flask, render_template, request, jsonify |
|
import json |
|
import random |
|
import os |
|
|
|
app = Flask(__name__) |
|
|
|
COINS_FILE = 'coins.json' |
|
LEADERBOARD_FILE = 'leaderboard.json' |
|
|
|
def load_coins(): |
|
if os.path.exists(COINS_FILE): |
|
with open(COINS_FILE, 'r') as f: |
|
return json.load(f) |
|
return {"bronze": {"color": "#CD7F32", "price": 0, "value": 0.10, "winrate": 0.50}} |
|
|
|
def save_coins(coins): |
|
with open(COINS_FILE, 'w') as f: |
|
json.dump(coins, f) |
|
|
|
def load_leaderboard(): |
|
if os.path.exists(LEADERBOARD_FILE): |
|
with open(LEADERBOARD_FILE, 'r') as f: |
|
return json.load(f) |
|
return [] |
|
|
|
def save_leaderboard(leaderboard): |
|
with open(LEADERBOARD_FILE, 'w') as f: |
|
json.dump(leaderboard, f) |
|
|
|
@app.route('/') |
|
def index(): |
|
coins = load_coins() |
|
return render_template('index.html', coins=coins) |
|
|
|
@app.route('/flip', methods=['POST']) |
|
def flip(): |
|
data = request.json |
|
coin = data['coin'] |
|
coins = load_coins() |
|
|
|
if coin not in coins: |
|
return jsonify({"error": "Invalid coin"}), 400 |
|
|
|
result = "heads" if random.random() < coins[coin]["winrate"] else "tails" |
|
value = coins[coin]["value"] if result == "heads" else 0 |
|
|
|
return jsonify({"result": result, "value": value}) |
|
|
|
@app.route('/mint', methods=['POST']) |
|
def mint(): |
|
coins = load_coins() |
|
new_coin = generate_new_coin() |
|
coins[new_coin["name"]] = new_coin |
|
save_coins(coins) |
|
return jsonify(new_coin) |
|
|
|
@app.route('/leaderboard', methods=['GET', 'POST']) |
|
def leaderboard(): |
|
if request.method == 'POST': |
|
data = request.json |
|
leaderboard = load_leaderboard() |
|
leaderboard.append({"initials": data["initials"], "score": data["score"]}) |
|
leaderboard.sort(key=lambda x: x["score"], reverse=True) |
|
leaderboard = leaderboard[:10] |
|
save_leaderboard(leaderboard) |
|
return jsonify({"status": "success"}) |
|
else: |
|
return jsonify(load_leaderboard()) |
|
|
|
def generate_new_coin(): |
|
name = f"coin_{random.randint(1000, 9999)}" |
|
color = "#{:06x}".format(random.randint(0, 0xFFFFFF)) |
|
winrate = random.uniform(0.3, 0.7) |
|
value = random.uniform(0.05, 1.0) |
|
price = int((winrate * value * 100) + random.uniform(-10, 10)) |
|
|
|
return { |
|
"name": name, |
|
"color": color, |
|
"price": max(1, price), |
|
"value": round(value, 2), |
|
"winrate": round(winrate, 2) |
|
} |
|
|
|
if __name__ == '__main__': |
|
app.run(debug=True) |