Skip to content

Commit

Permalink
a lot of groundwork for finding other players
Browse files Browse the repository at this point in the history
  • Loading branch information
Ball committed May 22, 2011
1 parent 44551a3 commit cb7f04e
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -49,6 +49,6 @@ task :shell => :compile do
end

task :test => :compile do
sh "erl -noshell -pa ebin -s mnesia start -s test_runner run -s init stop"
sh "erl -noshell -smp disable -pa ebin -s mnesia start -s test_runner run -s init stop"
end

2 changes: 1 addition & 1 deletion Readme.txt
Expand Up @@ -18,8 +18,8 @@ API from the user's stand point
- * player_proxy:move("north").
- * player_proxy:drop("my wallet").
- * player_proxy:take("my wallet").
- * player_proxy:who().

- player_proxy:who().
- player_proxy:say("Are those cookies?").
- player_proxy:shout("Looking for group!").
- player_proxy:tell("Liv", "Can you pass the chips?").
Expand Down
12 changes: 11 additions & 1 deletion src/player.erl
Expand Up @@ -12,6 +12,7 @@
login(Username,"Hello") ->
Player = #player{key=Username,name=Username,description="Stuff",location_key=lobby},
{ok,Pid} = gen_server:start_link(?MODULE, [Player], []),
registry:add_player(Username,lobby,Pid),
player_proxy:new(Pid);
login(_Username,_Password) ->
error.
Expand Down Expand Up @@ -58,7 +59,16 @@ handle_call(inventory, _From, State) ->
[] -> Message = "You aint got jack!";
_ -> Message = lists:flatten( string:join(["You have" | State#player.items], "\n\t"))
end,
{reply, {ok, Message}, State}.
{reply, {ok, Message}, State};

% who
handle_call(who, _From, State) ->
{ok, Players} = registry:players(),
PlayerLines = lists:map( fun ({Nic, Room, _Pid}) ->
Nic ++ " : " ++ atom_to_list(Room) end, Players),
String = "The players logged in are\n" ++
lists:flatten(string:join(PlayerLines, "\n")),
{reply, {ok,String}, State}.

handle_cast(stop, State)->
{stop, normal, State}.
Expand Down
6 changes: 5 additions & 1 deletion src/player_proxy.erl
@@ -1,5 +1,5 @@
-module(player_proxy,[Pid]).
-export([look/0,look/1,move/1,take/1,drop/1,inventory/0,command/2,exit/0]).
-export([look/0,look/1,move/1,take/1,drop/1,inventory/0,who/0,say/1,command/2,exit/0]).

look() ->
gen_server:call(Pid, look).
Expand All @@ -13,6 +13,10 @@ drop(Item) ->
gen_server:call(Pid, {drop, Item}).
inventory() ->
gen_server:call(Pid, inventory).
who() ->
gen_server:call(Pid, who).
say(Message) ->
gen_server:call(Pid, {say, Message}).
command(Command,Args) ->
Pid ! {command, self, Command, Args}.
exit() ->
Expand Down
19 changes: 16 additions & 3 deletions src/registry.erl
Expand Up @@ -3,7 +3,7 @@
-behavior(gen_server).

%% API
-export([add_player/3, players/0, start/0]).
-export([add_player/3, remove_player/1, players/0, players_in_room/1, start/0]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
Expand All @@ -14,15 +14,28 @@ start() ->
gen_server:start_link({local,?MODULE}, ?MODULE, [[]], []).
add_player(Nic, Location, Pid) ->
gen_server:call(?MODULE, {add_player, {Nic, Location, Pid}}).
remove_player(Nic) ->
gen_server:call(?MODULE, {remove_player, Nic}).
players() ->
gen_server:call(?MODULE, players).
players_in_room(Room) ->
gen_server:call(?MODULE, {players_in_room, Room}).

%% gen_server
init([State]) ->
{ok, State}.

handle_call({add_player, Player}, _From, Players) ->
{reply, ok, [Player | Players]};
handle_call({add_player, {Nic,Room,Pid}}, _From, Players) ->
OtherPlayers = lists:filter(fun ({PNic,_Room, _Pid}) -> PNic /= Nic end, Players),
{reply, ok, [{Nic,Room,Pid} | OtherPlayers]};

handle_call({remove_player, Nic}, _From, Players) ->
{reply, ok, (lists:filter(fun ({PNic,_Room, _Pid}) -> PNic /= Nic end, Players))};

handle_call({players_in_room, Room}, _From, Players) ->
Members = (lists:filter(fun ({_Nick, PRoom, _Pid}) -> PRoom =:= Room end, Players)),
{reply, {ok, Members}, Players};

handle_call(players, _From, Players) ->
{reply, {ok, Players}, Players}.

Expand Down
12 changes: 11 additions & 1 deletion test/player_tests.erl
Expand Up @@ -66,14 +66,24 @@ player_test_() ->[
?assertEqual({ok,"It's a kitchen\n\tmy wallet"},
Me:look()) end)
])
])
]),
?Describe("Communications",
[?It("should list the players registered", fun setup/0, fun cleanup/1,
begin
registry:add_player("Liv", kitchen, self),
Me = player:login("Tony", "Hello"),
?assertEqual({ok, "The players logged in are\nTony : lobby\nLiv : kitchen"},
Me:who()) end)
])
].

setup() ->
% I don't know why, but I need the print
% to make the kitchen test pass
io:format(""),
registry:start(),
stubs:fake_rooms().
cleanup(_Pid) ->
stubs:stop_fake_rooms(),
gen_server:cast(registry, stop),
true.
29 changes: 29 additions & 0 deletions test/registry_tests.erl
Expand Up @@ -7,7 +7,36 @@ registry_test_() -> [
begin
ok = registry:add_player("Tony", lobby, self) ,
?assertEqual({ok, [{"Tony", lobby, self}]},
registry:players()) end),
?It("should remove a player from a room", fun setup/0, fun cleanup/1,
begin
ok = registry:add_player("Tony", lobby, self),
ok = registry:remove_player("Tony"),
?assertEqual({ok, []},
registry:players()) end),
?It("should remove a player's old location if adding to a new room", fun setup/0, fun cleanup/1,
begin
ok = registry:add_player("Tony", lobby, self),
ok = registry:add_player("Tony", kitchen, self),
?assertEqual({ok, [{"Tony", kitchen, self}]},
registry:players()) end),
?It("should handle multiple players", fun setup/0, fun cleanup/1,
begin
ok = registry:add_player("Tony", lobby, self),
ok = registry:add_player("Liv", lobby, self),
ok = registry:add_player("Tony", kitchen, self),
?assertEqual({ok, [{"Tony", kitchen, self},
{"Liv", lobby, self}]},
registry:players()) end)
]),
?Describe("Interacting with rooms",
[?It("should show only people in a room", fun setup/0, fun cleanup/1,
begin
ok = registry:add_player("Tony", lobby, self),
ok = registry:add_player("Liv", lobby, self),
ok = registry:add_player("Marco", kitchen, self),
?assertEqual({ok, [{"Marco", kitchen, self}]},
registry:players_in_room(kitchen)) end)
])
].

Expand Down

0 comments on commit cb7f04e

Please sign in to comment.