467363d651
Made-with: Cursor
62 lines
1.7 KiB
PHP
62 lines
1.7 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
/**
|
||
* สาธารณะ — ไม่ต้องล็อกอินแอดมิน
|
||
* ใช้ jdPlayerKey จากเบราว์เซอร์ คู่กับบัญชี guest (providerUserId = key)
|
||
* ถ้ายังไม่มีบัญชี → สร้าง guest ใหม่ coins = 0
|
||
*/
|
||
require __DIR__ . '/_common.php';
|
||
|
||
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
|
||
json_response(['ok' => false, 'error' => 'Use GET'], 405);
|
||
}
|
||
|
||
$key = trim((string)($_GET['playerKey'] ?? ''));
|
||
if (!preg_match('/^[a-zA-Z0-9_-]{8,128}$/', $key)) {
|
||
json_response(['ok' => false, 'error' => 'playerKey ต้องยาว 8–128 (a-z 0-9 _ -)'], 400);
|
||
}
|
||
|
||
$store = read_store();
|
||
$accounts = $store['accounts'] ?? [];
|
||
|
||
foreach ($accounts as $a) {
|
||
if (($a['loginType'] ?? '') !== 'guest') {
|
||
continue;
|
||
}
|
||
if (($a['providerUserId'] ?? '') !== $key) {
|
||
continue;
|
||
}
|
||
$coins = max(0, (int)($a['coins'] ?? 0));
|
||
json_response([
|
||
'ok' => true,
|
||
'coins' => $coins,
|
||
'accountId' => $a['id'] ?? null,
|
||
'blocked' => !empty($a['blocked']),
|
||
]);
|
||
}
|
||
|
||
$new = [
|
||
'id' => new_id(),
|
||
'email' => '',
|
||
'displayName' => 'Guest',
|
||
'loginType' => 'guest',
|
||
'providerUserId' => $key,
|
||
'notes' => 'auto: player-coins',
|
||
'blocked' => false,
|
||
'coins' => 0,
|
||
'createdAt' => gmdate('c'),
|
||
'updatedAt' => gmdate('c'),
|
||
];
|
||
$store['accounts'][] = $new;
|
||
if (!write_store($store)) {
|
||
json_response(['ok' => false, 'error' => 'สร้างบัญชีไม่สำเร็จ'], 500);
|
||
}
|
||
|
||
json_response([
|
||
'ok' => true,
|
||
'coins' => 0,
|
||
'accountId' => $new['id'],
|
||
'blocked' => false,
|
||
]);
|