I am building a library in codeigniter for a roulette system.
I am looking to make the code very efficient and hack-proof.
this is the code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Name: Prad Roulette
*
* Version: 0.1
*
* Author: Pradyummna Reddy
* prad@hireprad.co.uk
*
*
* Location: https://github.com/pradyummna/prad_roulette
*
* Created: 21/10/2014
*
* Description: The library can be used for roulette system developers
*
* Requirements: PHP5 or above
*
*/
class Prad_roulette
{
/**
*
* Function to find if the number is a valid roulette number
*
* @param integer $number The number to which has to be checked
*
* @return boolean
*
* Anchor: 1num
*/
function isValidRouletteNumber($number)
{
if(ctype_digit($number) && $number < 37 && $number >= 0 ){return true;}else{return false;}
}
/**
* Function to finds the number's color` in a roulette table
*
* @param integer $number The number to which the colour is to be determined
*
* @return string
*
* Anchor : 2color
*/
function findColour($number)
{
// checking the input numbers
if(!($this->isValidRouletteNumber($number))){return 'InValidInput';}
$wheel = array('0','32','15','19','4','21','2','25','17','34','6','27','13','36','11','30','8','23','10','5','24','16','33','1','20','14','31','9','22','18','29','7','28','12','35','3','26');
$numberInArray = array_keys($wheel, $number);
if(isset($numberInArray[0]))
{
if($numberInArray[0] == '0')
{$blackRred='ZERO';}
else{
if($numberInArray[0] % 2 == 0)
{
//its black!!!
$blackRred='Black';
}
else{ $blackRred = 'Red'; }
}
}
else
{
$blackRred = 'OUT';
}
return $blackRred;
}
/**
* Function to finds the number's neighbours left to them
*
* @param integer $number The number to which the left neighbours is to be determined
* @param integer $howManyNeighbours The total neighbours to be found
* @return array
*
* Anchor: neiLside
*/
function neighboursLeft($number,$howManyNeighbours)
{
// checking the input numbers
if(!($this->isValidRouletteNumber($number)) || !($this->isValidRouletteNumber($howManyNeighbours))){return array('InValidInput');}
$x = $howManyNeighbours;
$wheel = array('0','32','15','19','4','21','2','25','17','34','6','27','13','36','11','30','8','23','10','5','24','16','33','1','20','14','31','9','22','18','29','7','28','12','35','3','26');
$numberInArray = array_keys($wheel, $number);
$neighboursLeft = null;
if(isset($numberInArray[0]))
{
$currentNumber = $numberInArray[0];
//finding the end of the array
$startValueInArray = reset($wheel);
//$endArrayKey = key($wheel);
//reset($wheel);
//moving to the current position value position in the array
while (key($wheel) !== $numberInArray[0]) next($wheel);
$neighboursLeft[0] = 'In';
for($i=0;$i<$x;$i++)
{
if(current($wheel) != $startValueInArray)
{
//echo next($wheel);echo '::'.$i; echo '<br/>';
$neighboursLeft[$i+1] = prev($wheel);
}
else
{
end($wheel);
//echo current($wheel);echo '::'.$i;echo '<br/>';
$neighboursLeft[$i+1] = current($wheel);
}
}
}
else
{
$neighboursLeft = array('OUT');
}
//$neighboursLeft[0] can be OUT or In
return $neighboursLeft;
}
/**
* Function to finds the number's neighbours right to them
*
* @param integer $number The number to which the right neighbours is to be determined
* @param integer $howManyNeighbours The total neighbours to be found
* @return array
*
* Anchor: neirside
*/
function neighboursRight($number,$howManyNeighbours)
{
// checking the input numbers
if(!($this->isValidRouletteNumber($number)) || !($this->isValidRouletteNumber($howManyNeighbours))){return array('InValidInput');}
$x = $howManyNeighbours;
$wheel = array('0','32','15','19','4','21','2','25','17','34','6','27','13','36','11','30','8','23','10','5','24','16','33','1','20','14','31','9','22','18','29','7','28','12','35','3','26');
$numberInArray = array_keys($wheel, $number);
$neighboursRight = null;
if(isset($numberInArray[0]))
{
$currentNumber = $numberInArray[0];
//finding the end of the array
$endValueInArray = end($wheel);
//$endArrayKey = key($wheel);
reset($wheel);
//moving to the current position value position in the array
while (key($wheel) !== $numberInArray[0]) next($wheel);
$neighboursRight[0] = 'In';
for($i=0;$i<$x;$i++)
{
if(current($wheel) != $endValueInArray)
{
//echo next($wheel);echo '::'.$i; echo '<br/>';
$neighboursRight[$i+1] = next($wheel);
}
else
{
reset($wheel);
//echo current($wheel);echo '::'.$i;echo '<br/>';
$neighboursRight[$i+1] = current($wheel);
}
}
}
else
{
$neighboursRight = array('OUT');
}
// $neighboursRight[0] can be OUT or In
return $neighboursRight;
}
/**
* Function to finds the number's neighbours right to them
*
* @param array $numbers The number to which the right neighbours is to be determined
* @param array $moneyOnNumbers The total neighbours to be found
* @param integer $winningNumber The winning number
* @return array payoutAmount, Profit, Invested amount
*
* Anchor: xPayoutProfit
*/
function payoutProfit($numbers,$moneyOnNumbers,$winningNumber)
{
// checking the input numbers
if(!($this->isValidRouletteNumber($number)) || !($this->isValidRouletteNumber($winningNumber))){return array('InValidInput');}
//find the payout
if(in_array($winningNumber,$numbers))
{
$amountOnNumber = $moneyOnNumbers[array_search($winningNumber)];
$payoutAmount = $amountOnNumber * 36;
}
else
{
$payoutAmount = 0;
}
$totalInvested = 0;
// find the amount invested on it
foreach($moneyOnNumbers as $money)
{
$totalInvested = $totalInvested + $money;
}
//calculate profit
$profit = $payoutAmount - $totalInvested;
//return
return array($payoutAmount,$totalInvested,$profit);
}
/**
* Function to finds the number's neighbour right to it
*
* @param integer $number The number to which the right xth neighbour is to be determined
* @param integer $distanceToXthNumber Distance to the xth neighbour
*
* @return integer number in the xth position
*
* Anchor: XthNeiRside
*/
function findXthNumberOnRight($number,$distanceToXthNumber)
{
// checking the input numbers
if(!($this->isValidRouletteNumber($number)) || !($this->isValidRouletteNumber($distanceToXthNumber))){return array('InValidInput');}
if($distanceToXthNumber != '0')
{
$xthNumber = $this->neighboursRight($number,$distanceToXthNumber);
$xthNumber = $xthNumber[$distanceToXthNumber];
}
else
{
return $number;
}
//find the payout
return $xthNumber;
}
/**
* Function to finds the number's xth neighbour left to it
*
* @param integer $number The number to which the left xth neighbour is to be determined
* @param integer $distanceToXthNumber Distance to the xth neighbour
*
* @return integer number in the xth position
*
* Anchor: XthNeiLside
*/
function findXthNumberOnLeft($number,$distanceToXthNumber)
{
// checking the input numbers
if(!($this->isValidRouletteNumber($number)) || !($this->isValidRouletteNumber($distanceToXthNumber))){return array('InValidInput');}
if($distanceToXthNumber != '0')
{
$xthNumber = $this->neighboursLeft($number,$distanceToXthNumber);
$xthNumber = $xthNumber[$distanceToXthNumber];
}
else
{
return $number;
}
//find the payout
return $xthNumber;
}
}
//1000861505
Function in the library 1. Function to check if the number is a valid number or not (input: number | output: true/false) [anchor: 1num]
Function to find xth neighbour on the left (input: number, distanceToXthNumber | output: number) [anchor: XthNeiLside]
Function to find the neighbour distance right (clock wise) (input: number1, number2 | output: total numbers inbetween)
You should do the following things to use it as a library
1. Place your library file inside application/third_party folder.
2.Create another file inside application/libraries folder and extend the class like below.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH."/third_party/Prad_roulette.php";
class Roulette extends Prad_roulette {
}
Note: If you need to use a constructor in your class make sure you extend the parent constructor:
class Roulette extends Prad_roulette {
public function __construct()
{
parent::__construct();
}
}
3.Finally load your library.
$this->load->library('roulette');
To check if the library is loaded or not, you can perform method_exits function like below
if(method_exists($this->roulette,'isValidRouletteNumber')){ /* isValidRouletteNumber is the method of Prad_roulette class. */
echo "Library is loaded successfully";
}else{
echo "Couldn't load the library";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.