/**
* Removes an item from stock or deposit; does not record the transaction
*
* @param type $table
* @param type $id
* @param type $amount
* @param type $player
* @return int
*/
function umc_db_take_item($table, $id, $amount, $player)
{
// $uuid = umc_uuid_getone($player, 'uuid');
$D = umc_mysql_fetch_all("SELECT amount FROM minecraft_iconomy.{$table} WHERE id='{$id}';");
$amount_row = $D[0];
$newstock = $amount_row['amount'] - $amount;
if ($table == 'stock') {
if ($newstock == 0) {
$sql = "DELETE FROM minecraft_iconomy.stock WHERE id='{$id}';";
umc_log('shop', 'stock_adjust', "Cleared all content from stock for ID {$id} by withdrawing {$amount_row['amount']}");
} else {
$sql = "UPDATE minecraft_iconomy.stock SET amount={$newstock} WHERE id='{$id}';";
umc_log('shop', 'stock_adjust', "Changed stock level for ID {$id} from {$amount_row['amount']} to {$newstock}");
}
} else {
// take from deposit
if ($newstock == 0) {
$sql = "DELETE FROM minecraft_iconomy.deposit WHERE id='{$id}';";
umc_log('shop', 'deposit_adjust', "Cleared all content from deposit for ID {$id} by withdrawing {$amount_row['amount']}");
} else {
$sql = "UPDATE minecraft_iconomy.deposit SET amount={$newstock} WHERE id='{$id}';";
umc_log('shop', 'deposit_adjust', "Changed deposit level for ID {$id} from {$amount_row['amount']} to {$newstock}");
}
}
umc_mysql_query($sql, true);
// check stock levels
$sql = "SELECT * FROM minecraft_iconomy.{$table} WHERE id={$id};";
$D2 = umc_mysql_fetch_all($sql);
if (count($D2)) {
return $D2[0]['amount'];
} else {
return 0;
}
}
function umc_ticket_close()
{
global $WSEND;
$player = $WSEND['player'];
$player_id = umc_user_id($player);
$args = $WSEND['args'];
if (!isset($args[1]) && !isset($args[2])) {
umc_show_help($args);
die;
}
$key = intval($args[2]);
if (!is_numeric($key)) {
umc_error("Invalid ticket ID");
}
$sql = "SELECT * FROM minecraft.wp_wpscst_tickets WHERE user_id={$player_id} AND resolution LIKE 'Open' AND primkey='{$key}';";
$D = umc_mysql_fetch_all($sql);
if (count($D) == 0) {
umc_error("Ticket ID {$key} not found!");
}
$sql2 = "UPDATE minecraft.`wp_wpscst_tickets` SET `resolution` = 'Closed' WHERE `primkey`={$key};";
umc_mysql_fetch_all($sql2);
umc_echo("Ticket ID {$key} was successfully closed!");
}
/**
* Reset TS user rights to "Guest" for a specific user
* This can be done even if the user is not online
*
* @param string $uuid
* @param boolean $echo
* @return boolean
*/
function umc_ts_clear_rights($uuid, $echo = false)
{
XMPP_ERROR_trace(__FUNCTION__, func_get_args());
umc_echo("Trying to remove old permissions:");
require_once '/home/includes/teamspeak_php/libraries/TeamSpeak3/TeamSpeak3.php';
global $UMC_TEAMSPEAK;
// find out the TS id the user has been using from the database
$check_sql = "SELECT ts_uuid FROM minecraft_srvr.UUID WHERE UUID='{$uuid}';";
$D = umc_mysql_fetch_all($check_sql);
if ($D[0]['ts_uuid'] == '') {
if ($echo) {
umc_echo("Old Client: No previous TS account detected.");
}
return false;
} else {
umc_echo("Found old permissions.");
$ts_uuid = $D[0]['ts_uuid'];
}
umc_echo("Connecting to TS server.");
if (!$UMC_TEAMSPEAK['server']) {
$UMC_TEAMSPEAK['server'] = TeamSpeak3::factory("serverquery://queryclient:[email protected]:10011/?server_port=9987");
}
// find the TS user by that TS UUID
umc_echo("Searching for you on the TS server.");
$ts_Clients_match = $UMC_TEAMSPEAK['server']->clientFindDb($ts_uuid, true);
if (count($ts_Clients_match) > 0) {
umc_echo("Found user entries on TS server");
$client_dbid = $ts_Clients_match[0];
// enumerate all the groups the user is part of
$servergroups = array_keys($UMC_TEAMSPEAK['server']->clientGetServerGroupsByDbid($client_dbid));
// remove all servergroups except 8 (Guest)
umc_echo("Removing all old usergroups:");
foreach ($servergroups as $sgid) {
if ($sgid != 8) {
$UMC_TEAMSPEAK['server']->serverGroupClientDel($sgid, $client_dbid);
if ($echo) {
umc_echo("Old Client: Removing Group " . $UMC_TEAMSPEAK['ts_groups'][$sgid]);
}
}
}
// also remove TS UUID from DB
$ins_sql = "UPDATE minecraft_srvr.UUID SET ts_uuid='' WHERE ts_uuid='{$ts_uuid}';";
umc_mysql_query($ins_sql, true);
return true;
} else {
if ($echo) {
umc_echo("Old Client: Previous TS UUID was invalid, nothing to do");
}
return false;
}
}
function umc_bottomkarma()
{
XMPP_ERROR_trace(__FUNCTION__, func_get_args());
$sql = "SELECT SUM(karma) as sum_karma, receivers.username as receiver_name FROM minecraft_srvr.karma\r\n LEFT JOIN minecraft_srvr.UUID as senders ON sender_uuid=senders.UUID\r\n LEFT JOIN minecraft_srvr.UUID as receivers ON receiver_uuid=receivers.UUID\r\n WHERE senders.lot_count > 0 AND receivers.lot_count > 0\r\n GROUP BY receivers.username\r\n HAVING sum(karma) < 0\r\n ORDER BY sum(karma) ASC LIMIT 0,10";
$D = umc_mysql_fetch_all($sql);
umc_echo("Bottom ten Karma users:");
umc_echo("-∞ => Uncovery");
foreach ($D as $row) {
$sum_karma = $row['sum_karma'];
$receiver = $row['receiver_name'];
if (!umc_user_is_banned($receiver)) {
umc_echo("{$sum_karma} => {$receiver}");
}
}
}
function umc_donation_level($user, $debug = false)
{
XMPP_ERROR_trace(__FUNCTION__, func_get_args());
$U = umc_uuid_getboth($user);
$uuid = $U['uuid'];
$username = $U['username'];
$debug_txt = '';
global $UMC_SETTING;
$date_now = new DateTime("now");
$sql = "SELECT amount, date FROM minecraft_srvr.donations WHERE uuid='{$uuid}';";
$level = umc_get_uuid_level($uuid);
if ($level == 'Owner') {
return false;
}
$D = umc_mysql_fetch_all($sql);
// if there are 0 donations, user should not be changes
if (count($D) == 0 && strstr($level, "Donator")) {
XMPP_ERROR_trigger("User {$username} ({$uuid}) never donated but has a donator level ({$level})");
} else {
if (count($D) == 0) {
$debug_txt .= "{$username} ({$uuid}) does not have any donations\n";
return;
}
}
$debug_txt .= "Checking donation upgrade of user {$username}, current UserLevel: {$level}\n";
$donation_level = 0;
// go through all donations and find out how much is still active
foreach ($D as $row) {
$date_donation = new DateTime($row['date']);
$interval = $date_donation->diff($date_now);
$years = $interval->format('%y');
$months = $interval->format('%m');
$donation_term = $years * 12 + $months;
$donation_leftover = $row['amount'] - $donation_term;
if ($donation_leftover < 0) {
$donation_leftover = 0;
// do not create negative carryforward
}
$donation_level = $donation_level + $donation_leftover;
$debug_txt .= "Amount donated {$row['amount']} {$years} years {$months} m ago = {$donation_term} months ago, {$donation_leftover} leftover, level: {$donation_level}\n";
}
$donation_level_rounded = ceil($donation_level);
// get userlevel and check if demotion / promotion is needed
$debug_txt .= "user {$username} ({$uuid}) has donation level of {$donation_level_rounded}, now is {$level}\n";
// current userlevel
$ranks_lvl = array_flip($UMC_SETTING['ranks']);
$cur_lvl = $ranks_lvl[$level];
// get current promotion level
if (strpos($level, 'DonatorPlus')) {
$current = 2;
} else {
if (strpos($level, 'Donator')) {
$current = 1;
} else {
$current = 0;
}
}
// get future promotion level
if (count($D) == 0) {
// this never happens since it's excluded above
$future = 0;
} else {
if ($donation_level_rounded >= 1) {
$future = 2;
} else {
if ($donation_level_rounded < 1) {
$future = 1;
}
}
}
$debug_txt .= "future = {$future}, current = {$current}\n";
$change = $future - $current;
if ($change == 0) {
$debug_txt .= "User has right level, nothing to do\n";
return false;
// bail if no change needed
} else {
// we have a change in level, let's get an error report
$debug = true;
}
$debug_txt .= "User will change {$change} levels\n";
// get currect rank index
$debug_txt .= "Current Rank index = {$cur_lvl}\n";
// calculate base level
$base_lvl = $cur_lvl - $current;
$debug_txt .= "User base level = {$base_lvl}\n";
$new_lvl = $base_lvl + $future;
if ($new_lvl == $cur_lvl) {
XMPP_ERROR_send_msg("Donations upgrade: Nothing to do, CHECK this should have bailed earlier!");
return false;
}
$new_rank = $UMC_SETTING['ranks'][$new_lvl];
$debug_txt .= "User {$username} upgraded from {$level} to {$new_rank}\n";
umc_exec_command("pex user {$uuid} group set {$new_rank}");
umc_log('Donations', 'User Level de/promotion', "User {$username} upgraded from {$level} to {$new_rank}");
if ($debug) {
XMPP_ERROR_send_msg($debug_txt);
}
return $donation_level_rounded;
// . "($donation_level $current - $future - $change)";
//.........这里部分代码省略.........
/**
* this checks if the user exists, and creats it if not. returns the amount in the account.
* @global type $UMC_ENV
* @param type $user
* @return int
*/
function umc_money_check($user)
{
XMPP_ERROR_trace(__FUNCTION__, func_get_args());
if ($user == '') {
XMPP_ERROR_trigger("Failed to get account status for empty user!");
}
// check if the user has an acoount
if (strlen($user) <= 17) {
$uuid = umc_user2uuid($user);
} else {
$uuid = $user;
$user = umc_user2uuid($uuid);
if ($user == '') {
XMPP_ERROR_trigger("Failed to get username for {$uuid}!");
die;
}
}
$sql = "SELECT balance FROM `minecraft_iconomy`.`mineconomy_accounts` WHERE uuid='{$uuid}';";
$data = umc_mysql_fetch_all($sql);
// has account to receive, return value
if (count($data) > 0) {
// get amount
return $data[0]['balance'];
} else {
if (count($data) == 0) {
// create account
// check if there is a user entry but no UUID
$sql2 = "SELECT balance FROM `minecraft_iconomy`.`mineconomy_accounts` WHERE account='{$user}';";
$data2 = umc_mysql_fetch_all($sql2);
if (count($data2) == 1) {
// yes, add the UUID
$fix_sql = "UPDATE `minecraft_iconomy`.`mineconomy_accounts`\r\n\t\t SET uuid='{$uuid}'\r\n WHERE account='{$user}';";
umc_mysql_query($fix_sql, true);
// try again
return umc_money_check($user);
} else {
// insert row
$sql3 = "INSERT INTO `minecraft_iconomy`.`mineconomy_accounts` (`uuid`, `account`, `balance`)\r\n VALUES ('{$uuid}', '{$user}', '100');";
umc_mysql_query($sql3, true);
return 0;
}
}
}
}
请发表评论