<?php
include "control/activity.php";
$dd = new activity();

SendRequestWingoCalculation($dd);


function SendRequestWingoCalculation($dd)
{
    $table = "wingo_game_runing";
    $condition = null;
    $getFullWingo = $dd->fetch_multi($table,$condition);
    foreach($getFullWingo as $key){
       
       $time_perods = $key->time_perods;
       if($time_perods <= 1){
               $prds = $key->periods;
               $gameNumber = $key->game_number;
               $manual_result = $key->manual_result;
               
               if($manual_result == "Automatic"){
                   if($key->bet_status == "active"){
                          $arrayReturnData = sendRequestToCalculateArray($dd,$prds,$gameNumber);
                          $arrayFromZone = sendRequestForNumberChoose($dd,$arrayReturnData,$prds,$gameNumber);
                          foreach($arrayFromZone as &$ma){
                            $tmp[] = &$ma["winamount"];
                           }
                           @ array_multisort($tmp, $arrayFromZone);
                           SendRequestForFinalNumberExtract($dd,$arrayFromZone,$prds,$gameNumber);
                   }else{
                       $snumber = mt_rand(0,9);
                       createNewGames($dd,$gameNumber,$prds,$snumber);
                   }
               }else{
                  $snumber = $key->manual_result;
                  SendRequestForResult($dd,$prds,$gameNumber,$snumber);
                  updateBalanceForLoassUser($dd,$gameNumber,$prds,$snumber);
               }
       
       }else{
          $prds = $key->periods;
          $gameNumber = $key->game_number;
          $manual_result = $key->manual_result;
          SendRequesttodicrease($dd,$gameNumber);
       }
       

    }
}





function SendRequesttodicrease($dd,$gameNumber)
{
    $table = "wingo_game_runing";
    $value = "time_perods=time_perods-1";
    $condition = " game_number='$gameNumber'";
    $dd->updateData($table,$value,$condition);
}




function sendRequestToCalculateArray($dd,$prds,$gameNumber)
{
   $arrayData = "red,green,voilet,big,small";
   $allReqData = explode(",",$arrayData);
   for($i=0; $i<count($allReqData); $i++){
      $clm = $allReqData[$i];
      $table = "wingo_game_calculation";
      $condition = " where game_number='$gameNumber' AND game_perods='$prds' AND selected_number='$clm'";
      $getDetails = $dd->single($table,$condition);
      $aarayArea[] = array("snumber"=>$getDetails['selected_number'],"winAmount"=>$getDetails['winAmount'],"gnumber"=>$getDetails['game_number']);
   }
   

   foreach($aarayArea as $key){
      $snumber = $key['snumber'];
      if($snumber == "red")
      {
         $tred = $key['winAmount'];
      }


      if($snumber == "green")
      {
         $tgreen = $key['winAmount'];
      }

      
      if($snumber == "voilet")
      {
         $tvoilet = $key['winAmount'];
      }

      
      if($snumber == "big")
      {
         $tbig = $key['winAmount'];
      }

      
      if($snumber == "small")
      {
         $tsmall = $key['winAmount'];
      }
   }

   return array("red" =>$tred,"green" => $tgreen,"voilet" => $tvoilet,"big" => $tbig,"small" => $tsmall);
   
}







function sendRequestForNumberChoose($dd,$arrayReturnData,$prds,$gameNumber)
{
   $small = $arrayReturnData['small'];
   $big = $arrayReturnData['big'];
   $red = $arrayReturnData['red'];
   $big = $arrayReturnData['big'];
   $voilet = $arrayReturnData['voilet'];
   $green = $arrayReturnData['green'];

   $table = "wingo_game_calculation";
   $condition = " where game_perods='$prds' AND game_number='$gameNumber'";
   $getFullDetails = $dd->fetch_multi($table,$condition);
   foreach($getFullDetails as $key){
      if($key->selected_number >=0 && $key->selected_number <=9){
         if($key->selected_number == 0){
            $totalAmount = $key->winAmount+$small+$voilet+$red;
         }

         if($key->selected_number == 1){
            $totalAmount = $key->winAmount+$small+$green;
         }

         if($key->selected_number == 2){
            $totalAmount = $key->winAmount+$small+$red;
         }

         if($key->selected_number == 3){
            $totalAmount = $key->winAmount+$small+$green;
         }

         if($key->selected_number == 4){
            $totalAmount = $key->winAmount+$small+$red;
         }

         if($key->selected_number == 5){
            $totalAmount = $key->winAmount+$big+$voilet+$green;
         }

         if($key->selected_number == 6){
            $totalAmount = $key->winAmount+$big+$red;
         }

         if($key->selected_number == 7){
            $totalAmount = $key->winAmount+$big+$green;
         }

         if($key->selected_number == 8){
            $totalAmount = $key->winAmount+$big+$red;
         }

         if($key->selected_number == 9){
            $totalAmount = $key->winAmount+$big+$green;
         }

         $arrayArea[] = array("gameNumber" => $gameNumber,"prds" => $prds,"winamount" => $totalAmount,"snumber" =>$key->selected_number);
      }
   }
   return $arrayArea;

}




function SendRequestForFinalNumberExtract($dd,$arrayFromZone,$prds,$gameNumber)
{
  $fromArray = $arrayFromZone[0];
  $snumber = $fromArray['snumber'];
  SendRequestForResult($dd,$prds,$gameNumber,$snumber);
  updateBalanceForLoassUser($dd,$gameNumber,$prds,$snumber);
}







function SendRequestForResult($dd,$prds,$gameNumber,$snumber)
{
  if($snumber == 0){
    $arraData = "red,voilet,small,0";
  }

  if($snumber == 1){
   $arraData = "green,small,1";
  }

  if($snumber == 2){
   $arraData = "red,small,2";
  }

  if($snumber == 3){
   $arraData = "green,small,3";
  }

  if($snumber == 4){
   $arraData = "red,small,4";
  }

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

  if($snumber == 6){
   $arraData = "red,big,6";
  }

  if($snumber == 7){
   $arraData = "green,big,7";
  }

  if($snumber == 8){
   $arraData = "red,big,8";
  }

  if($snumber == 9){
   $arraData = "green,big,9";
  }

  
  $exData = explode(",",$arraData);
  for($i=0; $i<count($exData); $i++){
     $clm = $exData[$i];
     $table = "wingo_order_details";
     $condition = " where game_number='$gameNumber' AND periods='$prds' AND status='pending'";
     $resultData = $dd->fetch_multi($table,$condition);
     foreach($resultData as $key){
        if($key->select_number==$clm){
         $orderKey = $key->orderKey;
         $userKey = $key->userKey;
         $amount = $key->amount;
         $winAmount = $key->winAmount;
         SendReqwestForUpdateBalanceOnWinner($dd,$gameNumber,$prds,$snumber,$orderKey,$userKey,$amount,$winAmount);
        }
     }



  }



   createNewGames($dd,$gameNumber,$prds,$snumber);
}




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






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





function createNewGames($dd,$gameNumber,$prds,$snumber)
{
        
        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($gameNumber == "1")
           {
            $timeDuration = 1;
            $clmname = "wingo1";
           }
        
        
           if($gameNumber == "2")
           {
            $timeDuration = 3;
            $clmname = "wingo2";
           }
        
        
           
           if($gameNumber == "3")
           {
            $timeDuration = 5;
            $clmname = "wingo3";
           }
        
        
           
           if($gameNumber == "4")
           {
            $timeDuration = 10;
            $clmname = "wingo4";
           }
       
       
    
    
           $newPeriods = getNewPeriods($dd,$clmname,$gameNumber);
        
           $date = date("h:i d/m/Y");
           $table = "existing_game_wingo";
           $value = "(`game_number`, `periods`, `number`, `bigorsmall`, `color`, `result_date`, `status`) 
           VALUES ('$gameNumber','$prds','$snumber','$bigsmall','$colr','$date','success')";
           $result = $dd->insertData($table,$value);
           if($result){
              $table = "wingo_game_runing";
              $value = "periods='$newPeriods',time_perods='$timeDuration',bet_status='not_avl',manual_bet='empty',manual_result='Automatic'";
              $condition = " game_number='$gameNumber'";
              $result = $dd->updateData($table,$value,$condition);
              if($result){
                 $table = "admin_calculation";
                 $value = "$clmname='$newPeriods'";
                 $condition = " adminKey='1'";
                 $result = $dd->updateData($table,$value,$condition);
                 if($result){
                  $table = "wingo_game_calculation";
                  $value = "total_bit='0',winAmount='0',game_perods='$newPeriods'";
                  $condition = " game_number='$gameNumber'";
                  $result = $dd->updateData($table,$value,$condition);
                 }
              }
           }

    
}



function getNewPeriods($dd,$clmname,$gameNumber)
{
    if($gameNumber == "1")
   {
    $timeDuration = 1;
    $clmname = "wingo1";
   }


   if($gameNumber == "2")
   {
    $timeDuration = 3;
    $clmname = "wingo2";
   }


   
   if($gameNumber == "3")
   {
    $timeDuration = 5;
    $clmname = "wingo3";
   }


   
   if($gameNumber == "4")
   {
    $timeDuration = 10;
    $clmname = "wingo4";
   }

    
    
   $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;
}


////////////////////////////////////----------------------K3 Section----------------////////////////


///////////////////////////------------------------CALCULATION FOR K3 GAME-----------////////////
calculationForK3Games($dd);



function calculationForK3Games($dd)
{
    $table = "k3_game_runing";
    $condition = null;
    $getKeRuningGame = $dd->fetch_multi($table,$condition);
    foreach($getKeRuningGame as $key)
    {
        $periods = $key->periods;
        $game_number = $key->game_number;
        $duration = $key->duration;
        $k3Key = $key->k3Key;
        $manual_result = $key->manual_result;
        $bet_status = $key->bet_status;

        if($duration <=1)
        {
           if($manual_result == "Automatic")
           {
              if($bet_status == "betActive"){
                sendRequestForMinimumbitCalculation($dd,$k3Key,$periods,$game_number,$duration);
              }else{
                  $a=array("2,5,6","5,1,2","1,5,4","2,5,3","6,6,5","1,5,6","5,1,3","1,2,4","5,6,6","6,6,6","5,2,1");
                  $random_keys=array_rand($a,2);
                  $snumber = $a[$random_keys[0]];
                  $manual_result = $snumber; 
                  createNewGameK3($dd,$k3Key,$periods,$game_number,$duration,$manual_result);
              } 
           }
           else{
            getManualCalculation($dd,$k3Key,$periods,$game_number,$duration,$manual_result);
            createNewGameK3($dd,$k3Key,$periods,$game_number,$duration,$manual_result);
           }
          
        }
        else{
            $table = "k3_game_runing";
            $value = "duration=duration-1";
            $condition = " k3Key='$k3Key'";
            $result = $dd->updateData($table,$value,$condition);
        }
        

    }
}






function getManualCalculation($dd,$k3Key,$periods,$game_number,$duration,$manual_result)
{
    $getNumber = explode(",",$manual_result);  
    $f1number = $getNumber[0];
    $f2number = $getNumber[1];
    $f3number = $getNumber[2];
    $winNumberSelected = $f1number+$f2number+$f3number;
   
    $table = "k3_orders";
    $condition = " where game_number='$game_number' AND periods='$periods' AND status='pending'";
    $getFullOrderDetails = $dd->fetch_multi($table,$condition);
    foreach($getFullOrderDetails as $key){

      $order_type = $key->order_type;
      $bigorsmall = $key->bigorsmall;
      $selected_number = $key->selected_number;

      if($winNumberSelected <= 9)
      {
         $bigsmall = "small";
      }
      elseif($winNumberSelected >=10){
         $bigsmall = "big";
      }
      
      if($winNumberSelected == 3 || $winNumberSelected == 5 || $winNumberSelected == 7 || $winNumberSelected == 9 || $winNumberSelected == 11 || $winNumberSelected == 13 || $winNumberSelected == 15 || $winNumberSelected == 17)
      {
         $oddEven = "odd";
      }elseif($winNumberSelected == 4 || $winNumberSelected == 6 || $winNumberSelected == 8 || $winNumberSelected == 10 || $winNumberSelected == 12 || $winNumberSelected == 14 || $winNumberSelected == 16 || $winNumberSelected == 18)
      {
          $oddEven = "even";
      }

      
      if($winNumberSelected == $selected_number || $bigorsmall == $oddEven || $bigsmall == $bigorsmall)
      {
         $userKey = $key->user_key;
         $k3orderKey = $key->k3orderKey;
         $amount = $key->amount;
         $winAmount = $key->winAmount;
         balanceUpdateFromManualRequest($dd,$k3Key,$periods,$game_number,$duration,$manual_result,$userKey,$k3orderKey,$winAmount,$amount);
      }else{
        $userKey = $key->user_key;
        $k3orderKey = $key->k3orderKey;
        $amount = $key->amount;
        $winAmount = $key->winAmount;
        lossUpdateFromManualRequest($dd,$k3Key,$periods,$game_number,$duration,$manual_result,$userKey,$k3orderKey,$winAmount,$amount);
      }

    }
}







/////////////////////////////--------------------Manual Request --------------------///////////
function balanceUpdateFromManualRequest($dd,$k3Key,$periods,$game_number,$duration,$manual_result,$userKey,$k3orderKey,$winAmount,$amount)
{
    //  $table = "userdetails";
    //  $value = "wallet=wallet+$winAmount,winamount=winamount+$winAmount,withdraw_balance=withdraw_balance+$winAmount";
    //  $condition = " userKey='$userKey'";
    //  $result = $dd->updateData($table,$value);
    //  if($result){
    //     $ref = mt_rand(1111111111,9999999999);
    //     $date = date("h:i d/m/Y");
    //     $table = "account_history";
    //     $value = "(`userKey`, `amount`, `action_date`, `lossorprofit`, `refrence`, `gameNumber`, `periods`,`result_number`,`game_name`) 
    //     SELECT '$userKey','$amount','$date','+$winAmount','$ref','$game_number','$periods','$manual_result','k3' FROM k3_orders WHERE game_number='$game_number' AND k3orderKey='$k3orderKey'";
    //     $result = $dd->insertData($table,$value);
    //     if($result){
    //       $table = "k3_orders";
    //       $value = "status='success'";
    //       $condition = " k3orderKey='$k3orderKey' AND game_number='$game_number'";
    //       $result = $dd->updateData($table,$value,$condition);
    //     }
    //  }
     
     
    $table = "userdetails";
    $value = "wallet=wallet+$winAmount,winamount=winamount+$winAmount,withdraw_balance=withdraw_balance+$winAmount";
    $condition = " userKey='$user_key'";
    $result = $dd->updateData($table,$value,$condition);
    if($result){
       $table = "k3_orders";
       $value = "status='success'";
       $condition = " k3orderKey='$k3orderKey' AND game_number='$game_number'";
       $result = $dd->updateData($table,$value,$condition);
       if($result){
          $date = date("h:i d/,/Y");
          $ref = mt_rand(1111111111,9999999999); 
          $table = "account_history";
          $value = "(`userKey`, `amount`,`tax`, `action_date`, `lossorprofit`, `refrence`, `gameNumber`, `periods`,`result_number`,`selected_number`,`game_name`) 
          SELECT user_key,amount,tax,'$date','+$winAmount','$ref',game_number,'$periods','$manual_result','big','k3' FROM k3_orders WHERE k3orderKey='$k3orderKey' AND game_number='$game_number'";
          $result = $dd->insertData($table,$value);
       }
    }
}







/////////////////////////-------------------------LOSSS UPDATE BY MANUAL REQUEST------------/////////
function lossUpdateFromManualRequest($dd,$k3Key,$periods,$game_number,$duration,$manual_result,$userKey,$k3orderKey,$winAmount,$amount)
{
  
    //   $ref = mt_rand(1111111111,9999999999);
    //   $date = date("h:i d/m/Y");

    //   $table = "account_history";
    //   $value = "(`userKey`, `amount`,`tax`, `action_date`, `lossorprofit`, `refrence`, `gameNumber`, `periods`,`result_number`,`game_name`) 
    //   SELECT '$userKey','$amount','$date','-$amount','$ref','$game_number','$periods','$manual_result','k3' FROM k3_orders WHERE game_number='$game_number' AND k3orderKey='$k3orderKey'";
    //   $result = $dd->insertData($table,$value);
    //   if($result){
    //     $table = "k3_orders";
    //     $value = "status='success'";
    //     $condition = " k3orderKey='$k3orderKey' AND game_number='$game_number'";
    //     $result = $dd->updateData($table,$value,$condition);
    //   }
      
      
       $table = "k3_orders";
       $value = "status='success'";
       $condition = " k3orderKey='$k3orderKey' AND game_number='$game_number'";
       $result = $dd->updateData($table,$value,$condition);
       if($result){
          $date = date("h:i d/,/Y");
          $ref = mt_rand(1111111111,9999999999); 
          $table = "account_history";
          $value = "(`userKey`, `amount`,`tax`, `action_date`, `lossorprofit`, `refrence`, `gameNumber`, `periods`,`result_number`,`selected_number`,`game_name`) 
          SELECT user_key,amount,tax,'$date',-amount,'$ref',game_number,'$periods','$manual_result','big','k3' FROM k3_orders WHERE k3orderKey='$k3orderKey' AND game_number='$game_number'";
          $result = $dd->insertData($table,$value);
       }
    
}









function createNewGameK3($dd,$k3Key,$periods,$game_number,$duration,$manual_result)
{

   $table = "k3existing_result";
   $condition = " where game_number='$game_number' AND periods='$periods'";
   $checkEntry = $dd->check($table,$condition);
   if($checkEntry['status'] != "success"){


      $getNumber = explode(",",$manual_result);  
      $f1number = $getNumber[0];
      $f2number = $getNumber[1];
      $f3number = $getNumber[2];
      $winNumberSelected = $f1number+$f2number+$f3number;

      if($winNumberSelected == "3" || $winNumberSelected == "5" || $winNumberSelected == "7" || $winNumberSelected == "9" || $winNumberSelected == "11" || $winNumberSelected == "13" || $winNumberSelected == "15" || $winNumberSelected == "17")
      {
           $oddEven = "odd";
      } 
      else{
         $oddEven = "even";
      }
    
    if($game_number == 1)
    {
        $newTime = "1";
        $clmname = "k1";
        $newPeriods = getNewPeriodsk3($dd,$clmname);
    }
    
    if($game_number == 2)
    {
        $newTime = "3";
        $clmname = "k2";
        $newPeriods = getNewPeriodsk3($dd,$clmname);
    }
    
    if($game_number == 3)
    {
        $newTime = "5";
        $clmname = "k3";
        $newPeriods = getNewPeriodsk3($dd,$clmname);
    }
    
    
    if($game_number == 4)
    {
        $newTime = "10";
        $clmname = "k4";
        $newPeriods = getNewPeriodsk3($dd,$clmname);
    }
    
    
   $table = "k3_game_runing";
   $value = "periods='$newPeriods',duration='$newTime',manual_result='Automatic',bet_status='not_avl'";
   $condition = " game_number='$game_number'";
   $result = $dd->updateData($table,$value,$condition);
   if($result){
     $table = "k3_game_calculation";
     $value = "bitAmount='0',winner_amount='0'";
     $condition = " game_number='$game_number'";
     $result = $dd->updateData($table,$value,$condition);
     if($result){
         $table = "admin_calculation";
         $value = "$clmname=$newPeriods";
         $condition = " adminKey='1'";
         $result = $dd->updateData($table,$value,$condition);
         if($result){
            $table = "k3_game_calculation";
            $condition = " smallbig='diffrent' AND game_number='$game_number'";
            $result = $dd->deleteData($table,$condition);
            if($result)
            {
                  $table = "k3existing_result";
                  $date = date("h:i d/m/Y");
                  $value = "(`periods`, `game_number`, `sum`, `oddoreven`, `result`, `excuting_date`, `status`) 
                  VALUES ('$periods','$game_number','$winNumberSelected','$oddEven','$manual_result','$date','success')";
                  $result = $dd->insertData($table,$value);
               
            }
         }
     }
   }
  }
}




function getNewPeriodsk3($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;
}





function sendRequestForMinimumbitCalculation($dd,$k3Key,$periods,$game_number,$duration)
{
   $arrayResult = array();
   $tempArray = explode(",","3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18");
   for($i=0; $i<16; $i++)
   {
     $checkNumber = $tempArray[$i];
     $arrayResult[] = checkNumberForResult($dd,$checkNumber,$game_number,$periods);
   }
   

   foreach($arrayResult as &$ma){
    $tmp[] = &$ma["totalAmount"];
   }
   @ array_multisort($tmp, $arrayResult);

   $winNumberSelected = $arrayResult[0]['winnumber'];
   $totalAmount = $arrayResult[0]['totalAmount'];
   $getArrayForNumber = json_encode($arrayResult[0]['numberArray']);
   calculationForResultAndPayment($dd,$k3Key,$periods,$game_number,$duration,$winNumberSelected,$totalAmount,$getArrayForNumber);
}





//////////////////////--------------------FINALLY CHECK RESULT PLEASE DONT'T EDIT ANY CODE IN THIS SECTION--------/////
function checkNumberForResult($dd,$checkNumber,$game_number,$periods)
{
    ////////////------Check Number---//////
    $table = "k3_game_calculation";
    $condition = " where selected_number='$checkNumber' AND game_number='$game_number'";
    $getFullDetais = $dd->single($table,$condition);
    @$winAmount = $getFullDetais['winner_amount'];
    

    /////////////------------Check Small OR Big-------///////
    if($getFullDetais)
    {
         $checkArray = array("big","small","odd","even");
          for($i=0; $i<4; $i++){
            $checkData = $checkArray[$i];
            $table = "k3_game_calculation";
            $condition = " where game_number='$game_number' AND selected_number='$checkNumber' AND smallbig like '%$checkData%'";
            $getFullDetails2 = $dd->single($table,$condition);
           @ @$winAmount2 += $getFullDetails2['winner_amount'];
          }
    }


    if($checkNumber == 3){
         $numbersArray = array();
         $checkArray = array("111");
          for($i=0; $i<count($checkArray); $i++){
            $checkData = $checkArray[$i];
            $getTwo = substr("$checkData",0,2);
            $table = "k3_game_calculation";
            $condition = " where  game_number='$game_number' AND selected_number='$checkData' OR selected_number='$getTwo'";
            $getFullDetails2 = $dd->fetch_multi($table,$condition);
            foreach($getFullDetails2 as $key)
            {
               @ $winAmount3 += $key->winner_amount;
                $winAm = $key->winner_amount;
                $numbersArray[] = array("winamount"=>"$winAm","renumber" => $checkData);
            }
          }
    }


    if($checkNumber == 4){
        $numbersArray = array();
        $checkArray = array("211","112");
         for($i=0; $i<count($checkArray); $i++){
           $checkData = $checkArray[$i];
           $getTwo = substr("$checkData",0,2);
           $table = "k3_game_calculation";
           $condition = " where game_number='$game_number' AND selected_number='$checkData' OR selected_number='$getTwo'";
           $getFullDetails2 = $dd->fetch_multi($table,$condition);
           foreach($getFullDetails2 as $key)
           {
              @ $winAmount3 += $key->winner_amount;
               $winAm = $key->winner_amount;
               $numbersArray[] = array("winamount"=>"$winAm","renumber" => $checkData);
           }
          
         }
   }


   if($checkNumber == 5){
    $numbersArray = array();
    $checkArray = array("221","113");
     for($i=0; $i<count($checkArray); $i++){
       $checkData = $checkArray[$i];
       $getTwo = substr("$checkData",0,2);
       $table = "k3_game_calculation";
       $condition = " where game_number='$game_number' AND selected_number='$checkData' OR selected_number='$getTwo'";
       $getFullDetails2 = $dd->fetch_multi($table,$condition);
       foreach($getFullDetails2 as $key)
       {
          @ $winAmount3 += $key->winner_amount;
           $winAm = $key->winner_amount;
           $numbersArray[] = array("winamount"=>"$winAm","renumber" => $checkData);
       }
      
     }
   }


   if($checkNumber == 6){
    $numbersArray = array();
    $checkArray = array("114","123","132","141","213","222","231","312","321","411");
     for($i=0; $i<count($checkArray); $i++){
       $checkData = $checkArray[$i];
       $getTwo = substr("$checkData",0,2);
       $table = "k3_game_calculation";
       $condition = " where game_number='$game_number' AND selected_number='$checkData' OR selected_number='$getTwo'";
       $getFullDetails2 = $dd->fetch_multi($table,$condition);
       foreach($getFullDetails2 as $key)
       {
          @ $winAmount3 += $key->winner_amount;
           $winAm = $key->winner_amount;
           $numbersArray[] = array("winamount"=>"$winAm","renumber" => $checkData);
       }
      
     }
   }


   if($checkNumber == 7){
    $numbersArray = array();
    $checkArray = array("115","124","133","142","151","214","223","232","241","313","322","331","412","421","511");
     for($i=0; $i<count($checkArray); $i++){
       $checkData = $checkArray[$i];
       $getTwo = substr("$checkData",0,2);
       $table = "k3_game_calculation";
       $condition = " where game_number='$game_number' AND selected_number='$checkData' OR selected_number='$getTwo'";
       $getFullDetails2 = $dd->fetch_multi($table,$condition);
       foreach($getFullDetails2 as $key)
       {
          @ $winAmount3 += $key->winner_amount;
           $winAm = $key->winner_amount;
           $numbersArray[] = array("winamount"=>"$winAm","renumber" => $checkData);
       }
      
     }
   }


   if($checkNumber == 8){
    $numbersArray = array();
    $checkArray = array("115","124","133","142","151","214","223","232","241","313","322","331","412","421","511");
     for($i=0; $i<count($checkArray); $i++){
       $checkData = $checkArray[$i];
       $getTwo = substr("$checkData",0,2);
       $table = "k3_game_calculation";
       $condition = " where game_number='$game_number' AND selected_number='$checkData' OR selected_number='$getTwo'";
       $getFullDetails2 = $dd->fetch_multi($table,$condition);
       foreach($getFullDetails2 as $key)
       {
          @ $winAmount3 += $key->winner_amount;
           $winAm = $key->winner_amount;
           $numbersArray[] = array("winamount"=>"$winAm","renumber" => $checkData);
       }
      
     }
   }


  
          

   if($checkNumber == 9){
    $numbersArray = array();
    $checkArray = array("126", "135", "144", "153", "162", "216", "225", "234", "243", "252", "261", "315", "324", "333", "342", "351", "414", "423", "432", "441", "513", "522", "531", "612", "621");
     for($i=0; $i<count($checkArray); $i++){
       $checkData = $checkArray[$i];
       $getTwo = substr("$checkData",0,2);
       $table = "k3_game_calculation";
       $condition = " where game_number='$game_number' AND selected_number='$checkData' OR selected_number='$getTwo'";
       $getFullDetails2 = $dd->fetch_multi($table,$condition);
       foreach($getFullDetails2 as $key)
       {
          @ $winAmount3 += $key->winner_amount;
           $winAm = $key->winner_amount;
           $numbersArray[] = array("winamount"=>"$winAm","renumber" => $checkData);
       }

        
     }
   }



   if($checkNumber == 10){
    $numbersArray = array();
    $checkArray = array("136", "145", "154", "163", "226", "235", "244", "253", "262", "316", "325", "334", "343", "352", "361", "415", "424", "433", "442", "451", "514", "523", "532", "541", "613", "622", "631");
     for($i=0; $i<count($checkArray); $i++){
       $checkData = $checkArray[$i];
       $getTwo = substr("$checkData",0,2);
       $table = "k3_game_calculation";
       $condition = " where game_number='$game_number' AND selected_number='$checkData' OR selected_number='$getTwo'";
       $getFullDetails2 = $dd->fetch_multi($table,$condition);
       foreach($getFullDetails2 as $key)
       {
          @ $winAmount3 += $key->winner_amount;
           $winAm = $key->winner_amount;
           $numbersArray[] = array("winamount"=>"$winAm","renumber" => $checkData);
       }
      
     }
   }


   if($checkNumber == 11){
    $numbersArray = array();
    $checkArray = array("146", "155", "164", "236", "245", "254", "263", "326", "335", "344", "353", "362", "416", "425", "434", "443", "452", "461", "515", "524", "533", "542", "551", "614", "623", "632", "641");
     for($i=0; $i<count($checkArray); $i++){
       $checkData = $checkArray[$i];
       $getTwo = substr("$checkData",0,2);
       $table = "k3_game_calculation";
       $condition = " where game_number='$game_number' AND selected_number='$checkData' OR selected_number='$getTwo'";
       $getFullDetails2 = $dd->fetch_multi($table,$condition);
       foreach($getFullDetails2 as $key)
       {
          @ $winAmount3 += $key->winner_amount;
           $winAm = $key->winner_amount;
           $numbersArray[] = array("winamount"=>"$winAm","renumber" => $checkData);
       }
      
     }
   }




   if($checkNumber == 12){
    $numbersArray = array();
    $checkArray = array("156", "165", "246", "255", "264", "336", "345", "354", "363", "426", "435", "444", "453", "462", "516", "525", "534", "543", "552", "561", "615", "624", "633", "642", "651");
     for($i=0; $i<count($checkArray); $i++){
       $checkData = $checkArray[$i];
       $getTwo = substr("$checkData",0,2);
       $table = "k3_game_calculation";
       $condition = " where game_number='$game_number' AND selected_number='$checkData' OR selected_number='$getTwo'";
       $getFullDetails2 = $dd->fetch_multi($table,$condition);
       foreach($getFullDetails2 as $key)
       {
          @ $winAmount3 += $key->winner_amount;
           $winAm = $key->winner_amount;
           $numbersArray[] = array("winamount"=>"$winAm","renumber" => $checkData);
       }
      
     }
   }


   if($checkNumber == 13){
    $numbersArray = array();
    $checkArray = array("166", "256", "265", "346", "355", "364", "436", "445", "454", "463", "526", "535", "544", "553", "562", "616", "625", "634", "643", "652", "661");
     for($i=0; $i<count($checkArray); $i++){
       $checkData = $checkArray[$i];
       $getTwo = substr("$checkData",0,2);
       $table = "k3_game_calculation";
       $condition = " where game_number='$game_number' AND selected_number='$checkData' OR selected_number='$getTwo'";
       $getFullDetails2 = $dd->fetch_multi($table,$condition);
       foreach($getFullDetails2 as $key)
       {
          @ $winAmount3 += $key->winner_amount;
           $winAm = $key->winner_amount;
           $numbersArray[] = array("winamount"=>"$winAm","renumber" => $checkData);
       }
      
     }
   }



   if($checkNumber == 14){
    $numbersArray = array();
    $checkArray = array("266", "356", "365", "446", "455", "464", "536", "545", "554", "563", "626", "635", "644", "653", "662");
     for($i=0; $i<count($checkArray); $i++){
       $checkData = $checkArray[$i];
       $getTwo = substr("$checkData",0,2);
       $table = "k3_game_calculation";
       $condition = " where game_number='$game_number' AND selected_number='$checkData' OR selected_number='$getTwo'";
       $getFullDetails2 = $dd->fetch_multi($table,$condition);
       foreach($getFullDetails2 as $key)
       {
          @ $winAmount3 += $key->winner_amount;
           $winAm = $key->winner_amount;
           $numbersArray[] = array("winamount"=>"$winAm","renumber" => $checkData);
       }
      
     }
   }




   if($checkNumber == 15){
    $numbersArray = array();
    $checkArray = array("366", "456", "465", "546", "555", "564", "636", "645", "654", "663");
     for($i=0; $i<count($checkArray); $i++){
       $checkData = $checkArray[$i];
       $getTwo = substr("$checkData",0,2);
       $table = "k3_game_calculation";
       $condition = " where game_number='$game_number' AND selected_number='$checkData' OR selected_number='$getTwo'";
       $getFullDetails2 = $dd->fetch_multi($table,$condition);
       foreach($getFullDetails2 as $key)
       {
          @ $winAmount3 += $key->winner_amount;
           $winAm = $key->winner_amount;
           $numbersArray[] = array("winamount"=>"$winAm","renumber" => $checkData);
       }
      
     }
   }


   if($checkNumber == 16){
    $numbersArray = array();
    $checkArray = array("466", "556", "565", "646", "655", "664");
     for($i=0; $i<count($checkArray); $i++){
       $checkData = $checkArray[$i];
       $getTwo = substr("$checkData",0,2);
       $table = "k3_game_calculation";
       $condition = " where game_number='$game_number' AND selected_number='$checkData' OR selected_number='$getTwo'";
       $getFullDetails2 = $dd->fetch_multi($table,$condition);
       foreach($getFullDetails2 as $key)
       {
          @ $winAmount3 += $key->winner_amount;
           $winAm = $key->winner_amount;
           $numbersArray[] = array("winamount"=>"$winAm","renumber" => $checkData);
       }
      
     }
   }


   if($checkNumber == 17){
    $numbersArray = array();
    $checkArray = array("566", "656", "665");
     for($i=0; $i<count($checkArray); $i++){
       $checkData = $checkArray[$i];
       $getTwo = substr("$checkData",0,2);
       $table = "k3_game_calculation";
       $condition = " where game_number='$game_number' AND selected_number='$checkData' OR selected_number='$getTwo'";
       $getFullDetails2 = $dd->fetch_multi($table,$condition);
       foreach($getFullDetails2 as $key)
       {
          @ $winAmount3 += $key->winner_amount;
           $winAm = $key->winner_amount;
           $numbersArray[] = array("winamount"=>"$winAm","renumber" => $checkData);
       }
     }
   }


   if($checkNumber == 18){
    $numbersArray = array();
    $checkArray = array("666");
     for($i=0; $i<count($checkArray); $i++){
       $checkData = $checkArray[$i];
       $getTwo = substr("$checkData",0,2);
       $table = "k3_game_calculation";
       $condition = " where game_number='$game_number' AND selected_number='$checkData' OR selected_number='$getTwo'";
       $getFullDetails2 = $dd->fetch_multi($table,$condition);
       foreach($getFullDetails2 as $key)
       {
          @ $winAmount3 += $key->winner_amount;
           $winAm = $key->winner_amount;
           $numbersArray[] = array("winamount"=>"$winAm","renumber" => $checkData);
       }
      
     }
   }



    $totalAmount = $winAmount+$winAmount2+$winAmount3;
    return array("winnumber" => $checkNumber,"totalAmount" => $totalAmount,"numberArray" => $numbersArray);
}






function calculationForResultAndPayment($dd,$k3Key,$periods,$game_number,$duration,$winNumberSelected,$totalAmount,$getArrayForNumber)
{
    $arrayResult = json_decode($getArrayForNumber);
    foreach($arrayResult as $key){
        $tmp[] = $key->winamount;
       }
       @ array_multisort($tmp, $arrayResult);
       $amount = $arrayResult[0]->winamount;

       ////////////////----------------CAlculation Done For Number's And Distribution----------/////////
      $resultNumber = $arrayResult[0]->renumber;
      $winNumberSelected;
       if($winNumberSelected <= 9)
       {
          $bigsmall = "small";
       }
       elseif($winNumberSelected >=10){
          $bigsmall = "big";
       }
       
       if($winNumberSelected == 3 || $winNumberSelected == 5 || $winNumberSelected == 7 || $winNumberSelected == 9 || $winNumberSelected == 11 || $winNumberSelected == 13 || $winNumberSelected == 15 || $winNumberSelected == 17)
       {
          $oddEven = "odd";
       }elseif($winNumberSelected == 4 || $winNumberSelected == 6 || $winNumberSelected == 8 || $winNumberSelected == 10 || $winNumberSelected == 12 || $winNumberSelected == 14 || $winNumberSelected == 16 || $winNumberSelected == 18)
       {
           $oddEven = "even";
       }

      $getTwo = substr("$resultNumber",0,2);
      $table = "k3_orders";
      $condition = " where periods='$periods' AND status='pending' AND game_number='$game_number'";
      $getBatDetails = $dd->fetch_multi($table,$condition);
      
      $f1 = substr($resultNumber,0,1);
      $ff = substr($resultNumber,0,2);
      $f2 = substr($ff,1);
      $f3 = substr($resultNumber,-1);
      $manual_result = "$f1,$f2,$f3";


      foreach($getBatDetails as $key){
          $user_key = $key->user_key;
          $order_type = $key->order_type;
          $periods = $key->periods;
          $k3orderKey = $key->k3orderKey;
          $amount = $key->amount;
          $winAmount = $key->winAmount;
          $selected_number = $key->selected_number;
          $bigorsmall = $key->bigorsmall;

          

          if($winNumberSelected==$selected_number || $selected_number == $resultNumber || $selected_number == $getTwo || $bigorsmall==$bigsmall || $bigorsmall==$oddEven)
          {
             fundUpdateOnUserAccount($dd,$user_key,$order_type,$periods,$k3orderKey,$winAmount,$amount,$game_number,$winNumberSelected);
             createNewGameK3($dd,$k3Key,$periods,$game_number,$duration,$manual_result);
          }
          else{
            fundsLossUpdateUserAccount($dd,$user_key,$order_type,$periods,$k3orderKey,$winAmount,$amount,$game_number,$winNumberSelected);
            createNewGameK3($dd,$k3Key,$periods,$game_number,$duration,$manual_result);
          }
      }
      createNewGameK3($dd,$k3Key,$periods,$game_number,$duration,$manual_result);
}




function fundUpdateOnUserAccount($dd,$user_key,$order_type,$periods,$k3orderKey,$winAmount,$amount,$game_number,$winNumberSelected)
{
    
    if($winNumberSelected <= 8){
        $smBig = "small";
    }else{
        $smBig = "big";
    }
    
    
    $table = "userdetails";
    $value = "wallet=wallet+$winAmount,winamount=winamount+$winAmount,withdraw_balance=withdraw_balance+$winAmount";
    $condition = " userKey='$user_key'";
    $result = $dd->updateData($table,$value,$condition);
    if($result){
       $table = "k3_orders";
       $value = "status='success'";
       $condition = " k3orderKey='$k3orderKey' AND game_number='$game_number'";
       $result = $dd->updateData($table,$value,$condition);
       if($result){
          $date = date("h:i d/,/Y");
          $ref = mt_rand(1111111111,9999999999); 
          $table = "account_history";
          $value = "(`userKey`, `amount`,`tax`, `action_date`, `lossorprofit`, `refrence`, `gameNumber`, `periods`,`result_number`,`selected_number`,`game_name`) 
          SELECT user_key,amount,tax,'$date','+$winAmount','$ref',game_number,'$periods','$winNumberSelected','$smBig','k3' FROM k3_orders WHERE k3orderKey='$k3orderKey' AND game_number='$game_number'";
          $result = $dd->insertData($table,$value);
       }
    }
}





function fundsLossUpdateUserAccount($dd,$user_key,$order_type,$periods,$k3orderKey,$winAmount,$amount,$game_number,$winNumberSelected)
{
     if($winNumberSelected <= 8){
        $smBig = "small";
    }else{
        $smBig = "big";
    }
    
  $table = "k3_orders";
  $value = "status='success'";
  $condition = " k3orderKey='$k3orderKey' AND game_number='$game_number'";
  $result = $dd->updateData($table,$value,$condition);
  if($result){
     $date = date("h:i d/,/Y");
     $ref = mt_rand(1111111111,9999999999); 
     $table = "account_history";
     $value = "(`userKey`, `amount`,`tax`, `action_date`, `lossorprofit`, `refrence`, `gameNumber`, `periods`,`result_number`,`selected_number`,`game_name`) 
     SELECT user_key,amount,tax,'$date','-$amount','$ref',game_number,'$periods','$winNumberSelected','$smBig','k3' FROM k3_orders WHERE k3orderKey='$k3orderKey' AND game_number='$game_number'";
     $result = $dd->insertData($table,$value);
  }
}





function createExistingGames($dd,$periods,$game_number,$bigsmall,$resultNumber,$oddEven,$winNumberSelected){
   $table = "k3existing_result";
   $condition = " where periods='$periods' AND game_number='$game_number'";
   $result = $dd->check($table,$condition);
   if($result['status'] != "success")
   {
        $f1 = substr($resultNumber,0,1);
        $getNumberArray2 = substr($resultNumber,0,2);
        $f2 = substr($getNumberArray2,1);
        $f3 = substr($resultNumber,-1);
        $finally = $f1.",".$f2.",".$f3;

       $date = date("h:i d/m/Y");
       $table = "k3existing_result";
       $value = "(`periods`,`game_number`, `sum`, `oddoreven`, `result`, `excuting_date`, `status`)
               VALUES('$periods','$game_number','$winNumberSelected','$oddEven','$finally','$date','success')";
       $result = $dd->insertData($table,$value);
   }
}




///////////////////////////////////////////-----------------K3 GAME CLOSED------------///////////







//////////////////////////----------------------5D GAME START---------------//////////////////





/////////////--------------5D GAME CLOSED-----------------//////////////////////




///////////////////////----------------------START LEVEL CALCULATION FOR DISTRIBUTION--------/////////


getValueAllnewUser($dd);


function getValueAllnewUser($dd)
{
   $table = "userdetails";
   $condition = " where status='inactive' AND wallet >1";
   $getUserDetails = $dd->fetch_multi($table,$condition);
   foreach($getUserDetails as $key)
   {
        $ramount = $key->wallet;
        if($ramount > 100)
        {
           $userKey = $key->userKey;
           $refral = $key->refral_from;
           $startFrom = 1;
           getCommissiontoRefralUser($dd,$userKey,$refral,$startFrom,$ramount);
           updateSuccessResponce($dd,$userKey);
        }
   }
}




function updateSuccessResponce($dd,$userKey)
{
   $table = "userdetails";
   $value = "status='success'";
   $condition = " userKey='$userKey'";
   $result = $dd->updateData($table,$value,$condition);
}






function getCommissiontoRefralUser($dd,$userKey,$refral,$startFrom,$ramount)
{
      if($refral != "")
      {
         $table = "level_distribution";
         $condition = null;
         $getCommission = $dd->single($table,$condition);
         $startFrom2 = "f".$startFrom;
         $getMargin = $ramount/100*$getCommission[$startFrom2];
         if($getCommission){
            $table = "userdetails";
            $condition = " where my_refral_id='$refral'";
            $getUserDetails = $dd->single($table,$condition);
            $newuserKey = $getUserDetails['userKey'];
            $refral_from = $getUserDetails['refral_from'];
            if($getUserDetails){
               if($getMargin >= 1){
               $table = "userdetails";
               $value = "wallet=wallet+$getMargin,refAmount=refAmount+$getMargin,withdraw_balance=withdraw_balance+$getMargin";
               $condition = " userKey='$newuserKey'";
               $result = $dd->updateData($table,$value,$condition);
               if($result){
                  $date = date("h:i d/m/Y");
                  $ref = mt_rand(1111111111,9999999999);
                  $table = "account_history";
                  $value = "(`userKey`, `amount`, `action_date`, `lossorprofit`, `refrence`,`game_name`)
                  SELECT '$newuserKey','$getMargin','$date','+$getMargin','$ref','ref' FROM userdetails WHERE userKey='$newuserKey'";
                  $result2 = $dd->insertData($table,$value);
                  if($result2){
                     $userKey = $newuserKey;
                     $refral = $refral_from;
                     $startFrom = $startFrom+1;
                     if($startFrom <=10){
                         getCommissiontoRefralUser($dd,$userKey,$refral,$startFrom,$ramount);
                     }
                  }
               }
             }else{
               $userKey = $newuserKey;
               $refral = $refral_from;
               $startFrom = $startFrom+1;
               if($startFrom <=10){
                     getCommissiontoRefralUser($dd,$userKey,$refral,$startFrom,$ramount);
               }
             }
            }
         }
      }
    
}


////////////////////////--------------------LEVEL COMMISSION AREA CLOSED---------------////////////////







/////////////////---------------BET COMMISSION AREA START--------------///////////
getCalculationForBetCommissionArea($dd);



function getCalculationForBetCommissionArea($dd)
{
   $table = "userdetails";
   $condition = " where newBet>1";
   $getBetDetails = $dd->fetch_multi($table,$condition);
   foreach($getBetDetails as $key){
      $userKey = $key->userKey;
      $refral_from = $key->refral_from;
      $ramount = $key->newBet;
      $lvlStart = 1;
      if($ramount > 0.1 && $refral_from != ""){
         getMarginCalculationForBet($dd,$userKey,$refral_from,$lvlStart,$ramount);
         changeNewBetBalanceZero($dd,$userKey);
      }
   }
}




function changeNewBetBalanceZero($dd,$userKey){
    $table = "userdetails";
    $value = "newBet='0'";
    $condition = " userKey='$userKey'";
    $result = $dd->updateData($table,$value,$condition);
}




function getMarginCalculationForBet($dd,$userKey,$refral_from,$lvlStart,$ramount)
{
   if($refral_from != ""){
   $table = "level_distribution";
   $condition = null;
   $getCommission = $dd->single($table,$condition);
   $startFrom2 = "bet".$lvlStart;
   $getMargin = $ramount/100*$getCommission[$startFrom2];
   if($getMargin > 0){
      $table = "userdetails";
      $value = "wallet=wallet+$getMargin,withdraw_balance=withdraw_balance+$getMargin";
      $condition = " my_refral_id='$refral_from'";
      $result = $dd->updateData($table,$value,$condition);
      if($result){
         $date = date("h:i d/m/Y");
         $ref = mt_rand(1111111111,9999999999);
         $table = "account_history";
         $value = "(`userKey`, `amount`, `action_date`, `lossorprofit`, `refrence`,`game_name`)
          SELECT userKey,'$getMargin','$date','+$getMargin','$ref','bet commission' FROM userdetails WHERE my_refral_id='$refral_from'";
          $result3 = $dd->insertData($table,$value);
         if($result3){
             $table = "userdetails";
             $condition = " where my_refral_id='$refral_from'";
             $getNewRef = $dd->single($table,$condition);
             $refral_from = $getNewRef['refral_from'];
             $userKey = $getNewRef['userKey'];
            $lvlStart = $lvlStart+1;
            if($lvlStart <= 10){
                getMarginCalculationForBet($dd,$userKey,$refral_from,$lvlStart,$ramount);
            }
         }
      }
   }
 } 
}







////////////////////////////////--------------------------DELETE WINGO ORDER DETAILS-------------------
updateeveryday($dd);



function updateeveryday($dd)
{
    $date = date("d");
    
    
    $table = "admin_calculation";
    $condition = " where adminKey='1'";
    $getAdmin = $dd->single($table,$condition);
    if($getAdmin['daily_task'] != $date)
    {
        $table = "userdetails";
        $value = "daily_task='0'";
        $condition = " daily_task > 0";
        $result = $dd->updateData($table,$value,$condition);
        if($result){
            $table = "admin_calculation";
            $value = "daily_task='$date'";
            $condition = " adminKey='1'";
            $result = $dd->updateData($table,$value,$condition);
        }
    }
}
?>

