PDA

View Full Version : Secure Random Adoption V2


fadillzzz
03-13-2011, 04:41 AM
This mod is intended for people who already or wanted to have a random adoption process on their adoptables site.

Compatibility: Mys V1.2.x
NOTE:Please make a backup of your files before installing this mod!


1. Open your adopt.php and find

$query = "SELECT * FROM ".$prefix."adoptables";
replace it with the code below to fetch only 1 random adoptables from the database

$query = "SELECT * FROM ".$prefix."adoptables ORDER BY RAND() LIMIT 1";
2. A few lines after that, find

$article_content .= "<form name='form1' method='get' action='doadopt.php'>
replace it with this

$article_content .= "<form name='form1' method='post' action='adopt.php'>
3. And then, go find this code

if($canadopt == "yes"){
//If we can adopt the adoptable, show the image and adoption link...
$article_content .= "<tr>
<td><input type='radio' name='id' id='id' value='{$aid}' /></td>
<td style='text-align: center'><img src='".$eggimage."' /></td>

<td><strong>{$type}</strong> - {$row['cost']} {$GLOBALS['settings']['cost']}.<br />{$description}</td></tr>";
}
Since we only want to show 1 adoptables and obviously without any information for randomness, replace that code with

if($canadopt == "yes"){
//If we can adopt the adoptable, show the image and adoption link...
$article_content .= "<tr>
<td style='text-align: center'><img src='".$eggimage."' /></td>";
}
else{
$eresult = runquery("SELECT * FROM ".$prefix."adoptables WHERE whenisavail='always' ORDER BY RAND() LIMIT 1");
while($erow = mysql_fetch_array($eresult)){
$aid=$erow['id']; //The adoptable's ID
$type=$erow['type'];
$description=$erow['description'];
$eggimage=$erow['eggimage'];
$article_content .= "<tr>

<td style='text-align: center'><img src='".$eggimage."' /></td>";
}

}
You may notice that I put an else statement. The else statement is used in case the first query fetch an adoptables that you can't adopt because you haven't met the necessary requirement. It will fetch an adoptables with the always available condition.
Anyway, let's move on.
4. Find the following

$query = "SELECT * FROM ".$prefix."adoptables WHERE id='$id' LIMIT 1";
and replace it with

$query = "SELECT * FROM ".$prefix."adoptables WHERE id='$id' AND whenisavail='promo' LIMIT 1";
Restrict the GET method only for adopting adoptables with promo code
5. Finally, find the last closing curly bracket

} // This bracket ends the else statements for whether or not an ID was entered
and below that add this code

if($_POST)
{
$id = $aid;
$name = $_POST["name"];

if($isloggedin == "yes"){
// I guess the first thing to do is see if we have a valid adoptable ID submitted...
if($id == "" or !is_numeric($id)){
$article_title = $err_idnoexist;
$article_content = $err_idnoexist_text;
}
else{
// The adoptable ID appears to be valid, so we need to double check that it is valid by pulling up the adoptable in the DB

$query = "SELECT * FROM ".$prefix."adoptables WHERE id='$id'";
$result = runquery($query);

$result = runquery($query);
$row = mysql_fetch_array($result);

$aid = $row['id'];
$type=$row['type'];
$description=$row['description'];
$eggimage=$row['eggimage'];

if($id == $aid){
// The ID submitted matches an existing adoptable type
$canadopt = canadopt($aid, "adopting", $promocode, $row);

// If we can adopt this creature, do the adoption
if($canadopt == "yes") {
if (changecash(-$row['cost'], $GLOBALS['loggedinname'], $GLOBALS['money'])==true) {
// BEGIN the actual adoption process

// First we see if we have a custom name; if not, we use the default name
if($name == ""){
$name = "Mystery Egg";
}

// Now we determine if we are using alternate images or not

$alts = getaltstatus($id, 0, 0);

// We need a unique code for the adoptable so we can show it to the user when we're done here...

$code = rand(1, 20000);
$genders = array('f', 'm');
$rand = rand(0,1);

runquery("INSERT INTO ".$prefix."owned_adoptables VALUES ('', '$type', '$name','$loggedinname','0','0', '$code', '','$alts','fortrade','no', '$genders[$rand]','0')");

// Adoption complete, show the user a confirmation screen...

$query = "SELECT * FROM ".$prefix."owned_adoptables WHERE code='$code' and owner='$loggedinname'";
$result = runquery($query);
$num = mysql_numrows($result);

$id=@mysql_result($result,0,"aid");

$article_title = $name." adopted successfully";
$article_content = "<img src='".$eggimage."'><br>".$congrats1." ".$name.". You can now manage ".$name." on the
<a href='myadopts.php'>My Adopts</a> page.<br><br><b><a href='myadopts.php?act=manage&id=".$id."'>Click Here to Manage ".$name."</a><br>
<a href='myadopts.php?act=bbcode&id=".$id."'>Click Here to get BBCodes / HTML Codes for ".$name."</a></b><br><br>
Be sure and <a href='levelup.php?id=".$id."'>feed</a> ".$name." with clicks so that they grow!";

// END the actual adoption process
}
else {
$article_title = "Not enough money.";
$article_content = "You don't have enough {$GLOBALS['settings']['cost']} to buy this adoptable. Earn some money and then try again.";
}
}
else {
$article_title = $accden;
$article_content = $adoptnoper;
}
} // End the if for if $id == $aid
else {
// Adoptable does not exist, show an error.

$article_title = $err_idnoexist;
$article_content = $err_idnoexist_text;
} // End the else for if $id == $aid
} // End the valid ID input else test statement (bulk of code goes above here)
} // End the log in check IF
else {
// Guests cannot adopt pets, so why bother...
$article_title = $guesttitleerror;
$article_content = $guesterror;
} // End the log in check ELSE
}

6. Now, to prevent some users from cheating. Open your doadopt.php, we'll just do a slight query modification.
Find this query

$query = "SELECT * FROM ".$prefix."adoptables WHERE id='$id'";
replace that query with this one

$query = "SELECT * FROM ".$prefix."adoptables WHERE id='$id' AND whenisavail = 'promo'";
We just restricted the doadopt.php file to only work with adoptables with promo code.

Done!

Let me know if there's any problem with this mod. :)

Missy Master
03-15-2011, 08:58 AM
Excellent, this happens to be exactly what I needed right now!! Thank you so much!!

RoconzaArt
03-18-2011, 11:50 AM
I'd like to thank you very much this is just what I needed for my site and it works perfectly.

fadillzzz
03-18-2011, 12:00 PM
You're welcome!
I'm glad that this mod help some people on building their adoptables site. :pleased:

Kesstryl
02-28-2012, 10:33 AM
I noticed that the code in the doadopt.php has some minor variation, will this effect the above changes?

Here's the current code, noticing brackets instead of single quotes:

$query = "SELECT * FROM {$prefix}adoptables WHERE id={$id}";

Hall of Famer
02-28-2012, 11:20 AM
Nope, the bracket and single quote both work perfectly. In fact the bracket version is preferred and used in current Mysidia Adoptables script, as it is easier to read from the viewpoints of mine and my coders'.

Kesstryl
02-28-2012, 11:29 AM
Thanks for the quick reply :pleased:

Kesstryl
02-28-2012, 01:57 PM
I'm not sure if this is the place to ask, and if not, let me know, but I would like to add an extra feature to this code which makes some of the random pets more rare than others. I found this code on this site which uses a 'chance' variable which will decrease the odds of the random pick on some of the images:

http://www.chickensmoothie.com/tutorial/tut3/index.html

If anyone has time to take a peek and let me know how this can be added into this mod code, I would be very grateful as I'm good at following directions, but not so good at knowing how to do this myself.