Ремонт лотореи. Централизованная работа через БД. Дополнительная защита от ложных срабатываний.

This commit is contained in:
2023-06-08 21:33:56 +03:00
parent 02c76e76ad
commit cf2aea847a
9 changed files with 477 additions and 780 deletions
+2 -1
View File
@@ -26,7 +26,8 @@ class Config
$c['thiscity'] = 'capitalcity';
$c['capitalcity'] = $c['host'];
$c['abandonedplain'] = $c['host'];
$c['https'] = '//' . $c['host'] . DIRECTORY_SEPARATOR;
$c['https'] = DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR . $c['host'] . DIRECTORY_SEPARATOR;
$c['img2'] = DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR . 'img.' . $c['host'];
$c['exit'] = '<script>top.location.href="//' . $c['host'] . '/";</script>';
$c['support'] = 'support@' . $c['host'];
+15 -1
View File
@@ -2,13 +2,27 @@
use Core\Db;
/** Ïðåäìåòû êîòîðûå íèêîìó íå ïðèíàäëåæàò. Íå ïåðåïóòàé! */
/** Предметы которые никому не принадлежат. Не перепутай! */
class ItemModel
{
private $item;
public function __construct(int $id)
{
$this->item = Db::getRow('select * from items_main where id = ?', [$id]);
}
public static function getItemData(int $id): string
{
return Db::getValue('select data from items_main_data where items_id = ?', [$id]);
}
public function getName()
{
return $this->item['name'];
}
public function getImage()
{
return $this->item['img'];
}
}
+148
View File
@@ -0,0 +1,148 @@
<?php
namespace Location;
use Chat;
use ChatMessage;
use Core\Config;
use Core\Db;
use ItemModel;
use Model\ActionModel;
use User;
use User\ItemsModel;
class Loto
{
private array $prizes = [];
private const PRIZE_IMAGE = 0;
private const PRIZE_NAME = 1;
private const PRIZE_RARITY = 2;
private const PRIZE_ROW_ID = 3;
private const PRIZE_ITEM_ID = 4;
public function __construct()
{
$loto = Db::getRows('select * from loto order by id');
foreach ($loto as $prize) {
$item = new ItemModel($prize['idgame']);
$this->prizes[] = [
$item->getImage(),
$item->getName(),
ltrim($prize['type'], $prize['type'][0]),
$prize['id'],
$prize['idgame'],
];
}
}
public function getPrizeListByRarity(): string
{
$prizelist = '';
$rarity = [1 => 'Частое', 'Нормальное', 'Нечастое', 'Редкое', 'Очень редкое', 'Невероятно редкое',];
$prizelistSortedByRarity = $this->prizes;
usort($prizelistSortedByRarity, fn($a, $b) => $a[self::PRIZE_RARITY] <=> $b[self::PRIZE_RARITY]);
foreach ($prizelistSortedByRarity as $prize) {
$prizelist .= '<tr>';
$prizelist .= '<td><img src="' . Config::get('img2') .
'/i/items/' . $prize[self::PRIZE_IMAGE] . '" alt="' . $prize[self::PRIZE_NAME] . '"></td>';
$prizelist .= '<td>' . $prize[self::PRIZE_NAME] . '</td>';
$prizelist .= '<td class="chanceitem">' . $rarity[$prize[self::PRIZE_RARITY]] . ' выпадение</td>';
$prizelist .= '</tr>';
}
return $prizelist;
}
public function getPrizes(): array
{
return $this->prizes;
}
public function roll(): array
{
$result = [];
$prizes = $this->prizes;
shuffle($prizes);
$d100 = rand(1, 1000);
if ($d100 <= 5) {
$itemTier = 6;
} elseif ($d100 <= 10) {
$itemTier = 5;
} elseif ($d100 <= 30) {
$itemTier = 4;
} elseif ($d100 <= 70) {
$itemTier = 3;
} elseif ($d100 <= 150) {
$itemTier = 2;
} else {
$itemTier = 1;
}
foreach ($prizes as $prize) {
if (intval($prize[self::PRIZE_RARITY]) === $itemTier) {
$result = $prize;
break;
}
}
return [
'row_id' => $result[self::PRIZE_ROW_ID],
'item_id' => $result[self::PRIZE_ITEM_ID],
'name' => $result[self::PRIZE_NAME],
];
}
public static function freeRoll(string $uidCheck): void
{
$user = User::start()->info;
$am = new ActionModel($user['id']);
$titm = $am->getLastByValsAndTime('loto', 24 * 60 * 60);
if (intval($uidCheck) != $user['id']) {
echo 'No user!';
exit;
}
if ($titm) {
echo 'Wait asign!';
exit;
}
$loto = new Loto();
$prize = $loto->roll();
ItemsModel::addItem($prize['item_id'], $user['id'], '|frompisher=1|nosale=1' . $user['login']);
$cmsg = new ChatMessage();
$cmsg->setText("<b>{$user['login']}</b> выиграл в бесплатной рулетке <b>{$prize['name']}</b>!");
$cmsg->setType(6);
(new Chat())->sendMsg($cmsg);
ActionModel::new($user, 'loto', $prize['item_id']);
echo $prize['row_id'];
exit;
}
public static function paidRoll(string $uidCheck): void
{
$u = User::start();
if ($uidCheck == 'ekr') {
echo $u->info['money2'];
exit;
}
if (intval($uidCheck) != $u->info['id']) {
echo "No user!";
exit;
}
if ($u->info['money2'] < 5) {
echo "No Ekr!";
exit;
}
$loto = new Loto();
$prize = $loto->roll();
ItemsModel::addItem($prize['item_id'], $u->info['id'], '|frompisher=1|nosale=1' . $u->info['login']);
$u->info['money2'] = $u->info['money2'] - 5;
$u->addEkr(-5);
$cmsg = new ChatMessage();
$cmsg->setText("<b>{$u->info['login']}</b> выиграл в платной рулетке <b>{$prize['name']}</b>!");
$cmsg->setType(6);
(new Chat())->sendMsg($cmsg);
echo $prize['row_id'];
exit;
}
}
+63
View File
@@ -0,0 +1,63 @@
<?php
namespace Model;
use Core\Db;
use User\UserIp;
class ActionModel
{
private int $uid;
public function __construct(int $uid)
{
$this->uid = $uid;
}
public function getByVals(string $vals)
{
return Db::getRow('select * from actions where uid = ? and vals = ?', [$this->uid, $vals]);
}
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)
{
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)
{
Db::sql('delete from actions where uid = ? and vals = ?', [$this->uid, $vals]);
}
public static function new(array $user, string $vals, string $vars)
{
Db::sql(
'insert into actions (uid, time, city, room, vars, ip, vals, val) values (?,unix_timestamp(),?,?,?,?,?,?)',
[
$user['id'],
$user['city'],
$user['room'],
$vars,
UserIp::get(),
$vals,
'',
]
);
}
public static function getAll(): array
{
return Db::getRows('select * from actions');
}
}
+1 -1
View File
@@ -107,7 +107,7 @@ class ItemsModel
$room = $user->info['city'];
}
$args = [
$i['overTypei'],
$i['overTypei'] ?? 0,
$i['id'],
$uid,
$data,