Now we have multi-table support. !!!CAP IS AT 2 PLAYERS!!!
This commit is contained in:
parent
48506c69b7
commit
901d4b724c
4 changed files with 29 additions and 13 deletions
|
@ -1 +1 @@
|
|||
Subproject commit eb8ebe0347e1f75cbfe70b52f40a63d13741a89a
|
||||
Subproject commit 00312859714bef6e9a4fdb9931a41fef56eeb89a
|
|
@ -112,9 +112,15 @@ handle_call({join_table, Table}, From, State) ->
|
|||
Tables = State#co_state.tables,
|
||||
case lists:keyfind(Table, 1, Tables) of
|
||||
{_TableID, TablePID} ->
|
||||
ggs_table:add_player(TablePID, FromPlayer),
|
||||
back_up(State),
|
||||
{reply, {ok, TablePID}, State};
|
||||
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),
|
||||
{reply, {ok, TablePID}, State};
|
||||
PN when (PN >= 2) -> {reply, {error, table_full}, State} % TODO: Fix this limit!!
|
||||
end;
|
||||
false ->
|
||||
back_up(State),
|
||||
{reply, {error, no_such_table}, State}
|
||||
|
@ -137,6 +143,7 @@ handle_call(get_all_players, _From, State) ->
|
|||
%% Conversion tools
|
||||
handle_call({table_token_to_pid, Token}, _From, State) ->
|
||||
Tables = State#co_state.tables,
|
||||
erlang:display("Pre-keyfind"),
|
||||
{_, Pid} = lists:keyfind(Token, 1, Tables),
|
||||
{reply, Pid, State};
|
||||
|
||||
|
|
|
@ -27,21 +27,27 @@
|
|||
start(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]) ->
|
||||
{ok, Protocol} = ggs_protocol:start_link(),
|
||||
{ok, Token} = ggs_coordinator:join_lobby(),
|
||||
|
||||
erlang:port_connect(Socket, self()),
|
||||
|
||||
case ggs_coordinator:join_table("1337") of
|
||||
{ok, T} ->
|
||||
Table = T;
|
||||
{error, no_such_table} ->
|
||||
ggs_coordinator:create_table({force, "1337"}),
|
||||
{ok, T} = ggs_coordinator:join_table("1337"),
|
||||
Table = T
|
||||
end,
|
||||
|
||||
Table = join_table(1337),
|
||||
State = #state{
|
||||
token = Token,
|
||||
socket = Socket,
|
||||
|
|
|
@ -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),
|
||||
{reply, {ok, TokenPlayers}, State};
|
||||
|
||||
handle_call(get_player_list_raw, _From, #state { players = Players } = State) ->
|
||||
{reply, {ok, Players}, State};
|
||||
|
||||
handle_call(Msg, _From, State) ->
|
||||
error_logger:error_report([unknown_msg, Msg]),
|
||||
{reply, ok, State}.
|
||||
|
|
Reference in a new issue