function buyCoin($user_id, $cc_id, $bill_id, $cpid, $tx_method, $order_total, $coins, $cc_id)
{
    $data = array();
    $bool = false;
    if ($tx_method == 'cc') {
        $tx_id = _payByCc();
    } else {
        // neteller
        $tx_id = _payByNeteller();
    }
    if ($tx_id != 'error') {
        $now = time();
        $q = "INSERT INTO transactions ";
        $q .= "(tr_tx_id, tr_method, user_id, tr_cc_nt_id, bill_id, cpid, tr_gw_tx_id, tr_amount, tr_coins, tr_date, tr_status) ";
        $q .= "VALUES";
        $q .= "('{$tx_id}', '{$tx_method}', '{$user_id}', '{$cc_id}', '{$bill_id}', '{$cpid}', '', '{$order_total}', '{$coins}', '{$now}', 1)";
        mysql_query($q);
        increaseUserCoins($user_id, $coins);
        insertUserCoins($user_id, $coins, $tx_id);
        $data['status'] = 'success';
        $data['message'] = '';
        $data['tx_id'] = $tx_id;
        $data['tx_date'] = $now;
    } else {
        $data['status'] = 'error';
        $data['message'] = 'error message here';
        $data['tx_id'] = false;
        $data['tx_date'] = false;
    }
    return $data;
}
function insertUserWinnings($winners, $coin_div, $total_bet_amt, $game_id)
{
    global $config;
    $basedir = $config['basedir'];
    $do_write = false;
    // get the cache file
    $cache_file = $config['basedir'] . '/temp/all_users.txt';
    if (!file_exists($cache_file)) {
        return;
    }
    $all_users = json_decode(file_get_contents($cache_file), TRUE);
    $won_users = array();
    $file = $basedir . '/temp/all_coin_deals.txt';
    if (file_exists($file)) {
        $data = json_decode(file_get_contents($file), true);
    } else {
        $data = array();
    }
    foreach ($winners as $w) {
        $user_id = $w['user_id'];
        $total_user_bet = $w['total_bet'];
        //$total_win_amt = $total_user_bet - ($total_user_bet * ($house_com / 100));
        $ration = $total_user_bet / $total_bet_amt * 100;
        // get the percentage of the user's total take
        $total_win_amt = $ration * $coin_div;
        // multiply by coins
        $now = time();
        $won_users[] = $all_users[$w['user_id']];
        increaseUserCoins($user_id, $total_win_amt);
        $q = "INSERT INTO coin_deals (user_id, g_id, cd_amount, cd_inout, tx_id, cd_type, cd_tx_date) VALUES ('{$user_id}', '{$game_id}', '{$total_win_amt}', 'in', '0', 'bet winning', '{$now}')";
        mysql_query($q);
        // append all_coin_deals.txt cache
        $coindeal['cd_id'] = mysql_insert_id();
        $coindeal['user_id'] = $user_id;
        $coindeal['g_id'] = $game_id;
        $coindeal['cd_amount'] = $total_win_amt;
        $coindeal['cd_inout'] = 'in';
        $coindeal['tx_id'] = 0;
        $coindeal['cd_type'] = 'bet winning';
        $coindeal['cd_tx_date'] = $now;
        if ($data) {
            $data[] = $coindeal;
            $do_write = true;
        }
        $activity = 'won';
        $seen = 0;
        $fieldname = 'g_id';
        $fieldvalue = $game_id;
        addActivity($user_id, $activity, $seen, $fieldname, $fieldvalue);
    }
    if ($do_write) {
        unlink($file);
        file_put_contents($file, json_encode($data));
    }
    notifyWonUsers($won_users, $game_id);
    return true;
}