PHP Bot Server Code for Transition to JS

This commit is contained in:
TheSomeoneXD
2018-10-16 20:08:33 -05:00
parent 0925778de0
commit b77357adce
9 changed files with 2724 additions and 0 deletions

2
PHPBotServer/.htaccess Normal file
View File

@@ -0,0 +1,2 @@
#.htaccess
Options -Indexes

File diff suppressed because one or more lines are too long

81
PHPBotServer/codex.php Normal file
View File

@@ -0,0 +1,81 @@
<?php
//header('Access-Control-Allow-Origin: *');
//header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
include_once 'functions.php';
if( isset($_GET['pk']) ){ $privateKey = $_GET['pk']; } else{ $privateKey = ''; };
$sqlterms = '';
$debug = false;
if($privateKey != "123"){
echo throwError("invalidPrivateKey");
exit;
}
?>
<HTML>
<HEAD>
<TITLE>THE CODEX</TITLE>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>
</HEAD>
<BODY>
<table id="table_id" class="display">
<thead>
<tr>
<th>ID</th>
<th>Level</th>
<th>Crystals</th>
<th>SPD</th>
<th>STR</th>
<th>STAM</th>
<th>HP</th>
</tr>
</thead>
<tbody>
<?php
$con = mysqlConnect();
$q = "SELECT discordUserID,wallet,speed,health,maxHealth,strength,maxStamina,stamina,lvl FROM users LIMIT 500;";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$discordUserID=stripslashes($a['discordUserID']);
$wallet=stripslashes($a['wallet']);
$speed=stripslashes($a['speed']);
$health=stripslashes($a['health']);
$maxHealth=stripslashes($a['maxHealth']);
$strength=stripslashes($a['strength']);
$maxStamina=stripslashes($a['maxStamina']);
$stamina=stripslashes($a['stamina']);
$lvl=stripslashes($a['lvl']);
?>
<tr>
<td><?php echo $discordUserID; ?></td>
<td><?php echo $lvl; ?></td>
<td><?php echo $wallet; ?></td>
<td><?php echo $speed; ?></td>
<td><?php echo $strength; ?></td>
<td><?php echo $stamina."/".$maxStamina; ?></td>
<td><?php echo $health."/".$maxHealth; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
<script>
$(document).ready( function () {
$('#table_id').DataTable();
} );
</script>
</BODY>
</HTML>

88
PHPBotServer/database.sql Normal file
View File

@@ -0,0 +1,88 @@
CREATE TABLE discordbot.userLog (
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
discordUserID bigint,
actionTime TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
actionType varchar(16),
actionData varchar(255)
);
CREATE TABLE discordbot.users (
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
discordUserID bigint,
timeJoined TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
wallet int,
speed int NOT NULL DEFAULT 5,
health int NOT NULL DEFAULT 100,
maxHealth int NOT NULL DEFAULT 100,
strength int NOT NULL DEFAULT 5,
stamina int NOT NULL DEFAULT 5,
maxStamina int NOT NULL DEFAULT 5,
xp int NOT NULL DEFAULT 0,
lvl int NOT NULL DEFAULT 0,
statPoints int NOT NULL DEFAULT 0
);
CREATE TABLE discordbot.factions (
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
discordRoleID bigint,
discordRoleName varchar(16),
account int,
isCurrentVictor bool
);
CREATE TABLE discordbot.hostiles (
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
timeCreated TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
hostileName varchar(32),
hostileType varchar(32),
maxHealth int NOT NULL DEFAULT 100,
health varchar(16) NOT NULL DEFAULT 100,
strength int NOT NULL DEFAULT 10,
speed int NOT NULL DEFAULT 10,
stash int NOT NULL DEFAULT 10,
alive bool NOT NULL DEFAULT 0,
fled bool NOT NULL DEFAULT 0,
claimID varchar(4)
);
CREATE TABLE discordbot.attackLog (
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
timeAttacked TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
discordUserID bigint,
hostileID varchar(16),
damage int NOT NULL DEFAULT 0
);
CREATE TABLE discordbot.artifacts (
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
discordUserID bigint,
scrap int NOT NULL DEFAULT 0,
common int NOT NULL DEFAULT 0,
uncommon int NOT NULL DEFAULT 0,
rare int NOT NULL DEFAULT 0,
ultrarare int NOT NULL DEFAULT 0
);
CREATE TABLE discordbot.artifactEvents (
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
timeStarted TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
eventType varchar(16) NOT NULL DEFAULT "default",
eventLevel int NOT NULL DEFAULT 0
);
CREATE TABLE discordbot.lootPool (
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
created TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
name varchar(128) NOT NULL DEFAULT "default",
gearType varchar(16) NOT NULL DEFAULT "weapon",
subType varchar(16) NOT NULL DEFAULT "melee",
baseLvl int NOT NULL DEFAULT 0,
baseAtk int NOT NULL DEFAULT 0,
baseDef int NOT NULL DEFAULT 0,
rarity int NOT NULL DEFAULT 0,
perksAvailable varchar(16) NOT NULL DEFAULT "none",
eventLevel int NOT NULL DEFAULT 0
);

201
PHPBotServer/functions.php Normal file
View File

@@ -0,0 +1,201 @@
<?php
function privateKey(){
return "INSERT PRIVATE KEY HERE";
}
function throwError($errorName){
switch ($errorName) {
case "invalidPrivateKey":
return "Sorry, this didnt work. Admins used an invalid key. Bug them about it!";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
}
}
function constants($tag){
//Base Domain Name
if ($tag == 'baseurl'){ return "http://skullboxstudios.com/"; }
//Base URL to the members section
if ($tag == 'ubaseurl'){ return "http://skullboxstudios.com/"; }
//Turns on PHP errors
if ($tag == 'debugmode'){ return true; }
}
function getLevel($xp,$lvlbase){
if($xp >= 1){
$lvl = $lvlbase * sqrt($xp);;
} else{
$lvl = 0;
}
//Returns level based total # of xp
return $lvl;
}
/*
function getLevel($xp,$lvlbase){
if($xp >= 1){
$lvl = log($xp,$lvlbase);
} else{
$lvl = 0;
}
//Returns level based total # of xp
return $lvl;
}
*/
function getCurrentLevelProgress($xp,$lvl){
$baseLevel = floor($lvl);
$levelDecimal = $lvl - $baseLevel;
$percentage = $levelDecimal*100;
$fullNumber = 100;
$actualPercentage = floor(($percentage / 100) * $fullNumber);
//Returns progress in percentage
return $actualPercentage;
}
/*
function getXp($lvl,$lvlbase){
$xp = floor(pow($lvlbase,ceil($lvl)) - pow($lvlbase,floor($lvl)));
//Returns xp needed from start to finish to complete current level
return $xp;
}
function getAllXp($lvl,$lvlbase){
$xp = pow($lvlbase,floor($lvl));
//Returns # of xp required to reach current level
return $xp;
}
function getCurrentLevelProgress($xp,$lvl,$lvlbase){
$xpInCurrentLevel = getXp($lvl,$lvlbase);
$totalXpRequiredForCurrentLevel = getAllXp($lvl,$lvlbase);
$xpTowardNextLevel = $xp - $totalXpRequiredForCurrentLevel;
if($xpInCurrentLevel > 0){
$xpProgress = round(($xpTowardNextLevel / $xpInCurrentLevel)*100,1);
}else{
$xpProgress = 1;
}
//Returns progress in percentage
return $xpProgress;
}
*/
function addXp($uid,$xpamount){
$con = mysqlConnect();
$q = "UPDATE users SET xp=(xp + $xpamount) WHERE discordUserID = '$uid'";
$r = mysqli_query($con,$q);
}
function getLevelBase(){
return 1.2;
}
function full_url()
{
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$sp = strtolower($_SERVER["SERVER_PROTOCOL"]);
$protocol = substr($sp, 0, strpos($sp, "/")) . $s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
return $protocol . "://" . $_SERVER['SERVER_NAME'] . $port . $_SERVER['REQUEST_URI'];
}
// CLASS FOR CONVERTING TIME TO AGO
class convertToAgo {
function convert_datetime($str) {
list($date, $time) = explode(' ', $str);
list($year, $month, $day) = explode('-', $date);
list($hour, $minute, $second) = explode(':', $time);
$timestamp = mktime($hour, $minute, $second, $month, $day, $year);
return $timestamp;
}
function makeAgo($timestamp){
$difference = time() - $timestamp;
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
for($j = 0; $difference >= $lengths[$j]; $j++)
$difference /= $lengths[$j];
$difference = round($difference);
if($difference != 1) $periods[$j].= "s";
if ( (($periods[$j] == "minute") or ($periods[$j] == "minutes") && ($difference < 3))
or (($periods[$j] == "second") or ($periods[$j] == "seconds")) ){
$text = "Just now";
} else {
$text = "$difference $periods[$j] ago";
}
return $text;
}
} // END CLASS
function checkArrayFor($array, $key, $val) {
foreach ($array as $item)
if (isset($item[$key]) && $item[$key] == $val)
return true;
return false;
}
function addhttp($url) {
if ($url != ''){
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
}
return $url;
}
function mysqlConnect() {
//$dbhost = 'localhost';
//$dbusername = 'root';
//$dbpassword = 'AbCd1234!';
//$dbtable = 'discordbot';
$dbhost = 'db750373126.db.1and1.com';
$dbusername = 'dbo750373126';
$dbpassword = 'AbCd1234!';
$dbtable = 'db750373126';
$userDB = 'user';
$userdataDB = 'userdata';
$memberDB = 'member';
$memberdataDB = 'memberdata';
$con = mysqli_connect($dbhost,$dbusername,$dbpassword,$dbtable);
if (!$con) { die('Could not connect: ' . mysqli_error());}
mysqli_select_db($con, "$dbtable")or die("cannot select DB");
return $con;
}
?>

222
PHPBotServer/getData.php Normal file
View File

@@ -0,0 +1,222 @@
<?php
//header('Access-Control-Allow-Origin: *');
//header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
include_once 'functions.php';
if( isset($_GET['dataToLoad']) ){ $dataToLoad = $_GET['dataToLoad']; } else{ $dataToLoad = ''; };
if( isset($_GET['userid']) ){ $userID = $_GET['userid']; } else{ $userID = ''; };
if( isset($_GET['pk']) ){ $privateKey = $_GET['pk']; } else{ $privateKey = ''; };
$sqlterms = '';
$debug = false;
if($privateKey != privateKey()){
echo throwError("invalidPrivateKey");
exit;
}
$con = mysqlConnect();
switch ($dataToLoad) {
case "hasConvertedToday":
$q = "SELECT id FROM userLog WHERE actionTime >= (DATE_SUB(now(), INTERVAL 30 DAY)) AND actionType = 'conversion' AND discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
echo 1;
exit;
} else{
echo 0;
}
break;
case "account":
$q = "SELECT wallet FROM users WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$account=stripslashes($a['wallet']);
}
echo $account;
exit;
} else{
echo "{ERROR}";
}
break;
case "bank":
$q = "SELECT account FROM factions WHERE discordRoleID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$account=stripslashes($a['account']);
}
echo $account;
exit;
} else{
echo "{ERROR}";
}
break;
case "victors":
$q = "SELECT discordRoleName FROM factions WHERE isCurrentVictor = '1';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$discordRoleName=stripslashes($a['discordRoleName']);
}
echo $discordRoleName;
exit;
} else{
echo "0";
}
break;
case "hostileActive":
$q = "SELECT id FROM hostiles WHERE alive = 1 AND health > 0 AND fled = 0;";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$hostileID=stripslashes($a['id']);
}
echo $hostileID;
exit;
} else{
echo "0";
}
break;
case "lastHostileActive":
$q = "SELECT id FROM hostiles ORDER BY id DESC LIMIT 1;";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$hostileID=stripslashes($a['id']);
}
echo $hostileID;
exit;
} else{
echo "0";
}
break;
case "userStats":
$q = "SELECT strength,speed,stamina,health,maxStamina,maxHealth,wallet,xp,lvl,statPoints,chests FROM users WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$strength=stripslashes($a['strength']);
$speed=stripslashes($a['speed']);
$stamina=stripslashes($a['stamina']);
$health=stripslashes($a['health']);
$maxHealth=stripslashes($a['maxHealth']);
$maxStamina=stripslashes($a['maxStamina']);
$wallet=stripslashes($a['wallet']);
$xp=stripslashes($a['xp']);
$recordedLVL=stripslashes($a['lvl']);
$statPoints=stripslashes($a['statPoints']);
$chests=stripslashes($a['chests']);
$lvlbase = getLevelBase();
$lvl = getLevel($xp,$lvlbase);
$lvlpercent = getCurrentLevelProgress($xp,$lvl);
}
echo $strength.",".$speed.",".$stamina.",".$health.",".$maxStamina.",".$maxHealth.",".$wallet.",".$xp.",".$recordedLVL.",".$lvlpercent.",".$statPoints.",".$chests;
exit;
} else{
echo "0";
}
break;
case "artifactsGet":
$q = "SELECT scrap,common,uncommon,rare,ultrarare FROM artifacts WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
$a = mysqli_fetch_assoc($r2);
$scrapQuantity=stripslashes($a['scrap']);
$commonQuantity=stripslashes($a['common']);
$uncommonQuantity=stripslashes($a['uncommon']);
$rareQuantity=stripslashes($a['rare']);
$ultrarareQuantity=stripslashes($a['ultrarare']);
echo "success,".$ultrarareQuantity.",".$rareQuantity.",".$uncommonQuantity.",".$commonQuantity.",".$scrapQuantity;
} else{
echo "failure";
}
break;
}
/*
if ($dataToLoad == "crystals"){
echo "343 Crystals in the vault!";
} else{
echo "No command found.";
}
if($featured==1){
$sqlterms .= "AND userartistinfo.featured = 1 ";
}
if($search !== ''){
$sqlterms .= "AND (userartistinfo.city LIKE '%".$search."%' OR userartistinfo.state LIKE '%".$search."%' OR userartistinfo.zip LIKE '%".$search."%')";
}
$q = "SELECT
user.id as artistid, user.slug, user.displayname, user.picurl,
userartistinfo.genre, userartistinfo.views,
userartistinfo.contactemail, userartistinfo.phone, userartistinfo.address, userartistinfo.city, userartistinfo.state, userartistinfo.zip, userartistinfo.website
FROM user, userartistinfo
where user.active = 1
AND user.id = userartistinfo.id
AND user.type = 'store'
".$sqlterms."
ORDER BY user.created DESC, views DESC
LIMIT 15";
$r2 = mysqli_query($con,$q);
$i=0;
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$displayname = stripslashes($a['displayname']);
$picurl = s3url('weedauthority-userimages', stripslashes($a['picurl']) );
$phone = stripslashes($a['phone']);
$address = stripslashes($a['address'])."<br />".stripslashes($a['city'])." ".stripslashes($a['state']).", ".stripslashes($a['zip']);
$addressPlainText = stripslashes($a['address'])." ".stripslashes($a['city'])." ".stripslashes($a['state']).", ".stripslashes($a['zip']);
$link = $baseurl."dispensary/".stripslashes($a['slug']);
//$maplink ="https://maps.google.com/maps?q=". urlencode($addressPlainText);
//$maplink = "https://www.google.com/maps/place/".urlencode($addressPlainText)."/";
$maplink = stripslashes($a['website']);
$statushtml = "<div id='build-" . $i . "' class='card'><div class='list-block beta-div'><div class='beta-title'>" . $displayname . "</div><img src='" . $picurl . "' class='beta-icon'>";
$statushtml .= "<div class='beta-version'><i class='fa fa-phone'></i> " . $phone . "</div><div class='beta-version'><i class='fa fa-map-marker'></i> " . $address . "</div><div class='beta-button'>";
$statushtml .= "<button class='button button-fill launchbutton launchbuttonactive launchbeta' data-url='" . $maplink . "'>View Shop</button>";
//$statushtml .= "<a href='geo://0,0?q=".$addressPlainText."' data-rel='external'><button class='button button-fill launchbutton launchbuttonactive launchbeta''>View Shop</button></a>";
$statushtml .= "</div><div style='clear:both;'></div><BR></div></div>";
$array[$i]=array($statushtml );
//$array[$i]=array($displayname,$picurl,$phone,$address,$link );
//echo $title;
$i++;
}
}else{
$array[0]=array("<BR /><BR /><center><i class='fa fa-search error-icon' aria-hidden='true'></i><BR /><BR />
Looks like we can't find any shops matching that criteria. <BR />Try searching again!</center>");
//echo json_encode($array);
}
echo json_encode($array);
//}
*/
if($debug){
echo "\n"."UID:".$userID;
}
//mysqli_close($con);
?>

752
PHPBotServer/sendData.php Normal file
View File

@@ -0,0 +1,752 @@
<?php
//header('Access-Control-Allow-Origin: *');
//header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
include_once 'functions.php';
if( isset($_GET['dataType']) ){ $dataType = $_GET['dataType']; } else{ $dataType = ''; };
if( isset($_GET['dataToSend']) ){ $dataToSend = $_GET['dataToSend']; } else{ $dataToSend = ''; };
if( isset($_GET['dataToSend2']) ){ $dataToSend2 = $_GET['dataToSend2']; } else{ $dataToSend2 = ''; };
if( isset($_GET['userid']) ){ $userID = $_GET['userid']; } else{ $userID = ''; };
if( isset($_GET['pk']) ){ $privateKey = $_GET['pk']; } else{ $privateKey = ''; };
$debug = false;
if($privateKey != privateKey()){
echo throwError("invalidPrivateKey");
exit;
}
$con = mysqlConnect();
switch ($dataType) {
case "conversion":
$q = "INSERT INTO userLog (discordUserID, actionType, actionData)
VALUES (" . $userID . ", '" . $dataType . "', '" . $dataToSend . "');";
$r2 = mysqli_query($con,$q);
break;
case "newUser":
/*
$q = "INSERT INTO users (discordUserID,wallet)
SELECT * FROM (SELECT '$userID',0) AS tmp
WHERE NOT EXISTS (
SELECT discordUserID FROM users WHERE discordUserID = '$userID'
) LIMIT 1;";
$r2 = mysqli_query($con,$q);
*/
$q = "SELECT id FROM users WHERE discordUserID = '$userID' LIMIT 1;";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
echo "userAlreadyExists";
} else{
$q = "INSERT INTO users (discordUserID,wallet)
SELECT * FROM (SELECT '$userID',0) AS tmp
WHERE NOT EXISTS (
SELECT discordUserID FROM users WHERE discordUserID = '$userID'
) LIMIT 1;";
$r2 = mysqli_query($con,$q);
echo "createdUser";
}
break;
case "checkin":
$q = "SELECT id,actionTime FROM userLog WHERE actionTime >= CURDATE() AND actionType = 'checkin' AND discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
//already checked in today.
$a = mysqli_fetch_assoc($r2);
$flastupdated=stripslashes($a['actionTime']);
$timeAgoObject = new convertToAgo; // Create an object for the time conversion functions
// Query your database here and get timestamp
$ts = $flastupdated;
$convertedTime = ($timeAgoObject -> convert_datetime($ts)); // Convert Date Time
$when = ($timeAgoObject -> makeAgo($convertedTime)); // Then convert to ago time
//date("F j, Y, g:i a", strtotime($flastupdated));
echo $when;
} else{
//Can check in.
$q = "UPDATE users SET wallet = wallet + $dataToSend WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "INSERT INTO userLog (discordUserID, actionType, actionData)
VALUES (" . $userID . ", '" . $dataType . "', 'Checked in for $dataToSend crystals.');";
$r2 = mysqli_query($con,$q);
echo 1;
exit;
}
break;
case "deposit":
$q = "SELECT wallet FROM users WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$account=stripslashes($a['wallet']);
}
if($account >= $dataToSend && $dataToSend > 0){
//has enough credits in their account
$q = "UPDATE users SET wallet = wallet - $dataToSend WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "UPDATE factions SET account = account + $dataToSend WHERE discordRoleID = '$dataToSend2' LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "INSERT INTO userLog (discordUserID, actionType, actionData)
VALUES (" . $userID . ", '" . $dataType . "', 'Deposited $dataToSend crystals to $dataToSend2.');";
$r2 = mysqli_query($con,$q);
echo 1;
} else{
echo "{ERROR} - Not enough crystals in your account.";
}
exit;
} else{
echo "{ERROR} - Cant find wallet. Something went wrong.";
}
break;
case "gambleWon":
$q = "UPDATE users SET wallet = wallet + $dataToSend WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "INSERT INTO userLog (discordUserID, actionType, actionData)
VALUES (" . $userID . ", '" . $dataType . "', 'Gambled and won for $dataToSend crystals.');";
$r2 = mysqli_query($con,$q);
echo 1;
exit;
break;
case "gambleLost":
$q = "UPDATE users SET wallet = wallet - $dataToSend WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "INSERT INTO userLog (discordUserID, actionType, actionData)
VALUES (" . $userID . ", '" . $dataType . "', 'Gambled and lost for $dataToSend crystals.');";
$r2 = mysqli_query($con,$q);
echo 1;
exit;
break;
case "transfer":
$q = "UPDATE users SET wallet = wallet - $dataToSend2 WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "UPDATE users SET wallet = wallet + $dataToSend2 WHERE discordUserID = '$dataToSend' LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "INSERT INTO userLog (discordUserID, actionType, actionData)
VALUES (" . $userID . ", '" . $dataType . "', '$userID gave $dataToSend2 crystals to $dataToSend.');";
$r2 = mysqli_query($con,$q);
echo 1;
exit;
break;
case "attack":
$q = "UPDATE hostiles SET health = health - $dataToSend WHERE id = '$dataToSend2' LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "UPDATE users SET stamina = stamina - 1 WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
//$q = "INSERT INTO userLog (discordUserID, actionType, actionData)
//VALUES (" . $userID . ", '" . $dataType . "', '$userID attacked Ravager#$dataToSend2.');";
//$r2 = mysqli_query($con,$q);
$q = "INSERT INTO attackLog (discordUserID, hostileID, damage)
VALUES ('$userID','$dataToSend2','$dataToSend');";
$r2 = mysqli_query($con,$q);
$q = "SELECT hostiles.health,hostiles.maxHealth,hostiles.speed,hostiles.strength,users.speed as userspeed,users.health as userhealth FROM hostiles,users WHERE hostiles.id = '$dataToSend2' AND users.discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$hostileHealth=stripslashes($a['health']);
$hostileMaxHealth=stripslashes($a['maxHealth']);
$hostileSpeed=stripslashes($a['speed']);
$hostileStrength=stripslashes($a['strength']);
$userSpeed=stripslashes($a['userspeed']);
$userHealth=stripslashes($a['userhealth']);
}
if($hostileHealth <= 0){
if($hostileHealth < 0){ $hostileHealth = 0;};
//returns health less than zero, kill enemy.
$q = "UPDATE hostiles SET alive = 0,health = 0 WHERE id = '$dataToSend2' LIMIT 1";
$r2 = mysqli_query($con,$q);
}
$criticalHit = 0;
$hitAmount = getEnemyDamage($hostileSpeed,$userSpeed,$hostileStrength);
if($hitAmount > 0){
if ($hitAmount >= $userHealth){$hitAmount = $userHealth; $criticalHit = 1;};
$q = "UPDATE users SET health = health - $hitAmount WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
}
echo $hostileHealth.",".$hostileMaxHealth.",".$hitAmount.",".$criticalHit;
exit;
} else{
echo "0";
}
exit;
break;
case "hostileAttackBack":
$q = "UPDATE users SET stamina = stamina - 1 WHERE discordUserID = '$userID' AND stamina > 0 LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "SELECT hostiles.health,hostiles.maxHealth,hostiles.speed,hostiles.strength,users.speed as userspeed,users.health as userhealth FROM hostiles,users WHERE hostiles.id = '$dataToSend2' AND users.discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$hostileHealth=stripslashes($a['health']);
$hostileMaxHealth=stripslashes($a['maxHealth']);
$hostileSpeed=stripslashes($a['speed']);
$hostileStrength=stripslashes($a['strength']);
$userSpeed=stripslashes($a['userspeed']);
$userHealth=stripslashes($a['userhealth']);
}
$criticalHit = 0;
$hitAmount = getEnemyDamage($hostileSpeed,$userSpeed,$hostileStrength);
if($hitAmount > 0){
if ($hitAmount >= $userHealth){$hitAmount = $userHealth; $criticalHit = 1;};
$q = "UPDATE users SET health = health - $hitAmount WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
}
echo $hostileHealth.",".$hostileMaxHealth.",".$hitAmount.",".$criticalHit;
exit;
} else{
echo "0";
}
exit;
break;
case "hostileFlee":
$q = "SELECT id FROM hostiles WHERE alive = 1 ORDER BY id DESC LIMIT 1;";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$hostileID=stripslashes($a['id']);
}
$q = "UPDATE hostiles SET fled = 1,alive=0 WHERE id = '$hostileID' LIMIT 1";
$r2 = mysqli_query($con,$q);
echo "fled";
} else{
echo "alreadyDead";
}
break;
case "newHostile":
$q = "SELECT id FROM hostiles WHERE alive = 1 LIMIT 1;";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
echo "notCreated";
} else{
$elvl = $dataToSend;
$healthBase = 50; $strengthBase = 3; $speedBase = 3; $stashBase = 3;
$healthMin = ($healthBase * $elvl) / 2; $healthMax = $healthBase * $elvl;
$strengthMin = ($strengthBase * $elvl) / 2; $strengthMax = $strengthBase * $elvl;
$speedMin = ($speedBase * $elvl) / 2; $speedMax = $speedBase * $elvl;
$stashMin = ($stashBase * $elvl) / 2; $stashMax = $stashBase * $elvl;
$health = floor(rand($healthMin,$healthMax));
$strength = floor(rand($strengthMin,$strengthMax));
$speed = floor(rand($speedMin,$speedMax));
$stash = floor(rand($stashMin,$stashMax));
$claimID = floor(rand(1000,9999));
$q = "INSERT INTO hostiles (hostileType, maxHealth, health, strength, speed, stash, alive, claimID)
VALUES ('ravager', '$health', '$health', '$strength', '$speed', '$stash', 1, '$claimID');";
$r2 = mysqli_query($con,$q);
echo $health.",".$speed.",".$strength.",".$claimID;
}
break;
case "claim":
$claimAmount = $dataToSend2;
$q = "SELECT stash FROM hostiles WHERE alive = 0 AND claimID = '$dataToSend' LIMIT 1;";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$stash=stripslashes($a['stash']);
}
if($claimAmount <= $stash){
//take money from the stash
$q = "UPDATE hostiles SET stash = stash - $claimAmount WHERE claimID = '$dataToSend' LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "UPDATE users SET wallet = wallet + $claimAmount WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "INSERT INTO userLog (discordUserID, actionType, actionData)
VALUES (" . $userID . ", '" . $dataType . "', '$userID claimed $claimAmount crystals from a Ravager.');";
$r2 = mysqli_query($con,$q);
$stash = $stash - $claimAmount;
if($stash == 0){
$q = "UPDATE hostiles SET claimID=0 WHERE claimID = '$dataToSend' LIMIT 1";
$r2 = mysqli_query($con,$q);
}
echo $stash;
}else{
echo "notEnough";
}
exit;
} else{
echo "noClaimID";
}
exit;
break;
case "getHostileData":
$q = "SELECT stash,claimID FROM hostiles WHERE alive = 0 AND id = '$dataToSend' LIMIT 1;";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$stash=stripslashes($a['stash']);
$claimID=stripslashes($a['claimID']);
}
echo $stash.",".$claimID;
}
exit;
break;
case "getDamageDistribution":
//Gets base stats for enemy
$q = "SELECT stash,maxHealth,fled FROM hostiles WHERE id = '$dataToSend' LIMIT 1;";
$r2 = mysqli_query($con,$q);
$a = mysqli_fetch_assoc($r2);
$stash=stripslashes($a['stash']);
$maxHealth=stripslashes($a['maxHealth']);
$fled=stripslashes($a['fled']);
$totalCrystalsInStash = 0;
if($fled == 1){
echo "fled";
}else{
//gets all dammage from users
$damageDistribution = array();
$q = "SELECT discordUserID,SUM(damage) totalDamage FROM attackLog WHERE hostileID = $dataToSend GROUP BY discordUserID;";
//$q = "SELECT attackLog.damage,attackLog.discordUserID,hostiles.stash,hostiles.maxHealth FROM attackLog WHERE hostiles.id = attackLog.hostileID AND attackLog.hostileID = '$dataToSend';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$damage=stripslashes($a['totalDamage']);
$discordUserID=stripslashes($a['discordUserID']);
$damagePercent = round(( $damage / $maxHealth ) * 100);
$percentStashAmount = round($stash * ($damagePercent/100));
$totalCrystalsInStash += $percentStashAmount;
// you can add single array values too
$damageDistribution[] = array('id'=>$discordUserID, 'totalDamage'=>$damage, 'damagePercent'=>$damagePercent, 'crystalsReceived'=>$percentStashAmount);
if($dataToSend2 == 1){
//Flag to actually distribute crystals
$q2 = "UPDATE users SET wallet = wallet + $percentStashAmount WHERE discordUserID = '$discordUserID' LIMIT 1";
$r3 = mysqli_query($con,$q2);
}
}
echo json_encode($damageDistribution);
} else{
echo 0;
}
exit;
}
break;
case "updateStamina":
$q = "UPDATE users SET stamina = stamina + 1 WHERE stamina < maxStamina;";
$r2 = mysqli_query($con,$q);
//UPDATE users SET health = min(floor(health + (maxHeath/100)), maxHealth)
$q = "UPDATE users SET health = least(floor(health + (maxHealth/100)), maxHealth) WHERE health < maxHealth AND health > 0;";
$r2 = mysqli_query($con,$q);
exit;
break;
case "reviveAll":
$q = "UPDATE users SET health = 1 WHERE health = 0;";
$r2 = mysqli_query($con,$q);
$q = "INSERT INTO userLog (discordUserID, actionType, actionData)
VALUES (0,'revive','reviveAll');";
$r2 = mysqli_query($con,$q);
exit;
break;
case "lvlinfo":
$q = "SELECT xp,lvl FROM users WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$xp=stripslashes($a['xp']);
$currentlvl=stripslashes($a['lvl']);
$lvlbase=getLevelBase();
$lvl=getLevel($xp,$lvlbase);
}
}
//echo "LEVEL: ".getLevel($xp,$lvlbase),"<BR>XP: ".$xp."<BR>CURRENT LEVEL PROGRESS:".getCurrentLevelProgress($xp,$lvl);
echo "LEVEL: ".getLevel($dataToSend,$lvlbase),"<BR>XP: ".$xp."<BR>CURRENT LEVEL PROGRESS:".getCurrentLevelProgress($xp,$lvl);
break;
case "upgradeStats":
//Changed it to just upgrade 1 point automatically
$dataToSend2 = 1;
$q = "SELECT statPoints FROM users WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$statPoints=stripslashes($a['statPoints']);
}
if($dataToSend2 <= $statPoints){
$tableName = "";
switch (strtoupper($dataToSend)) {
case "STR":
$tableName = "strength = strength + ".$dataToSend2;
break;
case "HP":
$tableName = "maxHealth = maxHealth + 10";
break;
case "SPD":
$tableName = "speed = speed + ".$dataToSend2;
break;
case "STAM":
$tableName = "maxStamina = maxStamina + ".$dataToSend2;
break;
}
$q = "UPDATE users SET statPoints = statPoints - $dataToSend2,$tableName WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
echo "success";
} else{
echo "notEnoughPoints";
}
} else{
echo "failure";
}
break;
case "heal":
$q = "SELECT health,maxHealth,wallet FROM users WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$health=stripslashes($a['health']);
$maxHealth=stripslashes($a['maxHealth']);
$crystals=stripslashes($a['wallet']);
}
$treatmentCost = $dataToSend;
$treatmentName = $dataToSend2;
$newHealth = $health;
if($health == $maxHealth){echo "fullHealth";exit;}
if($health > 0){
if($crystals >= $treatmentCost){
switch ($treatmentName) {
case "TREAT":
$newHealth += 15;
break;
case "TREATV2":
$newHealth = $maxHealth*0.15;
break;
case "PATCH":
$newHealth += 50;
break;
case "PATCHV2":
$newHealth = $maxHealth*0.5;
break;
case "REGEN":
$newHealth += 100;
break;
case "REGENV2":
$newHealth = $maxHealth;
break;
default:
echo "cantDoThat";exit;
break;
}
if($newHealth < $health){echo "lessThanYourHealth";exit;}
if($newHealth>$maxHealth){$newHealth = $maxHealth;};
$q = "UPDATE users SET health = $newHealth,wallet = wallet - $treatmentCost WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
echo "success,".$newHealth."/".$maxHealth;
} else{
echo "notEnoughCrystals";
}
} else{
if($crystals >= $treatmentCost){
switch ($treatmentName) {
case "REVIVE":
$newHealth = 25;
break;
case "REVIVEV2":
$newHealth = $maxHealth*0.5;
break;
case "REVIVEV3":
$newHealth = $maxHealth;
break;
case "TREAT":
echo "youreKnockedOut";exit;
break;
case "TREATV2":
echo "youreKnockedOut";exit;
break;
case "PATCH":
echo "youreKnockedOut";exit;
break;
case "PATCHV2":
echo "youreKnockedOut";exit;
break;
case "REGEN":
echo "youreKnockedOut";exit;
break;
default:
echo "cantDoThat";exit;
break;
}
if($newHealth < $health){echo "lessThanYourHealth";exit;}
if($newHealth>$maxHealth){$newHealth = $maxHealth;};
$q = "UPDATE users SET health = $newHealth,wallet = wallet - $treatmentCost WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
echo "success,".$newHealth."/".$maxHealth;
}else{
echo "notEnoughCrystals";
}
}
} else{
echo "failure";
}
break;
case "addXP":
addXp($userID,$dataToSend);
break;
case "getLevelUp":
//addXp($userID,$dataToSend);
$levelCap = 30;$levelCapXP = 625;
$q = "SELECT xp,lvl,statPoints,chests FROM users WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$xp=stripslashes($a['xp']);
$lvl=stripslashes($a['lvl']);
$statPoints=stripslashes($a['statPoints']);
$chests=stripslashes($a['chests']);
}
$lvlbase = getLevelBase();
$currentLVL = floor(getLevel($xp,$lvlbase));
if($currentLVL > $lvl){
if($currentLVL > $levelCap){
$chests += 1;
$q = "UPDATE users SET lvl = $levelCap,chests = chests + 1,xp = $levelCapXP WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
}else{
$statPoints += 1;
$q = "UPDATE users SET lvl = lvl + 1,statPoints = statPoints + 1 WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
$lvl = $lvl + 1;
}
echo "levelup,".$lvl.",".$statPoints.",".$statPoints;
} else{
echo "xpadded,".$currentLVL.",".$statPoints;
}
}
break;
case "scavenge":
$random = floor(rand(0,101));
$ultrarare = 0;$rare = 0; $uncommon = 0; $common = 0; $scrap = 0;
if($random <= 0.5){
$ultrarare = 1;
}
if($random <= 3 && $random > 0.5){
$rare = round(rand(1,2));
}
if($random <= 10 && $random > 3){
$uncommon = round(rand(1,3));
}
if($random <= 50 && $random > 10){
$common = round(rand(1,3));
}
if($random > 50){
$scrap = round(rand(1,7));
}
$staminaCost = $dataToSend;
$crystalCost = $dataToSend2;
$q = "UPDATE users SET stamina = stamina - $staminaCost,wallet = wallet - $crystalCost WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
$q = "SELECT id FROM artifacts WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
$q = "UPDATE artifacts SET scrap = scrap + $scrap,common = common + $common,uncommon = uncommon + $uncommon,rare = rare + $rare,ultrarare = ultrarare + $ultrarare WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
echo "success,".$ultrarare.",".$rare.",".$uncommon.",".$common.",".$scrap;
} else{
$q = "INSERT INTO artifacts (discordUserID, scrap, common, uncommon, rare, ultrarare)
VALUES ($userID,$scrap,$common,$uncommon,$rare,$ultrarare);";
$r2 = mysqli_query($con,$q);
echo "success,".$ultrarare.",".$rare.",".$uncommon.",".$common.",".$scrap;
}
break;
case "artifactSell":
$q = "SELECT scrap,common,uncommon,rare,ultrarare FROM artifacts WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
$a = mysqli_fetch_assoc($r2);
$scrapQuantity=stripslashes($a['scrap']);
$commonQuantity=stripslashes($a['common']);
$uncommonQuantity=stripslashes($a['uncommon']);
$rareQuantity=stripslashes($a['rare']);
$ultrarareQuantity=stripslashes($a['ultrarare']);
$itemToSell = strtolower ($dataToSend);
$price = 0;$totalPayout = 0;$itemQuantity = 0;
$price = 0.1;
$scrapTotalPayout = round($price * $scrapQuantity);
$price = 2;
$commonTotalPayout = $price * $commonQuantity;
$price = 5;
$uncommonTotalPayout = $price * $uncommonQuantity;
$price = 10;
$rareTotalPayout = $price * $rareQuantity;
$price = 30;
$ultrarareTotalPayout = $price * $ultrarareQuantity;
$itemQuantity = $scrapQuantity + $commonQuantity + $uncommonQuantity + $rareQuantity + $ultrarareQuantity;
$totalPayout = $scrapTotalPayout + $commonTotalPayout + $uncommonTotalPayout + $rareTotalPayout + $ultrarareTotalPayout;
if($itemToSell == "all"){
$q = "UPDATE artifacts SET scrap =0,common = 0,uncommon = 0,rare = 0,ultrarare = 0 WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
$q = "UPDATE users SET wallet = wallet + $totalPayout WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
echo "success,".$itemQuantity.",".$totalPayout;
}else{
$crystals = 0;
switch ($itemToSell) {
case "scrap":
$singlePayout = $scrapTotalPayout;
$itemQuantity = $scrapQuantity;
break;
case "common":
$singlePayout = $commonTotalPayout;
$itemQuantity = $commonQuantity;
break;
case "uncommon":
$singlePayout = $uncommonTotalPayout;
$itemQuantity = $uncommonQuantity;
break;
case "rare":
$singlePayout = $rareTotalPayout;
$itemQuantity = $rareQuantity;
break;
case "ultrarare":
$singlePayout = $ultrarareTotalPayout;
$itemQuantity = $ultrarareQuantity;
break;
}
$q = "UPDATE artifacts SET $itemToSell = 0 WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
$q = "UPDATE users SET wallet = wallet + $singlePayout WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
echo "success,".$itemQuantity.",".$singlePayout;
}
}else{
echo "failure";
}
break;
case "buyDrink":
$q = "UPDATE users SET wallet = wallet - $dataToSend WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
break;
}
//echo json_encode($array);
if($debug){
echo "\n"."UID:".$userID;
}
mysqli_close($con);
function getEnemyDamage($hostileSpeed,$userSpeed,$hostileStrength){
$hitAmount = 0;
$percentage = floor(rand(0,101));
if($hostileSpeed > $userSpeed){
if($percentage <= 80 ){
//80% chance to hit you back.
$hitAmount = $hostileStrength + rand(-($hostileStrength/4),$hostileStrength/4);
}
} else{
if($percentage <= 30){
//30% chance to hit you back.
$hitAmount = $hostileStrength + rand(-($hostileStrength/4),$hostileStrength/4);
}
}
/*
if($hostileSpeed > $userSpeed){
if(20 > rand(0,100)){
$hitAmount = $hostileStrength + rand(-($hostileStrength/4),$hostileStrength/4);
}
} elseif($hostileSpeed == $userSpeed){
if(50 >= rand(0,100)){
$hitAmount = $hostileStrength + rand(-($hostileStrength/4),$hostileStrength/4);
}
} else{
if(70 >= rand(0,100)){
$hitAmount = $hostileStrength + rand(-($hostileStrength/4),$hostileStrength/4);
}
}
*/
return $hitAmount;
}
?>

743
PHPBotServer/sendData2.php Normal file
View File

@@ -0,0 +1,743 @@
<?php
//header('Access-Control-Allow-Origin: *');
//header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
include_once 'functions.php';
if( isset($_GET['dataType']) ){ $dataType = $_GET['dataType']; } else{ $dataType = ''; };
if( isset($_GET['dataToSend']) ){ $dataToSend = $_GET['dataToSend']; } else{ $dataToSend = ''; };
if( isset($_GET['dataToSend2']) ){ $dataToSend2 = $_GET['dataToSend2']; } else{ $dataToSend2 = ''; };
if( isset($_GET['userid']) ){ $userID = $_GET['userid']; } else{ $userID = ''; };
if( isset($_GET['pk']) ){ $privateKey = $_GET['pk']; } else{ $privateKey = ''; };
$debug = false;
if($privateKey != privateKey()){
echo throwError("invalidPrivateKey");
exit;
}
$con = mysqlConnect();
switch ($dataType) {
case "conversion":
$q = "INSERT INTO userLog (discordUserID, actionType, actionData)
VALUES (" . $userID . ", '" . $dataType . "', '" . $dataToSend . "');";
$r2 = mysqli_query($con,$q);
break;
case "newUser":
/*
$q = "INSERT INTO users (discordUserID,wallet)
SELECT * FROM (SELECT '$userID',0) AS tmp
WHERE NOT EXISTS (
SELECT discordUserID FROM users WHERE discordUserID = '$userID'
) LIMIT 1;";
$r2 = mysqli_query($con,$q);
*/
$q = "SELECT id FROM users WHERE discordUserID = '$userID' LIMIT 1;";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
echo "userAlreadyExists";
} else{
$q = "INSERT INTO users (discordUserID,wallet)
SELECT * FROM (SELECT '$userID',0) AS tmp
WHERE NOT EXISTS (
SELECT discordUserID FROM users WHERE discordUserID = '$userID'
) LIMIT 1;";
$r2 = mysqli_query($con,$q);
echo "createdUser";
}
break;
case "checkin":
$q = "SELECT id,actionTime FROM userLog WHERE actionTime >= CURDATE() AND actionType = 'checkin' AND discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
//already checked in today.
$a = mysqli_fetch_assoc($r2);
$flastupdated=stripslashes($a['actionTime']);
$timeAgoObject = new convertToAgo; // Create an object for the time conversion functions
// Query your database here and get timestamp
$ts = $flastupdated;
$convertedTime = ($timeAgoObject -> convert_datetime($ts)); // Convert Date Time
$when = ($timeAgoObject -> makeAgo($convertedTime)); // Then convert to ago time
//date("F j, Y, g:i a", strtotime($flastupdated));
echo $when;
} else{
//Can check in.
$q = "UPDATE users SET wallet = wallet + $dataToSend WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "INSERT INTO userLog (discordUserID, actionType, actionData)
VALUES (" . $userID . ", '" . $dataType . "', 'Checked in for $dataToSend crystals.');";
$r2 = mysqli_query($con,$q);
echo 1;
exit;
}
break;
case "deposit":
$q = "SELECT wallet FROM users WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$account=stripslashes($a['wallet']);
}
if($account >= $dataToSend && $dataToSend > 0){
//has enough credits in their account
$q = "UPDATE users SET wallet = wallet - $dataToSend WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "UPDATE factions SET account = account + $dataToSend WHERE discordRoleID = '$dataToSend2' LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "INSERT INTO userLog (discordUserID, actionType, actionData)
VALUES (" . $userID . ", '" . $dataType . "', 'Deposited $dataToSend crystals to $dataToSend2.');";
$r2 = mysqli_query($con,$q);
echo 1;
} else{
echo "{ERROR} - Not enough crystals in your account.";
}
exit;
} else{
echo "{ERROR} - Cant find wallet. Something went wrong.";
}
break;
case "gambleWon":
$q = "UPDATE users SET wallet = wallet + $dataToSend WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "INSERT INTO userLog (discordUserID, actionType, actionData)
VALUES (" . $userID . ", '" . $dataType . "', 'Gambled and won for $dataToSend crystals.');";
$r2 = mysqli_query($con,$q);
echo 1;
exit;
break;
case "gambleLost":
$q = "UPDATE users SET wallet = wallet - $dataToSend WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "INSERT INTO userLog (discordUserID, actionType, actionData)
VALUES (" . $userID . ", '" . $dataType . "', 'Gambled and lost for $dataToSend crystals.');";
$r2 = mysqli_query($con,$q);
echo 1;
exit;
break;
case "transfer":
$q = "UPDATE users SET wallet = wallet - $dataToSend2 WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "UPDATE users SET wallet = wallet + $dataToSend2 WHERE discordUserID = '$dataToSend' LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "INSERT INTO userLog (discordUserID, actionType, actionData)
VALUES (" . $userID . ", '" . $dataType . "', '$userID gave $dataToSend2 crystals to $dataToSend.');";
$r2 = mysqli_query($con,$q);
echo 1;
exit;
break;
case "attack":
$q = "UPDATE hostiles SET health = health - $dataToSend WHERE id = '$dataToSend2' LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "UPDATE users SET stamina = stamina - 1 WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
//$q = "INSERT INTO userLog (discordUserID, actionType, actionData)
//VALUES (" . $userID . ", '" . $dataType . "', '$userID attacked Ravager#$dataToSend2.');";
//$r2 = mysqli_query($con,$q);
$q = "INSERT INTO attackLog (discordUserID, hostileID, damage)
VALUES ('$userID','$dataToSend2','$dataToSend');";
$r2 = mysqli_query($con,$q);
$q = "SELECT hostiles.health,hostiles.maxHealth,hostiles.speed,hostiles.strength,users.speed as userspeed,users.health as userhealth FROM hostiles,users WHERE hostiles.id = '$dataToSend2' AND users.discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$hostileHealth=stripslashes($a['health']);
$hostileMaxHealth=stripslashes($a['maxHealth']);
$hostileSpeed=stripslashes($a['speed']);
$hostileStrength=stripslashes($a['strength']);
$userSpeed=stripslashes($a['userspeed']);
$userHealth=stripslashes($a['userhealth']);
}
if($hostileHealth <= 0){
if($hostileHealth < 0){ $hostileHealth = 0;};
//returns health less than zero, kill enemy.
$q = "UPDATE hostiles SET alive = 0,health = 0 WHERE id = '$dataToSend2' LIMIT 1";
$r2 = mysqli_query($con,$q);
}
$criticalHit = 0;
$hitAmount = getEnemyDamage($hostileSpeed,$userSpeed,$hostileStrength);
if($hitAmount > 0){
if ($hitAmount >= $userHealth){$hitAmount = $userHealth; $criticalHit = 1;};
$q = "UPDATE users SET health = health - $hitAmount WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
}
echo $hostileHealth.",".$hostileMaxHealth.",".$hitAmount.",".$criticalHit;
exit;
} else{
echo "0";
}
exit;
break;
case "hostileAttackBack":
$q = "UPDATE users SET stamina = stamina - 1 WHERE discordUserID = '$userID' AND stamina > 0 LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "SELECT hostiles.health,hostiles.maxHealth,hostiles.speed,hostiles.strength,users.speed as userspeed,users.health as userhealth FROM hostiles,users WHERE hostiles.id = '$dataToSend2' AND users.discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$hostileHealth=stripslashes($a['health']);
$hostileMaxHealth=stripslashes($a['maxHealth']);
$hostileSpeed=stripslashes($a['speed']);
$hostileStrength=stripslashes($a['strength']);
$userSpeed=stripslashes($a['userspeed']);
$userHealth=stripslashes($a['userhealth']);
}
$criticalHit = 0;
$hitAmount = getEnemyDamage($hostileSpeed,$userSpeed,$hostileStrength);
if($hitAmount > 0){
if ($hitAmount >= $userHealth){$hitAmount = $userHealth; $criticalHit = 1;};
$q = "UPDATE users SET health = health - $hitAmount WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
}
echo $hostileHealth.",".$hostileMaxHealth.",".$hitAmount.",".$criticalHit;
exit;
} else{
echo "0";
}
exit;
break;
case "hostileFlee":
$q = "SELECT id FROM hostiles WHERE alive = 1 ORDER BY id DESC LIMIT 1;";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$hostileID=stripslashes($a['id']);
}
$q = "UPDATE hostiles SET fled = 1,alive=0 WHERE id = '$hostileID' LIMIT 1";
$r2 = mysqli_query($con,$q);
echo "fled";
} else{
echo "alreadyDead";
}
break;
case "newHostile":
$q = "SELECT id FROM hostiles WHERE alive = 1 LIMIT 1;";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
echo "notCreated";
} else{
$elvl = $dataToSend;
$healthBase = 150; $strengthBase = 6; $speedBase = 6; $stashBase = 9;
$healthMin = ($healthBase * $elvl) / 2; $healthMax = $healthBase * $elvl;
$strengthMin = ($strengthBase * $elvl) / 2; $strengthMax = $strengthBase * $elvl;
$speedMin = ($speedBase * $elvl) / 2; $speedMax = $speedBase * $elvl;
$stashMin = ($stashBase * $elvl) / 2; $stashMax = $stashBase * $elvl;
$health = floor(rand($healthMin,$healthMax));
$strength = floor(rand($strengthMin,$strengthMax));
$speed = floor(rand($speedMin,$speedMax));
$stash = floor(rand($stashMin,$stashMax));
$claimID = floor(rand(1000,9999));
$q = "INSERT INTO hostiles (hostileType, maxHealth, health, strength, speed, stash, alive, claimID)
VALUES ('ravager', '$health', '$health', '$strength', '$speed', '$stash', 1, '$claimID');";
$r2 = mysqli_query($con,$q);
echo $health.",".$speed.",".$strength.",".$claimID;
}
break;
case "claim":
$claimAmount = $dataToSend2;
$q = "SELECT stash FROM hostiles WHERE alive = 0 AND claimID = '$dataToSend' LIMIT 1;";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$stash=stripslashes($a['stash']);
}
if($claimAmount <= $stash){
//take money from the stash
$q = "UPDATE hostiles SET stash = stash - $claimAmount WHERE claimID = '$dataToSend' LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "UPDATE users SET wallet = wallet + $claimAmount WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "INSERT INTO userLog (discordUserID, actionType, actionData)
VALUES (" . $userID . ", '" . $dataType . "', '$userID claimed $claimAmount crystals from a Ravager.');";
$r2 = mysqli_query($con,$q);
$stash = $stash - $claimAmount;
if($stash == 0){
$q = "UPDATE hostiles SET claimID=0 WHERE claimID = '$dataToSend' LIMIT 1";
$r2 = mysqli_query($con,$q);
}
echo $stash;
}else{
echo "notEnough";
}
exit;
} else{
echo "noClaimID";
}
exit;
break;
case "getHostileData":
$q = "SELECT stash,claimID FROM hostiles WHERE alive = 0 AND id = '$dataToSend' LIMIT 1;";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$stash=stripslashes($a['stash']);
$claimID=stripslashes($a['claimID']);
}
echo $stash.",".$claimID;
}
exit;
break;
case "getDamageDistribution":
//Gets base stats for enemy
$q = "SELECT stash,maxHealth,fled FROM hostiles WHERE id = '$dataToSend' LIMIT 1;";
$r2 = mysqli_query($con,$q);
$a = mysqli_fetch_assoc($r2);
$stash=stripslashes($a['stash']);
$maxHealth=stripslashes($a['maxHealth']);
$fled=stripslashes($a['fled']);
$totalCrystalsInStash = 0;
if($fled == 1){
echo "fled";
}else{
//gets all dammage from users
$damageDistribution = array();
$q = "SELECT discordUserID,SUM(damage) totalDamage FROM attackLog WHERE hostileID = $dataToSend GROUP BY discordUserID;";
//$q = "SELECT attackLog.damage,attackLog.discordUserID,hostiles.stash,hostiles.maxHealth FROM attackLog WHERE hostiles.id = attackLog.hostileID AND attackLog.hostileID = '$dataToSend';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$damage=stripslashes($a['totalDamage']);
$discordUserID=stripslashes($a['discordUserID']);
$damagePercent = round(( $damage / $maxHealth ) * 100);
$percentStashAmount = round($stash * ($damagePercent/100));
$totalCrystalsInStash += $percentStashAmount;
// you can add single array values too
$damageDistribution[] = array('id'=>$discordUserID, 'totalDamage'=>$damage, 'damagePercent'=>$damagePercent, 'crystalsReceived'=>$percentStashAmount);
if($dataToSend2 == 1){
//Flag to actually distribute crystals
$q2 = "UPDATE users SET wallet = wallet + $percentStashAmount WHERE discordUserID = '$discordUserID' LIMIT 1";
$r3 = mysqli_query($con,$q2);
}
}
echo json_encode($damageDistribution);
} else{
echo 0;
}
exit;
}
break;
case "updateStamina":
$q = "UPDATE users SET stamina = stamina + 1 WHERE stamina < maxStamina;";
$r2 = mysqli_query($con,$q);
//UPDATE users SET health = min(floor(health + (maxHeath/100)), maxHealth)
$q = "UPDATE users SET health = least(floor(health + (maxHealth/100)), maxHealth) WHERE health < maxHealth AND health > 0;";
$r2 = mysqli_query($con,$q);
exit;
break;
case "reviveAll":
$q = "UPDATE users SET health = 1 WHERE health = 0;";
$r2 = mysqli_query($con,$q);
$q = "INSERT INTO userLog (discordUserID, actionType, actionData)
VALUES (0,'revive','reviveAll');";
$r2 = mysqli_query($con,$q);
exit;
break;
case "lvlinfo":
$q = "SELECT xp,lvl FROM users WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$xp=stripslashes($a['xp']);
$currentlvl=stripslashes($a['lvl']);
$lvlbase=getLevelBase();
$lvl=getLevel($xp,$lvlbase);
}
}
echo "LEVEL: ".getLevel($xp,$lvlbase),"<BR>XP: ".$xp."<BR>CURRENT LEVEL PROGRESS:".getCurrentLevelProgress($xp,$lvl);
break;
case "upgradeStats":
//Changed it to just upgrade 1 point automatically
$dataToSend2 = 1;
$q = "SELECT statPoints FROM users WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$statPoints=stripslashes($a['statPoints']);
}
if($dataToSend2 <= $statPoints){
$tableName = "";
switch (strtoupper($dataToSend)) {
case "STR":
$tableName = "strength = strength + ".$dataToSend2;
break;
case "HP":
$tableName = "maxHealth = maxHealth + 10";
break;
case "SPD":
$tableName = "speed = speed + ".$dataToSend2;
break;
case "STAM":
$tableName = "maxStamina = maxStamina + ".$dataToSend2;
break;
}
$q = "UPDATE users SET statPoints = statPoints - $dataToSend2,$tableName WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
echo "success";
} else{
echo "notEnoughPoints";
}
} else{
echo "failure";
}
break;
case "heal":
$q = "SELECT health,maxHealth,wallet FROM users WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$health=stripslashes($a['health']);
$maxHealth=stripslashes($a['maxHealth']);
$crystals=stripslashes($a['wallet']);
}
$treatmentCost = $dataToSend;
$treatmentName = $dataToSend2;
$newHealth = $health;
if($health == $maxHealth){echo "fullHealth";exit;}
if($health > 0){
if($crystals >= $treatmentCost){
switch ($treatmentName) {
case "TREAT":
$newHealth += 15;
break;
case "TREATV2":
$newHealth = $maxHealth*0.15;
break;
case "PATCH":
$newHealth += 50;
break;
case "PATCHV2":
$newHealth = $maxHealth*0.5;
break;
case "REGEN":
$newHealth += 100;
break;
case "REGENV2":
$newHealth = $maxHealth;
break;
default:
echo "cantDoThat";exit;
break;
}
if($newHealth < $health){echo "lessThanYourHealth";exit;}
if($newHealth>$maxHealth){$newHealth = $maxHealth;};
$q = "UPDATE users SET health = $newHealth,wallet = wallet - $treatmentCost WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
echo "success,".$newHealth."/".$maxHealth;
} else{
echo "notEnoughCrystals";
}
} else{
if($crystals >= $treatmentCost){
switch ($treatmentName) {
case "REVIVE":
$newHealth = 25;
break;
case "REVIVEV2":
$newHealth = $maxHealth*0.5;
break;
case "REVIVEV3":
$newHealth = $maxHealth;
break;
case "TREAT":
echo "youreKnockedOut";exit;
break;
case "TREATV2":
echo "youreKnockedOut";exit;
break;
case "PATCH":
echo "youreKnockedOut";exit;
break;
case "PATCHV2":
echo "youreKnockedOut";exit;
break;
case "REGEN":
echo "youreKnockedOut";exit;
break;
default:
echo "cantDoThat";exit;
break;
}
if($newHealth < $health){echo "lessThanYourHealth";exit;}
if($newHealth>$maxHealth){$newHealth = $maxHealth;};
$q = "UPDATE users SET health = $newHealth,wallet = wallet - $treatmentCost WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
echo "success,".$newHealth."/".$maxHealth;
}else{
echo "notEnoughCrystals";
}
}
} else{
echo "failure";
}
break;
case "addXP":
addXp($userID,$dataToSend);
break;
case "getLevelUp":
//addXp($userID,$dataToSend);
$q = "SELECT xp,lvl,statPoints FROM users WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$xp=stripslashes($a['xp']);
$lvl=stripslashes($a['lvl']);
$statPoints=stripslashes($a['statPoints']);
}
$lvlbase = getLevelBase();
$currentLVL = floor(getLevel($xp,$lvlbase));
if($currentLVL > $lvl){
$statPoints += 1;
$q = "UPDATE users SET lvl = lvl + 1,statPoints = statPoints + 1 WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
$lvl = $lvl + 1;
echo "levelup,".$lvl.",".$statPoints;
} else{
echo "xpadded,".$currentLVL.",".$statPoints;
}
}
break;
case "scavenge":
$random = floor(rand(0,101));
$ultrarare = 0;$rare = 0; $uncommon = 0; $common = 0; $scrap = 0;
if($random <= 0.5){
$ultrarare = 1;
}
if($random <= 3 && $random > 0.5){
$rare = round(rand(1,2));
}
if($random <= 10 && $random > 3){
$uncommon = round(rand(1,3));
}
if($random <= 50 && $random > 10){
$common = round(rand(1,3));
}
if($random > 50){
$scrap = round(rand(1,7));
}
$staminaCost = $dataToSend;
$crystalCost = $dataToSend2;
$q = "UPDATE users SET stamina = stamina - $staminaCost,wallet = wallet - $crystalCost WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
$q = "SELECT id FROM artifacts WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
$q = "UPDATE artifacts SET scrap = scrap + $scrap,common = common + $common,uncommon = uncommon + $uncommon,rare = rare + $rare,ultrarare = ultrarare + $ultrarare WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
echo "success,".$ultrarare.",".$rare.",".$uncommon.",".$common.",".$scrap;
} else{
$q = "INSERT INTO artifacts (discordUserID, scrap, common, uncommon, rare, ultrarare)
VALUES ($userID,$scrap,$common,$uncommon,$rare,$ultrarare);";
$r2 = mysqli_query($con,$q);
echo "success,".$ultrarare.",".$rare.",".$uncommon.",".$common.",".$scrap;
}
break;
case "artifactSell":
$q = "SELECT scrap,common,uncommon,rare,ultrarare FROM artifacts WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
$a = mysqli_fetch_assoc($r2);
$scrapQuantity=stripslashes($a['scrap']);
$commonQuantity=stripslashes($a['common']);
$uncommonQuantity=stripslashes($a['uncommon']);
$rareQuantity=stripslashes($a['rare']);
$ultrarareQuantity=stripslashes($a['ultrarare']);
$itemToSell = strtolower ($dataToSend);
$price = 0;$totalPayout = 0;$itemQuantity = 0;
$price = 0.1;
$scrapTotalPayout = round($price * $scrapQuantity);
$price = 2;
$commonTotalPayout = $price * $commonQuantity;
$price = 5;
$uncommonTotalPayout = $price * $uncommonQuantity;
$price = 10;
$rareTotalPayout = $price * $rareQuantity;
$price = 30;
$ultrarareTotalPayout = $price * $ultrarareQuantity;
$itemQuantity = $scrapQuantity + $commonQuantity + $uncommonQuantity + $rareQuantity + $ultrarareQuantity;
$totalPayout = $scrapTotalPayout + $commonTotalPayout + $uncommonTotalPayout + $rareTotalPayout + $ultrarareTotalPayout;
if($itemToSell == "all"){
$q = "UPDATE artifacts SET scrap =0,common = 0,uncommon = 0,rare = 0,ultrarare = 0 WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
$q = "UPDATE users SET wallet = wallet + $totalPayout WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
echo "success,".$itemQuantity.",".$totalPayout;
}else{
$crystals = 0;
switch ($itemToSell) {
case "scrap":
$singlePayout = $scrapTotalPayout;
$itemQuantity = $scrapQuantity;
break;
case "common":
$singlePayout = $commonTotalPayout;
$itemQuantity = $commonQuantity;
break;
case "uncommon":
$singlePayout = $uncommonTotalPayout;
$itemQuantity = $uncommonQuantity;
break;
case "rare":
$singlePayout = $rareTotalPayout;
$itemQuantity = $rareQuantity;
break;
case "ultrarare":
$singlePayout = $ultrarareTotalPayout;
$itemQuantity = $ultrarareQuantity;
break;
}
$q = "UPDATE artifacts SET $itemToSell = 0 WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
$q = "UPDATE users SET wallet = wallet + $singlePayout WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
echo "success,".$itemQuantity.",".$singlePayout;
}
}else{
echo "failure";
}
break;
case "buyDrink":
$q = "UPDATE users SET wallet = wallet - $dataToSend WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
break;
}
//echo json_encode($array);
if($debug){
echo "\n"."UID:".$userID;
}
mysqli_close($con);
function getEnemyDamage($hostileSpeed,$userSpeed,$hostileStrength){
$hitAmount = 0;
$percentage = floor(rand(0,101));
if($hostileSpeed > $userSpeed){
if($percentage <= 80 ){
//80% chance to hit you back.
$hitAmount = $hostileStrength + rand(-($hostileStrength/4),$hostileStrength/4);
}
} else{
if($percentage <= 30){
//30% chance to hit you back.
$hitAmount = $hostileStrength + rand(-($hostileStrength/4),$hostileStrength/4);
}
}
/*
if($hostileSpeed > $userSpeed){
if(20 > rand(0,100)){
$hitAmount = $hostileStrength + rand(-($hostileStrength/4),$hostileStrength/4);
}
} elseif($hostileSpeed == $userSpeed){
if(50 >= rand(0,100)){
$hitAmount = $hostileStrength + rand(-($hostileStrength/4),$hostileStrength/4);
}
} else{
if(70 >= rand(0,100)){
$hitAmount = $hostileStrength + rand(-($hostileStrength/4),$hostileStrength/4);
}
}
*/
return $hitAmount;
}
?>

View File

@@ -0,0 +1,400 @@
<?php
//header('Access-Control-Allow-Origin: *');
//header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
include_once 'functions.php';
if( isset($_GET['dataType']) ){ $dataType = $_GET['dataType']; } else{ $dataType = ''; };
if( isset($_GET['dataToSend']) ){ $dataToSend = $_GET['dataToSend']; } else{ $dataToSend = ''; };
if( isset($_GET['dataToSend2']) ){ $dataToSend2 = $_GET['dataToSend2']; } else{ $dataToSend2 = ''; };
if( isset($_GET['userid']) ){ $userID = $_GET['userid']; } else{ $userID = ''; };
if( isset($_GET['pk']) ){ $privateKey = $_GET['pk']; } else{ $privateKey = ''; };
$debug = false;
if($privateKey != privateKey()){
echo throwError("invalidPrivateKey");
exit;
}
$con = mysqlConnect();
switch ($dataType) {
//gets all dammage from users
case "sendAllAttacks":
$message = "";
$playerIDs = explode("|", $dataToSend);
$hostileType = $dataToSend2;
if (is_array($playerIDs)){
foreach($playerIDs as $item) {
$message .= "discordUserID = '".$item."' OR ";
}
$message = substr($message, 0, -4);
//echo json_encode($playerIDs);
//Get all user data
$attackerStats= array();
$q = "SELECT discordUserID,speed,maxHealth,health,strength FROM users WHERE $message;";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$discordUserID=stripslashes($a['discordUserID']);
$userHealth=stripslashes($a['health']);
$userMaxHealth=stripslashes($a['maxHealth']);
$userSpeed=stripslashes($a['speed']);
$userStrength=stripslashes($a['strength']);
$attackerStats[] = array('id'=>$discordUserID, 'maxHealth'=>$userHealth, 'health'=>$userHealth, 'speed'=>$userSpeed, 'strength'=>$userStrength, 'hitback'=>'');
}
}
//Get enemy data
$q = "SELECT hostiles.health,hostiles.maxHealth,hostiles.speed,hostiles.strength,hostiles.alive,hostiles.fled FROM hostiles WHERE hostileType = '$hostileType' ORDER BY id DESC LIMIT 1;";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$hostileHealth=stripslashes($a['health']);
$hostileMaxHealth=stripslashes($a['maxHealth']);
$hostileSpeed=stripslashes($a['speed']);
$hostileStrength=stripslashes($a['strength']);
$hostileAlive=stripslashes($a['alive']);
$hostileFled=stripslashes($a['fled']);
}
}
//do all the damage
$totalDamage = 0;
$returnInfo= array();
$query = "UPDATE users SET health = CASE discordUserID ";
$queryIDs = "";
for ($i=0;$i<count($attackerStats);$i++){
//$message += $attackerStats[$i][0];
//If bad guy is still alive, carry on.
if($hostileHealth > $attackerStats[$i]['strength']){
$totalDamage = $totalDamage + $attackerStats[$i]['strength'];
$hitAmount = getEnemyDamage($hostileSpeed,$attackerStats[$i]['speed'],$hostileStrength);
if($hitAmount > 0){
if ($hitAmount >= $attackerStats[$i]['health']){$hitAmount = $attackerStats[$i]['health'];};
$attackerStats[$i]['health'] = $attackerStats[$i]['health'] - $hitAmount;
$attackerStats[$i]['hitback'] = $hitAmount;
//$q = "UPDATE users SET health = health - $hitAmount WHERE discordUserID = '$userID' LIMIT 1";
//$r2 = mysqli_query($con,$q);
}
$query .= " WHEN ".$attackerStats[$i]['id']." THEN ".$attackerStats[$i]['health'];
$queryIDs .= $attackerStats[$i]['id'].",";
$hhealth = $hostileHealth-$totalDamage;
$returnInfo[] = array('hostileHealth'=>$hhealth.'|'.$hostileMaxHealth, 'atkDamage'=>$attackerStats[$i]['strength'], 'id'=>$attackerStats[$i]['id'], 'hitback'=>$hitAmount, 'userHealth'=>$attackerStats[$i]['health']."|".$attackerStats[$i]['maxHealth']);
}else{
//If the bad guy is not alive, finish up.
$q = "UPDATE hostiles SET health = 0 WHERE hostileType = '$hostileType' ORDER BY id DESC LIMIT 1";
$r2 = mysqli_query($con,$q);
$query .= " END
WHERE discordUserID IN (".substr($queryIDs, 0, -1).");";
$r2 = mysqli_query($con,$query);
echo json_encode($returnInfo);
exit;
}
}
//assemble the end of the query.
$query .= " END
WHERE discordUserID IN (".substr($queryIDs, 0, -1).");";
$r2 = mysqli_query($con,$query);
$q = "UPDATE hostiles SET health = health - $totalDamage WHERE hostileType = '$hostileType' ORDER BY id DESC LIMIT 1";
$r2 = mysqli_query($con,$q);
echo json_encode($returnInfo);
exit;
}else{
echo "notArray";
exit;
}
break;
case "lvlinfo":
$q = "SELECT xp,lvl FROM users WHERE discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$xp=stripslashes($a['xp']);
$currentlvl=stripslashes($a['lvl']);
$lvlbase=getLevelBase();
$lvl=getLevel($xp,$lvlbase);
$level = $dataToSend2;
$str = generateStatFromLevel($level,"str");
$spd = generateStatFromLevel($level,"spd");
$hp = generateStatFromLevel($level,"hp");
$stash = generateStatFromLevel($level,"stash");
}
}
//echo "LEVEL: ".getLevel($xp,$lvlbase),"<BR>XP: ".$xp."<BR>CURRENT LEVEL PROGRESS:".getCurrentLevelProgress($xp,$lvl);
echo "LEVEL: ".getLevel($dataToSend,$lvlbase),"<BR>XP: ".$xp."<BR>CURRENT LEVEL PROGRESS:".getCurrentLevelProgress($xp,$lvl)."<BR><BR>STR: ".$str." SPD: ".$spd." HP: ".$hp." STASH:: ".$stash;
break;
case "attack":
$q = "UPDATE hostiles SET health = health - $dataToSend WHERE id = '$dataToSend2' LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "UPDATE users SET stamina = stamina - 1 WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
//$q = "INSERT INTO userLog (discordUserID, actionType, actionData)
//VALUES (" . $userID . ", '" . $dataType . "', '$userID attacked Ravager#$dataToSend2.');";
//$r2 = mysqli_query($con,$q);
$q = "INSERT INTO attackLog (discordUserID, hostileID, damage)
VALUES ('$userID','$dataToSend2','$dataToSend');";
$r2 = mysqli_query($con,$q);
$q = "SELECT hostiles.health,hostiles.maxHealth,hostiles.speed,hostiles.strength,users.speed as userspeed,users.health as userhealth FROM hostiles,users WHERE hostiles.id = '$dataToSend2' AND users.discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$hostileHealth=stripslashes($a['health']);
$hostileMaxHealth=stripslashes($a['maxHealth']);
$hostileSpeed=stripslashes($a['speed']);
$hostileStrength=stripslashes($a['strength']);
$userSpeed=stripslashes($a['userspeed']);
$userHealth=stripslashes($a['userhealth']);
}
if($hostileHealth <= 0){
if($hostileHealth < 0){ $hostileHealth = 0;};
//returns health less than zero, kill enemy.
$q = "UPDATE hostiles SET alive = 0,health = 0 WHERE id = '$dataToSend2' LIMIT 1";
$r2 = mysqli_query($con,$q);
}
$criticalHit = 0;
$hitAmount = getEnemyDamage($hostileSpeed,$userSpeed,$hostileStrength);
if($hitAmount > 0){
if ($hitAmount >= $userHealth){$hitAmount = $userHealth; $criticalHit = 1;};
$q = "UPDATE users SET health = health - $hitAmount WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
}
echo $hostileHealth.",".$hostileMaxHealth.",".$hitAmount.",".$criticalHit;
exit;
} else{
echo "0";
}
exit;
break;
case "hostileAttackBack":
$q = "UPDATE users SET stamina = stamina - 1 WHERE discordUserID = '$userID' AND stamina > 0 LIMIT 1";
$r2 = mysqli_query($con,$q);
$q = "SELECT hostiles.health,hostiles.maxHealth,hostiles.speed,hostiles.strength,users.speed as userspeed,users.health as userhealth FROM hostiles,users WHERE hostiles.id = '$dataToSend2' AND users.discordUserID = '$userID';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$hostileHealth=stripslashes($a['health']);
$hostileMaxHealth=stripslashes($a['maxHealth']);
$hostileSpeed=stripslashes($a['speed']);
$hostileStrength=stripslashes($a['strength']);
$userSpeed=stripslashes($a['userspeed']);
$userHealth=stripslashes($a['userhealth']);
}
$criticalHit = 0;
$hitAmount = getEnemyDamage($hostileSpeed,$userSpeed,$hostileStrength);
if($hitAmount > 0){
if ($hitAmount >= $userHealth){$hitAmount = $userHealth; $criticalHit = 1;};
$q = "UPDATE users SET health = health - $hitAmount WHERE discordUserID = '$userID' LIMIT 1";
$r2 = mysqli_query($con,$q);
}
echo $hostileHealth.",".$hostileMaxHealth.",".$hitAmount.",".$criticalHit;
exit;
} else{
echo "0";
}
exit;
break;
case "hostileFlee":
$q = "SELECT id FROM hostiles WHERE alive = 1 ORDER BY id DESC LIMIT 1;";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$hostileID=stripslashes($a['id']);
}
$q = "UPDATE hostiles SET fled = 1,alive=0 WHERE id = '$hostileID' LIMIT 1";
$r2 = mysqli_query($con,$q);
echo "fled";
} else{
echo "alreadyDead";
}
break;
case "newHostile":
$q = "SELECT id FROM hostiles WHERE alive = 1 LIMIT 1;";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
echo "notCreated";
} else{
$elvl = $dataToSend;
$healthBase = 50; $strengthBase = 3; $speedBase = 3; $stashBase = 3;
$healthMin = ($healthBase * $elvl) / 2; $healthMax = $healthBase * $elvl;
$strengthMin = ($strengthBase * $elvl) / 2; $strengthMax = $strengthBase * $elvl;
$speedMin = ($speedBase * $elvl) / 2; $speedMax = $speedBase * $elvl;
$stashMin = ($stashBase * $elvl) / 2; $stashMax = $stashBase * $elvl;
$health = floor(rand($healthMin,$healthMax));
$strength = floor(rand($strengthMin,$strengthMax));
$speed = floor(rand($speedMin,$speedMax));
$stash = floor(rand($stashMin,$stashMax));
$claimID = floor(rand(1000,9999));
$q = "INSERT INTO hostiles (hostileType, maxHealth, health, strength, speed, stash, alive, claimID)
VALUES ('ravager', '$health', '$health', '$strength', '$speed', '$stash', 1, '$claimID');";
$r2 = mysqli_query($con,$q);
echo $health.",".$speed.",".$strength.",".$claimID;
}
break;
case "getHostileData":
$q = "SELECT stash,claimID FROM hostiles WHERE alive = 0 AND id = '$dataToSend' LIMIT 1;";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$stash=stripslashes($a['stash']);
$claimID=stripslashes($a['claimID']);
}
echo $stash.",".$claimID;
}
exit;
break;
case "getDamageDistribution":
//Gets base stats for enemy
$q = "SELECT stash,maxHealth,fled FROM hostiles WHERE id = '$dataToSend' LIMIT 1;";
$r2 = mysqli_query($con,$q);
$a = mysqli_fetch_assoc($r2);
$stash=stripslashes($a['stash']);
$maxHealth=stripslashes($a['maxHealth']);
$fled=stripslashes($a['fled']);
$totalCrystalsInStash = 0;
if($fled == 1){
echo "fled";
}else{
//gets all dammage from users
$damageDistribution = array();
$q = "SELECT discordUserID,SUM(damage) totalDamage FROM attackLog WHERE hostileID = $dataToSend GROUP BY discordUserID;";
//$q = "SELECT attackLog.damage,attackLog.discordUserID,hostiles.stash,hostiles.maxHealth FROM attackLog WHERE hostiles.id = attackLog.hostileID AND attackLog.hostileID = '$dataToSend';";
$r2 = mysqli_query($con,$q);
if ( $r2 !== false && mysqli_num_rows($r2) > 0 ) {
while ( $a = mysqli_fetch_assoc($r2) ) {
$damage=stripslashes($a['totalDamage']);
$discordUserID=stripslashes($a['discordUserID']);
$damagePercent = round(( $damage / $maxHealth ) * 100);
$percentStashAmount = round($stash * ($damagePercent/100));
$totalCrystalsInStash += $percentStashAmount;
// you can add single array values too
$damageDistribution[] = array('id'=>$discordUserID, 'totalDamage'=>$damage, 'damagePercent'=>$damagePercent, 'crystalsReceived'=>$percentStashAmount);
if($dataToSend2 == 1){
//Flag to actually distribute crystals
$q2 = "UPDATE users SET wallet = wallet + $percentStashAmount WHERE discordUserID = '$discordUserID' LIMIT 1";
$r3 = mysqli_query($con,$q2);
}
}
echo json_encode($damageDistribution);
} else{
echo 0;
}
exit;
}
break;
}
//echo json_encode($array);
if($debug){
echo "\n"."UID:".$userID;
}
mysqli_close($con);
function getEnemyDamage($hostileSpeed,$userSpeed,$hostileStrength){
$hitAmount = 0;
$percentage = floor(rand(0,101));
if($hostileSpeed > $userSpeed){
if($percentage <= 80 ){
//80% chance to hit you back.
$hitAmount = $hostileStrength + rand(-($hostileStrength/4),$hostileStrength/4);
}
} else{
if($percentage <= 30){
//30% chance to hit you back.
$hitAmount = $hostileStrength + rand(-($hostileStrength/4),$hostileStrength/4);
}
}
/*
if($hostileSpeed > $userSpeed){
if(20 > rand(0,100)){
$hitAmount = $hostileStrength + rand(-($hostileStrength/4),$hostileStrength/4);
}
} elseif($hostileSpeed == $userSpeed){
if(50 >= rand(0,100)){
$hitAmount = $hostileStrength + rand(-($hostileStrength/4),$hostileStrength/4);
}
} else{
if(70 >= rand(0,100)){
$hitAmount = $hostileStrength + rand(-($hostileStrength/4),$hostileStrength/4);
}
}
*/
return $hitAmount;
}
function generateStatFromLevel($level,$stat){
$value = 0;
if(strtolower($stat) === "str"){
$value = (round((((($level + 1) * log10($level + 1)) / (0.02 * ($level + 1))) + 0.6) * 0.4)) -2;
$value = round($value + (rand(-$value/10,$value/10)));
if($level < 15){$value = round($value * 0.9);};
}elseif(strtolower($stat) === "spd"){
$value = (round((((($level + 1) * log10($level + 1)) / (0.02 * ($level + 1))) + 0.6) * 0.4)) -2 ; //round(rand(-2,2))
$value = round($value + (rand(-$value/10,$value/10)));
if($level < 15){$value = round($value * 0.9);};
}elseif(strtolower($stat) === "hp"){
$value = floor(50 + (30 * $level) + pow($level, 1.5));
}elseif(strtolower($stat) === "stash"){
$value = (round((((($level + 1) * log10($level + 1)) / (0.02 * ($level + 1))) + 0.6) * 0.1)) ;
$value = rand(pow($value, 2.2),pow($value, 2.3));
if($level < 15){$value = round($value * 0.7);};
}
return $value;
}
?>