<?php
// worker.php - Bridge for 5-minute cron to trigger both 30s and 1min WinGo
// Run this file via a 5-minute cron job

ignore_user_abort(true);
set_time_limit(310);

$endTime = time() + 290;
$baseUrl = 'https://pakclub24.club/WinGo/wingo_cron.php';

while (time() < $endTime) {
    // Wait until the next 30-second mark
    $now = time();
    $seconds_to_wait = 30 - ($now % 30);
    sleep($seconds_to_wait);

    if (time() >= $endTime) break;

    // ── ALWAYS trigger 30s WinGo ────────────────────────────────────────
    $ch30 = curl_init($baseUrl . '?mode=30s');
    curl_setopt($ch30, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch30, CURLOPT_TIMEOUT, 3);
    curl_setopt($ch30, CURLOPT_SSL_VERIFYPEER, false);
    curl_exec($ch30);
    curl_close($ch30);

    // ── Trigger 1min WinGo only at 60-second boundaries ────────────────
    // At every even 30s mark (i.e., the minute mark), trigger 1min cron
    if (time() % 60 < 5) {
        $ch1m = curl_init($baseUrl . '?mode=1min');
        curl_setopt($ch1m, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch1m, CURLOPT_TIMEOUT, 3);
        curl_setopt($ch1m, CURLOPT_SSL_VERIFYPEER, false);
        curl_exec($ch1m);
        curl_close($ch1m);
    }
}