<?php

// Configuration values
define('DATA_PATH', 'ppcmarket.txt');
// Get market data from file
function market_info()
{
    $info = json_decode(file_get_contents(DATA_PATH));
    return $info;
}
// Return a JSON array of price / market cap / total supply
echo JSON_encode(market_info());
function update_market_info() {
    $info = fetch_cmc_market_info(); // CMC sourced

    if ($info['price'] == 0) { // CMC is freaking out, grab from Vircurex instead
        // Grab our old market info for reference to use its total supply
        $old_info = market_info();

        // "Dumb mode" only updates price and market cap, not total supply (can add later)
        $price = fetch_vircurex_market_price();
        $market_cap = $price * $old_info['total_supply']; // Calculate a market cap based on Vircurex price and last known supply
        
        $info['price'] = $price;
        $info['market_cap'] = round($market_cap, 2);
        $info['total_supply'] = $old_info['total_supply'];
    }

    $info['updated'] = time();
    file_put_contents(DATA_PATH, json_encode($info));

}