Now we have join_table, create_table and join_lobby + test cases
This commit is contained in:
parent
818205e19b
commit
3f41769233
3 changed files with 40 additions and 9 deletions
|
@ -1,7 +1,8 @@
|
|||
-module(ggs_coordinator).
|
||||
|
||||
%% API Exports
|
||||
-export([start_link/0, stop/1, join_table/1, create_table/1]).
|
||||
-export([start_link/0, stop/1, join_table/1, create_table/1, join_lobby/0,
|
||||
respawn_player/2, respawn_table/1, remove_player/2]).
|
||||
|
||||
%% gen_server callback exports
|
||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
|
||||
|
@ -11,7 +12,8 @@
|
|||
-record(co_state,
|
||||
{players = [], % List of all player processes
|
||||
player_table_map = [], % Players <-> Table map
|
||||
table_state_map = []}). % Table <-> Table state map
|
||||
table_state_map = [],
|
||||
tables = []}). % Table <-> Table state map
|
||||
|
||||
%% @doc This module act as "the man in the middle".
|
||||
%% Creates the starting connection between table and players.
|
||||
|
@ -36,7 +38,7 @@ create_table(Params) ->
|
|||
%% @doc This is the first function run by a newly created players.
|
||||
%% Generates a unique token that we use to identify the player.
|
||||
join_lobby() ->
|
||||
ggs_logger:not_implemented().
|
||||
gen_server:call(ggs_coordinator, join_lobby).
|
||||
|
||||
%% @doc Act as a supervisor to player and respawns player when it gets bad data.
|
||||
respawn_player(_Player, _Socket) ->
|
||||
|
@ -55,12 +57,26 @@ remove_player(_From, _Player) ->
|
|||
init([]) ->
|
||||
{ok, #co_state{}}.
|
||||
|
||||
handle_call({join_table, Table}, From, State) ->
|
||||
{reply, {error, no_such_table}, State};
|
||||
handle_call(join_lobby, _From, State) ->
|
||||
Token = helpers:get_new_token(),
|
||||
{reply, {ok, Token}, State};
|
||||
|
||||
handle_call({join_table, Table}, _From, State) ->
|
||||
Tables = State#co_state.tables,
|
||||
case lists:keyfind(Table, 1, Tables) of
|
||||
{Table} ->
|
||||
{reply, {ok, Table}, State}; %% @TODO: Also add player to table
|
||||
false ->
|
||||
{reply, {error, no_such_table}, State}
|
||||
end;
|
||||
|
||||
handle_call({create_table, {force, TID}}, From, State) ->
|
||||
TIDs = State#co_state.player_table_map,
|
||||
{reply, {ok, TID}, State#co_state{player_table_map = [TID | TIDs]}};
|
||||
TIDMap = State#co_state.player_table_map,
|
||||
Tables = State#co_state.tables,
|
||||
{reply, {ok, TID}, State#co_state{
|
||||
player_table_map = [{From, TID} | TIDMap],
|
||||
tables = [{TID} | TID]
|
||||
}};
|
||||
|
||||
handle_call(_Message, _From, State) ->
|
||||
{noreply, State}.
|
||||
|
|
|
@ -1,4 +1,8 @@
|
|||
-module(helpers).
|
||||
-export([not_implemented/0, get_new_token/0]).
|
||||
|
||||
not_implemented() ->
|
||||
exit("Not implemented").
|
||||
|
||||
get_new_token() ->
|
||||
string:strip(os:cmd("uuidgen"), right, $\n ).
|
||||
|
|
|
@ -15,7 +15,7 @@ coordinator_test_() ->
|
|||
fun test_start_link/0,
|
||||
fun test_stop/0,
|
||||
fun test_join_bad_table/0,
|
||||
fun test_create_table/0
|
||||
fun test_join_lobby/0
|
||||
]
|
||||
}.
|
||||
|
||||
|
@ -34,9 +34,20 @@ test_join_bad_table() ->
|
|||
Response = ggs_coordinator:join_table("Nonexistant table"),
|
||||
?assert(Response == {error, no_such_table}).
|
||||
|
||||
test_join_lobby() ->
|
||||
{Response, _} = ggs_coordinator:join_lobby(),
|
||||
?assert(Response /= error).
|
||||
|
||||
test_create_table() ->
|
||||
%% 'Manual' tests
|
||||
|
||||
create_table_test() ->
|
||||
{ok, _Coord} = ggs_coordinator:start_link(),
|
||||
timer:sleep(100),
|
||||
% Forcibly create a table. This functionality should be disabled
|
||||
% in the production system, but is pretty nice for testing.
|
||||
Response = ggs_coordinator:create_table({force, 1337}),
|
||||
?assert(Response == {ok, 1337}).
|
||||
|
||||
join_good_table_test() ->
|
||||
Response = ggs_coordinator:join_table(1337),
|
||||
?assert(Response == {ok, 1337}).
|
||||
|
|
Reference in a new issue