Skip to content

Commit

Permalink
add playerprofile portraits
Browse files Browse the repository at this point in the history
  • Loading branch information
HerrErde committed Sep 8, 2024
1 parent 8515cef commit b3335e4
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 1 deletion.
138 changes: 138 additions & 0 deletions src/generator/portraits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
$errors = [];

if (!empty($errors)) {
$_SESSION["error"] = implode("<br>", $errors);
header("Location: ../page/error.php");
exit();
}

function decompress($compressed)
{
$decompressed = [];

foreach ($compressed as $item) {
if (strpos($item, '-') !== false) {
list($start, $end) = array_map('intval', explode('-', $item));
$decompressed = array_merge($decompressed, range($start, $end));
} else {
$decompressed[] = intval($item);
}
}

return $decompressed;
}

function getItemId($itemNumber, $jsonArray)
{
foreach ($jsonArray as $index => $item) {
if ($index + 1 == $itemNumber) { // Match by index (1-based)
return [
'id' => $item,
'number' => $itemNumber
];
}
}
return null; // Return null if item number not found
}

$json_url = 'https://github.com/HerrErde/subway-source/releases/latest/download/playerprofile_data.json';
$json_data = file_get_contents($json_url);
$item_data = json_decode($json_data, true);

$profilePortraits = $item_data['profilePortraits'];

$selectNumber = isset($_GET['select']) ? $_GET['select'] : null;

$itemsParam = isset($_GET['items']) ? $_GET['items'] : '';
$items = [];

if (preg_match_all('/(\d+-\d+|\d+)/', $itemsParam, $matches)) {
$items = $matches[0];
}

$decompressedItems = decompress($items);

$itemIds = [];

$logScript = '<script>';
foreach ($decompressedItems as $item) {
$item = getItemId($item, $profilePortraits);
if ($item !== null) {
$itemIds[] = $item['id'];
$logScript .= 'console.log("Item: ' . $item['number'] . ', ID: ' . $item['id'] . '");';
}
}

// Fetch the item ID based on the provided number
$selectedItemId = null;
if ($selectNumber !== null) {
$item = getItemId($selectNumber, $profilePortraits);
if ($item !== null) {
$selectedItemId = $item['id'];
$logScript .= 'console.log("Select number: ' . $selectNumber . ', Selected ID: ' . $selectedItemId . '");';
}
}

$logScript .= '</script>';

$datalist = [];

foreach ($decompressedItems as $item) {
$item = getItemId($item, $profilePortraits);
if ($item !== null) {
$datalist[$item['id']] = [
"id" => $item['id'],
"isSeen" => True
];
}
}

$mainJsonObject = [
"version" => 1,
"data" => "{\"selected\":{\"$selectedItemId\"},\"owned\":" . json_encode($datalist) . "}",
];

$textareaContent = json_encode($mainJsonObject);
?>

<!DOCTYPE html>
<html lang="en">

<head>
<title>Code for the characters_inventory.json file</title>
<?php
$activePage = basename(__FILE__, '.php');
require "../require/connect.php";
?>
<script src="/assets/js/download.js"></script>
<script>
var filename = 'characters_inventory.json';
</script>
<?= $logScript ?>
</head>

<body>
<header>
<h1>Code for your Character Inventory</h1>
<p id="title">
Download or copy the generated code, find the file characters_inventory.json in the
folder "profile" and paste it there.
</p>
<p id="warning">
Note that this may restart some statistics and you're using it at your
own risk.
</p>
</header>

<textarea name="textarea" rows="35" cols="60" readonly><?= $textareaContent ?></textarea>

<?php
require "../require/down-copy.php";
require "../require/buttons.php";
require "../require/footer.php";
?>
<br><br><br>
</body>

</html>
83 changes: 83 additions & 0 deletions src/page/portraits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<!DOCTYPE html>
<html lang="en">

<?php
error_reporting(E_ERROR | E_PARSE);
// Fetch and decode the JSON data from the GitHub API
$json = json_decode(file_get_contents("https://api.github.com/repos/HerrErde/subway-source/releases/latest", false, stream_context_create(['http' => ['header' => "User-Agent: PHP"]])), true);

// Check the version and prepare the warning message
$warningMessage = ($json['tag_name'] == "3-34-0") ? "Generation will be inaccurate until the next version." : "";

echo "<script>
document.addEventListener('DOMContentLoaded', function() {
if ('$warningMessage' !== '') {
document.getElementById('title').innerHTML += '<p style=\"color: red;\">$warningMessage</p>';
}
});
</script>";
?>

<head>
<title>Generate your Characters</title>
<?php
$activePage = basename(__FILE__, '.php');
require "../require/connect.php";
require "../require/buttons.php";
?>
<link rel="stylesheet" href="/assets/css/gen.css">
</head>

<body>
<header>
<h1>Generate your Playerprofile</h1>
<p id="title">Fill out the options and generate your customized JSON template code.</p>
<label class="custom-checkbox">
<input type="checkbox" id="selectAll">
<span class="checkmark"></span>
Select All
</label>

</header>

<form id="form">
<fieldset>
<?php
$json_data = file_get_contents("https://github.com/HerrErde/subway-source/releases/latest/download/playerprofile_links.json");
$items = json_decode($json_data);

$counter = 1;
foreach ($items->Portraits as $item): ?>
<div class="item">
<label class="custom-checkbox">
<input class="select-checkbox" type="checkbox" name="item[]" value="<?= $counter ?>">
<span class="checkmark"></span>
<?= $item->name ?>
</label>
<label class="custom-checkbox default-checkbox">
<input class="default-select-checkbox" type="checkbox" name="defaultItem" value="<?= $counter ?>">
<span class="checkmark"></span>
Default
</label><br>
<img src="<?= $item->img_url ?>" alt="<?= $item->name ?>">
</div>
<?php
$counter++;
endforeach;
?>
</fieldset>
<div id="filteredItems"></div>
<input type="button" class="btn btn-success" value="Generate URL" onclick="generateUrlFunction()">
</form>


<script src="/assets/js/search.js"></script>
<script src="/assets/js/selall.js"></script>

<script src="/assets/js/generateUrl.js" url="../generator/portraits.php"></script>

<?php require "../require/footer.php"; ?>

</body>

</html>
2 changes: 1 addition & 1 deletion src/require/buttons.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
['url' => '/page/wallet.php', 'name' => 'Wallet', 'active' => $activePage == "wallet" || $activePage == "wallet-editor"],
['url' => '/page/character.php', 'name' => 'Characters', 'active' => $activePage == "character"],
['url' => '/page/hoverboard.php', 'name' => 'Hoverboards', 'active' => $activePage == "hoverboard"],
['url' => '/page/playerprofile.php', 'name' => 'Profile', 'active' => $activePage == "playerprofile"],
['url' => '/page/portraits.php', 'name' => 'Portraits', 'active' => $activePage == "portraits"],
['url' => '/page/badges.php', 'name' => 'Badges', 'active' => $activePage == "badges"],
['url' => '/page/upgrades.php', 'name' => 'Upgrades', 'active' => $activePage == "upgrades"],
['url' => '/page/toprun.php', 'name' => 'Top Run', 'active' => $activePage == "toprun"]
Expand Down

0 comments on commit b3335e4

Please sign in to comment.