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, ]);