コード例 #1
0
/**
 * Add items to a user inventory. If cancel=true, we check if the current user is owner of the goods
 *
 * @global type $UMC_USER
 * @param type $id
 * @param type $amount
 * @param type $table
 * @param boolean $cancel
 * @param type $to_deposit
 * @param string $uuid
 * @return string
 */
function umc_checkout_goods($id, $amount, $table = 'stock', $cancel = false, $to_deposit = false, $uuid = false)
{
    global $UMC_USER, $UMC_ENV;
    XMPP_ERROR_trace(__FUNCTION__, func_get_args());
    if (!$uuid) {
        $player = $UMC_USER['username'];
        $uuid = $UMC_USER['uuid'];
    } else {
        $player = umc_user2uuid($uuid);
    }
    if (!is_numeric($id)) {
        umc_error('{red}Invalid ID. Please use {yellow}/shophelp;');
    }
    // the fact that the source is also a condition prevents people to cancel other users' items.
    if ($table == 'stock') {
        if ($cancel) {
            $sql = "SELECT * FROM minecraft_iconomy.stock WHERE uuid='{$uuid}' AND id='{$id}' LIMIT 1;";
        } else {
            $sql = "SELECT * FROM minecraft_iconomy.stock WHERE id='{$id}' LIMIT 1;";
        }
    } else {
        if ($table == 'deposit') {
            $sql = "SELECT * FROM minecraft_iconomy.deposit WHERE (sender_uuid='{$uuid}' OR recipient_uuid='{$uuid}') AND id='{$id}' LIMIT 1;";
        }
    }
    $D = umc_mysql_fetch_all($sql);
    if (count($D) == 0) {
        umc_error("{red}Id {white}{$id}{red} not found! Please try again.;");
    } else {
        $row = $D[0];
        $item = umc_goods_get_text($row['item_name'], $row['damage'], $row['meta']);
        $meta_cmd = $meta = '';
        if ($row['meta'] != '') {
            $meta_arr = unserialize($row['meta']);
            if (!is_array($meta_arr)) {
                XMPP_ERROR_trigger("Could not get Meta Data array for {$table} id {$id}: " . var_export($row, true));
            }
            if ($row['item_name'] == "banner") {
                $meta_cmd = umc_banner_get_data($meta_arr);
            } else {
                foreach ($meta_arr as $type => $lvl) {
                    $meta_cmd .= " {$type}:{$lvl}";
                }
            }
        }
        // handle unlimited items
        $unlimited = false;
        if ($row['amount'] == -1) {
            $row['amount'] = $amount;
            $unlimited = true;
        }
        //umc_echo('There were ' . $row['amount'] . " pieces of " . $item['item_name'] . "$meta_txt stored.");
        // determine withdrawal amount
        if (is_numeric($amount) && $amount <= $row['amount']) {
            $sellamount = $amount;
        } else {
            if ($amount == 'max') {
                // withdraw all
                $sellamount = $row['amount'];
                //umc_echo("You are withdrawing all ($sellamount) {$item['name']}$meta_txt");
            } else {
                if (is_numeric($amount) && $amount > $row['amount']) {
                    umc_echo("{yellow}[!]{gray} Available amount ({yellow}{$row['amount']}{gray}) less than amount specified ({yellow}{$amount}{gray})");
                    $sellamount = $row['amount'];
                } else {
                    umc_error("{red}Amount {white}'{$amount}'{red} is not numeric;");
                }
            }
        }
        if ($table != 'stock') {
            umc_echo("{green}[+]{gray} You are withdrawing {yellow} {$amount} {gray} of {$item['full']}{gray}.");
        }
        if ($table == 'stock') {
            $cost = $sellamount * $row['price'];
            if ($cancel) {
                $target = $uuid;
                $source = 'cancel00-sell-0000-0000-000000000000';
            } else {
                $target = $uuid;
                $source = $row['uuid'];
            }
        } else {
            if ($table == 'deposit') {
                if ($row['recipient_uuid'] == $uuid) {
                    $cancel = true;
                }
                $cost = 0;
                if ($cancel) {
                    $target = $uuid;
                    $source = 'cancel00-depo-0000-0000-000000000000';
                } else {
                    $target = $row['recipient_uuid'];
                    $source = $row['sender_uuid'];
                }
            }
        }
        if (!$to_deposit) {
            umc_check_space($sellamount, $item['item_name'], $item['type']);
            // the in-game command does not understand item_names yet
            umc_ws_cmd("give {$player} {$item['item_name']}:{$item['type']} {$sellamount}{$meta_cmd};", 'asConsole');
            umc_log('inventory', 'give', "{$player} received {$item['full_clean']} {$sellamount}");
        } else {
            umc_deposit_give_item($target, $item['item_name'], $item['type'], $meta, $sellamount, $source);
            umc_log('inventory', 'give_deposit', "{$player} recived in deposit {$item['full_clean']} {$sellamount}");
        }
        //umc_echo("./give $player {$item['id']}:{$item['type']} $sellamount$meta_cmd");
        // check status
        umc_shop_transaction_record($source, $target, $sellamount, $cost, $item['item_name'], $item['type'], $meta);
        if ($unlimited) {
            return "unlimited";
        }
        // fix the stock levels
        $amount_left = umc_db_take_item($table, $id, $sellamount, $source);
        if ($UMC_ENV == 'websend') {
            if ($amount_left == 0) {
                umc_echo("{green}[+]{gray} No more {green}{$item['full']}{gray} now in stock.");
            } else {
                umc_echo("{green}[+]{yellow} {$amount_left}{green} {$item['full']}{gray} remaining in stock.");
            }
        }
        return $amount_left;
    }
}
コード例 #2
0
/**
 * Buy items
 *
 * @global type $UMC_USER
 * @param type $to_deposit
 * @return type
 */
function umc_do_buy_internal($to_deposit = false)
{
    global $UMC_USER;
    $player = $UMC_USER['username'];
    $args = $UMC_USER['args'];
    $uuid = $UMC_USER['uuid'];
    $userlevel = $UMC_USER['userlevel'];
    // we check first if the deposit is empty before we allow trading
    $remaining = umc_depositbox_checkspace($uuid, $userlevel);
    $minimum = 0;
    if ($to_deposit) {
        $minimum = 1;
    }
    if ($remaining < $minimum) {
        umc_log("shop", "cancel_sale", "sale cancelled, inventory full");
        umc_error("You cannot trade items if your deposit is overfilled!");
    }
    $do_check = false;
    $pos = array_search('check', $args);
    if ($pos) {
        $do_check = true;
        array_splice($args, $pos, 1);
    }
    if (!isset($args[2])) {
        umc_show_help();
        return;
    } elseif (!is_numeric($args[2])) {
        umc_error("{red}Invalid shop_id: '{yellow}{$args[2]}{red}'. Did you mean '{yellow}/find {$args[2]}{red}'?;");
    }
    $id = $args[2];
    if (!isset($args[3])) {
        umc_echo("{yellow}[!]{gray} You did not enter an amount. Attempting to buy all.");
        $amount = false;
    }
    $amount = umc_sanitize_input($args[3], 'amount');
    $sql = "SELECT * FROM minecraft_iconomy.stock WHERE id='{$id}' LIMIT 1;";
    $data_row = umc_mysql_fetch_all($sql);
    if (count($data_row) == 0) {
        umc_error("Sorry, there is no shop ID {$id}! Please try again.");
    }
    $row = $data_row[0];
    $seller = umc_user2uuid($row['uuid']);
    $item = umc_goods_get_text($row['item_name'], $row['damage'], $row['meta']);
    if ($row) {
        if (!$amount && $row['amount'] == -1) {
            umc_error("{red}That item has an unlimited supply. Please enter an amount!;");
        }
        if ($row['uuid'] == $uuid && $player != 'uncovery') {
            umc_error("{red}You cannot buy your own goods. Use {yellow}/cancel <id>{red} instead!;");
        }
        if (!$amount || $amount > $row['amount'] && $row['amount'] != -1) {
            // buy as much as possible
            $amount = $row['amount'];
        }
        $sum = $amount * $row['price'];
        if ($do_check) {
            umc_echo("{white}[?] {gray}Buying {yellow}{$amount} {$item['full']}{gray} for {cyan}{$row['price']}{gray} each from {gold}{$seller}");
        } else {
            umc_echo("{green}[+] {gray}Buying {yellow}{$amount} {$item['full']}{gray} for {cyan}{$row['price']}{gray} each from {gold}{$seller}");
        }
        $balance = umc_money_check($player);
        if ($balance < $sum) {
            umc_echo("{red}[!]{gray} Insufficient funds ({white}{$sum}{gray} needed).;{purple}[?]{white} Why don't you vote for the server and try again?");
        } else {
            $new_balance = $balance - $sum;
            if ($do_check) {
                umc_echo("{white}[?]{gray} You would have {green}{$new_balance} Uncs{gray} remaining after spending {cyan}{$sum}", true);
                return;
            }
            if (!$to_deposit) {
                umc_check_space($amount, $item['item_name'], $item['type']);
            }
            umc_echo("{green}[+]{gray} You have {green}{$new_balance} Uncs{gray} remaining after spending {cyan}{$sum}");
            // transfer money player1 > player2
            umc_money($uuid, $row['uuid'], $sum);
            $seller = umc_user2uuid($row['uuid']);
            umc_log('shop', 'buy', "{$player} bought from {$seller}/{$row['uuid']} {$amount} {$item['full_clean']} for {$sum}, money was tranferred");
            // give goods to player1
            $leftover = umc_checkout_goods($id, $amount, 'stock', false, $to_deposit);
            $msg = "{$player} bought {$amount} {$item['name']} for {$row['price']} Uncs/pcs (= {$sum} Uncs)! {$leftover} left in stock!;";
            umc_msg_user($seller, $msg);
        }
    } else {
        umc_error("{red}The shop-id {white}{$id}{red} could not be found. Please use {yellow}/find{red} and{yellow} /shophelp buy;");
    }
}