game/_incl_data/class/Location/Loto.php

149 lines
4.4 KiB
PHP

<?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;
}
}