2021-01-27 11:30:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace Battles;
|
2021-02-01 14:40:21 +00:00
|
|
|
|
2022-12-16 23:20:43 +00:00
|
|
|
use Battles\Database\Db;
|
2021-01-27 11:30:11 +00:00
|
|
|
|
|
|
|
class GameLogs
|
|
|
|
{
|
2022-12-16 23:20:43 +00:00
|
|
|
private const BANK = 1;
|
|
|
|
private const USER_ACTIONS = 2;
|
|
|
|
private const BATTLE = 3;
|
|
|
|
|
2021-01-27 11:30:11 +00:00
|
|
|
/**
|
|
|
|
* Добавление записи в лог банковских операций.
|
|
|
|
* @param int $senderId отправитель средств.
|
|
|
|
* @param int $receiverId получатель средств.
|
|
|
|
* @param int $amount сумма на счету после проведения операции.
|
|
|
|
* @param string $type тип операции.
|
|
|
|
* @param string $text комментарий.
|
|
|
|
*/
|
|
|
|
public static function addBankLog(int $senderId, int $receiverId, int $amount, string $type, string $text)
|
|
|
|
{
|
2022-12-16 23:20:43 +00:00
|
|
|
self::addLog(
|
|
|
|
json_encode([
|
|
|
|
'senderId' => $senderId,
|
|
|
|
'receiverId' => $receiverId,
|
|
|
|
'amount' => $amount,
|
|
|
|
'type' => $type,
|
|
|
|
'text' => $text,
|
|
|
|
]),
|
|
|
|
self::BANK,
|
|
|
|
);
|
2021-01-27 11:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Добавление записи в лог пользовательских операций (личное дело).
|
|
|
|
* @param int $userId кому добавляется запись.
|
|
|
|
* @param string $text текст записи.
|
|
|
|
* @param string $type тип записи.
|
|
|
|
* @param int $authorId кто добавляет запись. Использовать -1 для системной записи по умолчанию.
|
|
|
|
*/
|
|
|
|
public static function addUserLog(int $userId, string $text, string $type = '', int $authorId = 0)
|
|
|
|
{
|
|
|
|
if (empty($authorId)) {
|
|
|
|
$authorId = -1;
|
|
|
|
}
|
|
|
|
if (empty($type)) {
|
|
|
|
$type = "system";
|
|
|
|
}
|
2022-12-16 23:20:43 +00:00
|
|
|
|
|
|
|
self::addLog(
|
|
|
|
json_encode([
|
|
|
|
'userId' => $userId,
|
|
|
|
'authotId' => $authorId,
|
|
|
|
'type' => $type,
|
|
|
|
'text' => $text,
|
|
|
|
]),
|
|
|
|
self::USER_ACTIONS,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function addLog($jsonString, $logType)
|
|
|
|
{
|
|
|
|
Db::getInstance()->execute('insert into logs (json_data, type) VALUES (?,?)', [
|
|
|
|
$jsonString,
|
|
|
|
$logType
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function getLogByType(int $type): array
|
|
|
|
{
|
|
|
|
$result = [];
|
|
|
|
$logs = Db::getInstance()->fetchAll('select json_data from logs where type = ?', $type);
|
|
|
|
foreach ($logs as $log) {
|
|
|
|
$result[] = json_decode($log, true);
|
|
|
|
}
|
|
|
|
return $result;
|
2021-01-27 11:30:11 +00:00
|
|
|
}
|
2021-02-01 14:40:21 +00:00
|
|
|
|
2022-12-16 23:20:43 +00:00
|
|
|
public static function getUserLogs(?int $userId): array
|
2021-02-01 14:40:21 +00:00
|
|
|
{
|
2022-12-16 23:20:43 +00:00
|
|
|
$logs = self::getLogByType(self::USER_ACTIONS);
|
|
|
|
if (is_null($userId)) {
|
|
|
|
return $logs;
|
2021-02-01 14:40:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 23:20:43 +00:00
|
|
|
$result = [];
|
|
|
|
foreach ($logs as $log) {
|
|
|
|
if ($userId !== $log['userId']) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$result[] = $log;
|
|
|
|
}
|
|
|
|
return $result;
|
2021-02-01 14:40:21 +00:00
|
|
|
}
|
2021-08-26 14:44:14 +00:00
|
|
|
|
2022-12-16 23:20:43 +00:00
|
|
|
public static function addBattleLog(int $battleId, string $text)
|
2021-08-26 14:44:14 +00:00
|
|
|
{
|
2022-12-16 23:20:43 +00:00
|
|
|
self::addLog(
|
|
|
|
json_encode([
|
|
|
|
'battleId' => $battleId,
|
|
|
|
'text' => $text,
|
|
|
|
]),
|
|
|
|
self::BATTLE,
|
|
|
|
);
|
2021-08-26 14:44:14 +00:00
|
|
|
}
|
2022-12-16 23:20:43 +00:00
|
|
|
}
|