Files
Torn-Vault-Tracker/index.php
T

143 lines
4.4 KiB
PHP

<?php
/**
* Main entry point for the Torn Vault Tracker application.
*
* PHP version 8.1+
*
* @category Main
* @package TornVaultTracker
* @author Keith Solomon <ksolmon@gmail.com>
* @license Unlicense https://unlicense.org/
* @link https://github.com/ksolmon/torn-vault-tracker
*/
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/functions.php';
$bootstrapWarning = null;
$needsBackfill = false;
try {
foreach (USER_KEYS as $key => $value) {
syncUserLogs($key);
// If the sync didn't populate this user, backfill is still needed.
$pdo = getDatabaseConnection();
$stmt = $pdo->prepare('SELECT COUNT(*) FROM vault WHERE user = :user');
$stmt->bindValue(':user', $key);
$stmt->execute();
if ((int)$stmt->fetchColumn() === 0) {
$needsBackfill = true;
}
}
} catch (Exception $e) {
$bootstrapWarning = $e->getMessage();
}
if ($needsBackfill) {
// Heavy work remains. Return a loading page that auto-refreshes;
// each refresh does another page of API fetches.
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Torn Vault Tracker &mdash; loading</title>
<meta http-equiv="refresh" content="3">
<link rel="icon" type="image/png" href="favicon.png">
<link rel="stylesheet" href="style.css?v=<?php echo filemtime('style.css'); ?>">
</head>
<body>
<h1>Loading vault history&hellip;</h1>
<?php if ($bootstrapWarning !== null): ?>
<p>Warning: <?php echo htmlspecialchars($bootstrapWarning, ENT_QUOTES, 'UTF-8'); ?></p>
<?php endif; ?>
<p>One-time backfill in progress. The Torn API returns up to 100 vault entries per call;
we'll keep fetching until your full history is loaded. This page will refresh automatically.</p>
</body>
</html>
<?php
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Torn Vault Tracker</title>
<link rel="icon" type="image/png" href="favicon.png">
<link rel="stylesheet" href="style.css?v=<?php echo filemtime('style.css'); ?>">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto:ital@0;1&display=swap" rel="stylesheet">
</head>
<body>
<h1>Torn Vault Tracker</h1>
<?php if ($bootstrapWarning !== null): ?>
<div class="warning" style="background:#f77e82;color:#000;padding:.5em 1em;margin:1em 0;border:1px solid #333;">Warning: failed to fetch latest logs from Torn — showing cached data. (<?php echo htmlspecialchars($bootstrapWarning, ENT_QUOTES, 'UTF-8'); ?>)</div>
<?php endif; ?>
<div class="grid">
<div class="gridLeft">
<h1>Transactions</h1>
<table id="data">
<thead>
<tr>
<th><h4>User</h4></th>
<th><h4>Date / Time (TCT)</h4></th>
<th><h4>Operation</h4></th>
<th><h4>Amount</h4></th>
<th><h4>Balance After</h4></th>
</tr>
</thead>
<tbody>
<?php buildTable(); ?>
</tbody>
</table>
<div id="pagination-controls">
<button onclick="changePage(-999)">&laquo;</button>
<button onclick="changePage(-1)">&lsaquo;</button>
<div id="page-info">Page x of y</div>
<button onclick="changePage(1)">&rsaquo;</button>
<button onclick="changePage(999)">&raquo;</button>
</div>
</div>
<div class="gridRight">
<h1>Balances</h1>
<section class="vault">
<h2>Vault Balance: $<?php echo generateBalance(); ?></h2>
<h3>Space left: $<?php echo getSpace(); ?></h3>
</section>
<h2>Shares</h2>
<div class="grid">
<?php foreach (USER_KEYS as $key => $value) { ?>
<section class="user">
<h3><?php echo ucfirst($key); ?> balance: $<?php echo generateBalance($key); ?></h3>
<h4>Share left: $<?php echo getSpace($key); ?></h4>
</section>
<?php } ?>
</div>
</div>
</div>
<script src="script.js"></script>
<script>
let rowsPerPage = <?php echo ROWS_PER_PAGE; ?>; // Set in config.php
paginate();
</script>
</body>
</html>