Code smell.
This commit is contained in:
@@ -3,11 +3,14 @@
|
||||
|
||||
namespace Battles;
|
||||
|
||||
use SQLite3;
|
||||
use SQLite3Result;
|
||||
use Battles\Database\Db;
|
||||
|
||||
class GameLogs
|
||||
{
|
||||
private const BANK = 1;
|
||||
private const USER_ACTIONS = 2;
|
||||
private const BATTLE = 3;
|
||||
|
||||
/**
|
||||
* Добавление записи в лог банковских операций.
|
||||
* @param int $senderId отправитель средств.
|
||||
@@ -18,15 +21,16 @@ class GameLogs
|
||||
*/
|
||||
public static function addBankLog(int $senderId, int $receiverId, int $amount, string $type, string $text)
|
||||
{
|
||||
$db = new SQLite3(GameConfigs::DB_SQLITE);
|
||||
$row = $db->prepare("INSERT INTO bank_logs (sender_id, receiver_id, amount, type, text) VALUES (?, ?, ?, ?, ?)");
|
||||
$row->bindParam(1, $senderId, SQLITE3_INTEGER);
|
||||
$row->bindParam(2, $receiverId, SQLITE3_INTEGER);
|
||||
$row->bindParam(3, $amount, SQLITE3_INTEGER);
|
||||
$row->bindParam(4, $type, SQLITE3_TEXT);
|
||||
$row->bindParam(5, $text, SQLITE3_TEXT);
|
||||
$row->execute();
|
||||
$row->close();
|
||||
self::addLog(
|
||||
json_encode([
|
||||
'senderId' => $senderId,
|
||||
'receiverId' => $receiverId,
|
||||
'amount' => $amount,
|
||||
'type' => $type,
|
||||
'text' => $text,
|
||||
]),
|
||||
self::BANK,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,48 +48,61 @@ class GameLogs
|
||||
if (empty($type)) {
|
||||
$type = "system";
|
||||
}
|
||||
$db = new SQLite3(GameConfigs::DB_SQLITE);
|
||||
$row = $db->prepare("INSERT INTO users_logs (user_id, author_id, type, text) VALUES (?,?,?,?)");
|
||||
$row->bindParam(1, $userId, SQLITE3_INTEGER);
|
||||
$row->bindParam(2, $authorId, SQLITE3_INTEGER);
|
||||
$row->bindParam(3, $type, SQLITE3_TEXT);
|
||||
$row->bindParam(4, $text, SQLITE3_TEXT);
|
||||
$row->execute();
|
||||
$row->close();
|
||||
|
||||
self::addLog(
|
||||
json_encode([
|
||||
'userId' => $userId,
|
||||
'authotId' => $authorId,
|
||||
'type' => $type,
|
||||
'text' => $text,
|
||||
]),
|
||||
self::USER_ACTIONS,
|
||||
);
|
||||
}
|
||||
|
||||
public static function getUserLogs($userId = null, $type = null): SQLite3Result
|
||||
private static function addLog($jsonString, $logType)
|
||||
{
|
||||
$db = new SQLite3(GameConfigs::DB_SQLITE);
|
||||
$row = false;
|
||||
if ($userId && $type) {
|
||||
$query = "SELECT * FROM users_logs WHERE user_id = ? AND type = ?";
|
||||
$row = $db->prepare($query);
|
||||
$row->bindValue(1, $userId, SQLITE3_INTEGER);
|
||||
$row->bindValue(2, $type, SQLITE3_TEXT);
|
||||
} elseif ($userId && !$type) {
|
||||
$query = "SELECT * FROM users_logs WHERE user_id = ?";
|
||||
$row = $db->prepare($query);
|
||||
$row->bindValue(1, $userId, SQLITE3_INTEGER);
|
||||
} elseif (!$userId && $type) {
|
||||
$query = "SELECT * FROM users_logs WHERE type= ?";
|
||||
$row = $db->prepare($query);
|
||||
$row->bindValue(1, $type, SQLITE3_TEXT);
|
||||
} elseif (!$userId && !$type) {
|
||||
$query = "SELECT * FROM users_logs";
|
||||
$row = $db->prepare($query);
|
||||
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;
|
||||
}
|
||||
|
||||
return $row->execute();
|
||||
$result = [];
|
||||
foreach ($logs as $log) {
|
||||
if ($userId !== $log['userId']) {
|
||||
continue;
|
||||
}
|
||||
$result[] = $log;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function addBattleLog(int $battle_id, string $text)
|
||||
public static function addBattleLog(int $battleId, string $text)
|
||||
{
|
||||
$db = new SQLite3(__DIR__ . '../../Database/battle.logs.getInstance');
|
||||
$row = $db->prepare('insert into newbattles (battle_id, text) values (?,?)');
|
||||
$row->bindParam(1, $battle_id, SQLITE3_INTEGER);
|
||||
$row->bindParam(2, $text, SQLITE3_TEXT);
|
||||
$row->execute();
|
||||
$row->close();
|
||||
self::addLog(
|
||||
json_encode([
|
||||
'battleId' => $battleId,
|
||||
'text' => $text,
|
||||
]),
|
||||
self::BATTLE,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user