new selling price generation algorytm

This commit is contained in:
lopar 2019-01-14 22:28:47 +02:00
parent c6f86632ef
commit 0bd9308d01
2 changed files with 63 additions and 55 deletions

View File

@ -20,10 +20,15 @@ class ShopItem extends Item
/**
* Для кнопок управления под картинкой прелмета в зависимости от ситуации.
*/
public function printControls()
public function printControls($sell = false)
{
$btnPrice = "Купить за " . intval($this->cost) . " кр.";
$btnLink = "/shop.php?buy={$this->id}&rnd=".mt_rand();
if ($sell) {
$btnPrice = "Продать";
$btnLink = "/shop.php?sell={$this->id}&rnd=".mt_rand();
} else {
$btnPrice = "Купить за " . intval($this->cost) . " кр.";
$btnLink = "/shop.php?buy={$this->id}&rnd=" . mt_rand();
}
echo <<<BTN
<p><input type="button" style="background: darkgrey; border: 1px solid grey; border-radius: 2px;" value="{$btnPrice}"
onclick="location='{$btnLink}'">

107
shop.php
View File

@ -13,35 +13,63 @@ if ($user['battle'] != 0) {
die();
}
$sellItemId = filter_input(INPUT_GET, 'sellitem');
$saleItems = false;
if ($sellItemId && is_numeric($sellItemId)) {
$dress = db::c()->query('SELECT `id`, `name`, `cost`, `type`, `duration`, `maxdur`, `goden`, `dategoden`, `koll` FROM `inventory` WHERE `dressed` = 0 AND `id` = ?i AND `owner` = ?i', $sellItemId, $user['id'])->fetch_assoc();
$price = $dress['cost'] * 1;
$kols = '';
if ($dress['koll']) {
$price *= $dress['koll'];
$kols = '(x' . $dress['koll'] . ')';
if (false == $saleItems) {
$shopItems = db::c()->query('SELECT `shop`.*,
`magic`.`name` AS `magic_name`,
`magic`.`chanse` AS `magic_chanse`,
`magic`.`time` AS `magic_time`,
`magic`.`file` AS `magic_file`,
`magic`.`targeted` AS `magic_targeted`,
`magic`.`needcharge` AS `magic_needcharge`,
`magic`.`img` AS `magic_img`
FROM `shop` LEFT JOIN `magic` ON `magic` = `magic`.`id` WHERE `count` > 0 AND `cost` > 0 AND `razdel` = ?i ORDER by `nlevel` ASC, `id` ASC', $shopCategoryTypeNumber);
$iteminfo = [];
while ($row = $shopItems->fetch_assoc()) {
$iteminfo[] = new ShopItem($row);
}
destructitem($dress['id']);
if ($dress['type'] == 12) {
$allcost = round($price * (1 - $dress['duration'] / $dress['maxdur']), 2);
} else {
$allcost = round($price - $dress['duration'] * ($dress['cost'] / ($dress['maxdur'] * 10)), 2);
} else {
$inventoryItems = db::c()->query('SELECT `inventory`.*,
`magic`.`name` AS `magic_name`,
`magic`.`chanse` AS `magic_chanse`,
`magic`.`time` AS `magic_time`,
`magic`.`file` AS `magic_file`,
`magic`.`targeted` AS `magic_targeted`,
`magic`.`needcharge` AS `magic_needcharge`,
`magic`.`img` AS `magic_img`
FROM `inventory` LEFT JOIN `magic` ON `magic` = `magic`.`id` WHERE `dressed` = 0 AND `setsale` = 0 AND `cost` > 0 AND `present` = "?n" AND `tradesale` = 0 AND `podzem` = 0 AND `goden` = 0 AND `owner` = ?i ORDER BY `update` DESC', $_SESSION['uid'], null);
$iteminfo = [];
while ($row = $inventoryItems->fetch_assoc()) {
$iteminfo[] = new ShopItem($row);
}
if ($dress['goden'] != 0) {
$goden_max = $dress['goden'] * 24 * 60;
$goden_left = ($dress['dategoden'] - time()) / 60;
$per = $goden_left / $goden_max;
$allcost = round(($allcost * $per), 2);
}
$sellItemId = filter_input(INPUT_GET, 'sell');
if ($sellItemId) {
$dress = db::c()->query('SELECT `id`, `name`, `cost`, `koll` FROM `inventory` WHERE `dressed` = 0 AND `id` = ?i AND `owner` = ?i', $sellItemId, $user['id'])->fetch_assoc();
$kols = '';
if ($dress['koll']) {
$dress['cost'] *= $dress['koll'];
$kols = " (x{$dress['koll']}) ";
}
$user['money'] += $allcost;
$deloText = "\"" . $user['login'] . "\" продал в магазин товар : \"" . $dress['name'] . "\" $kols id:(cap" . $dress['id'] . ") [" . $dress['duration'] . "/" . $dress['maxdur'] . "] за " . $allcost . " кр. ";
db::c()->query('UPDATE `users` SET `money` = ?i WHERE `id` = ?i', $user['money'], $user['id']);
db::c()->query('INSERT INTO `delo` (`pers`, `text`, `type`, `date`) VALUES (?i, "?s", 1, ?i)', $user['id'], $deloText, time());
$status = "Вы продали \"{$dress['name']}\" $kols за " . $allcost . " кр.";
$allcost = mt_rand(0,$dress['cost']/2);
#destructitem($dress['id']);
db::c()->query('DELETE FROM `inventory` WHERE `id` = ?i', $dress['id']);
db::c()->query('UPDATE `users` SET `money` = ?i WHERE `id` = ?i', $allcost, $_SESSION['uid']);
$status = "Вы продали «{$dress['name']}» $kols за " . $allcost . " кр.";
$deloText = "{$user['login']} продал товар «{$dress['name']}»{$kols}id:({$dress['id']}) в магазине за {$allcost} кр.";
addToDelo($deloText);
}
if (!empty($_GET['buy'])) {
@ -51,7 +79,6 @@ if (!empty($_GET['buy'])) {
if (($dress['massa'] + $itemWeight['m']) > (get_meshok())) $status = 'Недостаточно места в рюкзаке.';
elseif ($user['money'] >= $dress['cost'] AND ($dress['count'] > 0 OR $dress['count'] == -1)) {
$newItem = [
'prototype' => $dress['id'],
'owner' => $user['id'],
@ -177,24 +204,6 @@ switch ($shopCategoryTypeNumber) {
$shopCategoryType = "Руны";
break;
}
if (false == $saleItems) {
$shopItems = db::c()->query('SELECT `shop`.*,
`magic`.`name` AS `magic_name`,
`magic`.`chanse` AS `magic_chanse`,
`magic`.`time` AS `magic_time`,
`magic`.`file` AS `magic_file`,
`magic`.`targeted` AS `magic_targeted`,
`magic`.`needcharge` AS `magic_needcharge`,
`magic`.`img` AS `magic_img`
FROM `shop` LEFT JOIN `magic` ON `magic` = `magic`.`id` WHERE `count` > 0 AND `cost` > 0 AND `razdel` = ?i ORDER by `nlevel` ASC, `id` ASC', $shopCategoryTypeNumber);
$iteminfo = [];
while ($row = $shopItems->fetch_assoc()) {
$iteminfo[] = new ShopItem($row);
}
}
?>
<!DOCTYPE html>
<html>
@ -232,7 +241,7 @@ FROM `shop` LEFT JOIN `magic` ON `magic` = `magic`.`id` WHERE `count` > 0 AND `c
<TD align=center>
<h3><?= $shopCategoryType ?></h3>
<?php if ($saleItems): ?>
Вы можете продать ваши предметы за 10% от стоимости.
Вы можете продать ваши предметы за сущие копейки.
<?php endif; ?>
</TD>
</TR>
@ -242,9 +251,8 @@ FROM `shop` LEFT JOIN `magic` ON `magic` = `magic`.`id` WHERE `count` > 0 AND `c
<?php
if ($saleItems == true) {
// Выбираем всё потому что эту выборку потом использует showitem();
$data = db::c()->query('SELECT * FROM `inventory` WHERE `owner` = ?i AND `dressed` = 0 AND `setsale` = 0 AND `cost` > 0 AND `present` = "?s" AND `tradesale` = 0 AND `podzem` = 0 ORDER BY `update` DESC', $user['id'], null);
$data = db::c()->query('SELECT * FROM `inventory` WHERE `owner` = ?i AND `dressed` = 0 AND `setsale` = 0 AND `cost` > 0 AND `present` = "?n" AND `tradesale` = 0 AND `podzem` = 0 AND `goden` = 0 ORDER BY `update` DESC', $user['id'], null);
while ($row = $data->fetch_assoc()) {
$row['count'] = 1;
$price = $row['cost'] * 0.1;
if ($row['koll']) {
$price *= $row['koll'];
@ -254,17 +262,12 @@ FROM `shop` LEFT JOIN `magic` ON `magic` = `magic`.`id` WHERE `count` > 0 AND `c
} else {
$allcost = round($price - $row['duration'] * ($row['cost'] / ($row['maxdur'] * 10)), 2);
}
if ($row['goden'] != 0) {
$goden_max = $row['goden'] * 24 * 60;
$goden_left = ($row['dategoden'] - time()) / 60;
$per = $goden_left / $goden_max;
$allcost = round(($allcost * $per), 2);
}
?>
<tr>
<td align=center style='width: 150px;'>
<img src="i/sh/<?= $row['img'] ?>" border=0>
<br><a href="shop.php?sellitem=<?= $row['id'] ?>&sid=&sale=1">продать
<br><a href="shop.php?sell=<?= $row['id'] ?>&sid=&sale=1">продать
за <?= $allcost ?></a>
</td>
<td valign=top class="row">