95 lines
3.4 KiB
PHP
95 lines
3.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* แอดมินเท่านั้น — จัดการกระดานผู้นำ (High Score)
|
|
* GET → รายชื่อบัญชีเรียงตามคะแนนสะสม (มาก→น้อย)
|
|
* POST { action: 'resetAll' } → ล้างคะแนนทุกคนเป็น 0
|
|
* POST { action: 'reset', id } → ล้างคะแนนคนเดียวเป็น 0
|
|
* (การตั้งคะแนนรายคน ใช้ accounts.php PATCH { id, score })
|
|
*/
|
|
require __DIR__ . '/_common.php';
|
|
require_login();
|
|
|
|
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
|
|
|
|
if ($method === 'GET') {
|
|
$store = read_store();
|
|
$rows = [];
|
|
foreach (($store['accounts'] ?? []) as $a) {
|
|
$score = max(0, (int) ($a['score'] ?? 0));
|
|
$name = trim((string) ($a['lbName'] ?? ''));
|
|
if ($name === '') $name = trim((string) ($a['displayName'] ?? ''));
|
|
if ($name === '') $name = 'ผู้เล่น';
|
|
$rows[] = [
|
|
'id' => (string) ($a['id'] ?? ''),
|
|
'name' => $name,
|
|
'score' => $score,
|
|
'coins' => max(0, (int) ($a['coins'] ?? 0)),
|
|
'blocked' => !empty($a['blocked']),
|
|
'loginType' => (string) ($a['loginType'] ?? ''),
|
|
'key' => (string) ($a['providerUserId'] ?? ''),
|
|
];
|
|
}
|
|
usort($rows, function ($x, $y) {
|
|
if ($y['score'] !== $x['score']) {
|
|
return $y['score'] - $x['score'];
|
|
}
|
|
return strcmp((string) $x['name'], (string) $y['name']);
|
|
});
|
|
foreach ($rows as $i => &$r) {
|
|
$r['rank'] = $i + 1;
|
|
}
|
|
unset($r);
|
|
json_response(['ok' => true, 'rows' => $rows, 'total' => count($rows)]);
|
|
}
|
|
|
|
if ($method === 'POST') {
|
|
$body = require_json_body();
|
|
$action = (string) ($body['action'] ?? '');
|
|
|
|
if ($action === 'resetAll') {
|
|
$store = read_store();
|
|
$n = 0;
|
|
foreach (($store['accounts'] ?? []) as $i => $a) {
|
|
if ((int) ($a['score'] ?? 0) !== 0) {
|
|
$store['accounts'][$i]['score'] = 0;
|
|
$store['accounts'][$i]['updatedAt'] = gmdate('c');
|
|
$n++;
|
|
}
|
|
}
|
|
if (!write_store($store)) {
|
|
json_response(['ok' => false, 'error' => 'บันทึกไม่สำเร็จ'], 500);
|
|
}
|
|
json_response(['ok' => true, 'reset' => $n]);
|
|
}
|
|
|
|
if ($action === 'reset') {
|
|
$id = trim((string) ($body['id'] ?? ''));
|
|
if ($id === '') {
|
|
json_response(['ok' => false, 'error' => 'ระบุ id'], 400);
|
|
}
|
|
$store = read_store();
|
|
$found = false;
|
|
foreach (($store['accounts'] ?? []) as $i => $a) {
|
|
if (($a['id'] ?? '') === $id) {
|
|
$store['accounts'][$i]['score'] = 0;
|
|
$store['accounts'][$i]['updatedAt'] = gmdate('c');
|
|
$found = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!$found) {
|
|
json_response(['ok' => false, 'error' => 'ไม่พบบัญชี'], 404);
|
|
}
|
|
if (!write_store($store)) {
|
|
json_response(['ok' => false, 'error' => 'บันทึกไม่สำเร็จ'], 500);
|
|
}
|
|
json_response(['ok' => true]);
|
|
}
|
|
|
|
json_response(['ok' => false, 'error' => 'action ไม่ถูกต้อง'], 400);
|
|
}
|
|
|
|
json_response(['ok' => false, 'error' => 'Use GET or POST'], 405);
|