68 lines
2.0 KiB
PHP
68 lines
2.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
function ensureFpsMatchesTable(): void
|
|
{
|
|
static $ensured = false;
|
|
if ($ensured) {
|
|
return;
|
|
}
|
|
|
|
$sql = <<<'SQL'
|
|
CREATE TABLE IF NOT EXISTS fps_matches (
|
|
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
|
player_name VARCHAR(80) NOT NULL,
|
|
weapon_key VARCHAR(40) NOT NULL,
|
|
weapon_name VARCHAR(80) NOT NULL,
|
|
kills INT UNSIGNED NOT NULL DEFAULT 0,
|
|
shots_fired INT UNSIGNED NOT NULL DEFAULT 0,
|
|
shots_hit INT UNSIGNED NOT NULL DEFAULT 0,
|
|
accuracy DECIMAL(5,2) NOT NULL DEFAULT 0.00,
|
|
damage_taken INT UNSIGNED NOT NULL DEFAULT 0,
|
|
duration_seconds INT UNSIGNED NOT NULL DEFAULT 0,
|
|
score INT UNSIGNED NOT NULL DEFAULT 0,
|
|
outcome VARCHAR(16) NOT NULL DEFAULT 'defeat',
|
|
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
INDEX idx_created_at (created_at),
|
|
INDEX idx_score (score)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
|
|
SQL;
|
|
|
|
db()->exec($sql);
|
|
$ensured = true;
|
|
}
|
|
|
|
function fetchRecentMatches(int $limit = 8): array
|
|
{
|
|
ensureFpsMatchesTable();
|
|
$limit = max(1, min($limit, 20));
|
|
$stmt = db()->prepare(
|
|
'SELECT id, player_name, weapon_key, weapon_name, kills, shots_fired, shots_hit, accuracy, damage_taken, duration_seconds, score, outcome, created_at
|
|
FROM fps_matches
|
|
ORDER BY created_at DESC, id DESC
|
|
LIMIT :limit'
|
|
);
|
|
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
|
|
$stmt->execute();
|
|
|
|
return $stmt->fetchAll();
|
|
}
|
|
|
|
function fetchMatchById(int $id): ?array
|
|
{
|
|
ensureFpsMatchesTable();
|
|
$stmt = db()->prepare(
|
|
'SELECT id, player_name, weapon_key, weapon_name, kills, shots_fired, shots_hit, accuracy, damage_taken, duration_seconds, score, outcome, created_at
|
|
FROM fps_matches
|
|
WHERE id = :id
|
|
LIMIT 1'
|
|
);
|
|
$stmt->bindValue(':id', $id, PDO::PARAM_INT);
|
|
$stmt->execute();
|
|
$row = $stmt->fetch();
|
|
|
|
return $row ?: null;
|
|
}
|