<?php
// update.php
$mysqli = new mysqli("localhost", "bant5829_admgame", "Dodol@25?", "bant5829_game");
if ($mysqli->connect_errno) {
  http_response_code(500);
  exit("DB connection failed");
}

$who   = $_POST['who'] ?? null;
$state = $_POST['state'] ?? null;

if (!$who || !$state) {
  http_response_code(400);
  exit("Missing who/state");
}

$data = json_decode($state, true);
if (!$data) {
  http_response_code(400);
  exit("Invalid JSON");
}

$stmt = $mysqli->prepare("UPDATE game_state SET x=?, y=?, direction=?, bullets=?, shield=? WHERE player=?");
$jsonBullets = json_encode($data['bullets']);
$shield = isset($data['shield']) ? (int)$data['shield'] : 0;
$stmt->bind_param("ddssis", $data['x'], $data['y'], $data['direction'], $jsonBullets, $shield, $who);
$stmt->execute();

echo "OK";
