File size: 2,562 Bytes
710118f edd627f 710118f edd627f 710118f edd627f 710118f edd627f 710118f edd627f 710118f edd627f 710118f edd627f 710118f edd627f 710118f edd627f 710118f edd627f 710118f edd627f 710118f edd627f 710118f edd627f 710118f edd627f 710118f edd627f 710118f edd627f 710118f edd627f 710118f |
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
from flask import Flask, render_template, jsonify, request
import json
import random
app = Flask(__name__)
# Load coins from JSON file
def load_coins():
with open('coins.json', 'r') as f:
return json.load(f)
# Save coins to JSON file
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
# Algorithm to balance cost, price, and value
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] # Keep only top 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) |