<?php
// Konfigurasi
$agent_code = 'kLTpRz3aVl';
$provider_code = 'PR';
$agent_key = 'YourAgentKey'; // Gantilah dengan kunci agen Anda
$signature = md5($agent_code . $agent_key);
$api_url = "https://api.apimax.site/v2/GetGameList.aspx?agent_code=$agent_code&provider_code=$provider_code&signature=$signature";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);

// Decode respons JSON
$response_data = json_decode($response, true);

if ($response_data['status'] == 'success') {
    include '../function/connect.php';
    $gamelist = $response_data['gamelist'];

    foreach ($gamelist as $game) {
        $game_code = mysqli_real_escape_string($koneksi, $game['game_code']);
        $game_name = mysqli_real_escape_string($koneksi, $game['game_name']);
        $game_provider = mysqli_real_escape_string($koneksi, $game['game_provider']);
        $game_type = mysqli_real_escape_string($koneksi, $game['game_type']);
        $game_image = mysqli_real_escape_string($koneksi, $game['game_image']);
        $game_status = mysqli_real_escape_string($koneksi, $game['game_status']);
        $game_vendor = mysqli_real_escape_string($koneksi, $game['game_vendor']);
        
        $check_query = "SELECT COUNT(*) as count FROM tb_games WHERE game_code = '$game_code' AND game_provider = '$game_provider'";
        $result = mysqli_query($koneksi, $check_query);
        $row = mysqli_fetch_assoc($result);

        if ($row['count'] == 0) {
            $insert_query = "INSERT INTO tb_games (game_code, game_name, game_provider, game_type, game_image, game_status, game_vendor)
                             VALUES ('$game_code', '$game_name', '$game_provider', '$game_type', '$game_image', '$game_status', '$game_vendor')";
            mysqli_query($koneksi, $insert_query);
        }
    }

    echo "Daftar game berhasil diperbarui.";
} else {
    echo "Gagal memperbarui daftar game: " . $response_data['msg'];
}
?>
