<?php
ob_start();
session_start();
date_default_timezone_set("Asia/Jakarta");
include('../config/koneksi.php');

$sql_0 = mysqli_query($conn,"SELECT * FROM `tb_seo` WHERE cuid = 1") or die(mysqli_error());
$s0 = mysqli_fetch_array($sql_0);
$urlweb = $s0['urlweb'];

$getProvider = mysqli_query($conn,"SELECT * FROM `tb_tripayapi` WHERE cuid = 1") or die(mysqli_error());
$gp = mysqli_fetch_array($getProvider);
$provider = $gp['provider'];
$urlRequest = $gp['urlRequest'];
$secureLogin = $gp['api_key'];
$secretKey = $gp['secret_key'];

$pid = 0;
$periode = 0;
$posisi = 'PragmaticPlay';
$menang = 0;
$diskon = 0;

$created_date = date('Y-m-d 00:00:00');
$datePlayed = date('Y-m-d');

$kabupaten = mysqli_query($conn,"SELECT * FROM `tb_ppplayer` WHERE status = 1 AND provider = 'PragmaticPlay'") or die(mysqli_error($conn));
while($k = mysqli_fetch_array($kabupaten)){
	$userID = $k['userID'];
	$externalPlayerId = $k['externalPlayerId'];

	$params = 'externalPlayerId='.$externalPlayerId.'&secureLogin='.$secureLogin.$secretKey;
    $hashNeed = md5($params);
    $curl = curl_init();
                                                
    curl_setopt_array($curl, array(
        CURLOPT_URL => $urlRequest.'/balance/current',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => "secureLogin=".$secureLogin."&externalPlayerId=".$externalPlayerId."&hash=".$hashNeed,
        CURLOPT_HTTPHEADER => array(
            "Content-Type: application/x-www-form-urlencoded",
            "Cache-Control: no-cache"
        ),
    ));
                                                           
    $response = curl_exec($curl);
    //echo $response;
    curl_close($curl);
    $hasil = json_decode($response, true);
    $newSaldo = $hasil['balance'];
    $update = mysqli_query($conn,"UPDATE `tb_ppplayer` SET `balance` = '$newSaldo' WHERE userID = '$userID' AND provider = 'PragmaticPlay'") or die(mysqli_error());

    $params2 = 'datePlayed='.$datePlayed.'&playerId='.$externalPlayerId.'&secureLogin='.$secureLogin.'&timeZone=GMT +07:00'.$secretKey;
    $hashNeed2 = md5($params2);
    $curl2 = curl_init();
                                                    
    curl_setopt_array($curl2, array(
        CURLOPT_URL => 'https://api.prerelease-env.biz/IntegrationService/v3/http/HistoryAPI/GetPlayedGames/',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => "secureLogin=".$secureLogin."&playerId=".$externalPlayerId."&datePlayed=".$datePlayed."&timeZone=GMT%20%2B07%3A00&hash=".$hashNeed2,
        CURLOPT_HTTPHEADER => array(
            "Content-Type: application/x-www-form-urlencoded",
            "Cache-Control: no-cache"
        ),
    ));
                                                             
    $response2 = curl_exec($curl2);
    //echo $response;
    curl_close($curl2);
    $hasil2 = json_decode($response2, true);

    for($i=0;$i<COUNT($hasil2['games']);$i++){
        $gameCode = $hasil2['games'][$i]['gameId'];
        $gameName = $hasil2['games'][$i]['gameName'];
        
        $params1 = 'datePlayed='.$datePlayed.'&gameId='.$gameCode.'&playerId='.$externalPlayerId.'&secureLogin='.$secureLogin.'&timeZone=GMT +07:00'.$secretKey;
        $hashNeed1 = md5($params1);
        $curl1 = curl_init();
                                                    
        curl_setopt_array($curl1, array(
            CURLOPT_URL => 'https://api.prerelease-env.biz/IntegrationService/v3/http/HistoryAPI/GetGameRounds/',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => "secureLogin=".$secureLogin."&playerId=".$externalPlayerId."&datePlayed=".$datePlayed."&timeZone=GMT%20%2B07%3A00&gameId=".$gameCode."&hash=".$hashNeed1,
            CURLOPT_HTTPHEADER => array(
                "Content-Type: application/x-www-form-urlencoded",
                "Cache-Control: no-cache"
            ),
        ));
                                                               
        $response1 = curl_exec($curl1);
        echo $response1;
        curl_close($curl1);
        $hasil1 = json_decode($response1, true);
        
        for($j=0;$j<COUNT($hasil1['rounds']);$j++){
            if($hasil1['rounds'][$j]['roundDetails'] != 'Free spin'){
                $created = $hasil1['rounds'][$j]['dateTime'];
                $nominal = $hasil1['rounds'][$j]['betAmount'];
                $bayar = $hasil1['rounds'][$j]['winAmount'];
                $jumlah = $hasil1['rounds'][$j]['balance'];
                $tebak = $hasil1['rounds'][$j]['roundId'];
                if($bayar < $nominal){
                    $keterangan = 'Kalah';
                }
                else {
                    $keterangan = 'Menang';
                }
                
                $cekTaruhan = mysqli_query($conn,"SELECT * FROM `tb_taruhan` WHERE `userID` = '$userID' AND `tebak` = '$tebak' AND `posisi` = '$posisi'") or die(mysqli_error());
                $cth = mysqli_num_rows($cekTaruhan);
                if($cth == 0){
                    $insertHistory = mysqli_query($conn,"INSERT INTO `tb_taruhan` (`userID`, `pid`, `gameid`, `code`, `periode`, `created_date`, `tebak`, `posisi`, `nominal`, `menang`, `diskon`, `bayar`, `jumlah`, `keterangan`, `status`) VALUES ('$userID','$pid','$gameCode','$gameName','$periode','$created','$tebak','$posisi','$nominal','$menang','$diskon','$bayar','$jumlah','$keterangan',1)") or die(mysqli_error());
                }
            }
        }
    }
}
?>