Initial commit

main
Pk11 2 years ago
commit 9e2bfd4319

1
.gitignore vendored

@ -0,0 +1 @@
vars.php

@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>

@ -0,0 +1,19 @@
# wordle.ピケ.コム
## hosting
1. install:
- PHP (I use 8.2.1)
- PostgreSQL (I use 15.6)
2. clone this repo, `git clone https://git.xn--rck9c.xn--tckwe/pk11/wordle.git`
2. copy `vars.php.example` to `vars.php`
3. edit `vars.php` as explained in the file
4. create the following tables in the configured database:
```sql
CREATE TABLE words (
id INT PRIMARY KEY,
solution VARCHAR(5) NOT NULL,
print_date VARCHAR(10) NOT NULL,
days_since_launch INT,
editor VARCHAR(64)
);
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

@ -0,0 +1,37 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Wordle DS</title>
</head>
<body>
<h1>Wordle DS</h1>
<img style="float: right" alt="The Wordle DS main menu" src="/assets/images/main-menu.png" width="256" height="384">
<h2>About</h2>
<p>
A clone of <a href="https://www.nytimes.com/games/wordle/index.html">Wordle</a> for the Nintendo DS(i). It features everything from the official version including matching daily word plus some bonus features.
</p>
<h3>Downloads</h3>
<ul>
<li><a href="https://github.com/Epicpkmn11/WordleDS/releases/latest/download/WordleDS.nds">WordleDS.nds</a> For use on flashcard or TWiLight Menu++</li>
<li><a href="https://github.com/Epicpkmn11/WordleDS/releases/latest/download/WordleDS.dsi">WordleDS.dsi</a> for use with TWiLight Menu++ or NTM</li>
<li><a href="https://github.com/Epicpkmn11/WordleDS/releases/latest/download/WordleDS.cia">WordleDS.cia</a> For use with FBI</li>
</ul>
<h2>API</h2>
<a href="/words.php?date=2021-06-19&limit=1&include=days_since_launch,solution,access_count">http://wordle.xn--rck9c.xn--tckwe/words.php</a>
<h3>Variables:</h3>
<dl>
<dt>date</dt>
<dd><b>[required]</b> The oldest date to include in YYYY-MM-DD format. Must be 2021-06-19 or later.</dd>
<dt>limit</dt>
<dd>The maximum words to include. Default 100.</dd>
<dt>include</dt>
<dd>Columns to include, comma separated list. Default: id, solution, print_date, days_since_launch, editor. Additional: access_count. When only one column is included the json will be a flat array, no objects.</dd>
</dl>
<h2>Share</h2>
<p>Wordle DS can share to a web page for QR readers that cannot copy text directly.</p>
<p><a href="/share.php?message=Wordle%20DS%200%20X/6%2000:00%0a%0a⬜⬜⬜⬜⬜%0a⬜⬜⬜⬜⬜%0a⬜⬜⬜⬜⬜%0a⬜⬜⬜⬜⬜%0a⬜⬜⬜⬜⬜%0a⬜⬜⬜⬜⬜%0a">Example message</a></p>
</body>
</html>

@ -0,0 +1,96 @@
<?php
/* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <http://unlicense.org/>
*/
// Return source code
if(isset($_GET['source'])) {
header("Content-Type: text/plain");
die(file_get_contents(basename($_SERVER['PHP_SELF'])));
}
$message = $_GET['message'];
$urlmessage = rawurlencode($message);
$subject = rawurlencode(strstr($message, "\n", TRUE));
$mobile = !!strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Wordle DS URL Share</title>
</head>
<body>
<h1>Wordle DS URL Share</h1>
<?php if(empty($message)) { ?>
<p>Error: No share message provided</p>
<?php } else { ?>
<blockquote>
<pre id="message"><?php echo $message; ?></pre>
</blockquote>
[<a href="/">back</a>]
[<a href="?source">source</a>]
[<a href="<?php echo "mailto:?subject=$subject&body=$urlmessage"; ?>">email</a>]
[<a href="<?php echo "sms:?body=$urlmessage"; ?>">sms</a>]
[<a target="_blank" href="<?php echo "https://bbs.xn--rck9c.xn--tckwe/?comment=$urlmessage"; ?>">bbs</a>]
[<a target="_blank" href="<?php echo "https://old.reddit.com/submit" . ($mobile ? '.compact' : '') . "?title=$subject&text=$urlmessage"; ?>">reddit</a>]
[<a target="_blank" href="<?php echo "https://www.tumblr.com/widgets/share/tool?shareSource=legacy&canonicalUrl=&posttype=text&url=xn--rck9c.xn--tckwe&title=$subject&content=$urlmessage"; ?>">tumblr</a>]
<script>
if(navigator.clipboard) {
document.write('[<a href="javascript:copyText()">copy</a>] ');
}
if(navigator.share) {
document.write('[<a href="javascript:share()">share</a>] ');
}
</script>
<?php } ?>
<script>
function copyText() {
var message = document.getElementById("message");
if(message && navigator.clipboard)
navigator.clipboard.writeText(message.innerText);
}
function share() {
var message = document.getElementById("message");
if(navigator.share) {
navigator.share({
title: message.innerText.substr(0, message.innerText.indexOf("\n")),
text: message.innerText
})
}
}
</script>
</body>
</html>
<?php

@ -0,0 +1,7 @@
<?php
// Database constants for PostgreSQL database
$DB_HOST = 'localhost';
$DB_NAME = '<db name>';
$DB_USER = '<db role name>';
$DB_PASSWORD = '<db role password>';

@ -0,0 +1,180 @@
<?php
/* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <http://unlicense.org/>
*/
// Return source code
if(isset($_GET['source'])) {
header("Content-Type: text/plain");
die(file_get_contents(basename($_SERVER['PHP_SELF'])));
}
header("Content-Type: application/json");
/* vars.php should contain the following, but with your variables as needed:
*
* // Database constants for PostgreSQL database
* $DB_HOST = 'localhost';
* $DB_NAME = '<db name>';
* $DB_USER = '<db role name>';
* $DB_PASSWORD = '<db role password>';
*
* You also need to run the following SQL:
*
* CREATE TABLE words (
* id INT PRIMARY KEY,
* solution VARCHAR(5) NOT NULL,
* print_date VARCHAR(10) NOT NULL,
* days_since_launch INT,
* editor VARCHAR(64),
* access_count INT DEFAULT 0
* );
*
* CREATE TABLE update (
* time TIMESTAMPTZ NOT NULL DEFAULT NOW(),
* lock BOOL PRIMARY KEY DEFAULT TRUE,
* CONSTRAINT lock_unique CHECK(lock)
* );
*
* INSERT INTO update VALUES (NOW());
*/
require_once('vars.php');
//// Functions: ////
function get_words($date, $limit, $include) {
$query = "SELECT $include FROM words WHERE print_date>=$1 ORDER BY print_date ASC LIMIT $2";
$res = pg_query_params($query, [$date, $limit]) or die('Query failed: ' . pg_last_error());
if(pg_num_rows($res) == 0) {
return [
'status' => 'ERROR',
'message' => 'Date too late'
];
}
while($row = pg_fetch_array($res, null, PGSQL_ASSOC)) {
$toint = ['id', 'days_since_launch', 'access_count'];
foreach($toint as $key) {
if(array_key_exists($key, $row))
$row[$key] = intval($row[$key]);
}
$words[] = $row;
}
$query = 'UPDATE words SET access_count=access_count+1 WHERE print_date>=$1';
pg_query_params($query, [$date]) or die('Query failed ' . pg_last_error());
// If just one column, do a straight array
if(!str_contains($include, ','))
$words = array_column($words, $include);
return $words;
}
function add_words($datestamp) {
$ret = false;
while(true) {
$date = date('Y-m-d', $datestamp);
$query = 'SELECT FROM words WHERE print_date=$1';
$res = pg_query_params($query, [$date]) or die('Query failed: ' . pg_last_error());
if(pg_num_rows($res) == 0) {
$content = file_get_contents("https://www.nytimes.com/svc/wordle/v2/$date.json");
if(!$content)
break;
$word = json_decode($content, true);
// Add word to database
$query = 'INSERT INTO words (id, solution, print_date, days_since_launch, editor) ' .
'VALUES ($1, $2, $3, $4, $5)';
$params = [
$word['id'],
$word['solution'],
$word['print_date'],
$word['days_since_launch'],
$word['editor']
];
pg_query_params($query, $params) or die('Query failed: ' . pg_last_error());
$ret = true; // (at least one) word added
}
$datestamp += 60 * 60 * 24;
}
return $ret;
}
//// Code start: ////
// Connect to the database
$dbc = pg_connect("host=$DB_HOST dbname=$DB_NAME user=$DB_USER password=$DB_PASSWORD")
or die('Could not connect: ' . pg_last_error());
// Check get vars
$date = $_GET['date'];
$limit = isset($_GET['limit']) ? intval($_GET['limit']) : 100;
$include = isset($_GET['include']) ? $_GET['include'] : 'id,solution,print_date,days_since_launch,editor';
if(!preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) {
die(json_encode([
'status' => 'ERROR',
'message' => 'Invalid date format (YYYY-MM-DD)'
]));
} else if($date < '2021-06-19') {
die(json_encode([
'status' => 'ERROR',
'message' => 'Date too early'
]));
} else if($limit <= 0) {
die(json_encode([
'status' => 'ERROR',
'message' => 'Invalid limit'
]));
} else if(!preg_match('/^[a-z,_]+$/', $include)) {
die(json_encode([
'status' => 'ERROR',
'message' => 'Invalid include'
]));
}
// Check for words once a day
$res = pg_query('SELECT EXTRACT(EPOCH FROM time) AS time FROM update') or die('Query failed: ' . pg_last_error());
$cur_time = time();
$last_time = pg_fetch_array($res)['time'];
if($cur_time > $last_time + (60 * 60 * 24)) {
add_words($cur_time);
$query = 'UPDATE update SET time=TO_TIMESTAMP($1)';
pg_query_params($query, [$cur_time]) or die('Query failed: ' . pg_last_error());
}
// Get words
echo json_encode(get_words($date, $limit, $include));
pg_close($dbc);
Loading…
Cancel
Save