update eetc
This commit is contained in:
@@ -228,6 +228,43 @@ if ($action === 'progress' && $method === 'POST') {
|
||||
json_response(['ok' => true, 'id' => $id, 'value' => $val, 'unlocked' => $val >= $target]);
|
||||
}
|
||||
|
||||
/* streak — สำหรับ achievement แบบ "ติดต่อกัน" (a6/d4/e1): event 'hit' นับต่อ, 'miss' รีเซ็ต 0.
|
||||
* เก็บ current streak แยกใน accounts[].achvStreak {id:count} · achievement = best streak ที่เคยถึง (ไม่ลดลงเวลารีเซ็ต) */
|
||||
if ($action === 'streak' && $method === 'POST') {
|
||||
$secretFile = ADMIN_PRIVATE_DIR . '/game-award-secret.txt';
|
||||
$expected = is_file($secretFile) ? trim((string) @file_get_contents($secretFile)) : '';
|
||||
$secret = (string) ($body['secret'] ?? '');
|
||||
if ($expected === '' || strlen($secret) < 16 || !hash_equals($expected, $secret)) {
|
||||
json_response(['ok' => false, 'error' => 'unauthorized'], 403);
|
||||
}
|
||||
$key = trim((string) ($body['playerKey'] ?? ''));
|
||||
$id = preg_replace('/[^a-zA-Z0-9_]/', '', (string) ($body['id'] ?? ''));
|
||||
$event = (string) ($body['event'] ?? '');
|
||||
if (!achv_valid_key($key) || $id === '' || ($event !== 'hit' && $event !== 'miss')) {
|
||||
json_response(['ok' => false, 'error' => 'bad params'], 400);
|
||||
}
|
||||
$catalog = achv_load_catalog();
|
||||
$target = 0;
|
||||
foreach ($catalog as $c) { if ($c['id'] === $id) { $target = (int) $c['target']; break; } }
|
||||
if ($target <= 0) json_response(['ok' => false, 'error' => 'unknown id'], 404);
|
||||
|
||||
$store = read_store();
|
||||
$i = achv_find_or_create($store, $key);
|
||||
$acct = $store['accounts'][$i];
|
||||
$streaks = (isset($acct['achvStreak']) && is_array($acct['achvStreak'])) ? $acct['achvStreak'] : [];
|
||||
$sc = max(0, (int) ($streaks[$id] ?? 0));
|
||||
$sc = ($event === 'hit') ? ($sc + 1) : 0;
|
||||
$streaks[$id] = $sc;
|
||||
$cur = achv_progress_of($acct);
|
||||
$best = max(0, min($target, max((int) ($cur[$id] ?? 0), $sc))); /* achievement = best ไม่ลดลง */
|
||||
$cur[$id] = $best;
|
||||
$store['accounts'][$i]['achievements'] = (object) $cur;
|
||||
$store['accounts'][$i]['achvStreak'] = (object) $streaks;
|
||||
$store['accounts'][$i]['updatedAt'] = gmdate('c');
|
||||
if (!write_store($store)) json_response(['ok' => false, 'error' => 'save failed'], 500);
|
||||
json_response(['ok' => true, 'id' => $id, 'streak' => $sc, 'value' => $best, 'unlocked' => $best >= $target]);
|
||||
}
|
||||
|
||||
/* ---------- Admin ---------- */
|
||||
require_login();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user