<?php
// getstate.php
header('Content-Type: application/json');
$mysqli = new mysqli("localhost", "bant5829_admgame", "Dodol@25?", "bant5829_game");
if ($mysqli->connect_errno) {
  http_response_code(500);
  exit("DB connection failed");
}

$result = $mysqli->query("SELECT player, x, y, direction, bullets, shield FROM game_state");
$state = [];
while ($row = $result->fetch_assoc()) {
  $state[$row['player']] = [
    "x" => (float)$row['x'],
    "y" => (float)$row['y'],
    "direction" => $row['direction'],
    "bullets" => json_decode($row['bullets'], true),
    "shield" => (int)$row['shield']
  ];
}
echo json_encode($state);
