Added javascript Life Viewer to template, adjusted the HTML template to work with it and the ordered lists and

using the existing textareas (thank goodness it already used textarea!)

Cleaned the one-page conditional logic to use UNLESS and work right,
 and also to let RLE put into input box just slip through into the RLE output box and thus get plugged into the Life Viewer

Added more comments
This commit is contained in:
peteyboy 2022-02-14 07:04:32 +00:00
parent b59827746e
commit ef42cd452e
1 changed files with 181 additions and 17 deletions

View File

@ -7,13 +7,15 @@ use lib qw( /usr/pkg/lib/perl5/5.24.0 /meta/p/peteyboy/perl5/lib/perl5 );
use CGI::Tiny;
use Mojo::Template;
use Mojo::Loader 'data_section';
#use Routes::Tiny;
#use Routes::Tiny; #don't even need
use Readonly;
my $ORIGIN_PAGE = "/wordle-life.cgi";
my $ORIGIN_PAGE = "/wordle-life-f.cgi";
my $defaulttext = "Paste your wordle share here";
cgi {
#error handler from the CGI:Tiny cookbook
my $cgi = $_;
$cgi->set_error_handler(sub {
@ -34,22 +36,34 @@ use Readonly;
my $method = $cgi->method;
my $rle = "";
#Simple switch: make a one page app, and always go back to the same page.
#GET or HEAD loads the page blank, POST runs the Life file maker
if ($method eq 'GET' or $method eq 'HEAD') {
$wordle = $defaulttext;
} elsif ($method eq 'POST') {
$wordle = $cgi->body_param('wordle-result'); #textarea name
if ($wordle =~ /\Q$defaulttext\E/){
}else{
$rle = generate_rle($wordle);
$wordle = $defaulttext;
#$wordle = "";
} elsif ($method eq 'POST') {
#textarea name, pull the value from POST and reload into textarea
$wordle = $cgi->body_param('wordle-result');
#if input textarea not changed from default text, do nothing
unless ($wordle =~ /\Q$defaulttext\E/) {
#[Cc] /){ #cheat, if they put in something that looks like an RLE, pass output through
if ($wordle =~ /^#[Cc] /){
$rle = $wordle;
#if none of that, do the thing: generate RLE
}else{
$rle = generate_rle($wordle);
}
}
} else {
$cgi->set_response_status(405)->render;
exit;
}
} else { #some other request? PUT, DELETE?
$cgi->set_response_status(405)->render;
exit;
}
die "Invalid wordle parameter" unless length $wordle;
#Load template from DATA section and output
my $mt = Mojo::Template->new(auto_escape => 1, vars => 1);
my $template = data_section __PACKAGE__, 'index.html.ep';
my $output = $mt->render($template, { ORIGIN_PAGE =>$ORIGIN_PAGE, defaulttext => $defaulttext, wordle => $wordle, rle => $rle });
@ -59,7 +73,7 @@ use Readonly;
};
#Try to Generate the RLE. Makes a lot of assumptions
sub generate_rle {
my $headline = "";
@ -101,14 +115,14 @@ sub generate_rle {
$myline =~ s/green_square/o/g;
#skip blank lines, count rows added
if(length($myline) > 1){
$rowcount += 1;
$rleline= $rleline . $myline;
}
}
}
#this is the format with b,o, and $. Done lazily because I don't compress the repeated b or o.
return "$headline\n$row_prefix$col_prefix$rowcount\n$rleline";
}
@ -119,7 +133,134 @@ __DATA__
<html>
<head>
<title>Play Conway's Life with your Wordle Score</title>
<link rel="stylesheet" href="https://unpkg.com/some-nice-basic-css/global.css" />
<meta name="LifeViewer" content="viewer textarea"> <!--required tag-->
<script src="lv-plugin.js"></script>
</head>
<body><h1>Wordle->Life</h1>
<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>
<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>
<form id="wordle-form" name="wordle-form" action="<%= $ORIGIN_PAGE =%>" method="POST" class="flow">
<label for="wordle">Wordle Share Chart:</label>
<textarea id="wordle" name="wordle-result" rows="9" cols="50" placeholder="<%= $defaulttext =%>" style="vertical-align: middle">
<%= $wordle %>
</textarea>
</p>
<br>
</li>
<li>
<p>Click "Submit" and and RLE format file for Conway's Game of Life will appear in the box below and get loaded into the Life Viewer.</p>
<input type="submit" value="Submit">
<!--viewer container-->
</li>
<li>
<p>Watch your Wordle score play LIFE!</p>
<div class="viewer" class="flow">
<label for="wordle">
<p>If the conversion is successful, you should see your Wordle converted to a Run Length Encoded Conway Life file in this box:</p>
</label>
<textarea id="life" name="Life file" rows="9" cols="50" style="vertical-align: middle">
<%= $rle %>
</textarea>
<p>If the RLE is correct, it should be loaded in the Life Viewer, and so you are ready to go!</p>
<ul>
<li><p>For best results with the Life Viewer, adjust the Zoom and the playback speed (from "1x", slide left to "3/s" or so) and press the Play button:</p>
<canvas height=400 width=400></canvas></li>
<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!
<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
boobo$bobbo$ooooo$
</pre code>
<p>For details about the Life RLE file format above, read <a href=https://conwaylife.com/wiki/Run_Length_Encoded>this</a>.
</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>
<p>
<p>
<hr 100%>
<em>Disclaimer: I made this! People and/or corporations may own their marks or copyrights, etc, on words mentioned on this page I don't claim to</em>
<p>
Questions, kudos or comments, mail me @sdf.org
<p>
Thanks to the community at <a href="https://conwaylife.com">conwaylife.com</a>, and the friendly folks in the forums there,
and for their <a href="https://conwaylife.com/wiki/Tutorials/LifeViewer_JavaScript_plug-in">Javascript Life Viewer</a>, which I use here.
</p>
<p>
Thanks to <a href="https://github.com/hankchizljaw/some-nice-basic-css"> hankchizljaw</a> for making his basic css publicly available
</p>
<p> Made in February 2022</p>
<p>
<center>
Hosted by SDF.org
<p><a href="https://sdf.org"><img src=https://mab.sdf.org/sdfbanner.png alt="SDF.org"></a>
</center>
</body></html>
@@ viewer.html.ep
<html>
<head>
<title>Play Conway's Life with your Wordle Score</title>
<link rel="stylesheet" href="https://peteyboy.freeshell.org/air.css">
<meta name="LifeViewer" content="viewer textarea"> <!--required tag-->
<script src="lv-plugin.js"></script>
</head>
<body><h1>Wordle->Life</h1>
@ -157,6 +298,21 @@ __DATA__
<textarea id="life" name="Life file" rows="9" cols="50" style="vertical-align: middle; border:none;">
<%= $rle %>
</textarea>
<!--viewer container-->
<div class="viewer" class="flow">
<div class="lv-buttons">
<button onclick="document.getElementById('life').innerHTML=''">Clear</button>
<button onclick="updateMe(this)">Show in Viewer</button>
<!--the element that calls updateMe must be in the viewer container-->
</div>
<canvas height=400 width=600></canvas>
</div>
<!--end viewer container-->
</form>
</div>
<p> if there is extra text or lines in your paste, you might not get a valid file here, so look and make sure it looks like a proper RLE file, for example:
@ -171,6 +327,13 @@ boobo$bobbo$ooooo$
<p>
<strong>Next steps:</strong>
<!-- not embedded -->
<ol>
<li>copy the contents of the Conway Life box above
<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>
@ -197,9 +360,10 @@ Questions, kudos or comments, mail me @sdf.org
<p>
Thanks to <a href="https://github.com/hankchizljaw/some-nice-basic-css"> hankchizljaw</a> for making his basic css publicly available
<p> Made in February 2022
<p>
<center>
Hosted by SDF.org
<p><a href="https://sdf.org"><img src=http://mab.sdf.org/sdfbanner.png alt="SDF.org"></a>
<p><a href="https://sdf.org"><img src=https://mab.sdf.org/sdfbanner.png alt="SDF.org"></a>
</center>
</body></html>