Added hackfix for join_table hangups
This commit is contained in:
parent
901d4b724c
commit
231bbfdf16
3 changed files with 30 additions and 16 deletions
|
@ -112,14 +112,21 @@ 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} ->
|
||||||
TP = TablePID,
|
% TP = TablePID,
|
||||||
{ok, Players} = (gen_server:call(TP, get_player_list_raw)), % Hack.. deadlock otherwise?
|
% {ok, Players} = (gen_server:call(TP, get_player_list_raw)), % Hack.. deadlock otherwise?
|
||||||
NumPlayers = length(Players),
|
% %Players = [1],
|
||||||
case NumPlayers of
|
% NumPlayers = length(Players),
|
||||||
PN when (PN < 2) -> ggs_table:add_player(TablePID, FromPlayer),
|
% case NumPlayers of
|
||||||
back_up(State),
|
% PN when (PN < 2) -> ggs_table:add_player(TablePID, FromPlayer),
|
||||||
{reply, {ok, TablePID}, State};
|
% back_up(State),
|
||||||
PN when (PN >= 2) -> {reply, {error, table_full}, State} % TODO: Fix this limit!!
|
% {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;
|
end;
|
||||||
false ->
|
false ->
|
||||||
back_up(State),
|
back_up(State),
|
||||||
|
|
|
@ -33,11 +33,17 @@ join_table(Num) ->
|
||||||
io:format("Joining existing table: ~p~n", [T]),
|
io:format("Joining existing table: ~p~n", [T]),
|
||||||
T;
|
T;
|
||||||
{error, no_such_table} ->
|
{error, no_such_table} ->
|
||||||
ggs_coordinator:create_table({force, integer_to_list(Num)}),
|
case ggs_coordinator:create_table({force, integer_to_list(Num)}) of
|
||||||
{ok, T} = ggs_coordinator:join_table(integer_to_list(Num)),
|
{ok, TBToken} -> ok
|
||||||
io:format("Creating new table: ~p~n", [T]),
|
end,
|
||||||
T;
|
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} ->
|
{error, table_full} ->
|
||||||
|
erlang:display("Table full!"),
|
||||||
join_table(Num+1)
|
join_table(Num+1)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
@ -47,7 +53,7 @@ init([Socket]) ->
|
||||||
|
|
||||||
erlang:port_connect(Socket, self()),
|
erlang:port_connect(Socket, self()),
|
||||||
|
|
||||||
Table = join_table(1337),
|
Table = join_table(1),
|
||||||
State = #state{
|
State = #state{
|
||||||
token = Token,
|
token = Token,
|
||||||
socket = Socket,
|
socket = Socket,
|
||||||
|
|
|
@ -35,7 +35,7 @@ call(Pid, Msg) ->
|
||||||
|
|
||||||
% @doc adds a player to a table
|
% @doc adds a player to a table
|
||||||
add_player(Table, Player) ->
|
add_player(Table, Player) ->
|
||||||
call(Table, {add_player, Player}).
|
gen_server:cast(Table, {add_player, Player}).
|
||||||
|
|
||||||
% @doc removes player form a table
|
% @doc removes player form a table
|
||||||
remove_player(Table, Player) ->
|
remove_player(Table, Player) ->
|
||||||
|
@ -80,8 +80,6 @@ init([TableToken]) ->
|
||||||
players = [] }}.
|
players = [] }}.
|
||||||
|
|
||||||
%% @private
|
%% @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) ->
|
handle_call({remove_player, Player}, _From, #state { players = Players } = State) ->
|
||||||
{reply, ok, State#state { players = Players -- [Player] }};
|
{reply, ok, State#state { players = Players -- [Player] }};
|
||||||
|
@ -109,6 +107,9 @@ handle_cast({notify, Player, Message}, #state { game_vm = GameVM } = State) ->
|
||||||
end,
|
end,
|
||||||
{noreply, State};
|
{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) ->
|
handle_cast({notify_game, Message, From}, #state { game_vm = GameVM } = State) ->
|
||||||
ggs_gamevm_p:player_command(GameVM, From, Message, ""),
|
ggs_gamevm_p:player_command(GameVM, From, Message, ""),
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
|
|
Reference in a new issue