2014-11-03 395 views
0

我正在建立一個輪盤賭系統的codeigniter庫。我正在創建一個Codeigniter庫,我如何測試代碼?

我正在尋找使代碼非常有效和防黑客。

  1. 是否有任何工具可以用來測試代碼,如功能測試等?
  2. 代碼是否有效?如果不是如何測試/提高效率?

這是代碼

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 
/** 
* Name: Prad Roulette 
* 
* Version: 0.1 
* 
* Author: Pradyummna Reddy 
*   [email protected] 
*   
* 
* 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 

功能庫中的 1.功能檢查,如果號碼是有效的號碼或沒有(輸入:編號|輸出:真/假)[錨: 1num]

  • 函數返回基於所述給定數量的顏色(輸入:編號|輸出:顏色)[錨:2color]
  • 功能上找到向右鄰居給定數字(inpu t:數字,鄰居數量|輸出:數組數組)[定位:neirside]
  • 在給定數字的左側找到鄰居的函數(輸入:number,鄰居數|輸出:數組數組)[anchor:neiLside]
  • Function (輸入:數組(投入數),數組(投入金額),中獎數|輸出:數組(amountReceivedAtTheEnd,profit,lossAmount,investAmount))[anchor:xPayoutProfit]
  • 函數查找(輸入:數字,distanceToXthNumber |輸出:數字)[anchor:XthNeiRside]
  • 在左側找到第x個鄰居的函數(輸入:number,distanceToXthNumber | output:number)[a nchor:XthNeiLside]

  • 函數查找鄰居距離右(時鐘智者)(輸入:number1,number2 |輸出:總數插圖中)

  • 功能找到鄰居距離左(反時針)(輸入:數字1,數字|輸出:總數插圖中)
  • 功能知道哪個打的號碼屬於(輸入:輸入:打號或0)
  • 查看數字是偶數還是奇數(輸入:數字|輸出:偶數r奇數0)的函數
  • 用於查找Low1to18OrHigh19to36函數(輸入:中獎數|輸出數:低或高或0)
  • 查找列號第1個第2或第3個(輸入:中獎號碼:輸出:列號或0)的功能
  • 函數返回決賽(1,11,21,31,2,22,32 ..)爲給定的數字(輸入:中獎號碼|輸出:決賽號)
  • 函數返回輪的部門(輸入:中獎號碼|輸出:部門名稱[零和遊戲,零鄰居..])
  • +1

    包括庫執行method_exits功能和調用庫函數 – 2014-11-03 13:11:03

    +0

    沒有什麼在此代碼,它關係到codeignitor - 你可以測試它,但通常你會測試php代碼。就效率而言,肯定會有一些可以改進的地方,但是這樣的問題可能應該在codereview上,而不是stackoverflow – Steve 2014-11-03 13:16:05

    +0

    錯誤。第一行是。 – f4der 2014-11-03 13:50:18

    回答

    5

    你應該做以下事情使用它作爲一個庫
    1.將你的庫文件放在application/third_party文件夾中。

    enter image description here

    2.創建內部應用/庫文件夾和擴展類等下方的另一文件。

    enter image description here

    <?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 
    require_once APPPATH."/third_party/Prad_roulette.php"; 
    
    class Roulette extends Prad_roulette { 
    
    } 
    

    注意:如果你需要使用一個構造函數在您的類確保您擴展父類的構造:

    class Roulette extends Prad_roulette { 
        public function __construct() 
        { 
         parent::__construct(); 
        } 
    } 
    

    3.最後您的書架。

    $this->load->library('roulette'); 
    

    要檢查庫加載與否,你可以像下面

    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"; 
    } 
    
    +0

    我喜歡你解決問題的方法。 – 2014-11-13 10:53:10

    相關問題