<?php

$db = mysqli_connect("localhost", "soxperts_Raika89", "b0z*YW7sD*v)", "soxperts_RTP_Dewa89");

$bonus = "";

$css = "CSS_Index.css?ver2.19.4";
$css1 = "../" . $css;

$js = "java.js?ver2.2.5";

// Bagian Coding Penting Yang Bisa Diedit
$jam_acak = rand(1,3);

$array_provider = [
    [
        "Provider" => "Pragmatic_Play",
        "Nama_Provider" => "Pragmatic Play",
        "Gambar" => "slot-prag.png",
        "Gambar_Brand" => "pp.png",
        "Nama_Gambar" => "https://img.viva88athenae.com/pp/images/",
    ],

    [
        "Provider" => "PG_Soft",
        "Nama_Provider" => "PG Soft",
        "Gambar" => "slot-pg.png",
        "Gambar_Brand" => "pg.png",
        "Nama_Gambar" => "https://img.viva88athenae.com/pg/images/",
    ],

    [
        "Provider" => "Habanero",
        "Nama_Provider" => "Habanero",
        "Gambar" => "slot-hab.png",
        "Gambar_Brand" => "hb.png",
        "Nama_Gambar" => "https://img.viva88athenae.com/hb/images/circle/",
    ],

    [
        "Provider" => "Microgaming",
        "Nama_Provider" => "Microgaming",
        "Gambar" => "slot-mg.png",
        "Gambar_Brand" => "mg2.png",
        "Nama_Gambar" => "https://img.viva88athenae.com/mg/images/",
    ],

    [
        "Provider" => "Joker",
        "Nama_Provider" => "Joker",
        "Gambar" => "slot-jok.png",
        "Gambar_Brand" => "jg.png",
        "Nama_Gambar" => "https://img.viva88athenae.com/jg/images/",
    ],

    [
        "Provider" => "Playstar",
        "Nama_Provider" => "Playstar",
        "Gambar" => "slot-ps.png",
        "Gambar_Brand" => "ps.png",
        "Nama_Gambar" => "https://img.viva88athenae.com/ps/images/",
    ],
    
    [
        "Provider" => "Spade_Gaming",
        "Nama_Provider" => "Spade Gaming",
        "Gambar" => "slot-spad.png",
        "Gambar_Brand" => "sg.png",
        "Nama_Gambar" => "https://img.viva88athenae.com/sg/images/",
    ],
];

function query($query) {
    global $db;

    $array_data = [];

    $data = mysqli_query($db, $query);

    while ($rows = mysqli_fetch_assoc($data)) {
        $array_data[] = $rows;
    }

    return $array_data;

}

function cari_permainan($nama_permainan) {
    $query = "SELECT * FROM provider_permainan WHERE Nama_Permainan LIKE '%$nama_permainan%'";
    return query($query);
}

function cek_waktu($data_rtp) {

    foreach ($data_rtp as $data) {
        urai_waktu($data);
    }

    $jam_sekarang = date("H:i");

}

function urai_waktu($data) {
    if ($data["Jam_Sesudah"] == 0 && $data["Jam_Sebelum"] == 0) {
        rtp_baru($data);
    }

    $waktu = explode(":", $data["Jam_Sesudah"]);
    $tanggal = explode("/", $data["Tanggal"]);

    $waktu_pola = $tanggal[2] . $tanggal[1] . $tanggal[0] . $waktu[0] . $waktu[1];
    $waktu_sekarang = date("YmdHi");

    // // Untuk localhost
    // $waktu_sekarang = date("YmdHi", time()+21600);

    // edit_waktu($data);

    if ($waktu_pola < $waktu_sekarang) {
        // Ganti Pola saat jam sekarang sudah melewati jam sebelum
        ubah_waktu($data);
    }
}

function edit_waktu($data) {
    global $db;
    
    $id = $data["Id"];
    $tanggal = date("d/m/Y", time()-86400);
    
    $query = "UPDATE provider_permainan SET
    Tanggal = '$tanggal'
    WHERE Id = '$id'
    ";

    mysqli_query($db, $query);
    
}

function ubah_waktu($data) {
    global $db, $jam_acak;

    $array_waktu = [];
    $id = $data["Id"];
    $jam_sebelum = "";
    $jam_sesudah = "";
    $tanggal = $data["Tanggal"];
    // $tanggal = $tanggal[2] . $tanggal[1] . $tanggal[0];
    // $tanggal_sekarang = date("Ymd");
    
    // Jika waktu update pola melewati 24jam, hari += 1
    $tambah_tanggal = false;

    $pola = pola_baru($data);

    for ($i = 0; $i < 2; $i++) {
        $menit_acak = rand(0, 59);
        
        if ($i == 0) {
            $jam_sebelum = explode(":", $data["Jam_Sesudah"]);
            $jam = $jam_sebelum[0];
            $menit = $jam_sebelum[1];

            $jam_sebelum = $jam . ":" . $menit; 
        }

        
        if ($i == 1) {
            $jam_sesudah = explode(":", $jam_sebelum);
            $jam = $jam_sesudah[0] + $jam_acak;
            $menit = $jam_sesudah[1] + $menit_acak;

            if ($menit < 10) {
                $menit = "0" . $menit;
            } elseif ($menit > 59) {
                $menit -= 60;
                if ($menit < 10) {
                    $menit = "0" . $menit;
                }
            }

            if ($jam < 10) {
                $jam = "0" . $jam;
            } elseif ($jam > 23) {
                $jam -= 24;
                $tambah_tanggal = true;
                if ($jam < 10) {
                    $jam = "0" . $jam;
                }
            }

            $jam_sesudah = $jam . ":" . $menit;
            
        }
    }

    if ($tambah_tanggal == true) {
        $tanggal = date("d/m/Y");
        $tambah_tanggal = false;
    } else {
        $tanggal = $data["Tanggal"];
    }

    $rtp = rand(60, 99);

    $query = "UPDATE provider_permainan SET
    Jam_Sebelum = '$jam_sebelum',
    Jam_Sesudah = '$jam_sesudah',
    RTP = '$rtp',
    Pola = '$pola',
    Tanggal = '$tanggal'
    WHERE Id = '$id'
    ";

    mysqli_query($db, $query);
    
    header("Refresh:0");

} 

function rtp_baru($data) {
    global $db, $tambah_tanggal;

    $id = $data["Id"];
    $tanggal = date("d/m/Y");

    $jam_sebelum = rand(0, 21);
    $menit_sebelum = rand(0 ,59);

    $jam_sesudah = $jam_sebelum + rand(1, 3);
    $menit_sesudah = $menit_sebelum + rand(0, 59);

    $rtp = rand(60, 99);

    if ($menit_sebelum < 10) {
        $menit_sebelum = "0" . $menit_sebelum;
    } elseif ($menit_sebelum > 59) {
        $menit_sebelum -= 60;
        if ($menit_sebelum < 10) {
            $menit_sebelum = "0" . $menit_sebelum;
        }
    }

    if ($jam_sebelum < 10) {
        $jam_sebelum = "0" . $jam_sebelum;
    } elseif ($jam_sebelum > 23) {
        $jam_sebelum -= 24;
        $tambah_tanggal = true;
        if ($jam_sebelum < 10) {
            $jam_sebelum = "0" . $jam_sebelum;
        }
    }

    if ($menit_sesudah < 10) {
        $menit_sesudah = "0" . $menit_sesudah;
    } elseif ($menit_sesudah > 59) {
        $menit_sesudah -= 60;
        if ($menit_sesudah < 10) {
            $menit_sesudah = "0" . $menit_sesudah;
        }
    }

    if ($jam_sesudah < 10) {
        $jam_sesudah = "0" . $jam_sesudah;
    } elseif ($jam_sesudah > 23) {
        $jam_sesudah -= 24;
        $tambah_tanggal = true;
        if ($jam_sesudah < 10) {
            $jam_sesudah = "0" . $jam_sesudah;
        }
    }

    if ($tambah_tanggal == true) {
        $tanggal = date("d/m/Y", time()+86400);
        $tambah_tanggal = false;
    }

    $waktu_sebelum = $jam_sebelum . ":" . $menit_sebelum;
    $waktu_sesudah = $jam_sesudah . ":" . $menit_sesudah;

    $pola = pola_baru($data);

    $query = "UPDATE provider_permainan SET
    Jam_Sebelum = '$waktu_sebelum',
    Jam_Sesudah = '$waktu_sesudah',
    RTP = '$rtp',
    Tanggal = '$tanggal',
    Pola = '$pola'
    WHERE Id = '$id'
    ";

    mysqli_query($db, $query);
}

function pola_baru($data) {

    $provider_pola = $data["Provider"];

    $jumlah_pola = 0;
    $hasil_pola = [];

    $query = "SELECT * FROM pola_gacor";
    $data_pola = query($query);

    foreach ($data_pola as $pola) {
        if ($provider_pola == $pola["Nama_Provider"]) {
            $data_pola = $pola;
        }
    }

    while ($jumlah_pola < 3) {

        $manual_spin = jadikan_array($pola["Manual_Spin"]);
        $manual_spin = manual_spin($manual_spin);
    
        $auto_spin = jadikan_array($pola["Auto_Spin"]);
        $auto_spin = auto_spin($auto_spin);

        $array_pola = ["Manual", "Auto"];
        $angka_pola = rand(0,1);
        
        if ($array_pola[$angka_pola] == "Manual") {
            $hpola = $array_pola[$angka_pola] . " " . $manual_spin;
        } else {
            $hpola = $array_pola[$angka_pola] . " " . $auto_spin;
        }

        if (!in_array($hpola, $hasil_pola)) {
            $hasil_pola[] = $hpola;
            $jumlah_pola ++;
        }

    }

    $hasil_pola = jadikan_teks_untuk_database($hasil_pola);

    return $hasil_pola;

}

function jadikan_array($angka) {
    $angka = explode(",", $angka);

    return $angka;
}

function manual_spin($array_angka) {
    $angka_acak = rand($array_angka[0], $array_angka[1]);

    if ($angka_acak < 10) {
        $angka_acak = "0" . $angka_acak;
    }

    return $angka_acak;

}

function auto_spin($array_angka) {
    $jumlah_array = 0;

    foreach ($array_angka as $angka) {
        $jumlah_array++;
    }

    $angka_acak = rand(0, ($jumlah_array - 1));

    $hasil_angka_acak = $array_angka[$angka_acak];

    return $hasil_angka_acak;

}

function jadikan_teks_untuk_database($array_teks) {
    $jumlah_array = 0;
    $hasil_teks = "";

    foreach ($array_teks as $teks) {
        $jumlah_array++;
    }

    while ($jumlah_array > 0) {
        $array_turbo = ["On", "Off"];
        $turbo_acak = rand(0,1);

        if ($jumlah_array != 1) {
            $hasil_teks .= $array_teks[($jumlah_array - 1)] . "/" . $array_turbo[$turbo_acak] . "/";
        } else {
            $hasil_teks .= $array_teks[($jumlah_array - 1)] . "/" . $array_turbo[$turbo_acak];
        }

        $jumlah_array--;
    }

    return $hasil_teks;

}

function uraikan_teks_pola($teks) {
    $teks = explode("/", $teks);
    $jumlah_pola = 0;
    $jumlah_pengulangan = 2;
    $array_pola = [];

    foreach ($teks as $t) {
        if ($jumlah_pengulangan > 0) {
            $array_pola[$jumlah_pola][] = $t;
            $jumlah_pengulangan -= 1;
        } elseif ( $jumlah_pengulangan == 0) {
            $jumlah_pola++;
            $jumlah_pengulangan = 2;
            $array_pola[$jumlah_pola][] = $t;
            $jumlah_pengulangan -= 1;
        }

    }

    return $array_pola;
}

function clamp($min, $maks, $nilai) {
    $nilai_clamp = max($min, min($maks, $nilai));
    return $nilai_clamp;
}

?>