86 lines
2.8 KiB
PHP
86 lines
2.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Player activity log
|
|
*
|
|
* Public POST: { action:'track', playerKey, event, displayName?, loginType?, page?, meta? }
|
|
* Admin GET : ?action=summary|events|players
|
|
* Admin DELETE: ล้าง log ทั้งหมด
|
|
*/
|
|
require __DIR__ . '/_common.php';
|
|
require __DIR__ . '/_player_log.php';
|
|
|
|
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
|
|
|
|
if ($method === 'POST') {
|
|
$body = require_json_body();
|
|
$action = (string) ($body['action'] ?? 'track');
|
|
if ($action !== 'track') {
|
|
json_response(['ok' => false, 'error' => 'unknown action'], 400);
|
|
}
|
|
|
|
$playerKey = trim((string) ($body['playerKey'] ?? ''));
|
|
$event = trim((string) ($body['event'] ?? ''));
|
|
$displayName = trim((string) ($body['displayName'] ?? ''));
|
|
$loginType = trim((string) ($body['loginType'] ?? 'guest'));
|
|
$page = trim((string) ($body['page'] ?? ''));
|
|
$meta = isset($body['meta']) && is_array($body['meta']) ? $body['meta'] : [];
|
|
|
|
$result = player_log_track($playerKey, $event, $displayName, $loginType, $page, $meta);
|
|
if (empty($result['ok'])) {
|
|
json_response($result, 400);
|
|
}
|
|
json_response($result);
|
|
}
|
|
|
|
if ($method === 'GET') {
|
|
require_login();
|
|
$action = (string) ($_GET['action'] ?? 'summary');
|
|
$log = player_log_read();
|
|
|
|
if ($action === 'summary') {
|
|
json_response([
|
|
'ok' => true,
|
|
'summary' => player_log_public_summary($log),
|
|
'eventTypes' => array_keys(player_log_allowed_events()),
|
|
]);
|
|
}
|
|
|
|
if ($action === 'players') {
|
|
$search = trim((string) ($_GET['search'] ?? ''));
|
|
$limit = (int) ($_GET['limit'] ?? 200);
|
|
json_response([
|
|
'ok' => true,
|
|
'players' => player_log_players_list($log, $search, $limit),
|
|
]);
|
|
}
|
|
|
|
if ($action === 'events') {
|
|
$result = player_log_events_list($log, [
|
|
'search' => (string) ($_GET['search'] ?? ''),
|
|
'event' => (string) ($_GET['event'] ?? ''),
|
|
'playerKey' => (string) ($_GET['playerKey'] ?? ''),
|
|
'limit' => (int) ($_GET['limit'] ?? 100),
|
|
'offset' => (int) ($_GET['offset'] ?? 0),
|
|
]);
|
|
json_response([
|
|
'ok' => true,
|
|
'events' => $result['rows'],
|
|
'total' => $result['total'],
|
|
]);
|
|
}
|
|
|
|
json_response(['ok' => false, 'error' => 'unknown action'], 400);
|
|
}
|
|
|
|
if ($method === 'DELETE') {
|
|
require_login();
|
|
if (!player_log_write(player_log_default())) {
|
|
json_response(['ok' => false, 'error' => 'ล้าง log ไม่สำเร็จ'], 500);
|
|
}
|
|
json_response(['ok' => true]);
|
|
}
|
|
|
|
json_response(['ok' => false, 'error' => 'Use GET, POST or DELETE'], 405);
|