Now we have multi-table support. !!!CAP IS AT 2 PLAYERS!!!

This commit is contained in:
Jonatan Pålsson 2011-04-13 11:38:36 +02:00
parent 48506c69b7
commit 901d4b724c
4 changed files with 29 additions and 13 deletions

@ -1 +1 @@
Subproject commit eb8ebe0347e1f75cbfe70b52f40a63d13741a89a Subproject commit 00312859714bef6e9a4fdb9931a41fef56eeb89a

View file

@ -112,9 +112,15 @@ handle_call({join_table, Table}, From, State) ->
Tables = State#co_state.tables, Tables = State#co_state.tables,
case lists:keyfind(Table, 1, Tables) of case lists:keyfind(Table, 1, Tables) of
{_TableID, TablePID} -> {_TableID, TablePID} ->
ggs_table:add_player(TablePID, FromPlayer), TP = TablePID,
{ok, Players} = (gen_server:call(TP, get_player_list_raw)), % Hack.. deadlock otherwise?
NumPlayers = length(Players),
case NumPlayers of
PN when (PN < 2) -> ggs_table:add_player(TablePID, FromPlayer),
back_up(State), back_up(State),
{reply, {ok, TablePID}, State}; {reply, {ok, TablePID}, State};
PN when (PN >= 2) -> {reply, {error, table_full}, State} % TODO: Fix this limit!!
end;
false -> false ->
back_up(State), back_up(State),
{reply, {error, no_such_table}, State} {reply, {error, no_such_table}, State}
@ -137,6 +143,7 @@ handle_call(get_all_players, _From, State) ->
%% Conversion tools %% Conversion tools
handle_call({table_token_to_pid, Token}, _From, State) -> handle_call({table_token_to_pid, Token}, _From, State) ->
Tables = State#co_state.tables, Tables = State#co_state.tables,
erlang:display("Pre-keyfind"),
{_, Pid} = lists:keyfind(Token, 1, Tables), {_, Pid} = lists:keyfind(Token, 1, Tables),
{reply, Pid, State}; {reply, Pid, State};

View file

@ -27,21 +27,27 @@
start(Socket) -> start(Socket) ->
gen_server:start(?MODULE, [Socket], []). gen_server:start(?MODULE, [Socket], []).
join_table(Num) ->
case ggs_coordinator:join_table(integer_to_list(Num)) of
{ok, T} ->
io:format("Joining existing table: ~p~n", [T]),
T;
{error, no_such_table} ->
ggs_coordinator:create_table({force, integer_to_list(Num)}),
{ok, T} = ggs_coordinator:join_table(integer_to_list(Num)),
io:format("Creating new table: ~p~n", [T]),
T;
{error, table_full} ->
join_table(Num+1)
end.
init([Socket]) -> init([Socket]) ->
{ok, Protocol} = ggs_protocol:start_link(), {ok, Protocol} = ggs_protocol:start_link(),
{ok, Token} = ggs_coordinator:join_lobby(), {ok, Token} = ggs_coordinator:join_lobby(),
erlang:port_connect(Socket, self()), erlang:port_connect(Socket, self()),
case ggs_coordinator:join_table("1337") of Table = join_table(1337),
{ok, T} ->
Table = T;
{error, no_such_table} ->
ggs_coordinator:create_table({force, "1337"}),
{ok, T} = ggs_coordinator:join_table("1337"),
Table = T
end,
State = #state{ State = #state{
token = Token, token = Token,
socket = Socket, socket = Socket,

View file

@ -91,6 +91,9 @@ handle_call(get_player_list, _From, #state { players = Players } = State) ->
fun (Pid) -> ggs_coordinator:player_pid_to_token(Pid) end, Players), fun (Pid) -> ggs_coordinator:player_pid_to_token(Pid) end, Players),
{reply, {ok, TokenPlayers}, State}; {reply, {ok, TokenPlayers}, State};
handle_call(get_player_list_raw, _From, #state { players = Players } = State) ->
{reply, {ok, Players}, State};
handle_call(Msg, _From, State) -> handle_call(Msg, _From, State) ->
error_logger:error_report([unknown_msg, Msg]), error_logger:error_report([unknown_msg, Msg]),
{reply, ok, State}. {reply, ok, State}.