Added hackfix for join_table hangups

This commit is contained in:
Jonatan Pålsson 2011-04-13 15:32:10 +02:00
parent 901d4b724c
commit 231bbfdf16
3 changed files with 30 additions and 16 deletions

View file

@ -112,14 +112,21 @@ handle_call({join_table, Table}, From, State) ->
Tables = State#co_state.tables,
case lists:keyfind(Table, 1, Tables) of
{_TableID, TablePID} ->
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!!
% TP = TablePID,
% {ok, Players} = (gen_server:call(TP, get_player_list_raw)), % Hack.. deadlock otherwise?
% %Players = [1],
% 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;
{PlayersAtTable,_} = string:to_integer(Table),
case ((length(State#co_state.players) / 2) < PlayersAtTable) and (length(State#co_state.players) > 1) of
true -> {reply , {error, table_full}, State};
false -> ggs_table:add_player(TablePID, FromPlayer),
{reply, {ok, TablePID}, State}
end;
false ->
back_up(State),

View file

@ -33,11 +33,17 @@ join_table(Num) ->
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]),
case ggs_coordinator:create_table({force, integer_to_list(Num)}) of
{ok, TBToken} -> ok
end,
case ggs_coordinator:join_table(integer_to_list(Num)) of
{ok, T} -> io:format("Creating new table: ~p~n", [T]),
T;
{error, E} -> erlang:display(E),
join_table(Num+1)
end;
{error, table_full} ->
erlang:display("Table full!"),
join_table(Num+1)
end.
@ -47,7 +53,7 @@ init([Socket]) ->
erlang:port_connect(Socket, self()),
Table = join_table(1337),
Table = join_table(1),
State = #state{
token = Token,
socket = Socket,

View file

@ -35,7 +35,7 @@ call(Pid, Msg) ->
% @doc adds a player to a table
add_player(Table, Player) ->
call(Table, {add_player, Player}).
gen_server:cast(Table, {add_player, Player}).
% @doc removes player form a table
remove_player(Table, Player) ->
@ -80,8 +80,6 @@ init([TableToken]) ->
players = [] }}.
%% @private
handle_call({add_player, Player}, _From, #state { players = Players } = State) ->
{reply, ok, State#state { players = [Player | Players] }};
handle_call({remove_player, Player}, _From, #state { players = Players } = State) ->
{reply, ok, State#state { players = Players -- [Player] }};
@ -109,6 +107,9 @@ handle_cast({notify, Player, Message}, #state { game_vm = GameVM } = State) ->
end,
{noreply, State};
handle_cast({add_player, Player}, #state { players = Players } = State) ->
{noreply, State#state { players = [Player | Players] }};
handle_cast({notify_game, Message, From}, #state { game_vm = GameVM } = State) ->
ggs_gamevm_p:player_command(GameVM, From, Message, ""),
{noreply, State};