(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);