<?php
session_start();
require_once "koneksi.php";

if (isset($_POST['score']) && isset($_SESSION['users'])) {
    $score = (int)$_POST['score'];
    $username = $_SESSION['users']['username'];
    
    $sql = "UPDATE users SET score = $score WHERE username = '$username' AND (score < $score OR score IS NULL)";
    $result = mysqli_query($koneksi, $sql);
    
    if ($result) {
        echo "<script>console.log('Score $score tersimpan untuk $username');</script>";
    }
}

// Redirect kembali ke game
echo "<script>window.history.back();</script>";
?>