Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[bin/crypt] can't take something twice

  • Loading branch information...
commit 08ad1f3e30fe3e11d02dbf7e6ec818f27721fffc 1 parent aea7145
Carl Mäsak authored

Showing 1 changed file with 32 additions and 0 deletions. Show diff stats Hide diff stats

  1. 32  bin/crypt
32  bin/crypt
@@ -401,6 +401,13 @@ class X::Adventure::ThingNotCarryable is X::Adventure {
401 401
         "You cannot $.action the $.thing"
402 402
     }
403 403
 }
  404
+class X::Adventure::PlayerAlreadyCarries is X::Adventure {
  405
+    has $.thing;
  406
+
  407
+    method message {
  408
+        "You already have the $.thing"
  409
+    }
  410
+}
404 411
 
405 412
 class Adventure::Engine {
406 413
     my @possible_directions = <
@@ -635,6 +642,9 @@ class Adventure::Engine {
635 642
         die X::Adventure::ThingNotCarryable.new(:action<take>, :$thing)
636 643
             unless %!carryable_things{$thing};
637 644
 
  645
+        die X::Adventure::PlayerAlreadyCarries.new(:$thing)
  646
+            if %!thing_rooms{$thing} eq 'player inventory';
  647
+
638 648
         my @events = Adventure::PlayerTook.new(:$thing);
639 649
         self!apply_and_return: @events;
640 650
     }
@@ -723,6 +733,9 @@ class Adventure::Engine {
723 733
         when Adventure::ThingMadeCarryable {
724 734
             %!carryable_things{.thing} = True;
725 735
         }
  736
+        when Adventure::PlayerTook {
  737
+            %!thing_rooms{.thing} = 'player inventory';
  738
+        }
726 739
     }
727 740
 }
728 741
 
@@ -1446,6 +1459,7 @@ multi MAIN('test') {
1446 1459
 
1447 1460
         $engine.place_thing('box', 'saloon');
1448 1461
         $engine.make_thing_carryable('box');
  1462
+        $engine.place_player('saloon');
1449 1463
         is $engine.take('box'),
1450 1464
             Adventure::PlayerTook.new(
1451 1465
                 :thing<box>,
@@ -1507,6 +1521,24 @@ multi MAIN('test') {
1507 1521
             };
1508 1522
     }
1509 1523
 
  1524
+    {
  1525
+        my $engine = Adventure::Engine.new();
  1526
+
  1527
+        $engine.place_thing('cup', 'porch');
  1528
+        $engine.make_thing_carryable('cup');
  1529
+        $engine.place_player('porch');
  1530
+        $engine.take('cup');
  1531
+        throws_exception
  1532
+            { $engine.take('cup') },
  1533
+            X::Adventure::PlayerAlreadyCarries,
  1534
+            'taking a thing (-) player already has',
  1535
+            {
  1536
+                is .thing, 'cup', '.thing attribute';
  1537
+                is .message, "You already have the cup",
  1538
+                    '.message attribute';
  1539
+            };
  1540
+    }
  1541
+
1510 1542
     done;
1511 1543
 }
1512 1544
 

0 notes on commit 08ad1f3

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