Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[bin/crypt] error if the thing isn't there

  • Loading branch information...
commit fcd3e8d0927d447907867137777cc0a6072ab464 1 parent 015b292
@masak authored
Showing with 39 additions and 0 deletions.
  1. +39 −0 bin/crypt
View
39 bin/crypt
@@ -256,6 +256,11 @@ class Adventure::PlayerExamined does Event {
has $.thing;
}
+class Adventure::ThingPlaced does Event {
+ has $.thing;
+ has $.room;
+}
+
class X::Adventure is Exception {
}
@@ -281,6 +286,14 @@ class X::Adventure::PlayerIsNowhere is X::Adventure {
}
}
+class X::Adventure::NoSuchThingHere is X::Adventure {
+ has $.thing;
+
+ method message {
+ "You see no $.thing here"
+ }
+}
+
class Adventure::Engine {
my @possible_directions = <
north south east west
@@ -294,6 +307,7 @@ class Adventure::Engine {
has %!exit_aliases;
has %!seen_room;
has %!try_exit_hooks;
+ has %!thing_rooms;
method connect(@rooms, $direction) {
die X::Adventure::NoSuchDirection.new(:action('connect rooms'), :$direction)
@@ -364,9 +378,17 @@ class Adventure::Engine {
}
method place_thing($thing, $room) {
+ my @events = Adventure::ThingPlaced.new(
+ :$thing, :$room
+ );
+ self!apply($_) for @events;
+ @events;
}
method examine($thing) {
+ die X::Adventure::NoSuchThingHere.new(:$thing)
+ unless (%!thing_rooms{$thing} // '') eq $!player_location;
+
my @events = Adventure::PlayerExamined.new(
:$thing
);
@@ -425,6 +447,9 @@ class Adventure::Engine {
when Adventure::DirectionAliased {
%!exit_aliases{.room}{.alias} = .direction;
}
+ when Adventure::ThingPlaced {
+ %!thing_rooms{.thing} = .room;
+ }
}
}
@@ -868,6 +893,20 @@ multi MAIN('test') {
'examining an object (+)';
}
+ {
+ my $engine = Adventure::Engine.new();
+
+ $engine.place_player('street');
+ throws_exception
+ { $engine.examine('ball') },
+ X::Adventure::NoSuchThingHere,
+ 'examining an object (-) no such object here',
+ {
+ is .thing, 'ball', '.thing attribute';
+ is .message, "You see no ball here", '.message attribute';
+ };
+ }
+
done;
}
Please sign in to comment.
Something went wrong with that request. Please try again.