1kcoinsA / app.py
Sergidev's picture
v1
570a1ab
from flask import Flask, render_template, jsonify, request
from flask import session
from flask_session import Session
import json
import random
import os
app = Flask(__name__)
app.config['SECRET_KEY'] = os.urandom(24)
app.config['SESSION_TYPE'] = 'filesystem'
Session(app)
# Load coins from JSON file
def load_coins():
if not os.path.exists('coins.json'):
return [{"id": 1, "color": "#CD7F32", "price": 0, "value": 0.10, "winrate": 0.50}]
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():
if 'coins' not in session:
session['coins'] = load_coins()
return jsonify(session['coins'])
@app.route('/api/flip', methods=['POST'])
def flip_coin():
data = request.json
coin_id = data['coinId']
if 'coins' not in session:
session['coins'] = load_coins()
coins = session['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():
if 'coins' not in session:
session['coins'] = load_coins()
coins = session['coins']
new_id = max(coin['id'] for coin in coins) + 1
# More balanced algorithm for cost, price, and value
base_price = 4 + (new_id - 1) * 2 # Base price increases with each new coin
winrate = random.uniform(0.42, 0.98) # More balanced win rate
value = random.uniform(0.5, 1.5) * (base_price / 4) # Value scales with base price
price = int(base_price * (1 + random.uniform(-0.1, 0.1))) # Price varies slightly around base
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)
session['coins'] = coins
session.modified = True
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)