Repositories | Polyback | Log in to see private repositories


polyback-website / ac78c54296647694d1132210737d1561aac5f7d1 / my/trade.php

4,467 bytes | parent directory

<?php require $_SERVER['DOCUMENT_ROOT'].'/api/private/core.php';

// Making a trade offer to somebody. /my/trade?user=5

use pizzaboxer\ProjectPolygon\PageBuilder;
use pizzaboxer\ProjectPolygon\Trades;
use pizzaboxer\ProjectPolygon\Users;

Users::RequireLogin();
$Me = (int) SESSION["user"]["id"];
$Other = Users::GetInfoFromID((int) ($_GET["user"] ?? 0));
if (!$Other || (int) $Other->id === $Me) PageBuilder::instance()->errorCode(404);

$Error = false;
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
	if (!is_string($_POST["polygon_csrf"] ?? null) || !hash_equals((string) SESSION["csrfToken"], $_POST["polygon_csrf"])) die(http_response_code(403));
	$give = ["assets" => $_POST["give_assets"] ?? [], "items" => $_POST["give_items"] ?? [], "new" => $_POST["give_new"] ?? []];
	$get = ["assets" => $_POST["get_assets"] ?? [], "items" => $_POST["get_items"] ?? [], "new" => $_POST["get_new"] ?? []];
	$result = Trades::Send($Me, (int) $Other->id, $give, $get, $_POST["give_pizzas"] ?? 0, $_POST["get_pizzas"] ?? 0);
	if (is_int($result)) redirect("/my/trades");
	$Error = $result;
}

$Mine = Trades::Inventory($Me);
$Theirs = Trades::Inventory((int) $Other->id);
$csrf = '<input type="hidden" name="polygon_csrf" value="' . htmlspecialchars(SESSION["csrfToken"]) . '">';

// one side's checkboxes: $prefix is "give" or "get"
function things($inv, $prefix, $ownerLabel)
{
	$any = false;
	foreach ($inv->Assets as $a) { $any = true; ?>
		<div class="custom-control custom-checkbox"><input type="checkbox" class="custom-control-input" id="<?=$prefix?>a<?=(int) $a->OwnedID?>" name="<?=$prefix?>_assets[]" value="<?=(int) $a->OwnedID?>">
		<label class="custom-control-label" for="<?=$prefix?>a<?=(int) $a->OwnedID?>"><a href="/item?ID=<?=(int) $a->AssetID?>" target="_blank"><?=htmlspecialchars($a->Name)?></a></label></div>
	<?php }
	foreach ($inv->Passes as $p) { $any = true; ?>
		<div class="custom-control custom-checkbox"><input type="checkbox" class="custom-control-input" id="<?=$prefix?>i<?=(int) $p->ItemID?>" name="<?=$prefix?>_items[]" value="<?=(int) $p->ItemID?>">
		<label class="custom-control-label" for="<?=$prefix?>i<?=(int) $p->ItemID?>"><?=htmlspecialchars($p->Name)?> <small class="text-muted">pass of <?=htmlspecialchars($p->PlaceName)?></small></label></div>
	<?php }
	foreach ($inv->OwnPasses as $p) { $any = true; ?>
		<div class="custom-control custom-checkbox"><input type="checkbox" class="custom-control-input" id="<?=$prefix?>n<?=(int) $p->ProductID?>" name="<?=$prefix?>_new[]" value="<?=(int) $p->ProductID?>">
		<label class="custom-control-label" for="<?=$prefix?>n<?=(int) $p->ProductID?>">A new <?=htmlspecialchars($p->Name)?> <small class="text-muted">pass of <?=htmlspecialchars($ownerLabel)?> game <?=htmlspecialchars($p->PlaceName)?>: as many as wanted, never refunded</small></label></div>
	<?php }
	if (!$any) { ?><p class="text-muted">Nothing that can be traded.</p><?php }
}

$pageBuilder = new PageBuilder(["title" => "Trade with " . $Other->username]);
$pageBuilder->buildHeader();
?>
<h2 class="font-weight-normal">Trade with <a href="/user?ID=<?=(int) $Other->id?>"><?=htmlspecialchars($Other->username)?></a></h2>
<p>Pick what you give and what you'd like back (at most <?=Trades::MAX_THINGS?> things each). They decide; until then you can cancel it on <a href="/my/trades">your trades</a>. Nothing is taken as a fee.</p>
<?php if ($Error) { ?><div class="alert alert-danger px-2 py-1" role="alert"><?=htmlspecialchars($Error)?></div><?php } ?>
<form method="post">
	<?=$csrf?>
	<div class="row">
		<div class="col-md-6">
			<h4 class="font-weight-normal">You give</h4>
			<?php things($Mine, "give", "your"); ?>
			<div class="form-group mt-2"><label for="give_pizzas"><i class="fal fa-pizza-slice"></i> Pizzas:</label>
				<input class="form-control form-control-sm" id="give_pizzas" name="give_pizzas" type="number" min="0" max="<?=Trades::MAX_PIZZAS?>" value="0" style="max-width:10rem"></div>
		</div>
		<div class="col-md-6">
			<h4 class="font-weight-normal">You get</h4>
			<?php things($Theirs, "get", "their"); ?>
			<div class="form-group mt-2"><label for="get_pizzas"><i class="fal fa-pizza-slice"></i> Pizzas:</label>
				<input class="form-control form-control-sm" id="get_pizzas" name="get_pizzas" type="number" min="0" max="<?=Trades::MAX_PIZZAS?>" value="0" style="max-width:10rem"></div>
		</div>
	</div>
	<button class="btn btn-primary">Send trade</button>
</form>
<?php $pageBuilder->buildFooter(); ?>