Skip to content

Commit

Permalink
Added some database awareness to rooms
Browse files Browse the repository at this point in the history
  • Loading branch information
Ball committed Jun 13, 2011
1 parent d24f41f commit 42a6166
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/room.erl
@@ -1,21 +1,33 @@
-module(room). -module(room).
-include("src/records.hrl"). -include("src/records.hrl").
-include_lib("stdlib/include/qlc.hrl").
-behavior(gen_server). -behavior(gen_server).


%% API %% API
-export([start_room/4,start_room/3, add_to_room/2, take_from_room/2, direction/2, describe/1]). -export([start_from_db/0, start_from_db/1, start_room/4,start_room/3, add_to_room/2, take_from_room/2, direction/2, describe/1]).


%% gen_server callbacks %% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]). terminate/2, code_change/3]).


start_room(Record=#room{key=Key}) ->
gen_server:start_link({global,Key}, ?MODULE, [Record], []).

start_room(Key,Name,Description,Exits) -> start_room(Key,Name,Description,Exits) ->
Room = #room{key=Key,name=Name,description=Description,exits=Exits}, Room = #room{key=Key,name=Name,description=Description,exits=Exits},
gen_server:start_link({global,Key}, ?MODULE,[Room], []). gen_server:start_link({global,Key}, ?MODULE,[Room], []).


start_room(Key,Name,Description) -> start_room(Key,Name,Description) ->
Room = #room{key=Key,name=Name,description=Description}, Room = #room{key=Key,name=Name,description=Description},
gen_server:start_link({global,Key}, ?MODULE, [Room], []). gen_server:start_link({global,Key}, ?MODULE, [Room], []).
start_from_db() ->
{atomic, Rows} = mnesia:transaction(fun() ->
qlc:eval( qlc:q([ X || X <- mnesia:table(room)])) end),
lists:map( fun (A) -> start_room(A) end, Rows).
start_from_db(Key) ->
{atomic, [Row]}=mnesia:transaction(fun() ->
mnesia:read({room,Key}) end),
start_room(Row).


add_to_room(RoomName,Item) -> add_to_room(RoomName,Item) ->
gen_server:call({global,RoomName}, {add_item, Item}). gen_server:call({global,RoomName}, {add_item, Item}).
Expand Down
44 changes: 44 additions & 0 deletions test/room_tests.erl
@@ -1,4 +1,5 @@
-module(room_tests). -module(room_tests).
-include("../src/records.hrl").
-include("tests.hrl"). -include("tests.hrl").


room_test_() -> [ room_test_() -> [
Expand Down Expand Up @@ -35,5 +36,48 @@ room_test_() -> [
]) ])
]. ].


room_database_test_() -> [
?Describe("run a room from the database",
[?It("should load a stable from the database",
fun insert_stable/0, fun delete_stable/1,
begin
room:start_from_db(stable),
{ok,Description} = room:describe(stable),
?assertEqual("A stable from the database",Description)
end)
]),
?Describe("run all rooms in the database",
[?It("should start multiple rooms",
fun insert_stable/0, fun delete_stable/1,
begin
room:start_from_db(),
{ok,Description1} = room:describe(stable),
{ok,Description2} = room:describe(yard),
?assertEqual("A stable from the database", Description1),
?assertEqual("A yard from the database", Description2)
end)
])
].

insert_stable() ->
mnesia:transaction(fun() ->
mnesia:write(
#room{key=yard,
name="The horse yard",
description="A yard from the database"}
),
mnesia:write(
#room{key=stable,
name="The king's stable",
description="A stable from the database"}
)
end).
delete_stable(_Pid) ->
gen_server:cast({global, stable}, stop),
mnesia:transaction(fun() ->
mnesia:delete(room, yard),
mnesia:delete(room, stable) end),
true.

setup()-> io:format(""),stubs:fake_rooms(). setup()-> io:format(""),stubs:fake_rooms().
cleanup(_Pid) -> stubs:stop_fake_rooms(), true. cleanup(_Pid) -> stubs:stop_fake_rooms(), true.

0 comments on commit 42a6166

Please sign in to comment.