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

SendRequestWingoCalculation($dd);


function SendRequestWingoCalculation($dd){
    for($i=1; $i < 5; $i++){
        sendRequestToCalculate($dd,$i);
    }
}


function sendRequestToCalculate($dd,$game){
    $getDetails = $dd->single("wingo_game_runing"," where game_number='$game'");
    $manualNumber = $getDetails['manual_result'];
    $timeAction = $getDetails['time_perods'];
    if($timeAction == "1"){
        if($manualNumber == "Automatic"){
            $rnumber = mt_rand(0,9);
            sendRequestTo($dd,$rnumber,$game);
        }else{
            $rnumber = $manualNumber;
            sendRequestTo($dd,$rnumber,$game);
        }
    }else{
        $update = $dd->updateData("wingo_game_runing","time_perods=time_perods-1"," game_number='$game'");
    }
}













function sendRequestTo($dd,$rnumber,$game){
    
    if($rnumber == "0"){
        $clr = "voilet";
        $clr2 = "red";
        $bigg = "small";
    }else if($rnumber == "1"){
        $clr = "green";
        $bigg = "small";
    }else if($rnumber == "2"){
        $clr = "red";
        $bigg = "small";
    }else if($rnumber == "3"){
        $clr = "red";
        $bigg = "small";
    }else if($rnumber == "4"){
        $clr = "green";
        $bigg = "small";
    }else if($rnumber == "5"){
        $clr = "voilet";
        $bigg = "big";
        $clr2 = "green";
    }else if($rnumber == "6"){
        $clr = "red";
        $bigg = "big";
    }else if($rnumber == "7"){
        $clr = "green";
        $bigg = "big";
    }else if($rnumber == "8"){
        $clr = "red";
        $bigg = "big";
    }else if($rnumber == "9"){
        $clr = "green";
        $bigg = "big";
    }
    
    
    GetCalculationWining($dd,$rnumber,$game,$clr,$clr2,$bigg);
    // GettoCalculateLossUsers($dd,$rnumber,$game,$clr,$clr2,$bigg);
    // resetGameActivity($dd,$rnumber,$game,$clr,$clr2,$bigg);
    // updateAdminDate($dd,$game);
}







function GetCalculationWining($dd,$rnumber,$game,$clr,$clr2,$bigg)
{
    $getAllWinnerList = $dd->fetch_multi("account_history"," where status='pending' AND gameNumber='$game' AND selected_number='$clr' OR selected_number='$clr2' OR selected_number='$rnumber' OR selected_number='$bigg'");
    if(count($getAllWinnerList) > 0){
    
    foreach($getAllWinnerList as $key){
        $accountKey = $key->accountKey;
        $userKey = $key->userKey;
        $bet_amount = $key->bet_amount;
        $amount = $key->amount;
        $lossorprofit = $key->lossorprofit;
        
        
        // getToupdateWinAmount($dd,$rnumber,$game,$accountKey,$userKey,$bet_amount,$amount,$lossorprofit);
    }
    }
}





function getToupdateWinAmount($dd,$rnumber,$game,$accountKey,$userKey,$bet_amount,$amount,$lossorprofit)
{
    $update = $dd->updateData("userdetails","wallet=wallet+$lossorprofit,winamount=winamount+$lossorprofit,withdraw_balance=withdraw_balance+$lossorprofit"," userKey='$userKey'");
    if($update){
        $date = date("H:i:s d/m/Y");
        $update = $dd->updateData("account_history","time_calculation='$date',status='success',result_number='$rnumber'"," accountKey='$accountKey'");
    }
}






function GettoCalculateLossUsers($dd,$rnumber,$game,$clr,$clr2,$bigg)
{
    $date = date("H:i:s d/m/Y");
    $value = "status='success',time_calculation='$date',lossorprofit=-bet_amount,result_number='$rnumber'";
    $update = $dd->updateData("account_history",$value," status='pending' AND gameNumber='$game'");
}








function resetGameActivity($dd,$rnumber,$game,$clr,$clr2,$bigg)
{
    if($game == "1"){
        $ttt = "1";
    }else if($game == "2"){
        $ttt = "3";
    }else if($game == "3"){
        $ttt = "5";
    }else if($game == "4"){
        $ttt = "10";
    }
    
    
    
    $getAdmin = $dd->single("wingo_game_runing"," where game_number='$game'");
    if($getAdmin['lastUpdate'] == date("D")){
        $prds = "periods+1";
    }else{
        $prds = date("Ymd")."0001";
    }
    
    
    
    
    $update = $dd->updateData("wingo_game_runing","manual_result='Automatic',periods=$prds,time_perods='$ttt'"," game_number='$game'");
    if($update){
        $date = date("H:i:s d/m/Y");
        $value = "(`game_number`, `periods`, `number`, `bigorsmall`, `color`, `result_date`, `status`) 
        SELECT '$game',periods,'$rnumber','$bigg','$clr','$date','success' FROM wingo_game_runing where game_number='$game'";
        $insert = $dd->insertData("existing_game_wingo",$value);
    }
}






function updateAdminDate($dd,$game)
{
    $day = date("D");
    $update = $dd->updateData("wingo_game_runing","lastUpdate='$day'"," game_number='$game'");
}
?>