Skip to content

Commit

Permalink
Uno player now registers with game.
Browse files Browse the repository at this point in the history
Oliver & Dave
  • Loading branch information
oferrigni committed Dec 15, 2011
1 parent eb565f6 commit 195043b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
11 changes: 7 additions & 4 deletions src/uno_game.erl
Expand Up @@ -11,7 +11,7 @@
-behaviour(gen_server).

%% API
-export([start_link/0, hello_amos/0, get_arg/0, register/0]).
-export([start_link/0, hello_amos/0, get_arg/0, register_player/0]).

%% gen_server callbacks
-export([init/1,
Expand All @@ -23,14 +23,14 @@

-define(SERVER, ?MODULE).

-record(state, {last_arg}).
-record(state, {last_arg, players = []}).

%%%===================================================================
%%% API
%%%===================================================================

register() ->
ok.
register_player() ->
gen_server:cast(?SERVER, {register,self()}).

%%--------------------------------------------------------------------
%% @doc
Expand Down Expand Up @@ -97,6 +97,9 @@ handle_call(_Request, _From, State) ->
%%--------------------------------------------------------------------
handle_cast({hello_amos, Arg}, _State) ->
{noreply, #state{last_arg = Arg}};
handle_cast({register, From}, State) ->
#state{players = Players} = State,
{noreply, #state{players = Players ++ [From]}};
handle_cast(_Msg, State) ->
{noreply, State}.

Expand Down
2 changes: 1 addition & 1 deletion src/uno_player.erl
Expand Up @@ -36,7 +36,7 @@ handle_cast(_Msg, State) ->

handle_info(timeout, _State) ->
io:format('in timeout~n'),
uno_game:register(),
uno_game:register_player(),
{noreply, ok};

handle_info(_Info, State) ->
Expand Down
9 changes: 7 additions & 2 deletions test/uno_game_test.erl
Expand Up @@ -5,5 +5,10 @@
-compile([export_all]).

handle_call_get_arg_should_return_last_arg_in_state_record_test() ->
Result = uno_game:handle_call(get_arg, from, {state, foo}),
?assertMatch({reply, foo, {state, foo}}, Result).
Result = uno_game:handle_call(get_arg, from, {state, foo, []}),
?assertMatch({reply, foo, {state, foo, []}}, Result).

handle_cast_when_register_should_add_from_state_test() ->
{noreply, State} = uno_game:handle_cast({register, from}, {state,
last_arg, []}),
?assertEqual({state,undefined, [from]}, State).
2 changes: 1 addition & 1 deletion test/uno_player_test.erl
Expand Up @@ -15,7 +15,7 @@ init_should_return_zero_to_cause_timeout_test() ->
handle_info_when_timeout_should_register_with_game_test() ->
meck:new(uno_game),
try
meck:expect(uno_game, register, 0, []),
meck:expect(uno_game, register_player, 0, []),
uno_player:handle_info(timeout, state)
after
meck:unload()
Expand Down

0 comments on commit 195043b

Please sign in to comment.