Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[bin/crypt] added verb binding logic

  • Loading branch information...
commit 457e63f913a539b3f7e991c2736627fbe180e859 1 parent 6dc3673
Carl Mäsak authored

Showing 1 changed file with 18 additions and 6 deletions. Show diff stats Hide diff stats

  1. 24  bin/crypt
24  bin/crypt
@@ -337,6 +337,13 @@ multi MAIN() {
337 337
         %descriptions{$0} = ~$1;
338 338
     }
339 339
 
  340
+    sub params($method) {
  341
+        $method.signature.params
  342
+            ==> grep { .positional && !.invocant }
  343
+            ==> map { .name.substr(1) }
  344
+    }
  345
+    my %commands = map { $^m.name => params($m) }, $game.^methods;
  346
+
340 347
     loop {
341 348
         my $command = prompt('> ');
342 349
         given $command {
@@ -348,12 +355,17 @@ multi MAIN() {
348 355
             $command .= lc;
349 356
             $command .= trim;
350 357
 
351  
-            when 'look' {
352  
-                my @events = $game.look();
353  
-                for @events {
354  
-                    when Crypt::PlayerLooked {
355  
-                        say %descriptions{.description_of};
356  
-                    }
  358
+            my $verb = $command.words[0];
  359
+            my @args = $command.words[1..*];
  360
+            when %commands.exists($verb) {
  361
+                my @req_args = %commands{$verb}.list;
  362
+                when @args != @req_args {
  363
+                    say "You passed in {+@args} arguments, but $verb requires {+@req_args}.";
  364
+                    say "The arguments are {map { "<$_>" }, @req_args}.";
  365
+                }
  366
+                my @events = $game."$verb"(|@args);
  367
+                CATCH {
  368
+                    when X::Adventure { say .message, '.' }
357 369
                 }
358 370
             }
359 371
 

0 notes on commit 457e63f

Please sign in to comment.
Something went wrong with that request. Please try again.