zoopz-classic/functions.php

510 lines
15 KiB
PHP
Raw Normal View History

<?php
function TimeAgo($timestamp, $type = 'ago') {
// Store the current time
$current_time = time();
// Determine the difference, between the time now and the timestamp
if ($type=='ago') {
$difference = $current_time - $timestamp;
} elseif ($type=='until') {
$difference = $timestamp - $current_time;
}
// Set the periods of time
$periods = array('second', 'minute', 'hour', 'day', 'week', 'month', 'year', 'decade');
// Set the number of seconds per period
$lengths = array(1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600);
// Determine which period we should use, based on the number of seconds lapsed.
// If the difference divided by the seconds is more than 1, we use that. Eg 1 year / 1 decade = 0.1, so we move on
// Go from decades backwards to seconds
for ($val = sizeof($lengths) - 1; ($val >= 0) && (($number = $difference / $lengths[$val]) <= 1); $val--);
// Ensure the script has found a match
if ($val < 0) $val = 0;
// Determine the minor value, to recurse through
$new_time = $current_time - ($difference % $lengths[$val]);
// Set the current value to be floored
$number = floor($number);
// If required create a plural
if($number != 1) $periods[$val].= "s";
// Return text
$text = sprintf("%d %s ", $number, $periods[$val]);
// Ensure there is still something to recurse through, and we have not found 1 minute and 0 seconds.
if (($val >= 1) && (($current_time - $new_time) > 0)){
$text .= TimeAgo($new_time);
}
return $text;
}
function do_updates($type, $nextrun) {
global $dblink, $update_halfhour_nextrun, $update_day_nextrun;
if ($type == 'halfhour') {
$numupdates = floor((time()-$nextrun)/1800)+1;
$result = zoopz_query('SELECT * FROM `accounts`');
while ($line = $result->fetch_assoc()) {
$newattackturns = $line['attackturns'] + ($numupdates * 5);
if ($newattackturns > 400) {
$newattackturns = 400;
}
$maxhp = calculateMaxHP($line['zoop_level'], $line['zoop_stamina']);
if ($line['zoop_hp'] < $maxhp) {
$newhp = min(floor($line['zoop_hp'] + ($maxhp / 3)), $maxhp);
} else {
$newhp = $line['zoop_hp'];
}
$potatoesmined = $line['potatoesmined'];
if ($line['potatominers'] > 0) {
$potatoesmined += $line['potatominers'] * $numupdates;
$newpotatoes = $line['potatoes'] + ($line['potatominers'] * $numupdates);
} else {
$newpotatoes = $line['potatoes'];
}
zoopz_query('UPDATE `accounts` SET `potatoes` = ' . $newpotatoes . ' , `attackturns` = ' . $newattackturns . ' , `zoop_hp` = ' . $newhp . ' , `potatoesmined` = ' . $potatoesmined . ' WHERE `id` = ' . $line['id']);
}
$newnextrun = $nextrun+($numupdates*1800);
zoopz_query("UPDATE `updates` SET `nextrun` = '".$newnextrun."' WHERE `name` = '".$type."'");
$update_halfhour_nextrun = $newnextrun;
} elseif ($type=='day') {
$numupdates = floor((time()-$nextrun)/86400)+1;
$result = zoopz_query("SELECT * FROM `accounts`");
while ($line = $result->fetch_assoc()) {
$newtrainingpoints = $line['trainingpoints']+($numupdates*10);
if ($newtrainingpoints>100) {
$newtrainingpoints = 100;
}
$newpeopleabletobehired = min(100,$line['peopleabletobehired']+1);
zoopz_query("UPDATE `accounts` SET `trainingpoints` = '".$newtrainingpoints."' , `peopleabletobehired` = ".$newpeopleabletobehired." WHERE `id` = '".$line['id']."'");
}
zoopz_query("DELETE FROM `attacks`");
zoopz_query("DELETE FROM `votes`");
zoopz_query("UPDATE `accounts` SET `bank_deposits` = '0', `potato_sells` = '0'");
// Lint collection
zoopz_query('UPDATE `accounts` SET `zoop_lint` = `zoop_lint` + FLOOR(1 + (RAND() * 5))');
// Old accounts deletion - TODO: Reactivate? no
//zoopz_query('DELETE FROM `accounts` WHERE (' . time() . ' - `lastactive`) > 15778463');
// Week old events deletion
zoopz_query('DELETE FROM `events` WHERE (' . time() . ' - `timestamp`) > 604800');
$newnextrun = $nextrun+($numupdates*86400);
zoopz_query("UPDATE `updates` SET `nextrun` = '".$newnextrun."' WHERE `name` = '".$type."'");
$update_day_nextrun = $newnextrun;
}
}
function calculateLint($lint) {
return str_pad(($lint * 0.09991) + ($lint * 0.001327), 8, '0');
}
function userInfo($userid) {
global $dblink;
$result = zoopz_query("SELECT * FROM `accounts` WHERE `id` = '".mysqli_real_escape_string($dblink, $userid)."'");
$rows = $result->num_rows;
if ($rows>0) {
while ($line = $result->fetch_assoc()) {
return $line;
}
}
return false;
}
function zoopImage($user, $flip = false) {
$return = '<img src="/zoop.php?id=' . $user['id'];
if ($flip) {
$return .= '&amp;flip';
}
$return .= '" border="0" alt="' . $user['zoop_name'] . '" title="' . $user['zoop_name'] . ' Level ' . $user['zoop_level'] . ' (Owned by ' . $user['username'] . ')">';
return $return;
}
function itemImage($item) {
return '<img src="/img/items/' . $item['renderimage'] . '.png" border="0" alt="' . $item['name'] . '" title="' . $item['name'] . '">';
}
function itemInformation($item) {
$return = '<b>' . $item['name'] . '</b><br>' . "\n" .
$item['desc'];
if ($item['offense'] > 0) {
$return .= '<br>Offense: <b>+' . $item['offense'] . '</b>';
}
if ($item['defense'] > 0) {
$return .= '<br>Defense: <b>+' . $item['defense'] . '</b>';
}
return $return;
}
function itemEquip($user, $itemid) {
global $dblink;
zoopz_query('UPDATE `inventory` SET `equipped` = 1 WHERE `userid` = ' . $user['id'] . ' AND `itemid` = ' . mysqli_real_escape_string($dblink, $itemid) . ' LIMIT 1');
}
function itemUnequip($user, $itemid) {
global $dblink;
zoopz_query('UPDATE `inventory` SET `equipped` = 0 WHERE `userid` = ' . $user['id'] . ' AND `itemid` = ' . mysqli_real_escape_string($dblink, $itemid) . ' LIMIT 1');
}
function getUserItems($user, $onlyequipped = false, $type = '') {
$query = 'SELECT * FROM `inventory` WHERE `userid` = ' . $user['id'];
if ($onlyequipped) {
$query .= ' AND `equipped` = 1';
}
$query .= ' ORDER BY `equipped` DESC';
$result = zoopz_query($query);
$rows = $result->num_rows;
if ($rows>0) {
$return = array();
if ($type != '') {
while ($line = $result->fetch_assoc()) {
$item = getItem($line['itemid']);
if ($item['type'] == $type) {
$return[] = $line;
}
}
} else {
while ($line = $result->fetch_assoc()) {
$return[] = $line;
}
}
return $return;
}
return array();
}
function getItem($itemid) {
global $dblink;
$result = zoopz_query('SELECT * FROM `items` WHERE `id` = ' . mysqli_real_escape_string($dblink, $itemid) . ' LIMIT 1');
$rows = $result->num_rows;
if ($rows>0) {
while ($line = $result->fetch_assoc()) {
return $line;
}
}
return false;
}
function hasItem($user, $itemid) {
global $dblink;
$result = zoopz_query('SELECT * FROM `inventory` WHERE `itemid` = ' . mysqli_real_escape_string($dblink, $itemid) . ' AND `userid` = ' . $user['id'] . ' LIMIT 1');
$itemsheld = $result->num_rows;
if ($itemsheld > 0) {
return true;
} else {
return false;
}
}
function hasItemTypeEquipped($user, $itemtype) {
$result = zoopz_query('SELECT * FROM `inventory` JOIN `items` ON `inventory`.`itemid` = `items`.`id` AND `items`.`type` = \'' . $itemtype . '\' WHERE `inventory`.`userid` = ' . $user['id'] . ' AND `inventory`.`equipped` = 1 LIMIT 1');
$itemsheld = $result->num_rows;
if ($itemsheld > 0) {
return true;
} else {
return false;
}
}
function giveItem($user, $itemid) {
zoopz_query('INSERT INTO `inventory` ( `userid`, `itemid` ) VALUES ( \'' . $user['id'] . '\', \'' . $itemid . '\' )');
}
function deleteItem($user, $itemid) {
zoopz_query('DELETE FROM `inventory` WHERE `userid` = \'' . $user['id'] . '\' AND `itemid` = \'' . $itemid . '\' LIMIT 1');
}
function insertEvent($user, $message, $category = 0) {
global $dblink;
/*
1 - ?
2 - Fight
3 - Items
*/
zoopz_query('INSERT INTO `events` ( `userid`, `category`, `timestamp`, `message` ) VALUES ( \'' . $user['id'] . '\', \'' . $category . '\', \'' . time() . '\', \'' . mysqli_real_escape_string($dblink, $message) . '\' )');
}
function rewardLevelUp() {
global $user;
zoopz_query('UPDATE `accounts` SET `trainingpoints` = (`trainingpoints` + ' . ($user['zoop_level'] * 10) . ') WHERE `id` = ' . $user['id'] . '');
}
function canFight($user) {
$halfhp = (calculateMaxHP($user['zoop_level'], $user['zoop_stamina']) / 2);
if ($user['zoop_hp'] >= $halfhp) {
return true;
}
return false;
}
function calculateMaxHP($level, $stamina) {
return ($level * 50 + calculateStaminaHPBonus($stamina, $level));
}
function calculateStaminaHPBonus($stamina, $level) {
return floor($stamina * ($level * 0.25));
}
function battleEndMessage($won, $money, $potatoes, $xp) {
$return = 'You have ';
$return .= ($won) ? 'gained' : 'lost';
$return .= ' <b>' . $money . '</b> Quetzals';
if ($potatoes > 0) {
$return .= ', <b>' . $potatoes . '</b> potatoes';
}
if ($xp > 0) {
$return .= ', <b>' . $xp . '</b> XP';
}
$return .= ' from the fight.';
return $return;
}
function battleEndEvent($won, $username, $money, $potatoes, $xp) {
$return = $username . ' attacked your zoop and ';
$return .= ($won) ? 'lost!' : 'won!';
$return .= ' You ';
$return .= ($won) ? 'gained' : 'lost';
$return .= ' ' . $money . ' Quetzals';
if ($potatoes > 0) {
$return .= ' and ' . $potatoes . ' potatoes';
}
$return .= ' from the fight.';
return $return;
}
function potatoSackCapacity($user) {
$useritems = getUserItems($user, true, 'potatosack');
foreach ($useritems as $useritem) {
$item = getItem($useritem['itemid']);
return $item['special'];
}
return 0;
}
function handleBattlePotatoes($winner, $loser) {
$potatoesgained = 0;
if ($loser['potatoes'] > 0) {
$potatosackcapacity = potatoSackCapacity($winner);
if ($potatosackcapacity > 0) {
$potatoesgained = min($potatosackcapacity, $loser['potatoes']);
}
}
return $potatoesgained;
}
function handleBattleItems($user, $attacker = true) {
$useritems = getUserItems($user, true);
$attack_buff = 0;
$defend_buff = 0;
foreach ($useritems as $useritem) {
$item = getItem($useritem['itemid']);
if ($item['type'] == 'offense') {
if ($attacker) {
$attack_buff += rand(floor($item['offense'] / 3), $item['offense']);
}
}
if ($item['type'] == 'defense') {
if (!$attacker) {
$defend_buff += rand(floor($item['defense'] / 3), $item['defense']);
}
}
}
return array('atk' => $attack_buff, 'def' => $defend_buff);
}
function handleBreakableItems($user) {
$useritems = getUserItems($user, true);
foreach ($useritems as $useritem) {
$item = getItem($useritem['itemid']);
if ($item['type'] == 'offense' || $item['type'] == 'defense') {
$rand = rand(1, 333);
if ($rand == 111) {
insertEvent($user, 'Your ' . $item['name'] . ' fell apart! It wasn\'t the best of quality; it was most likely made by a zoop.', 3);
deleteItem($user, $item['id']);
}
} elseif ($item['type'] == 'potatosack') {
$rand = rand(1, 200);
if ($rand == 9) {
insertEvent($user, 'Your ' . $item['name'] . ' fell apart! You should probably go buy a new one in the market.', 3);
deleteItem($user, $item['id']);
}
}
}
}
function checkCaptcha() {
// CAPTCHA is currently disabled.
return true;
}
function resetCaptchaTimer() {
global $user;
zoopz_query('UPDATE `accounts` SET `nextcaptcha` = ' . (time() + 1200) . ' WHERE `id` = ' . $user['id'] . ' LIMIT 1');
}
function do_display() {
global $smarty, $page_main, $page_infobar, $page_menu, $page_notification_bad, $page_notification_good, $update_halfhour_nextrun, $user_isloggedin, $user;
$page_menu .= '<p class="sideBarTitle">Menu</p>
<ul>';
if ($user_isloggedin) {
$numevents = zoopz_query('SELECT *FROM `events` WHERE `userid` = ' . $user['id'] . ' AND `read` = 0');
$numevents = $numevents->num_rows;
$page_menu .= '<li><a href="?act=zoop">Your Zoop</a></li>
<li><a href="?act=events">Events';
if ($numevents > 0) {
$page_menu .= ' <b>(' . $numevents . ')</b>';
}
$page_menu .= '</a></li>
<li><a href="?act=inventory">Inventory</a></li>
<li><a href="?act=buildings">Buildings</a></li>';
} else {
$page_menu .= '<li><a href="?act=login">Log in</a></li>
<li><a href="?act=signup">Sign up</a></li>';
}
$page_menu .= '</ul>';
$page_menu .= '<p class="sideBarTitle">Game</p>
<ul>
<li><a href="?act=lore">Lore</a></li>
<li><a href="?act=howtoplay">How to Play</a></li>
<li><a href="?act=top">Top Zoopz</a></li>
<li><a href="?act=stats">Zoopz Stats</a></li>
<li><a href="?act=art">Artwork</a></li>
<!--<li><a href="http://www.printfection.com/zoopz" target="_blank">Merchandise</a></li>-->
</ul>';
if ($user_isloggedin) {
$page_menu .= '<p class="sideBarTitle">Places</p>
<ul>';
if ($user['potatomine_built']) {
$page_menu .= '<li><a href="?act=potatomine">Potato Mine</a></li>';
}
$page_menu .= '<li><a href="?act=arena">Arena</a></li>
<li><a href="?act=gym">Gym</a></li>
<li><a href="?act=market">Market</a></li>
<li><a href="?act=hireworkers">Hire Workers</a></li>
<li><a href="?act=hospital">Hospital</a></li>
<li><a href="?act=bank">Bank</a></li>
</ul>
<p class="sideBarTitle">Account</p>
<ul>
<!--<li><a href="?act=account">Options</a></li>-->
<li><a href="?act=rewards">Rewards</a></li>
<!-- <li><a href="?act=upgrade">Upgrade</a></li> -->
<li><a href="?act=logout">Log out</a></li>
</ul>';
}
$page_menu .= '<p class="sideBarTitle">Updates</p>
<ul>
<li>Next turn:<br>' . TimeAgo($update_halfhour_nextrun,'until') . '</li>
</ul>';
/* Calculate at the end to provide up to date stats when the page is loaded, instead of requiring the user to get a new page */
if ($user_isloggedin) {
$hp_percentage = round($user['zoop_hp'] / $user['zoop_maxhp'], 2) * 100;
$bars_filled = floor($hp_percentage / 10);
$bars_notfilled = 10 - $bars_filled;
$page_infobar .= '<b>Zoop: ' . $user['zoop_name'] .
' | HP:&nbsp;&nbsp;';
if ($bars_filled > 0) {
$page_infobar .= '<span style="background-color: green;">';
for ($i = 0; $i < $bars_filled; $i++) {
$page_infobar .= '&nbsp;';
}
$page_infobar .= '</span>';
}
if ($bars_notfilled > 0) {
$page_infobar .= '<span style="background-color: red;">';
for ($i = 0; $i < $bars_notfilled; $i++) {
$page_infobar .= '&nbsp;';
}
$page_infobar .= '</span>';
}
$page_infobar .= '&nbsp;&nbsp;' . $user['zoop_hp'] . '/' . $user['zoop_maxhp'] . '&nbsp;&nbsp;[' . $hp_percentage . '%]' .
' | Level: ' . $user['zoop_level'] .
' | XP: ' . $user['zoop_xp'] . '/' . $user['zoop_xp_nextlevel'] .
' | Attack Turns: ' . $user['attackturns'] .
' | Quetzals: ' . $user['money'].'</b>';
} else {
$page_infobar .= '<a href="?act=login">Log in or sign up to view your Zoop\'s stats!</a>';
}
$page_notification = '';
if ($page_notification_bad != array()) {
$page_notification .= '<div style="background-color: #FF3333; layer-background-color: #FF3333; text-align: center;">';
foreach ($page_notification_bad as $notification) {
$page_notification .= '<h3>'.$notification.'</h3>';
}
$page_notification .= '</div>';
}
if ($page_notification_good != array()) {
$page_notification .= '<div style="background-color: #33FF00; layer-background-color: #33FF00; text-align: center;">';
foreach ($page_notification_good as $notification) {
$page_notification .= '<h3>' . $notification . '</h3>';
}
$page_notification .= '</div>';
}
$smarty->assign('main', $page_main);
$smarty->assign('infobar', $page_infobar);
$smarty->assign('menu', $page_menu);
$smarty->assign('notification', $page_notification);
$smarty->display('index.tpl');
die();
}