Permalink
Browse files

prepared for options when creating matches

  • Loading branch information...
1 parent 8972fe5 commit b34efc0331b9356bef0804d66823bda101ced6ac @polettix committed Oct 2, 2012
@@ -5,6 +5,7 @@ use Dancer::Plugin::FlashNote qw< flash >;
use 5.012;
use LWP::UserAgent;
use URI;
+use JSON qw< encode_json >;
use Try::Tiny;
use Storable qw< dclone >;
@@ -212,6 +213,14 @@ sub get_matches_for {
return $stuff;
} ## end sub get_matches_for
+sub class_for {
+ my ($gameid) = @_;
+ my $class = "Narsil::Frontend::$gameid";
+ (my $package = $class . '.pm') =~ s{(?: :: | ')}{/}gmxs;
+ require $package;
+ return $class;
+}
+
get '/match/:id' => sub {
my $userid = user()->{username};
my $matchid = param('id');
@@ -231,10 +240,7 @@ get '/match/:id' => sub {
my @movers = map { $_->[0] } @{$match->{movers}};
my @winners = map { $_->[0] } @{$match->{winners}};
try {
- my $class = "Narsil::Frontend::$gameid";
- (my $package = $class . '.pm') =~ s{(?: :: | ')}{/}gmxs;
- require $package;
- ($match, $template) = $class->adapt($match, $userid);
+ ($match, $template) = class_for($gameid)->adapt($match, $userid);
} ## end try
catch {
warning $_;
@@ -251,9 +257,11 @@ get '/match/:id' => sub {
post '/match' => sub {
my $gameid = param('game');
+ my $params = params();
my $match = rest_call(
post => '/match',
{
+ configuration => encode_json($params),
user => user()->{username},
game => rest_uri_for("/game/$gameid"),
},
@@ -323,6 +331,18 @@ post '/move' => sub {
get '/game/:id' => sub {
my $gameid = param('id');
+ my $userid = user()->{username};
+ my $game = rest_call(get => "/game/$gameid", {user => $userid});
+ my ($template, $params);
+ try {
+ ($template, $params) = class_for($gameid)->game($game, $userid);
+ }
+ catch {
+ warning "caught: $_";
+ flash warn => 'no_game_info' => $game;
+ };
+ return redirect request()->uri_for('/') unless defined $params;
+ return template $template, { user => $userid, game => $game, %$params };
};
get '/games' => sub {
@@ -80,6 +80,11 @@ sub build_move {
return @retval;
}
+sub game {
+ my ($package, $game, $user) = @_;
+ return 'games/battleship/game.tt', {};
+}
+
1;
__END__
@@ -0,0 +1,15 @@
+<style>
+input.chars2 {
+ width: 2em;
+}
+</style>
+<h2><% game.name %></h2>
+<p>The sea war you played when you were a child</p>
+<%
+ uri = request.uri_for('/match');
+%>
+<form method="POST" action="<% uri %>">
+ <input type="hidden" name="game" value="<% game.id %>">
+ size: <input class="chars2" type="text" id="size" name="size" value="10">
+ <input type="submit" name="" value="play">
+</form>
@@ -0,0 +1 @@
+No info available on game <% report.2.name %>

0 comments on commit b34efc0

Please sign in to comment.