2022-04-20 02:10:43 -04:00
#!/usr/bin/perl
2022-02-10 02:17:11 -05:00
use strict;
use warnings;
use utf8;
2022-04-20 02:10:43 -04:00
use Mojolicious::Lite -signatures;
use experimental 'smartmatch';
2022-02-10 02:17:11 -05:00
2022-02-14 02:27:14 -05:00
#===
# MAKE SURE ORIGIN PAGE MATCHES FILENAME (especially if you are testing changes)!
#
#===
2022-04-20 02:37:05 -04:00
my $ORIGIN_PAGE = "/cgi-bin/wordle-life.cgi";
2022-04-20 02:10:43 -04:00
my $defaulttext = "Paste your wordle share here (replace this text)";
my $PATH_PARAM = "PATH_INFO";
my $WORDLE_INPUT_PARAM = "wordle-result";
my $SHARENOTE_PARAM = "share-note";
2022-02-10 02:17:11 -05:00
2022-04-20 02:10:43 -04:00
my $wordle;
my $rle = "";
my $isshare = 0; #flag for sharing, default false
my $sharenote = ""; #a note someone can tack onto their share
get '/' => sub ($cgi) {
#if there are sharing query parameters put them in $wordle and activate share page logic
if (first { $_ eq $WORDLE_INPUT_PARAM } @{$cgi->req->query_params} ){
$wordle = $cgi->param($WORDLE_INPUT_PARAM);
$isshare = 1; #true
$sharenote = $cgi->param($SHARENOTE_PARAM);
}else{
2022-02-14 02:04:32 -05:00
$wordle = $defaulttext;
2022-04-20 02:10:43 -04:00
}
2022-02-23 16:21:21 -05:00
2022-04-20 02:10:43 -04:00
my $template_name = 'index';
$cgi->stash( WORDLE_INPUT_PARAM => $WORDLE_INPUT_PARAM, ORIGIN_PAGE => $ORIGIN_PAGE, defaulttext => $defaulttext, wordle => $wordle, rle => $rle, isshare => $isshare, sharenote => $sharenote );
$cgi->render(template => $template_name);
};
get '/*path_info' => sub ($cgi) {
my $template_name = $cgi->stash("path_info");
#TODO: Check against a list of valid templates!
if ( $template_name ~~ ['about'] ) {
$cgi->stash( ORIGIN_PAGE => $ORIGIN_PAGE); #this isn't general purpose, what template needs what variables?
}else{ #TODO: fix so as not to need copied code from the else below?
$template_name = 'index';
$cgi->stash( WORDLE_INPUT_PARAM => $WORDLE_INPUT_PARAM, ORIGIN_PAGE => $ORIGIN_PAGE, defaulttext => $defaulttext, wordle => $wordle, rle => $rle, isshare => $isshare, sharenote => $sharenote );
}
$cgi->render(template => $template_name);
};
post '/' => sub($cgi) {
#textarea name, pull the value from POST and reload into textarea
$wordle = $cgi->param($WORDLE_INPUT_PARAM);
#if input textarea not changed from default text, do nothing
unless ($wordle =~ /\Q$defaulttext\E/ or $wordle eq '') {
#if they put in something that looks like an RLE, pass output through, for share or to just use the life viewer
if ($wordle =~ /^#[Cc] /){
2022-02-14 02:04:32 -05:00
$rle = $wordle;
#if none of that, do the thing: generate RLE
2022-04-20 02:10:43 -04:00
}else{
2022-02-14 02:04:32 -05:00
$rle = generate_rle($wordle);
2022-04-20 02:10:43 -04:00
}
}
$cgi->stash( WORDLE_INPUT_PARAM => $WORDLE_INPUT_PARAM, ORIGIN_PAGE => $ORIGIN_PAGE, defaulttext => $defaulttext, wordle => $wordle, rle => $rle, isshare => $isshare, sharenote => $sharenote );
$cgi->render(template =>'index');
};
app->start;
sub template_from_path_info {
my $template='';
my $path = shift;
if ($path =~ /^\//){
$template = $path;
$template =~ s/^.//;
$template =~ tr/\//./;
}
return $template;
}
2022-02-10 02:17:11 -05:00
2022-02-14 02:04:32 -05:00
#Try to Generate the RLE. Makes a lot of assumptions
2022-02-10 02:17:11 -05:00
sub generate_rle {
my $headline = "";
2022-02-22 14:11:41 -05:00
my $rleline = "";
2022-02-10 02:17:11 -05:00
my $rowcount;
my $myline;
Readonly my $headline_prefix => "#C "; #Part of RLE spec https://conwaylife.com/wiki/Run_Length_Encoded
Readonly my $row_prefix => "x = 5"; #for wordle always 5
Readonly my $col_prefix => ",y = ";
2022-02-23 16:21:21 -05:00
Readonly my $lifeviewer_settings => "\n[[\n GPS 3\n ZOOM 23\n COLOR ALIVE LIME\n COLOR BACKGROUND MIDNIGHTBLUE\n STOP 100\n GRID\n]]"; #Part of Viewer Spec
#Readonly my $lifeviewer_settings => "\n[[\n GPS 3\n ZOOM 23\n THEME INVERSE\n STOP 100\n GRID\n]]"; #Part of Viewer Spec
2022-02-16 03:35:53 -05:00
2022-02-10 02:17:11 -05:00
my $wordle = shift; # this is how you get variables in your function call!
$rowcount = 0;
my @lines = split /^/, $wordle;
foreach $myline (@lines) {
chomp($myline);
if ($headline eq "" && $myline =~ /Wordle/){
$headline = $headline_prefix . $myline;
}else{
2022-02-16 03:35:53 -05:00
#remove all line endings with magic perl identifier R
2022-02-10 02:17:11 -05:00
$myline =~ s/\R//;
$myline =~ s/^(.*)$/$1\$/;
2022-02-16 03:35:53 -05:00
#these replaces are logicless, so they need to be done in this order!
#in case of text mode (Discord)
2022-02-10 02:17:11 -05:00
$myline =~ s/:yellow_square:/o/g;
$myline =~ s/:green_square:/o/g;
2022-02-16 03:35:53 -05:00
$myline =~ s/:blue_square:/o/g; #HC equiv yellow
$myline =~ s/:orange_square:/o/g; #HC equiv green
2022-02-10 02:17:11 -05:00
2022-02-16 03:35:53 -05:00
$myline =~ s/:[a-z_]*square:/b/g;
# for normal unicode
$myline =~ s/\N{U+1f7e8}/o/g; #yellow hit
$myline =~ s/\N{U+1f7e9}/o/g; #green hit
$myline =~ s/\N{U+1F7E6}/o/g; #blue
$myline =~ s/\N{U+1F7E7}/o/g; #orange
2022-02-10 02:17:11 -05:00
$myline =~ s/\N{U+2B1B}/b/g; #black
$myline =~ s/\N{U+2B1C}/b/g; #white
#broken copy/paste text
$myline =~ s/black_large_square/b/g;
$myline =~ s/white_large_square/b/g;
$myline =~ s/yellow_square/o/g;
$myline =~ s/green_square/o/g;
2022-02-16 03:35:53 -05:00
$myline =~ s/blue_square/o/g;
$myline =~ s/orange_square/o/g;
2022-02-10 02:17:11 -05:00
2022-02-14 02:04:32 -05:00
#skip blank lines, count rows added
2022-02-10 02:17:11 -05:00
if(length($myline) > 1){
$rowcount += 1;
$rleline= $rleline . $myline;
}
}
}
2022-02-14 02:04:32 -05:00
#this is the format with b,o, and $. Done lazily because I don't compress the repeated b or o.
2022-02-16 03:35:53 -05:00
$rleline =~ s/\$$/\!/; #Correct termination of last line of RLE is !, not $, so replace it.
return "$headline\n$row_prefix$col_prefix$rowcount\n$rleline\n$lifeviewer_settings";
2022-02-10 02:17:11 -05:00
}
__DATA__
@@ index.html.ep
<html>
<head>
<title>Play Conway's Life with your Wordle Score</title>
2022-02-14 02:04:32 -05:00
2022-02-10 02:17:11 -05:00
<link rel="stylesheet" href="https://unpkg.com/some-nice-basic-css/global.css" />
2022-02-14 02:04:32 -05:00
2022-02-16 03:35:53 -05:00
<style>
2022-04-20 02:10:43 -04:00
.
lv-rle{
2022-02-23 16:21:21 -05:00
min-height:200px;
2022-02-16 03:35:53 -05:00
}
</style>
2022-02-14 02:04:32 -05:00
<meta name="LifeViewer" content="viewer textarea"> <!--required tag-->
2022-04-20 02:10:43 -04:00
<script src="/lv-plugin.js"></script>
2022-02-14 02:04:32 -05:00
</head>
2022-04-20 02:10:43 -04:00
<body>
<p align= left> <a href="/">back to Site Home</a>
</p>
2022-02-14 02:04:32 -05:00
2022-04-20 02:10:43 -04:00
<h1>Wordle->Life</h1>
<p align= right> <a href="<%=$ORIGIN_PAGE%>/about">about</a></p>
2022-02-23 16:21:21 -05:00
% if ($isshare) {
<span>
<h3> Hey, someone wants to share their Wordle->Life score with you!</h3>
%if ($sharenote){
<p>They said: <em><%= $sharenote =%></em></p>
%};
<p><strong>Skip to step 2 of the instructions below and see the shared wordle-life happen!</strong></p>
</span>
%}else{
<span>
<p><em>Play your Wordle Share score in <a href=https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life> Conway's Life!</a></em></p>
</span>
% };
2022-02-14 02:04:32 -05:00
<p>
<p> Hey, nerds, isn't it funny how the Wordle Scores can look like a glider in that old computer programming exercise, Conway's Life?</p> <img src="https://conwaylife.com/w/images/e/e2/Lwss.png" alt="Glider image from Conwaylife.org, it looks like an 8-bit staple gun" align= float />
<ol>
<li>
<p> Paste your wordle share below and submit to convert it to a Conway's Life file so you can run it (to learn more about Conway's Life, read <a href=https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life> this)</a>.</p>
2022-02-23 16:21:21 -05:00
<form id="wordle-form" name="wordle-form" action="<%== $ORIGIN_PAGE %>" method="POST" class="flow">
2022-02-14 02:04:32 -05:00
<label for="wordle">Wordle Share Chart:</label>
2022-02-23 16:21:21 -05:00
<textarea id="wordle" name="wordle-result" rows="12" cols="50" placeholder="<%== $defaulttext %>" style="vertical-align: middle" class="lv-rle" class="flow">
<%== $wordle %></textarea>
2022-02-14 02:04:32 -05:00
</p>
<br>
</li>
<li>
2022-02-16 03:35:53 -05:00
<p>Click "Submit" and an RLE format file made for Conway's Game of Life will appear in the box below and get loaded into the Life Viewer.</p>
2022-02-14 02:04:32 -05:00
<input type="submit" value="Submit">
2022-02-16 03:35:53 -05:00
2022-02-14 02:04:32 -05:00
</li>
<li>
<p>Watch your Wordle score play LIFE!</p>
2022-02-16 03:35:53 -05:00
<!--viewer container-->
2022-02-14 02:04:32 -05:00
<div class="viewer" class="flow">
<label for="wordle">
2022-02-23 16:21:21 -05:00
<p>If the conversion is successful, you should see your Wordle converted to a Run Length Encoded Conway Life file (RLE) in this box:</p>
2022-02-14 02:04:32 -05:00
</label>
2022-02-23 16:21:21 -05:00
<label for="wordle">Converted RLE:</label>
<textarea id="life" name="life-file" rows="12" cols="50" style="vertical-align: middle" class="lv-rle" class="flow">
<%== $rle %></textarea>
2022-02-14 02:04:32 -05:00
<p>If the RLE is correct, it should be loaded in the Life Viewer, and so you are ready to go!</p>
<ul>
2022-02-16 03:35:53 -05:00
<li><p>For best results with the Life Viewer, you may adjust the default Zoom and the playback speed, then press the Play button:</p>
2022-02-23 16:21:21 -05:00
<canvas height=400 width=450></canvas></li>
2022-02-14 02:04:32 -05:00
<li><p>Press "[Pause]" when it seems stable. </p>
</li>
</ul>
</div>
</li>
</ol>
<!--end viewer container-->
</form>
<p>
<p> This should work with the share/copy button in wordle, or copy/pasting your friends' wordles out of Discord or wherever as well. See whose Wordle Score runs the coolest!
2022-02-23 16:21:21 -05:00
<p>
% if ($rle ne ""){
<span>
<form id="life-share" name="life-share" action="<%== $ORIGIN_PAGE %>" method="GET" class="flow">
<p><strong> Share your wordle-life!</strong>
<p> You can copy/paste the RLE into your favorite social media post or email
<br><em>OR</em><br>
Click this button to go to a share page where you can copy/paste the URL to someone:</p>
<textarea id="share-rle" name="<%= $WORDLE_INPUT_PARAM %>" rows="9" cols="50" style="display:none;">
<%=$rle %></textarea>
<input id="share-note" type="text" name="share-note" placeholder="Include a note, if you'd like" style="vertical-align: bottom">
<input type="submit" value="Go to sharing page">
</form>
% };
</span>
</p>
2022-02-14 02:04:32 -05:00
<p>
<strong>If it doesn't seem to be working:</strong>
<ol>
<li><p> If there is extra text or lines in your paste, you might not get a valid file in the box above, so look and make sure it looks like a proper RLE file, for example:
<p>
<pre code>
#C Wordle 235 3/6
x = 5,y = 3
2022-02-16 03:35:53 -05:00
boobo$bobbo$ooooo!
2022-02-23 20:44:25 -05:00
[[
GPS 3
2022-02-16 03:35:53 -05:00
ZOOM 23
COLOR ALIVE LIME
COLOR BACKGROUND BLUE
STOP 100
GRID
]]
2022-02-14 02:04:32 -05:00
</pre code>
2022-02-23 16:21:21 -05:00
<p>(the main thing that needs to be right are the first 3 lines. If the 3rd line has any colon <strong> :</strong> symbols, just try deleting them. The stuff between double brackets aren't part of the RLE but are comments that set some defaults for the Life Viewer)</p>
2022-02-16 03:35:53 -05:00
<p>For details about the Life RLE file format above, read <a href=https://conwaylife.com/wiki/Run_Length_Encoded>this</a>.</p>
2022-02-14 02:04:32 -05:00
</li>
<li>You can also copy the contents of the Conway Life box above and try it somewhere else:
<ol>
<li>go to a Conway's Life site, such as <a href= https://copy.sh/life/> this one at copy.sh</a> or <a href="https://lazyslug.com/lifeviewer/">this one at lazyslug.com</a> and load and run your file.</li>
<ol>
<li> specifically for the one at copy.sh:</li>
<li> Press the [Import] button at the top</li>
<li> paste in your Life file contents in the box and press [Import]</li>
<li> It will put up a box telling you it loaded your Wordle, press [OK]</li>
<li> Press the [Run] button and watch your Wordle score play LIFE!</li>
</ol>
</ol>
</ol>
<p>
</body></html>
2022-04-20 02:10:43 -04:00
@@ about.html.ep
2022-02-14 02:04:32 -05:00
<html>
<head>
2022-04-20 02:10:43 -04:00
<title>About Wordle-Life</title>
2022-02-14 02:04:32 -05:00
2022-04-20 02:10:43 -04:00
<link rel="stylesheet" href="https://unpkg.com/some-nice-basic-css/global.css" />
2022-02-10 02:17:11 -05:00
</head>
<body><h1>Wordle->Life</h1>
<p>
2022-04-20 02:10:43 -04:00
<p> This is an about page. </p>
2022-02-10 02:17:11 -05:00
2022-04-20 02:10:43 -04:00
<p> Click <a href=<%== $ORIGIN_PAGE %>>here</a> to return to Wordle-Life page </p>
<p> Click <a href="/">here</a> to go to website Home</p>
2022-02-10 02:17:11 -05:00
<p>
<hr 100%>
2022-04-20 02:10:43 -04:00
<em>Disclaimer: I made this! People and/or corporations may own their marks or copyrights, etc, on words mentioned on these pages I don't claim to</em>
2022-02-10 02:17:11 -05:00
<p>
Thanks to <a href="https://github.com/hankchizljaw/some-nice-basic-css"> hankchizljaw</a> for making his basic css publicly available
2022-04-20 02:10:43 -04:00
<p> Made in February 2022, revised April 2022 for this site, using Perl Mojolicious
2022-02-10 02:17:11 -05:00
<p>
<center>
2022-04-20 02:10:43 -04:00
Hosted by Bisect Hosting
<p><a href="https://www.bisecthosting.com"><img src=https://www.bisecthosting.com/images/logos/logo.svg alt="bisecthosting.com" height= 10% width=auto></a>
2022-02-10 02:17:11 -05:00
</center>
</body></html>
2022-04-20 02:10:43 -04:00