mirror of
https://git.lolcat.ca/lolcat/4get.git
synced 2025-07-23 22:50:50 -04:00
add structure for `Oracles' (special answers depending on queries + a few implementations (#10)
incl. a calculator, a hash encoder + rot13 and b64!, and a "what time is it" with timezone selection frontend injected in $payload["left"] in web.php you can see this live [on my instance](https://4get.silly.computer/web?s=7%2B8(9%5E2)&scraper=brave&nsfw=yes) (there are some issues that aren't related to this PR. favicons, etc. I'll fix them later.) Reviewed-on: https://git.lolcat.ca/lolcat/4get/pulls/10 Co-authored-by: cynic <admin@cynic.moe> Co-committed-by: cynic <admin@cynic.moe>
This commit is contained in:
parent
d312674df7
commit
8762d68466
6 changed files with 349 additions and 0 deletions
54
oracles/numerics.php
Normal file
54
oracles/numerics.php
Normal file
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
include_once("oracles/base.php");
|
||||
class numerics extends oracle {
|
||||
public $info = [
|
||||
"name" => "numeric base conversion"
|
||||
];
|
||||
public function check_query($q) {
|
||||
if (str_contains($q, " ")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$q = strtolower($q);
|
||||
|
||||
$profiles = [
|
||||
["0x", str_split("0123456789abcdef")],
|
||||
["", str_split("1234567890")],
|
||||
["b", str_split("10")]
|
||||
];
|
||||
|
||||
foreach ($profiles as $profile) {
|
||||
$good = true;
|
||||
$good &= str_starts_with($q, $profile[0]);
|
||||
$nq = substr($q, strlen($profile[0]));
|
||||
foreach (str_split($nq) as $c) {
|
||||
$good &= in_array($c, $profile[1]);
|
||||
}
|
||||
if ($good) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public function generate_response($q) {
|
||||
$n = 0;
|
||||
if (str_starts_with($q, "0x")) {
|
||||
$nq = substr($q, strlen("0x"));
|
||||
$n = hexdec($nq);
|
||||
}
|
||||
elseif (str_starts_with($q, "b")) {
|
||||
$nq = substr($q, strlen("b"));
|
||||
$n = bindec($nq);
|
||||
}
|
||||
else {
|
||||
$n = (int)$q;
|
||||
}
|
||||
return [
|
||||
"decimal (base 10)" => (string)$n,
|
||||
"hexadecimal (base 16)" => "0x".(string)dechex($n),
|
||||
"binary (base 2)" => "b".(string)decbin($n),
|
||||
"" => "binary inputs should be prefixed with 'b', hex with '0x'."
|
||||
];
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue