<?php
@session_start();
include "control/activity.php";
$dd = new activity();
$userKey = $_SESSION['loginKey'];
$gmaeNumber = $_REQUEST['gamenumber'];
$getHascode = $dd->single("trx_existing_result"," where game_number='$gmaeNumber' ORDER BY 1 DESC");
$getGameDetails = $dd->single("trx_runnning"," where game_name='$gmaeNumber'");
$walletBalance = $dd->single("userdetails"," where userKey='$userKey'")['wallet'];
$accountHistory = $dd->single("account_history"," where userKey='$userKey' AND game_name='TRX' AND gameNumber='$gmaeNumber' ORDER BY 1 DESC");
$endTime = $getGameDetails['duration']*60-date("s");
$time_calculation = explode(" ",$accountHistory['time_calculation'])[0];
$resultNumber = $accountHistory['result_number'];

if(isDifferenceBelowFiveSeconds($time_calculation, date("H:i:s"))){
    $popupStatus = "success";
}else{
    $popupStatus = "failed";
}


if($gmaeNumber == "1"){
    $resetTime = "60";
}else if($gmaeNumber == "2"){
    $resetTime = "180";
}else if($gmaeNumber == "3"){
    $resetTime = "300";
}else if($gmaeNumber == "4"){
    $resetTime = "600";
}



$last6digit = str_split(strtoupper(substr($getHascode['hase_key'], -5)));

$num1 = $last6digit[0];
$num2 = $last6digit[1];
$num3 = $last6digit[2];
$num4 = $last6digit[3];
$num5 = $last6digit[4];

print_r(json_encode(array("gameData" => $getGameDetails,"endTime" => $endTime,"resetTime" => "$resetTime","wallet" => $walletBalance,"popup_status" => $popupStatus,"accountHistory" => $accountHistory,"num1" => $num1,"num2" => $num2,"num3" => $num3,"num4" => $num4,"num5" => $num5)));






//////////////////////////----------------------------time calculation
function isDifferenceBelowFiveSeconds($time1, $time2) {
    // Convert the times to timestamps
    $timestamp1 = strtotime($time1);
    $timestamp2 = strtotime($time2);
    
    // Calculate the absolute difference in seconds
    $difference = abs($timestamp2 - $timestamp1);
    
    // Check if the difference is less than 5 seconds
    return $difference < 5;
}
?>