128 lines
3.5 KiB
PHP
128 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace Model;
|
|
|
|
use Core\Db;
|
|
use Exception;
|
|
use Helper\QueryBuilder;
|
|
use User\UserIp;
|
|
|
|
class ActionModel
|
|
{
|
|
private const TABLE_NAME = 'actions';
|
|
private int $uid;
|
|
|
|
public function __construct(int $uid)
|
|
{
|
|
$this->uid = $uid;
|
|
}
|
|
|
|
public static function new(array $user, string $vals, string $vars, int $time = 0): void
|
|
{
|
|
if (!$time) {
|
|
$time = time();
|
|
}
|
|
Db::sql(
|
|
'insert into actions (uid, time, city, room, vars, ip, vals, val) values (?,?,?,?,?,?,?,?)',
|
|
[
|
|
$user['id'],
|
|
$time,
|
|
'',
|
|
$user['room'],
|
|
$vars,
|
|
UserIp::get(),
|
|
$vals,
|
|
'',
|
|
]
|
|
);
|
|
}
|
|
|
|
public static function get(array $filters, string $columns = '*'): array
|
|
{
|
|
$query = new QueryBuilder(self::TABLE_NAME, $columns);
|
|
try {
|
|
$stmt = $query->select($filters, 1);
|
|
return Db::getRow($stmt['sql'], $stmt['binds']);
|
|
} catch (Exception $e) {
|
|
echo $e->getMessage();
|
|
trigger_error(__METHOD__ . ': ' . $e->getMessage(), E_USER_ERROR);
|
|
}
|
|
}
|
|
|
|
public static function getOne(array $filters, string $column): mixed
|
|
{
|
|
$result = self::get($filters, $column);
|
|
if (!empty($result)) {
|
|
return array_shift($result);
|
|
}
|
|
return '';
|
|
}
|
|
|
|
public static function deleteById(int $id): void
|
|
{
|
|
Db::sql('delete from actions where id = ?', [$id]);
|
|
}
|
|
|
|
public static function testCount(array $filters, int $limit = 0): int
|
|
{
|
|
$query = new QueryBuilder(self::TABLE_NAME, 'count(id)');
|
|
try {
|
|
$stmt = $query->select($filters, $limit);
|
|
return Db::getValue($stmt['sql'], $stmt['binds']);
|
|
} catch (Exception $e) {
|
|
echo $e->getMessage();
|
|
trigger_error(__METHOD__ . ': ' . $e->getMessage(), E_USER_ERROR);
|
|
}
|
|
}
|
|
|
|
public static function testCountCustom(string $filter, array $binds, int $limit = 0): int
|
|
{
|
|
$sql = "select count(id) from actions where $filter";
|
|
if ($limit > 0) {
|
|
$sql .= ' limit ' . $limit;
|
|
}
|
|
return (int)Db::getValue($sql, $binds);
|
|
}
|
|
|
|
public function getLastByVals(string $vals)
|
|
{
|
|
return Db::getRow(
|
|
'select * from actions where uid = ? and vals = ? order by time desc limit 1',
|
|
[$this->uid, $vals]
|
|
);
|
|
}
|
|
|
|
public function getLastByValsAndTime(string $vals, int $time)
|
|
{
|
|
$filter = [
|
|
"uid = $this->uid",
|
|
"vals = '$vals'",
|
|
"time > unix_timestamp() - $time",
|
|
];
|
|
return Db::getRow(
|
|
'select * from actions where uid = ? and vals = ? and time > unix_timestamp() - ? order by time desc limit 1',
|
|
[$this->uid, $vals, $time]
|
|
);
|
|
}
|
|
|
|
public function deleteByVals(string $vals): void
|
|
{
|
|
Db::sql('delete from actions where uid = ? and vals = ?', [$this->uid, $vals]);
|
|
}
|
|
|
|
public function getFinishedDailyQuestTasks(int $timeout): array
|
|
{
|
|
$arr = [];
|
|
$counter = Db::getRows(
|
|
"select count(*) as c, vars
|
|
from actions
|
|
where vars in ('end_trup', 'end_xaot', 'psh0', 'trup_sun', 'izlom', 'win') and time > ? and uid = ?
|
|
group by vars",
|
|
[$timeout, $this->uid]
|
|
);
|
|
foreach ($counter as $c) {
|
|
$arr[$c['vars']] = $c['c'];
|
|
}
|
|
return $arr;
|
|
}
|
|
} |