


//////////////////------------Get to update randum number---------///////
function getToUpdateRandumNumber($dd,$periods,$game_number,$snumber)
{
   if($snumber <=4){
      $bigsmall = "small";
   }
   else{
      $bigsmall = "big";
   }


   
   if($snumber == "0")
   {
      $colr = "red,voilet";
   }


   if($snumber == "1")
   {
      $colr = "green";
   }


   if($snumber == "2")
   {
      $colr = "red";
   }


   if($snumber == "3")
   {
      $colr = "green";
   }


   if($snumber == "4")
   {
      $colr = "red";
   }


   if($snumber == "5")
   {
      $colr = "green,voilet";
   }


   
   if($snumber == "6")
   {
      $colr = "red";
   }

   
   if($snumber == "7")
   {
      $colr = "green";
   }


   
   if($snumber == "8")
   {
      $colr = "red";
   }


   
   if($snumber == "9")
   {
      $colr = "green";
   }


   if($game_number == "1")
   {
      $timeprds = 1;
      $clmname = "wingo1";
      $wingoPeriods = getNewPeriods($dd,$clmname);
   }


   if($game_number == "2")
   {
      $timeprds = 3;
      $clmname = "wingo2";
      $wingoPeriods = getNewPeriods($dd,$clmname);
   }


   if($game_number == "3")
   {
      $timeprds = 5;
      $clmname = "wingo3";
      $wingoPeriods = getNewPeriods($dd,$clmname);
   }

   if($game_number == "4")
   {
      $timeprds = 10;
      $clmname = "wingo4";
      $wingoPeriods = getNewPeriods($dd,$clmname);
   }

   


   $date = date("h:i d/m/Y");
   $table = "existing_game_wingo";
   $value = "(`game_number`, `periods`, `number`, `bigorsmall`, `color`, `result_date`, `status`) 
   VALUES ('$game_number','$periods','$snumber','$bigsmall','$colr','$date','success')";
   $result = $dd->insertData($table,$value);
   if($result){
      $table = "wingo_game_runing";
      $value = "periods='$wingoPeriods',time_perods='$timeprds'";
      $condition = " game_number='$game_number'";
      $result = $dd->updateData($table,$value,$condition);
   }
}










///////////////////-----------------GET MINIMUM VALUE-----------//////

//////////////////////////-----------------------------------WINGO GAME------------//////////
function getMiniBitValue($dd,$periods,$game_number)
{
    $calculationArray = getCalculationLowValue($dd,$periods,$game_number);
    foreach($calculationArray as &$ma){
      $tmp[] = &$ma["amount"];
     }
     @ array_multisort($tmp, $calculationArray);

    $snumber = $calculationArray[0]['snumber'];
    balanceUpdateForWinnerMembers($dd,$periods,$game_number,$snumber);
    createnewgameforNext($dd,$periods,$game_number,$snumber);
}







function createnewgameforNext($dd,$periods,$game_number,$snumber)
{
   if($game_number == "1")
   {
    $timeDuration = 1;
   }


   if($game_number == "2")
   {
    $timeDuration = 3;
   }


   
   if($game_number == "3")
   {
    $timeDuration = 5;
   }


   
   if($game_number == "4")
   {
    $timeDuration = 10;
   }


   if($snumber == "0")
   {
      $bigsmall = "small";
      $colr = "red,voilet";
   }


   if($snumber == "1")
   {
      $bigsmall = "small";
      $colr = "green";
   }


   if($snumber == "2")
   {
      $bigsmall = "small";
      $colr = "red";
   }


   if($snumber == "3")
   {
      $bigsmall = "small";
      $colr = "green";
   }


   if($snumber == "4")
   {
      $bigsmall = "small";
      $colr = "red";
   }


   if($snumber == "5")
   {
      $bigsmall = "big";
      $colr = "green,voilet";
   }


   
   if($snumber == "6")
   {
      $bigsmall = "big";
      $colr = "red";
   }

   
   if($snumber == "7")
   {
      $bigsmall = "big";
      $colr = "green";
   }


   
   if($snumber == "8")
   {
      $bigsmall = "big";
      $colr = "red";
   }


   
   if($snumber == "9")
   {
      $bigsmall = "big";
      $colr = "green";
   }


   if($game_number == "1")
   {
      $clmname = "wingo1";
      $wingoPeriods = getNewPeriods($dd,$clmname);
   }


   if($game_number == "2")
   {
      $clmname = "wingo2";
      $wingoPeriods = getNewPeriods($dd,$clmname);
   }


   if($game_number == "3")
   {
      $clmname = "wingo3";
      $wingoPeriods = getNewPeriods($dd,$clmname);
   }

   if($game_number == "4")
   {
      $clmname = "wingo4";
      $wingoPeriods = getNewPeriods($dd,$clmname);
   }
      

   $date = date("h:i d/m/Y");
   $table = "wingo_game_runing";
   $value = "manual_result='Automatic',periods='$wingoPeriods',time_perods='$timeDuration',bet_status='not_avl'";
   $condition = " game_number='$game_number'";
   $result = $dd->updateData($table,$value,$condition);
   if($result)
   {
      $table = "wingo_game_calculation";
      $value = "total_bit='0',game_perods='$wingoPeriods',betAmount='0'";
      $condition = " game_number='$game_number'";
      $result = $dd->updateData($table,$value,$condition);
      if($result){
         $table = "existing_game_wingo";
         $value = "(`game_number`, `periods`, `number`, `bigorsmall`, `color`, `result_date`, `status`) 
         VALUES ('$game_number','$periods','$snumber','$bigsmall','$colr','$date','success')";
         $result = $dd->insertData($table,$value);
         if($result)
         {
               $tableName = "wingo".$game_number;
               $table = "admin_calculation";
               $value = " $tableName='$wingoPeriods'";
               $condition = " adminKey='1'";
               $result = $dd->updateData($table,$value,$condition);
         }
      }
   }
}







function getNewPeriods($dd,$clmname)
{
   $table = "admin_calculation";
   $condition = null;
   $getPeriodsDetails = $dd->single($table,$condition);
   $wingoPeriods1 = $getPeriodsDetails[$clmname];


   if(substr($wingoPeriods1,0,-4) != date("Ymd")){
      $wingoPeriods1 = date("Ymd")."0001";
   }else{
      $wingoPeriods1 = $wingoPeriods1+1;
   }

   return $wingoPeriods1;
}




//////////////////--------------CALCULATION WINGO GAME PLEASE DON'T TRY TO EDIT IT---------/////////
function getCalculationLowValue($dd,$periods,$game_number)
{
      $getFullCalculateAmount = array();

      $table = "wingo_game_calculation";
      $condition = " where game_number='$game_number'";
      $getFullbetDetails = $dd->fetch_multi($table,$condition);
      foreach($getFullbetDetails as $key)
      {
         $snumber = $key->selected_number;

         if($snumber == "0"){
            $extraArray = "0,red,voilet,small";
            $getam = getColorCalculatioAndBigSamall($dd,$extraArray,$periods,$game_number);
            $getFullCalculateAmount[] = array("snumber" => "$snumber","amount" => "$getam");
         }


         if($snumber == "1"){
          $extraArray = "1,green,small";
          $getam = getColorCalculatioAndBigSamall($dd,$extraArray,$periods,$game_number);
          $getFullCalculateAmount[] = array("snumber" => "$snumber","amount" => "$getam");
         }


         if($snumber == "2"){
          $extraArray = "2,red,small";
          $getam = getColorCalculatioAndBigSamall($dd,$extraArray,$periods,$game_number);
          $getFullCalculateAmount[] = array("snumber" => "$snumber","amount" => "$getam");
         }


         if($snumber == "3"){
          $extraArray = "3,green,small";
          $getam = getColorCalculatioAndBigSamall($dd,$extraArray,$periods,$game_number);
         $getFullCalculateAmount[] = array("snumber" => "$snumber","amount" => "$getam");
         }


         if($snumber == "4"){
          $extraArray = "4,red,small";
          $getam = getColorCalculatioAndBigSamall($dd,$extraArray,$periods,$game_number);
          $getFullCalculateAmount[] = array("snumber" => "$snumber","amount" => "$getam");
         }


         if($snumber == "5"){
          $extraArray = "5,green,voilet,big";
          $getam = getColorCalculatioAndBigSamall($dd,$extraArray,$periods,$game_number);
         $getFullCalculateAmount[] = array("snumber" => "$snumber","amount" => "$getam");
         }


         if($snumber == "6"){
          $extraArray = "6,red,big";
          $getam = getColorCalculatioAndBigSamall($dd,$extraArray,$periods,$game_number);
         $getFullCalculateAmount[] = array("snumber" => "$snumber","amount" => "$getam");
         }


         if($snumber == "7"){
          $extraArray = "7,green,big";
          $getam = getColorCalculatioAndBigSamall($dd,$extraArray,$periods,$game_number);
         $getFullCalculateAmount[] = array("snumber" => "$snumber","amount" => "$getam");
         }


         if($snumber == "8"){
          $extraArray = "8,red,big";
          $getam = getColorCalculatioAndBigSamall($dd,$extraArray,$periods,$game_number);
         $getFullCalculateAmount[] = array("snumber" => "$snumber","amount" => "$getam");
         }


         
         if($snumber == "9"){
          $extraArray = "9,green,big";
          $getam = getColorCalculatioAndBigSamall($dd,$extraArray,$periods,$game_number);
          $getFullCalculateAmount[] = array("snumber" => "$snumber","amount" => "$getam");
         }

      }


      return $getFullCalculateAmount;
}




///////////-------------EXTRA CALCULATION---------//////////
function getColorCalculatioAndBigSamall($dd,$extraArray,$periods,$game_number)
{
   $getAnArray = explode(",",$extraArray); 
   
   for($i=0; $i<count($getAnArray); $i++)
   {
       @$fromCond = $getAnArray[$i];
       if($fromCond != ""){
          $table = "wingo_game_calculation";
          $condition = " where selected_number='$fromCond' AND game_number='$game_number'";
          $getGameDetails = $dd->single($table,$condition);
          @$amount += $getGameDetails['total_bit'];
       }
   }

   return $amount;
}



/////////////////--------------------WINGO BALANCE UPDATE-------////////
function balanceUpdateForWinnerMembers($dd,$periods,$game_number,$snumber)
{ 

   if($snumber == "0")
   {
      $table = "wingo_order_details";
      $condition = " where status='pending' AND game_number='$game_number' AND select_number='$snumber' OR select_number='red' OR select_number='voilet'";
      $getBetDetails = $dd->fetch_multi($table,$condition);
      foreach($getBetDetails as $key){
         $select_number = $key->select_number;
         $orderKey = $key->orderKey;
         $userKey = $key->userKey;
         $amount = $key->amount;
         $winAmount = $key->winAmount;
         $game_number = $key->game_number;
         if($key->status == "pending"){
            sendRequestToUpdateWinnerBalance($dd,$orderKey,$userKey,$periods,$amount,$winAmount,$snumber,$game_number,$select_number);
         }
      }
   }




   if($snumber == "1")
   {
      $table = "wingo_order_details";
      $condition = " where status='pending' AND game_number='$game_number' AND select_number='$snumber' OR select_number='green'";
      $getBetDetails = $dd->fetch_multi($table,$condition);
      foreach($getBetDetails as $key){
         $select_number = $key->select_number;
         $orderKey = $key->orderKey;
         $userKey = $key->userKey;
         $amount = $key->amount;
         $winAmount = $key->winAmount;
         $game_number = $key->game_number;
         if($key->status == "pending"){
            sendRequestToUpdateWinnerBalance($dd,$orderKey,$userKey,$periods,$amount,$winAmount,$snumber,$game_number,$select_number);
         }
      }
   }





   
   if($snumber == "2")
   {
      $table = "wingo_order_details";
      $condition = " where status='pending' AND game_number='$game_number' AND select_number='$snumber' OR select_number='red'";
      $getBetDetails = $dd->fetch_multi($table,$condition);
      foreach($getBetDetails as $key){
         $select_number = $key->select_number;
         $orderKey = $key->orderKey;
         $userKey = $key->userKey;
         $amount = $key->amount;
         $winAmount = $key->winAmount;
         $game_number = $key->game_number;
         if($key->status == "pending"){
            sendRequestToUpdateWinnerBalance($dd,$orderKey,$userKey,$periods,$amount,$winAmount,$snumber,$game_number,$select_number);
         }
      }
   }


   



   
   if($snumber == "3")
   {
      $table = "wingo_order_details";
      $condition = " where status='pending' AND game_number='$game_number' AND select_number='$snumber' OR select_number='green'";
      $getBetDetails = $dd->fetch_multi($table,$condition);
      foreach($getBetDetails as $key){
         $select_number = $key->select_number;
         $orderKey = $key->orderKey;
         $userKey = $key->userKey;
         $amount = $key->amount;
         $winAmount = $key->winAmount;
         $game_number = $key->game_number;
         if($key->status == "pending"){
            sendRequestToUpdateWinnerBalance($dd,$orderKey,$userKey,$periods,$amount,$winAmount,$snumber,$game_number,$select_number);
         }
      }
   }


   
   
   if($snumber == "4")
   {
      $table = "wingo_order_details";
      $condition = " where status='pending' AND game_number='$game_number' AND select_number='$snumber' OR select_number='red'";
      $getBetDetails = $dd->fetch_multi($table,$condition);
      foreach($getBetDetails as $key){
         $select_number = $key->select_number;
         $orderKey = $key->orderKey;
         $userKey = $key->userKey;
         $amount = $key->amount;
         $winAmount = $key->winAmount;
         $game_number = $key->game_number;
         if($key->status == "pending"){
            sendRequestToUpdateWinnerBalance($dd,$orderKey,$userKey,$periods,$amount,$winAmount,$snumber,$game_number,$select_number);
         }
      }
   }



   

   if($snumber == "5")
   {
      $table = "wingo_order_details";
      $condition = " where status='pending' AND game_number='$game_number' AND select_number='$snumber' OR select_number='green' OR select_number='voilet'";
      $getBetDetails = $dd->fetch_multi($table,$condition);
      foreach($getBetDetails as $key){
         $select_number = $key->select_number;
         $orderKey = $key->orderKey;
         $userKey = $key->userKey;
         $amount = $key->amount;
         $winAmount = $key->winAmount;
         $game_number = $key->game_number;
         if($key->status == "pending"){
            sendRequestToUpdateWinnerBalance($dd,$orderKey,$userKey,$periods,$amount,$winAmount,$snumber,$game_number,$select_number);
         }
      }
   }




   if($snumber == "6")
   {
      $table = "wingo_order_details";
      $condition = " where status='pending' AND game_number='$game_number' AND select_number='$snumber' OR select_number='red'";
      $getBetDetails = $dd->fetch_multi($table,$condition);
      foreach($getBetDetails as $key){
         $select_number = $key->select_number;
         $orderKey = $key->orderKey;
         $userKey = $key->userKey;
         $amount = $key->amount;
         $winAmount = $key->winAmount;
         $game_number = $key->game_number;
         if($key->status == "pending"){
            sendRequestToUpdateWinnerBalance($dd,$orderKey,$userKey,$periods,$amount,$winAmount,$snumber,$game_number,$select_number);
         }
      }
   }



   
   if($snumber == "7")
   {
      $table = "wingo_order_details";
      $condition = " where status='pending' AND game_number='$game_number' AND select_number='$snumber' OR select_number='green'";
      $getBetDetails = $dd->fetch_multi($table,$condition);
      foreach($getBetDetails as $key){
         $select_number = $key->select_number;
         $orderKey = $key->orderKey;
         $userKey = $key->userKey;
         $amount = $key->amount;
         $winAmount = $key->winAmount;
         $game_number = $key->game_number;
         if($key->status == "pending"){
            sendRequestToUpdateWinnerBalance($dd,$orderKey,$userKey,$periods,$amount,$winAmount,$snumber,$game_number,$select_number);
         }
      }
   }



   
   
   if($snumber == "8")
   {
      $table = "wingo_order_details";
      $condition = " where status='pending' AND game_number='$game_number' AND select_number='$snumber' OR select_number='red'";
      $getBetDetails = $dd->fetch_multi($table,$condition);
      foreach($getBetDetails as $key){
         $select_number = $key->select_number;
         $orderKey = $key->orderKey;
         $userKey = $key->userKey;
         $amount = $key->amount;
         $winAmount = $key->winAmount;
         $game_number = $key->game_number;
         if($key->status == "pending"){
            sendRequestToUpdateWinnerBalance($dd,$orderKey,$userKey,$periods,$amount,$winAmount,$snumber,$game_number,$select_number);
         }
      }
   }



      
   if($snumber == "9")
   {
      $table = "wingo_order_details";
      $condition = " where status='pending' AND game_number='$game_number' AND select_number='$snumber' OR select_number='green'";
      $getBetDetails = $dd->fetch_multi($table,$condition);
      foreach($getBetDetails as $key){
         $select_number = $key->select_number;
         $orderKey = $key->orderKey;
         $userKey = $key->userKey;
         $amount = $key->amount;
         $winAmount = $key->winAmount;
         $game_number = $key->game_number;
         if($key->status == "pending"){
            sendRequestToUpdateWinnerBalance($dd,$orderKey,$userKey,$periods,$amount,$winAmount,$snumber,$game_number,$select_number);
         }
      }
   }

   
   updatependingbalanceLoss($dd,$periods,$game_number,$snumber);

}






function sendRequestToUpdateWinnerBalance($dd,$orderKey,$userKey,$periods,$amount,$winAmount,$snumber,$game_number,$select_number)
{
    $table = "userdetails";
    $value = "wallet=wallet+$winAmount";
    $condition = " userKey='$userKey'";
    $result = $dd->updateData($table,$value,$condition);
    if($result){
       $table = "wingo_order_details";
       $value = "status='success'";
       $condition = " orderKey='$orderKey'";
       $result2 = $dd->updateData($table,$value,$condition);
       if($result2){
           $date = date("h:i d/m/Y");
           $ref = mt_rand(1111111111,9999999999); 
          $table = "account_history";
          $value = "(`userKey`, `amount`, `action_date`, `lossorprofit`, `refrence`, `gameNumber`, `periods`,`result_number`,`selected_number`,`game_name`)
          SELECT '$userKey','$amount','$date','+$winAmount','$ref',game_number,'$periods','$snumber','$select_number','Wingo' FROM wingo_order_details WHERE orderKey='$orderKey'";
          $result3 = $dd->insertData($table,$value);
       }
    }
}





//////////////////----------------FAIL Game-----------//////////
function updatependingbalanceLoss($dd,$periods,$game_number,$snumber)
{
   $table = "wingo_order_details";
   $condition = " where status='pending' AND game_number='$game_number'";
   $result = $dd->fetch_multi($table,$condition);
   foreach($result as $key){
      $select_number = $key->select_number;
      $userKey = $key->userKey;
      $orderKey = $key->orderKey;
      $amount = $key->amount;
      updateLossUser($dd,$userKey,$orderKey,$snumber,$periods,$amount,$game_number,$select_number);
   }
}




function updateLossUser($dd,$userKey,$orderKey,$snumber,$periods,$amount,$game_number,$select_number)
{
    $date = date("h:i d/m/Y");
    $ref = mt_rand(1111111111,9999999999);
    $table = "account_history";
    $value = "(`userKey`, `amount`, `action_date`, `lossorprofit`, `refrence`, `gameNumber`, `periods`,`result_number`,`selected_number`,`game_name`)
    VALUES ('$userKey','$amount','$date','-$amount','$ref','$game_number','$periods','$snumber','$select_number','Wingo')";
    $result = $dd->insertData($table,$value);
    if($result){
       $table = "wingo_order_details";
       $value = "status='success'";
       $condition = " orderKey='$orderKey'";
       $result2 = $dd->updateData($table,$value,$condition);
    }
}

////////////////////------------------------wingo game closed-----------//////////////////////









