} else {
    // close the game; process winners
    closeGame($game_id);
    // close this game
    setBetItemWinner($bi_id);
    // bet_item db table
    setUserBetWinners($game_id, $bi_id);
    // tag the winners in user_bets db table
    $winners = getUserBetWinners($game_id, $bi_id);
    // get all user bets that won from this game
    //$total_bet_amt = returnUserBets($winners); // return users' bets and update users' coins
    $house_com_amt = $house_com / 100 * $total_bet_amt;
    $total_bet_amt_after_commission = $total_bet_amt - $house_com_amt;
    if ($total_bet_amt) {
        insertHouseCommission($game_id, $house_com_amt);
        // insert house commission in coin_deals
    }
    $bool = insertUserWinnings($winners, $coin_div, $total_bet_amt, $game_id);
    // insert users' winnings and update users' coins
}
if ($bool) {
    $all_users = getAllUsers();
    recreateCache('all_users.txt', $all_users);
    getAllUserBetsNoWinnerAndWriteToCache();
    // rewrite 'all_user_bets.php' cache file
    echo json_encode(array('error' => '', 'status' => 'success'));
    exit;
} else {
    echo json_encode(array('error' => '', 'status' => 'fail'));
    exit;
}
function getCoinDeals()
{
    $data = array();
    $q = "SELECT * FROM coin_deals";
    $result = mysql_query($q);
    $numrows = mysql_num_rows($result);
    if ($numrows) {
        while ($row = mysql_fetch_array($result)) {
            $data[] = $row;
        }
    }
    // check if no cache already exists
    if (!checkCacheExists('all_coin_deals.txt')) {
        recreateCache('all_coin_deals.txt', $data);
    }
    return $data;
}
session_start();
require_once "../include/config.php";
require_once $basedir . "/include/functions.php";
$data = array();
$private_key = $config['private_key'];
$hash = isset($_POST['hash']) ? $_POST['hash'] : 0;
$public_key = isset($_POST['public']) ? $_POST['public'] : 0;
$time = isset($_POST['t']) ? $_POST['t'] : 0;
$myhash = md5($public_key . $private_key . $time);
if ($hash != $myhash) {
    echo json_encode(array('error' => 1, 'status' => 'Hash is invalid'));
    exit;
}
$email = isset($_POST['email']) ? $_POST['email'] : false;
$username = isset($_POST['uname']) ? $_POST['uname'] : false;
$data = register(htmlspecialchars($email), htmlspecialchars($username));
if ($data == 'success') {
    $newdata = array();
    $all_users = getAllUsers();
    foreach ($all_users as $au) {
        $newdata[$au['user_id']] = $au;
    }
    recreateCache('all_users.txt', $newdata);
    echo json_encode(array('error' => 0, 'status' => $data));
    // 'success'
    exit;
} else {
    echo json_encode(array('error' => 1, 'status' => $data));
    exit;
}