156 lines
8.6 KiB
PHP
156 lines
8.6 KiB
PHP
<?php
|
||
|
||
use Battles\GameLogs;
|
||
use Battles\ShopItem;
|
||
use Battles\Template;
|
||
|
||
require_once 'functions.php';
|
||
$get = urldecode(filter_input(INPUT_SERVER, 'QUERY_STRING'));
|
||
$putItemCost = (int)filter_input(INPUT_POST, 'cost', FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
|
||
$putItemId = (int)filter_input(INPUT_POST, 'putId', FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
|
||
$returningItemId = (int)filter_input(INPUT_GET, 'back', FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
|
||
$byingItemId = (int)filter_input(INPUT_GET, 'set', FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
|
||
|
||
if ($putItemId) {
|
||
$query = 'select name from inventory where dressed_slot = 0 and owner_id = ? and item_id = ?';
|
||
\Battles\Database\DBPDO::INIT()->ofetch($query, [$_SESSION['uid'], $putItemId]);
|
||
$dress = db::c()->query('SELECT `name`,`duration`,`maxdur`,`cost` FROM `inventory` WHERE `dressed` = 0 AND `id` = ?i AND `owner` = ?i', $putItemId, $_SESSION['uid'])->fetch_assoc();
|
||
if (empty($putItemCost)) {
|
||
$putItemCost = $dress['cost'];
|
||
}
|
||
$commission = ceil($putItemCost / 10); # 10% от суммы с округлением вверх.
|
||
if ($user->getMoney() > $commission) {
|
||
|
||
if (db::c()->getAffectedRows()) {
|
||
$deloText = "{$user->getLogin()} выставил товар: «{$dress['name']}» id:({$putItemId}) [{$dress['duration']}/{$dress['maxdur']}] на продажу в комиссионку за {$putItemCost} кр. ";
|
||
GameLogs::addUserLog($user->getId(), $deloText);
|
||
db::c()->query('UPDATE `inventory` SET `setsale` = ?i WHERE `id` = ?i', $putItemCost, $putItemId);
|
||
db::c()->query('UPDATE `users` SET `money` = `money` - ?i WHERE `id` = ?i', $commission, $_SESSION['uid']);
|
||
$status = "Вы сдали в магазин «{$dress['name']}» за {$putItemCost} кр. Вычтено за услуги магазина: {$commission} кр.";
|
||
} else {
|
||
$status = "Предмет не найден в инвентаре!";
|
||
}
|
||
} else {
|
||
$status = "У вас не хватает " . $commission - $user->getMoney() . " кр. чтобы оплатить комиссию!";
|
||
}
|
||
}
|
||
|
||
if ($returningItemId) {
|
||
$dress = db::c()->query('SELECT `name`,`duration`,`maxdur` FROM `inventory` WHERE `dressed` = 0 AND `setsale` > 0 AND `id` = ?i AND `owner` = ?i', $returningItemId, $_SESSION['uid'])->fetch_assoc();
|
||
if (db::c()->getAffectedRows()) {
|
||
$deloText = "{$user->getLogin()} забрал свой товар: «{$dress['name']}» id:({$returningItemId}) [{$dress['duration']}/{$dress['maxdur']}] из комиссионки.";
|
||
GameLogs::addUserLog($user->getId(), $deloText);
|
||
db::c()->query('UPDATE `inventory` SET `setsale` = 0 WHERE `id` = ?i', $returningItemId);
|
||
$status = "Вы забрали из магазина ваш «{$dress['name']}».";
|
||
} else {
|
||
$status = "Предмет не найден на полках магазина!";
|
||
}
|
||
}
|
||
|
||
if ($byingItemId) {
|
||
$dress = db::c()->query('SELECT `name`,`owner`,`duration`,`maxdur`,`setsale` FROM `inventory` WHERE `dressed` = 0 AND `setsale` > 0 AND `id` = ?i', $byingItemId)->fetch_assoc();
|
||
$seller = db::c()->query('SELECT `login` FROM `users` WHERE `id` =?i', $dress['owner'])->fetch_assoc();
|
||
|
||
if ($dress['setsale']) {
|
||
if ($user->getMoney() >= $dress['setsale']) {
|
||
db::c()->query('UPDATE `inventory` SET `setsale` = 0, `owner` = ?i WHERE `id` = ?i', $user->getId(), $byingItemId);
|
||
db::c()->query('UPDATE `users` set `money` = `money` - ?i WHERE `id` = ?i', $dress['setsale'], $user->getId());
|
||
db::c()->query('UPDATE `users` set `money` = `money` + ?i WHERE `id` = ?i', $dress['setsale'], $dress['owner']);
|
||
$status = "Вы купили «{$dress['name']}» за {$dress['setsale']} кр.";
|
||
$deloText = "{$user->getLogin()} купил на рынке товар: «{$dress['name']}» id:({$byingItemId}) [{$dress['duration']}/{$dress['maxdur']}] у {$seller['login']} за {$dress['setsale']} кр.";
|
||
GameLogs::addUserLog($user->getId(), $deloText);
|
||
$deloText = "{$seller['login']} продал на рынке товар: «{$dress['name']}» id:({$byingItemId}) [{$dress['duration']}/{$dress['maxdur']}] персонажу {$user->getLogin()} за {$dress['setsale']} кр.";
|
||
GameLogs::addUserLog($dress['owner'], $deloText);
|
||
$user->setMoney($user->getMoney() - $dress['setsale']);
|
||
telegraph($dress['owner'], "Предмет «{$dress['name']}» продан на рынке за {$dress['setsale']} кр.");
|
||
} else {
|
||
$status = "Недостаточно средств!";
|
||
}
|
||
} else {
|
||
$status = "Предмет не найден на полках магазина!";
|
||
}
|
||
}
|
||
|
||
$classPrintControlName = "marketbuy";
|
||
|
||
if ($get === 'sale') {
|
||
$search = $_SESSION['uid'];
|
||
$query = 'select * from inventory where on_sale = 0 and dressed_slot = 0 and present is null and owner_id = ? order by name';
|
||
$classPrintControlName = "marketput";
|
||
} elseif ($get === 'unsale') {
|
||
$search = $_SESSION['uid'];
|
||
$query = 'select * from inventory where on_sale > 0 and dressed_slot = 0 and owner_id = ? order by name';
|
||
$classPrintControlName = "marketgetback";
|
||
} else if (!empty($_POST['search'])) {
|
||
$search = "%{$_POST['search']}%";
|
||
$query = 'select * from inventory where on_sale > 0 and dressed_slot = 0 and name like ? order by item_id';
|
||
} else {
|
||
$query = 'select * from inventory where on_sale > 0 and dressed_slot = 0 order by name';
|
||
}
|
||
|
||
if (isset($search)) {
|
||
$data = \Battles\Database\DBPDO::INIT()->ofetchAll($query, $search);
|
||
unset($search);
|
||
} else {
|
||
$data = \Battles\Database\DBPDO::INIT()->ofetchAll($query);
|
||
}
|
||
|
||
$iteminfo = [];
|
||
foreach ($data as $itemObject) {
|
||
$iteminfo[] = new ShopItem($itemObject, 'buymarket');
|
||
}
|
||
|
||
Template::header('Рынок');
|
||
?>
|
||
<script src="js/main.js"></script>
|
||
<h1>Рынок</h1>
|
||
<a href=# onclick=hrefToFrame('city.php?cp')> ← выйти на Центральную площадь</a>
|
||
<div><?php if (!empty($status)) err($status); ?></div>
|
||
<table width=100% cellspacing="0" cellpadding="4">
|
||
<tr>
|
||
<td valign=top align=left>
|
||
<table class="zebra" width=100%>
|
||
<tr>
|
||
<th>
|
||
<?php if ($get === 'sale'): ?>
|
||
Выставить товар на продажу.
|
||
<br>Комиссия за услуги магазина составляет 10% от цены, по которой вы предлагаете предмет.
|
||
<?php elseif ($get === 'unsale'): ?>
|
||
Забрать свой товар.
|
||
<?php else: ?>
|
||
Список товаров.<br>
|
||
<form method="post">
|
||
<input name="search"> <input type="submit" value="Искать товар">
|
||
</form>
|
||
<?php endif; ?>
|
||
<tr>
|
||
<td><!--Рюкзак-->
|
||
<table width=100%>
|
||
<?php
|
||
foreach ($iteminfo as $ii) {
|
||
echo "<tr><td style='width: 150px; text-align: center;'>";
|
||
$ii->printImage();
|
||
$ii->printControls($classPrintControlName);
|
||
echo "</td>";
|
||
echo "<td style='vertical-align: top;'>";
|
||
$ii->printInfo();
|
||
echo "</td></tr>";
|
||
}
|
||
?>
|
||
</table>
|
||
</table>
|
||
<TD valign=top width=280>
|
||
<div style="margin-left:15px; margin-top: 10px;">
|
||
<b>Масса всех ваших вещей: <?= getItemsMassaInfo() ?>
|
||
<br>У вас в наличии: <span style="color: darkgreen;"><?= $user['money'] ?></span> кр.</b>
|
||
<hr>
|
||
<br>
|
||
<button onclick="hrefToFrame('?show')">Осмотреть товары</button>
|
||
<br>
|
||
<button onclick="hrefToFrame('?sale')">Сдать вещи в магазин</button>
|
||
<br>
|
||
<button onclick="hrefToFrame('?unsale')">Забрать вещи из магазина</button>
|
||
<br>
|
||
<button onclick="hrefToFrame('city.php?cp=1')">Вернуться</button>
|
||
</div>
|
||
</table>
|