Re: [mccode v2] Roulette
When i installed this, i noticed that the main problems where that the colors where often in the wrong order. So i decided to sort that out. So Here It Is.
<?php
include "globals.php";
function getAvailableCash( )
{
global $ir;
return $ir['money'];
}
function getMaximumBet( )
{
global $ir;
return min($ir['level'] * 150, getAvailableCash());
}
function creditPlayer( $amount )
{
global $db, $userid, $ir;
$sql = sprintf("UPDATE `users` SET `money` = `money` + %u WHERE (`userid` = %u)", $amount, $userid);
$db->query($sql);
$ir['money'] += $amount;
}
function debitPlayer( $amount )
{
global $db, $userid, $ir;
$sql = sprintf("UPDATE `users` SET `money` = GREATEST(0, `money` - %u) WHERE (`userid` = %u)", $amount, $userid);
$db->query($sql);
$ir['money'] = max(0, $ir['money'] - $amount);
}
function pageFooter( )
{
global $h;
$h->endpage();
}
function lookupBet( $bet )
{
$data = array
(
'0' => "0,35,0",
'00' => "00,35,00",
'1' => "1,35,1",
'2' => "2,35,2",
'3' => "3,35,3",
'4' => "4,35,4",
'5' => "5,35,5",
'6' => "6,35,6",
'7' => "7,35,7",
'8' => "8,35,8",
'9' => "9,35,9",
'10' => "10,35,10",
'11' => "11,35,11",
'12' => "12,35,12",
'13' => "13,35,13",
'14' => "14,35,14",
'15' => "15,35,15",
'16' => "16,35,16",
'17' => "17,35,17",
'18' => "18,35,18",
'10' => "19,35,19",
'20' => "20,35,20",
'21' => "21,35,21",
'22' => "22,35,22",
'23' => "23,35,23",
'24' => "24,35,24",
'25' => "25,35,25",
'26' => "26,35,26",
'27' => "27,35,27",
'28' => "28,35,28",
'29' => "29,35,29",
'30' => "30,35,30",
'31' => "31,35,31",
'32' => "32,35,32",
'33' => "33,35,33",
'34' => "34,35,34",
'35' => "35,35,35",
'36' => "36,35,36",
'R0' => "Row 0,17,0-00",
'R1' => "Row 1,11,1-2-3",
'R2' => "Row 2,11,4-5-6",
'R3' => "Row 3,11,7-8-9",
'R4' => "Row 4,11,10-11-12",
'R5' => "Row 5,11,13-14-15",
'R6' => "Row 6,11,16-17-18",
'R7' => "Row 7,11,19-20-21",
'R8' => "Row 8,11,22-23-24",
'R9' => "Row 9,11,25-26-27",
'R10' => "Row 10,11,28-29-30",
'R11' => "Row 11,11,31-32-33",
'R12' => "Row 12,11,34-35-36",
'C1' => "Column 1,2,1-4-7-10-13-16-19-22-25-28-31-34",
'C2' => "Column 2,2,2-5-8-11-14-17-20-23-26-29-32-35",
'C3' => "Column 3,2,3-6-9-12-15-18-21-24-27-30-33-36",
'F12' => "First 12,2,1-2-3-4-5-6-7-8-9-10-11-12",
'M12' => "Second 12,2,13-14-15-16-17-18-19-20-21-22-23-24",
'L12' => "Third 12,2,25-26-27-28-29-30-31-32-33-34-35-36",
'F18' => "First 18,1,1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18",
'L18' => "Last 18,1,19-20-21-22-23-24-25-26-27-28-29-30-31-32-33-34-35-36",
'ODD' => "Odd,1,1-3-5-7-9-11-13-15-17-19-21-23-25-27-29-31-33-35",
'EVEN' => "Even,1,2-4-6-8-10-12-14-16-18-20-22-24-26-28-30-32-34-36",
'RED' => "Red,1,1-3-5-7-9-12-14-16-18-19-21-23-25-27-30-32-34-36",
'BLACK' => "Black,1,2-4-6-8-10-11-13-15-17-20-22-24-26-28-29-31-33-35",
);
if (is_string($bet))
{
$bet = strtoupper(trim($bet));
if (strlen($bet) && isset($data[$bet]))
{
list($name, $payoff, $numbers) = explode(",", $data[$bet]);
return array('name' => $name, 'payoff' => $payoff, 'numbers' => explode("-", $numbers));
}
}
return null;
}
function formatLink( $text, $link )
{
$found = false;
foreach ($_SESSION['roulette']['bets'] as $bet)
if ($bet == $link)
$found = lookupBet($bet);
if ($found)
return sprintf("[img=/images/roulette.png]", $found['name'], $found['name']);
if (count($_SESSION['roulette']['bets']) == 5)
return $text;
else
return sprintf("[url='roulette.php?action=place&bet=%s']%s[/url]", $link, $text);
}
function renderPage( $spun = null, $winAmount = null, $betAmount = null )
{
$green_background = "#3C3";
$red_background = "#F33";
$black_background = "#333";
$border_color = "#333";
$green_link = "#111";
$red_link = "#EEE";
$black_link = "#EEE";
echo "<style type='text/css'>";
echo "table.roulette-table { border-collapse:collapse; }";
echo sprintf("table.roulette-table td { text-align:center; padding:0.5em; border:solid 1px %s; }", $border_color);
echo sprintf("table.roulette-table td.g { background:%s; }", $green_background);
echo sprintf("table.roulette-table td.r { background:#F33;color:#EEE; }", $red_background);
echo sprintf("table.roulette-table td.b { background:#333;color:#EEE; }", $black_background);
echo sprintf("table.roulette-table td.g a { color: #111; }", $green_link);
echo sprintf("table.roulette-table td.r a { color: #EEE; }", $red_link);
echo sprintf("table.roulette-table td.b a { color: #EEE; }", $black_link);
echo "</style>";
echo "<h3>Roulette</h3>";
echo "<table cellpadding='0' cellspacing='0' style='width:95%;'>";
echo "<tr>";
echo "<td align='left' style='vertical-align:top;'>";
echo "You may place up to 5 bets before 'spinning' the wheel…";
echo "
";
$bets = $_SESSION['roulette']['bets'];
$i = 0;
$numbers = array();
foreach ($bets as $bet)
{
$info = lookupBet($bet);
foreach ($info['numbers'] as $number)
$numbers[$number] = true;
echo sprintf(" Bet #%u: %s ([url='roulette.php?action=clear&bet=%s']Clear[/url])
", ++$i, $info['name'], $bet);
}
if ($i)
{
echo "
";
echo " [url='roulette.php?action=reset']Clear All[/url]";
echo "
";
echo "Winning numbers:
";
$numbers = array_keys($numbers);
sort($numbers);
for ($i = 0, $count = count($numbers); $i < $count; $i++)
echo sprintf("%s%s%u", $i % 6 ? "" : "
", $i % 6 ? ", " : "", $numbers[$i]);
echo "
";
}
echo sprintf("You may bet up to a maximum of $%s
", number_format(getMaximumBet()));
echo sprintf(" Current bet: $%s
", number_format($_SESSION['roulette']['cash']));
$bets = array(5, 25, 50, 100, 250, 500, 1000, 2500, 5000);
echo sprintf(" Bet: ");
for ($i = 0, $count = count($bets); $i < $count; $i++)
echo sprintf("%s[url='roulette.php?action=money&amount=%u']$%s[/url]", $i ? ", " : "", $bets[$i], number_format($bets[$i]));
echo "
";
if (!is_null($spun))
{
if ($winAmount > 0)
{
echo sprintf("<span style='color:#070;'>[b]%u[/b] – Congratulations, you win $%s</span>", $spun, number_format($winAmount));
}
else
{
echo sprintf("<span style='color:#700;'>[b]%u[/b] – Sorry, you lose $%s</span>", $spun, number_format($betAmount));
}
}
else
{
echo "Spin the wheel to gamble…";
}
echo "
";
echo "<form action='roulette.php?action=play' method='post'>";
echo "<input type='submit' value='Spin the Wheel' />";
echo "</form>";
echo "</td>";
echo "<td align='right' style='vertical-align:top;'>";
echo "<table cellpadding='0' cellspacing='0' border='1' class='roulette-table'>";
echo "<tr>";
echo "<td class='g' colspan='2'></td>";
echo "<td class='g'>" . formatLink("0", "0") . "</td>";
echo "<td class='g'></td>";
echo "<td class='g'>" . formatLink("00", "00") . "</td>";
echo "<td class='g'>" . formatLink("«", "R0") . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='g' rowspan='2'>" . formatLink("1 to 18", "F18") . "</td>";
echo "<td class='g' rowspan='4'>" . formatLink("1<sup>st</sup> 12", "F12") . "</td>";
echo "<td class='r'>" . formatLink("1", "1") . "</td>";
echo "<td class='b'>" . formatLink("2", "2") . "</td>";
echo "<td class='r'>" . formatLink("3", "3") . "</td>";
echo "<td class='g'>" . formatLink("«", "R1") . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='b'>" . formatLink("4", "4") . "</td>";
echo "<td class='r'>" . formatLink("5", "5") . "</td>";
echo "<td class='b'>" . formatLink("6", "6") . "</td>";
echo "<td class='g'>" . formatLink("«", "R2") . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='g' rowspan='2'>" . formatLink("ODD", "ODD") . "</td>";
echo "<td class='r'>" . formatLink("7", "7") . "</td>";
echo "<td class='b'>" . formatLink("8", "8") . "</td>";
echo "<td class='r'>" . formatLink("9", "9") . "</td>";
echo "<td class='g'>" . formatLink("«", "R3") . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='b'>" . formatLink("10", "10") . "</td>";
echo "<td class='r'>" . formatLink("11", "11") . "</td>";
echo "<td class='b'>" . formatLink("12", "12") . "</td>";
echo "<td class='g'>" . formatLink("«", "R4") . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='r' rowspan='2'>" . formatLink("RED", "RED") . "</td>";
echo "<td class='g' rowspan='4'>" . formatLink("2<sup>nd</sup> 12", "M12") . "</td>";
echo "<td class='r'>" . formatLink("13", "13") . "</td>";
echo "<td class='b'>" . formatLink("14", "14") . "</td>";
echo "<td class='r'>" . formatLink("15", "15") . "</td>";
echo "<td class='g'>" . formatLink("«", "R5") . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='b'>" . formatLink("16", "16") . "</td>";
echo "<td class='r'>" . formatLink("17", "17") . "</td>";
echo "<td class='b'>" . formatLink("18", "18") . "</td>";
echo "<td class='g'>" . formatLink("«", "R6") . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='b' rowspan='2'>" . formatLink("BLACK", "BLACK") . "</td>";
echo "<td class='r'>" . formatLink("19", "19") . "</td>";
echo "<td class='b'>" . formatLink("20", "20") . "</td>";
echo "<td class='r'>" . formatLink("21", "21") . "</td>";
echo "<td class='g'>" . formatLink("«", "R7") . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='b'>" . formatLink("22", "22") . "</td>";
echo "<td class='r'>" . formatLink("23", "23") . "</td>";
echo "<td class='b'>" . formatLink("24", "24") . "</td>";
echo "<td class='g'>" . formatLink("«", "R8") . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='g' rowspan='2'>" . formatLink("EVEN", "EVEN") . "</td>";
echo "<td class='g' rowspan='4'>" . formatLink("3<sup>rd</sup> 12", "L12") . "</td>";
echo "<td class='r'>" . formatLink("25", "25") . "</td>";
echo "<td class='b'>" . formatLink("26", "26") . "</td>";
echo "<td class='r'>" . formatLink("27", "27") . "</td>";
echo "<td class='g'>" . formatLink("«", "R9") . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='b'>" . formatLink("28", "28") . "</td>";
echo "<td class='r'>" . formatLink("29", "29") . "</td>";
echo "<td class='b'>" . formatLink("30", "30") . "</td>";
echo "<td class='g'>" . formatLink("«", "R10") . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='g' rowspan='2'>" . formatLink("19 to 36", "L18") . "</td>";
echo "<td class='r'>" . formatLink("31", "31") . "</td>";
echo "<td class='b'>" . formatLink("32", "32") . "</td>";
echo "<td class='r'>" . formatLink("33", "33") . "</td>";
echo "<td class='g'>" . formatLink("«", "R11") . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='b'>" . formatLink("34", "34") . "</td>";
echo "<td class='r'>" . formatLink("35", "35") . "</td>";
echo "<td class='b'>" . formatLink("36", "36") . "</td>";
echo "<td class='g'>" . formatLink("«", "R12") . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='g' colspan='2'></td>";
echo "<td class='g'>" . formatLink("^", "C1") . "</td>";
echo "<td class='g'>" . formatLink("^", "C2") . "</td>";
echo "<td class='g'>" . formatLink("^", "C3") . "</td>";
echo "<td class='g'></td>";
echo "</tr>";
echo "</table>";
echo "</td>";
echo "</tr>";
echo "</table>";
}
if (!isset($_SESSION['roulette']))
{
$_SESSION['roulette'] = array
(
'bets' => array(),
'cash' => 0,
);
}
function act_index( )
{
renderPage();
}
function act_place( )
{
if (is_string($_GET['bet']))
{
$bet = strtoupper(trim($_GET['bet']));
if (strlen($bet))
{
if (!is_null($info = lookupBet($bet)))
{
if (count($_SESSION['roulette']['bets']) < 5)
{
$duplicate = false;
foreach ($_SESSION['roulette']['bets'] as $temp)
if ($temp == $bet)
$duplicate = true;
if (!$duplicate)
$_SESSION['roulette']['bets'][] = $bet;
}
}
}
}
renderPage();
}
function act_clear( )
{
if (is_string($_GET['bet']))
{
$bet = strtoupper(trim($_GET['bet']));
if (strlen($bet))
{
if (!is_null($info = lookupBet($bet)))
{
$bets = array();
foreach ($_SESSION['roulette']['bets'] as $temp)
if ($temp != $bet)
$bets[] = $temp;
$_SESSION['roulette']['bets'] = $bets;
}
}
}
renderPage();
}
function act_reset( )
{
$_SESSION['roulette']['bets'] = array();
renderPage();
}
function act_money( )
{
$amount = isset($_GET['amount']) && is_string($_GET['amount']) && preg_match("`^\d+$`ims", $_GET['amount']) ? @intval($_GET['amount']) : null;
if (!is_null($amount))
$_SESSION['roulette']['cash'] = min($amount, getMaximumBet());
renderPage();
}
function act_play( )
{
if (!count($_SESSION['roulette']['bets']))
return act_index();
$_SESSION['roulette']['cash'] = min($_SESSION['roulette']['cash'], getMaximumBet());
if (!$_SESSION['roulette']['cash'])
return act_index();
$wheel = "0-28-9-26-30-11-7-20-32-17-5-22-34-15-3-24-36-13-1-00-27-10-25-29-12-8-19-31-18-6-21-33-16-4-23-35-14-2";
$slots = explode("-", $wheel);
$count = count($slots);
$spin = $slots[mt_rand(1, $count)];
$cash = $_SESSION['roulette']['cash'];
$count = count($_SESSION['roulette']['bets']);
$split = $cash / $count;
$win = 0;
$lose = 0;
foreach ($_SESSION['roulette']['bets'] as $bet)
{
$info = lookupBet($bet);
if (in_array($spin, $info['numbers']))
{
$payoff = $info['payoff'] * $split;
$win += $split + $payoff;
}
else
$lose += $split;
}
if ($lose)
debitPlayer(ceil($lose));
if ($win)
creditPlayer(floor($win));
$_SESSION['roulette']['cash'] = min($_SESSION['roulette']['cash'], getMaximumBet());
renderPage($spin, $win, $cash);
}
$actions = array
(
'index' => "act_index",
'place' => "act_place",
'clear' => "act_clear",
'reset' => "act_reset",
'money' => "act_money",
'play' => "act_play",
);
$keys = array_keys($actions);
$action = isset($_GET['action']) && is_string($_GET['action']) && preg_match("`^((" . implode(")|(", $keys) . "))$`ims", $_GET['action']) ? strtolower($_GET['action']) : array_shift($keys);
$actions[$action]();
pageFooter();
?>
Im also aware that most of you can't get nyna's roulette symbol. So i am offering mine to you and it need to be uploaded into the images folder.