I'm looking to tidy up a couple of functions if possible. The second one is the longer of the two. These are both quite old and i'm wondering if there's a better way of doing them
First one
// Update eggs
$commons = array(1, 3, 4);
$egg = array();
for($i=0;$i<5;$i++)
{
$rand = rand(1,100);
if($rand <= 60)
{
$egg[] = 1;
}
elseif($rand > 60 && $rand <= 80)
{
$egg[] = 2;
}
elseif($rand > 80 && $rand <= 94)
{
$egg[] = 3;
}else
{
$egg[] = 4;
}
}
$sql = "INSERT INTO calona_user_eggs (uid, id1, id2, id3, id4, id5, date)
SELECT
u.user_id,
(select CASE a.cid
WHEN NULL THEN ".array_rand($commons)."
ELSE a.cid
END
from calona_user_access as a
left join calona_creatures as c on c.ID = a.cid
where a.uid = u.user_id
and c.rarity = ".$egg[0]."
ORDER BY rand()
LIMIT 1
) AS id1,
(select CASE a.cid
WHEN NULL THEN ".array_rand($commons)."
ELSE a.cid
END
from calona_user_access as a
left join calona_creatures as c on c.ID = a.cid
where a.uid = u.user_id
and c.rarity = ".$egg[1]."
ORDER BY rand()
LIMIT 1
) AS id2,
(select CASE a.cid
WHEN NULL THEN ".array_rand($commons)."
ELSE a.cid
END
from calona_user_access as a
left join calona_creatures as c on c.ID = a.cid
where a.uid = u.user_id
and c.rarity = ".$egg[2]."
ORDER BY rand()
LIMIT 1
) AS id3,
(select CASE a.cid
WHEN NULL THEN ".array_rand($commons)."
ELSE a.cid
END
from calona_user_access as a
left join calona_creatures as c on c.ID = a.cid
where a.uid = u.user_id
and c.rarity = ".$egg[3]."
ORDER BY rand()
LIMIT 1
) AS id4,
(select CASE a.cid
WHEN NULL THEN ".array_rand($commons)."
ELSE a.cid
END
from calona_user_access as a
left join calona_creatures as c on c.ID = a.cid
where a.uid = u.user_id
and c.rarity = ".$egg[4]."
ORDER BY rand()
LIMIT 1
) AS id5, '".date("Y-m-d")."'
FROM
phpbb_users u";
mysql_query($sql) or die("Insert Error: ".$sql."".mysql_error());
and the second one
// Update bred eggs
$nobreed = array(32, 35, 23, 26, 98); // Eggs not allowed to be bred
$sql = "SELECT ID, uid FROM calona_user_breeding ORDER BY uid ASC";
$result = mysql_query($sql) or die("Select Error: ".$sql."".mysql_error());
while($row = mysql_fetch_array($result))
{
$chance = rand(1,100);
// Get the two creatures breeding
$bsql = "SELECT e.ID AS cid, e.rarity, u.gender, c.family
FROM calona_user_breed AS b
LEFT JOIN calona_user_creatures AS u ON u.ID = b.cid
LEFT JOIN calona_creatures AS c ON u.cid = c.ID
LEFT JOIN calona_creatures AS e ON e.family = c.family
AND e.type = 'Egg'
WHERE bid = ".$row['ID'];
$bresult = mysql_query($bsql) or die("Select Error: ".$bsql."".mysql_error());
if(mysql_num_rows($bresult) == 2 && ($chance >= 25 && $chance <= 75))
{
// User has a breeding pair
while($brow = mysql_fetch_array($bresult))
{
if(!isset($num1))
{
// Creature 1
$num1 = $brow['cid'];
$rare1 = $brow['rarity'];
$gen1 = $brow['gender'];
$fam1 = $brow['family'];
}
else
{
$num2 = $brow['cid'];
$rare2 = $brow['rarity'];
$gen2 = $brow['gender'];
$fam2 = $brow['family'];
}
}
if( !in_array($num1, $nobreed) && !in_array($num2, $nobreed) )
{
if(isset($num1) && isset($num2) && (($gen1 == 'f' && $gen2 == 'm') || ($gen1 = 'm' && $gen2 = 'f'))) {
$rarity = array(1=>100,2=>60,3=>30, 4=>10);
$egg1 = $rarity[$rare1];
$egg2 = $rarity[$rare2];
$chance2 = rand(1,100);
$chance3 = rand(1,100);
$asql = "SELECT aid
FROM calona_user_areas
WHERE name = 'Free Roaming'
AND uid = " . $row['uid'];
$aresult = mysql_query($asql) or die(mysql_error());
$arow = mysql_fetch_array($aresult);
$area = $arow['aid'];
// Get the gender of the parents
$gsql = "SELECT u.ID, u.gender
FROM calona_user_breed as b
LEFT JOIN calona_user_breeding AS bg ON bg.ID = b.bid
LEFT JOIN calona_user_creatures AS u ON u.ID = b.cid
WHERE bg.uid = ".$row['uid'];
$gresult = mysql_query($gsql) or die(mysql_error()."\n".$gsql);
while($grow = mysql_fetch_array($gresult))
{
if($grow['gender'] == 'f')
{
$mum = $grow['ID'];
}
else
{
$dad = $grow['ID'];
}
}
if($chance2 >=1 && $chance2 <= $egg1)
{
$str = rand(1,5);
$int = rand(1,5);
$agi = rand(1,5);
$dex = rand(1,5);
// Egg 1 can be obtained ($egg1 == $rare1 == $num1)
$sql = "INSERT INTO calona_user_creatures (uid, cid, name, str, agi, `int`, dex, area, gained, breed) VALUES
(".$row['uid'].", ".$num1.", 'Egg', ".$str.", ".$agi.", ".$int.", ".$dex.", ".$area.", ".time().", 1)";
mysql_query($sql) or die(mysql_error()."\n".$sql);
// Insert parent info
$sql = "INSERT INTO calona_user_parents (child, mother, father) VALUES
(".mysql_insert_id().", ".$mum.", ".$dad.")";
mysql_query($sql) or die(mysql_error()."\n".$sql);
print $row['uid']." Egg 1\n";
}
if($chance3 >=1 && $chance3 <= $egg2)
{
$str = rand(1,5);
$int = rand(1,5);
$agi = rand(1,5);
$dex = rand(1,5);
// Egg 2 can be obtained ($egg2 == $rare2 == $num2)
$sql = "INSERT INTO calona_user_creatures (uid, cid, name, str, agi, `int`, dex, area, gained, breed) VALUES
(".$row['uid'].", ".$num2.", 'Egg', ".$str.", ".$agi.", ".$int.", ".$dex.", ".$area.", ".time().", 1)";
mysql_query($sql) or die(mysql_error()."\n".$sql);
// Insert parent info
$sql = "INSERT INTO calona_user_parents (child, mother, father) VALUES
(".mysql_insert_id().", ".$mum.", ".$dad.")";
mysql_query($sql) or die(mysql_error()."\n".$sql);
print $row['uid']." Egg 2\n";
}
}
}
}
unset($num1, $num2, $rare1, $rare2, $gen1, $gen2, $fam1, $fam2, $chance, $egg1, $egg2, $chance2, $chance3);
}
thanks to those that can give hints