Advertisement
Snuggledash

2b2t queue.php

Jul 19th, 2021
1,227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.12 KB | None | 0 0
  1. <?php
  2. const DB_FILE = '2b2t_queue.db';
  3. const ENDPOINT_MAGIC = '69042'; // nice
  4.  
  5. $db = new SQLite3(DB_FILE);
  6.  
  7. if (isset($_POST['update']) && $_POST['auth'] == ENDPOINT_MAGIC) {
  8.     // update mode
  9.     $ts = intval($_POST['ts']);
  10.     $qp = intval($_POST['qp']);
  11.     $ret = $db->query('SELECT seq, qp FROM `2b2q_log` ORDER BY id DESC LIMIT 1')->fetchArray();
  12.     $seq = $ret[0];
  13.     $last_qp = $ret[1];
  14.     if ($last_qp < $qp) {
  15.         $seq++;
  16.     }
  17.     $stmt = $db->prepare('INSERT INTO `2b2q_log` (seq, ts, qp) VALUES (:seq, :ts, :qp)');
  18.     $stmt->bindValue(':seq', $seq);
  19.     $stmt->bindValue(':ts', $ts);
  20.     $stmt->bindValue(':qp', $qp);
  21.     $stmt->execute();
  22.     header('HTTP/1.0 204 No Content');
  23. } else {
  24.     // stats mode
  25.     $now = time();
  26.     $last_seq = $db->query('SELECT seq FROM `2b2q_log` ORDER BY id DESC LIMIT 1')->fetchArray()[0];
  27.     $ret = $db->query("SELECT MIN(id), MAX(id) FROM `2b2q_log` WHERE seq = $last_seq")->fetchArray();
  28.     $first_id = $ret[0];
  29.     $last_id = $ret[1];
  30.     if ($first_id == $last_id) {
  31.         die('Insufficient data');
  32.     }
  33.     $ret = $db->query("SELECT ts, qp FROM `2b2q_log` WHERE id = $first_id")->fetchArray();
  34.     $initial_ts = $ret[0];
  35.     $initial_qp = $ret[1];
  36.     $ret = $db->query("SELECT ts, qp FROM `2b2q_log` WHERE id = $last_id")->fetchArray();
  37.     $current_ts = $ret[0];
  38.     $current_qp = $ret[1];
  39.     // don't divide by zero dummy
  40.     if ($initial_ts == $current_ts || $initial_qp == $current_qp) {
  41.         die('Insufficient data');
  42.     }
  43.     // linear regression using only two points isn't that great, the head of the queue moves slower
  44.     $est_total_wait = $current_qp * ($current_ts - $initial_ts) / ($initial_qp - $current_qp);
  45.     $est_remaining_wait = $initial_ts + $est_total_wait - $now;
  46.     if ($est_remaining_wait < 0) {
  47.         $est_remaining_wait = 0;
  48.     }
  49. ?><head>
  50. <meta charset="utf-8">
  51. <meta http-equiv="Refresh" content="60">
  52. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  53. <title>2b2t Queue Info</title>
  54. </head>
  55. <body>
  56. <p>Last update: <?= date(DATE_RFC1123, $current_ts); ?></p>
  57. <p>Queue position: <?= $current_qp ?></p>
  58. <p>Estimated wait time: <?= round($est_remaining_wait / 60) ?> minutes</p>
  59. </body>
  60. <?php
  61. } // end else
  62.  
  63. $db->close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement