109 lines
3.1 KiB
PHP
109 lines
3.1 KiB
PHP
<?php
|
|
|
|
|
|
namespace Battles;
|
|
|
|
use Battles\Database\Db;
|
|
|
|
class GameLogs
|
|
{
|
|
private const BANK = 1;
|
|
private const USER_ACTIONS = 2;
|
|
private const BATTLE = 3;
|
|
|
|
/**
|
|
* Добавление записи в лог банковских операций.
|
|
* @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)
|
|
{
|
|
self::addLog(
|
|
json_encode([
|
|
'senderId' => $senderId,
|
|
'receiverId' => $receiverId,
|
|
'amount' => $amount,
|
|
'type' => $type,
|
|
'text' => $text,
|
|
]),
|
|
self::BANK,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Добавление записи в лог пользовательских операций (личное дело).
|
|
* @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";
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public static function getUserLogs(?int $userId): array
|
|
{
|
|
$logs = self::getLogByType(self::USER_ACTIONS);
|
|
if (is_null($userId)) {
|
|
return $logs;
|
|
}
|
|
|
|
$result = [];
|
|
foreach ($logs as $log) {
|
|
if ($userId !== $log['userId']) {
|
|
continue;
|
|
}
|
|
$result[] = $log;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
public static function addBattleLog(int $battleId, string $text)
|
|
{
|
|
self::addLog(
|
|
json_encode([
|
|
'battleId' => $battleId,
|
|
'text' => $text,
|
|
]),
|
|
self::BATTLE,
|
|
);
|
|
}
|
|
}
|