<?php
$id   = $_POST['id'] ?? '';
$x    = intval($_POST['x'] ?? 0);
$y    = intval($_POST['y'] ?? 0);
$arah = $_POST['arah'] ?? null;
$aksi = $_POST['aksi'] ?? null;

$host = "localhost";
$user = "bant5829_admgame";
$pass = "Dodol@25?";
$db   = "bant5829_game";

$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
  http_response_code(500);
  echo "Koneksi gagal";
  exit;
}

// Tentukan query berdasarkan apakah arah dan aksi tersedia
if ($arah !== null && $aksi !== null) {
  $stmt = $conn->prepare("UPDATE posisi_pc SET x = ?, y = ?, arah = ?, aksi = ? WHERE id = ?");
  $stmt->bind_param("iisss", $x, $y, $arah, $aksi, $id);
} elseif ($arah !== null) {
  $stmt = $conn->prepare("UPDATE posisi_pc SET x = ?, y = ?, arah = ? WHERE id = ?");
  $stmt->bind_param("iiss", $x, $y, $arah, $id);
} elseif ($aksi !== null) {
  $stmt = $conn->prepare("UPDATE posisi_pc SET x = ?, y = ?, aksi = ? WHERE id = ?");
  $stmt->bind_param("iiss", $x, $y, $aksi, $id);
} else {
  $stmt = $conn->prepare("UPDATE posisi_pc SET x = ?, y = ? WHERE id = ?");
  $stmt->bind_param("iis", $x, $y, $id);
}

$stmt->execute();
echo "OK";
$conn->close();
?>
