Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 49 lines (40 sloc) 1.194 kb
284af54 @masak [Druid::Webapp] factored out web app logic
authored
1 use v6;
16afaed @masak [web-druid] changed to use the new simple regime
authored
2
3 use Web::Request;
4 use Web::Response;
5
67b372d Remove whitespace [Gun.io WhitespaceBot]
Gun.io Whitespace Robot authored
6 use Druid::Game;
284af54 @masak [Druid::Webapp] factored out web app logic
authored
7 use Druid::View::Text;
8
16afaed @masak [web-druid] changed to use the new simple regime
authored
9 class Druid::Webapp does Callable {
10 method postcircumfix:<( )>($env) {
39cfb05 @masak added persistence to Druid::Webapp
authored
11 my Web::Request $req .= new($env);
12
13 my $GAME_STATE_FILE = 'board.data';
14
284af54 @masak [Druid::Webapp] factored out web app logic
authored
15 my $board-size = 8;
16 my Druid::Game $game .= new(:size($board-size));
17 my Druid::View $view = Druid::View::Text.new(:$game);
f6fb370 @masak [Druid::Webapp] allowed game restart
authored
18 if $GAME_STATE_FILE ~~ :e && !$req.GET.<restart> {
39cfb05 @masak added persistence to Druid::Webapp
authored
19 $game.melt(slurp($GAME_STATE_FILE));
20 }
21
22 if $req.GET.<move> -> $move {
23 $game.make-move($move);
24 }
25 if open($GAME_STATE_FILE, :w) -> $fh {
26 $fh.print($game.gelatinize());
27 $fh.close();
28 }
284af54 @masak [Druid::Webapp] factored out web app logic
authored
29
16afaed @masak [web-druid] changed to use the new simple regime
authored
30 my Web::Response $res .= new;
31 $res.write($_) for
32 '<title>Druid</title>',
33 '<pre>',
34 $view,
39cfb05 @masak added persistence to Druid::Webapp
authored
35 '</pre>',
36 '<p>';
f6fb370 @masak [Druid::Webapp] allowed game restart
authored
37
38 if $game.finished {
39 $res.write('<a href="?restart=1">restart</a>');
40 }
41 else {
42 $res.write("<a href='?move=$_'>$_</a> ")
43 for $game.possible-moves();
44 $res.write('</p>');
45 }
16afaed @masak [web-druid] changed to use the new simple regime
authored
46 $res.finish();
284af54 @masak [Druid::Webapp] factored out web app logic
authored
47 }
48 }
Something went wrong with that request. Please try again.