Files
justice/www/html/Admin/api/_player_account.php
T
2026-07-31 03:36:58 +00:00

117 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
/**
* Shared helpers — lookup player account by jdPlayerKey (providerUserId).
* รองรับทั้ง guest และ google (และ loginType อื่นในอนาคต)
*/
function valid_player_key(string $key): bool
{
return (bool) preg_match('/^[a-zA-Z0-9_-]{8,128}$/', $key);
}
function find_account_index_by_player_key(array $accounts, string $key): int
{
foreach ($accounts as $i => $a) {
if (($a['providerUserId'] ?? '') === $key) {
return (int) $i;
}
}
return -1;
}
function find_account_by_player_key(array $accounts, string $key): ?array
{
$idx = find_account_index_by_player_key($accounts, $key);
return $idx >= 0 ? $accounts[$idx] : null;
}
function default_guest_account(string $key, string $notes = 'auto', array $extra = []): array
{
$now = gmdate('c');
return array_merge([
'id' => new_id(),
'email' => '',
'displayName' => 'Guest',
'loginType' => 'guest',
'providerUserId' => $key,
'notes' => $notes,
'blocked' => false,
'coins' => 0,
'createdAt' => $now,
'updatedAt' => $now,
], $extra);
}
function google_player_key(string $googleSub): string
{
$sub = trim($googleSub);
if ($sub === '') {
return '';
}
if (str_starts_with($sub, 'g_')) {
return $sub;
}
return 'g_' . $sub;
}
function merge_guest_account_into(array &$store, int $targetIdx, string $guestKey): void
{
if ($guestKey === '' || !valid_player_key($guestKey)) {
return;
}
$guestIdx = find_account_index_by_player_key($store['accounts'] ?? [], $guestKey);
if ($guestIdx < 0 || $guestIdx === $targetIdx) {
return;
}
$guest = $store['accounts'][$guestIdx];
if (($guest['loginType'] ?? '') !== 'guest') {
return;
}
$target = &$store['accounts'][$targetIdx];
$target['coins'] = max(0, (int) ($target['coins'] ?? 0)) + max(0, (int) ($guest['coins'] ?? 0));
$target['score'] = max(0, (int) ($target['score'] ?? 0)) + max(0, (int) ($guest['score'] ?? 0));
if (isset($guest['scoreByCase']) && is_array($guest['scoreByCase'])) {
if (!isset($target['scoreByCase']) || !is_array($target['scoreByCase'])) {
$target['scoreByCase'] = [];
}
foreach ($guest['scoreByCase'] as $cid => $sc) {
$target['scoreByCase'][$cid] = max(0, (int) ($target['scoreByCase'][$cid] ?? 0)) + max(0, (int) $sc);
}
}
if (isset($guest['achievements']) && is_array($guest['achievements'])) {
if (!isset($target['achievements']) || !is_array($target['achievements'])) {
$target['achievements'] = [];
}
foreach ($guest['achievements'] as $aid => $cnt) {
$target['achievements'][$aid] = max((int) ($target['achievements'][$aid] ?? 0), (int) $cnt);
}
}
if (isset($guest['daily']) && is_array($guest['daily']) && empty($target['daily'])) {
$target['daily'] = $guest['daily'];
}
if (!isset($target['lobbyColorThemeIndex']) && isset($guest['lobbyColorThemeIndex'])) {
$target['lobbyColorThemeIndex'] = $guest['lobbyColorThemeIndex'];
}
if (!isset($target['lobbySkinToneIndex']) && isset($guest['lobbySkinToneIndex'])) {
$target['lobbySkinToneIndex'] = $guest['lobbySkinToneIndex'];
}
if (($target['displayName'] ?? '') === '' || ($target['displayName'] ?? '') === 'Guest') {
$gn = trim((string) ($guest['displayName'] ?? ''));
if ($gn !== '' && $gn !== 'Guest') {
$target['displayName'] = $gn;
}
}
array_splice($store['accounts'], $guestIdx, 1);
if ($guestIdx < $targetIdx) {
$targetIdx--;
}
}