Now all the tokens work
This commit is contained in:
parent
83cddf1c09
commit
9ff48a90b3
3 changed files with 47 additions and 18 deletions
|
@ -11,7 +11,9 @@
|
||||||
remove_player/2,
|
remove_player/2,
|
||||||
get_all_players/0,
|
get_all_players/0,
|
||||||
table_token_to_pid/1,
|
table_token_to_pid/1,
|
||||||
table_pid_to_token/1]).
|
table_pid_to_token/1,
|
||||||
|
player_pid_to_token/1,
|
||||||
|
player_token_to_pid/1]).
|
||||||
|
|
||||||
%% gen_server callback exports
|
%% gen_server callback exports
|
||||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
|
||||||
|
@ -74,6 +76,12 @@ table_token_to_pid(Token) ->
|
||||||
table_pid_to_token(Pid) ->
|
table_pid_to_token(Pid) ->
|
||||||
gen_server:call(?SERVER, {table_pid_to_token, Pid}).
|
gen_server:call(?SERVER, {table_pid_to_token, Pid}).
|
||||||
|
|
||||||
|
player_pid_to_token(Pid) ->
|
||||||
|
gen_server:call(?SERVER, {player_pid_to_token, Pid}).
|
||||||
|
|
||||||
|
player_token_to_pid(Token) ->
|
||||||
|
gen_server:call(?SERVER, {player_token_to_pid, Token}).
|
||||||
|
|
||||||
%% Just to shorten the name
|
%% Just to shorten the name
|
||||||
back_up(State) ->
|
back_up(State) ->
|
||||||
ggs_coordinator_backup:back_up(State),
|
ggs_coordinator_backup:back_up(State),
|
||||||
|
@ -91,10 +99,14 @@ init([]) ->
|
||||||
{ok, State}
|
{ok, State}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
handle_call(join_lobby, _From, State) ->
|
handle_call(join_lobby, From, State) ->
|
||||||
Token = helpers:get_new_token(),
|
Token = helpers:get_new_token(),
|
||||||
back_up(State),
|
Players = State#co_state.players,
|
||||||
{reply, {ok, Token}, State};
|
io:format("join_lobby from: ~p~n", [From]),
|
||||||
|
{Pid, Sock} = From,
|
||||||
|
NewState = State#co_state{players = [{Pid, Token} | Players]},
|
||||||
|
back_up(NewState),
|
||||||
|
{reply, {ok, Token}, NewState};
|
||||||
|
|
||||||
handle_call({join_table, Table}, From, State) ->
|
handle_call({join_table, Table}, From, State) ->
|
||||||
{FromPlayer, _Ref} = From,
|
{FromPlayer, _Ref} = From,
|
||||||
|
@ -134,6 +146,16 @@ handle_call({table_pid_to_token, Pid}, _From, State) ->
|
||||||
{Token, _} = lists:keyfind(Pid, 2, Tables),
|
{Token, _} = lists:keyfind(Pid, 2, Tables),
|
||||||
{reply, Token, State};
|
{reply, Token, State};
|
||||||
|
|
||||||
|
handle_call({player_pid_to_token, Pid}, _From, State) ->
|
||||||
|
Players = State#co_state.players,
|
||||||
|
{Pid, Token} = lists:keyfind(Pid, 1, Players),
|
||||||
|
{reply, Token, State};
|
||||||
|
|
||||||
|
handle_call({player_token_to_pid, Token}, _From, State) ->
|
||||||
|
Players = State#co_state.players,
|
||||||
|
{Pid, Token} = lists:keyfind(Token, 2, Players),
|
||||||
|
{reply, Pid, State};
|
||||||
|
|
||||||
handle_call(_Message, _From, State) ->
|
handle_call(_Message, _From, State) ->
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
||||||
|
|
|
@ -41,23 +41,23 @@ loop(Table) ->
|
||||||
loop(Table)
|
loop(Table)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
do_stuff(Command, Args, Player, Table) ->
|
do_stuff(Command, Args, PlayerToken, Table) ->
|
||||||
case Command of
|
case Command of
|
||||||
"greet" ->
|
"greet" ->
|
||||||
ggs_table:notify_player(Table, Player, server, "Hello there!\n");
|
ggs_table:notify_player(Table, PlayerToken, server, "Hello there!\n");
|
||||||
"chat" ->
|
"chat" ->
|
||||||
Nick = ggs_db:getItem(Table, nicks, Player),
|
Nick = ggs_db:getItem(Table, nicks, PlayerToken),
|
||||||
ggs_table:notify_all_players(Table, "<"++Nick++"> "++ Args ++ "\n");
|
ggs_table:notify_all_players(Table, "<"++Nick++"> "++ Args ++ "\n");
|
||||||
"uname" ->
|
"uname" ->
|
||||||
Uname = os:cmd("uname -a"),
|
Uname = os:cmd("uname -a"),
|
||||||
ggs_table:notify_player(Table, Player, server, Uname);
|
ggs_table:notify_player(Table, PlayerToken, server, Uname);
|
||||||
"lusers" ->
|
"lusers" ->
|
||||||
{ok, Players} = ggs_table:get_player_list(Table),
|
{ok, Players} = ggs_table:get_player_list(Table),
|
||||||
Nicks = lists:map(fun (P) -> ggs_db:getItem(Table, nicks, P) end, Players),
|
Nicks = lists:map(fun (P) -> ggs_db:getItem(Table, nicks, P) end, Players),
|
||||||
ggs_table:notify_player(Table, Player, server,io_lib:format("LUSERS ~p\n",[Nicks]));
|
ggs_table:notify_player(Table, PlayerToken, server,io_lib:format("LUSERS ~p\n",[Nicks]));
|
||||||
"nick" ->
|
"nick" ->
|
||||||
ggs_db:setItem(Table,nicks,Player,Args),
|
ggs_db:setItem(Table,nicks,PlayerToken,Args),
|
||||||
io:format("Changing nickname of ~p to ~p.", [Player, Args]);
|
io:format("Changing nickname of ~p to ~p.", [PlayerToken, Args]);
|
||||||
_Other ->
|
_Other ->
|
||||||
ggs_table:notify_player(Table, Player, server, "I don't know that command..\n")
|
ggs_table:notify_player(Table, PlayerToken, server, "I don't know that command..\n")
|
||||||
end.
|
end.
|
||||||
|
|
|
@ -44,6 +44,7 @@ remove_player(Table, Player) ->
|
||||||
%% @doc Get a list of all player processes attached to this table
|
%% @doc Get a list of all player processes attached to this table
|
||||||
get_player_list(TableToken) ->
|
get_player_list(TableToken) ->
|
||||||
TablePid = ggs_coordinator:table_token_to_pid(TableToken),
|
TablePid = ggs_coordinator:table_token_to_pid(TableToken),
|
||||||
|
erlang:display(TablePid),
|
||||||
gen_server:call(TablePid, get_player_list).
|
gen_server:call(TablePid, get_player_list).
|
||||||
|
|
||||||
% @doc stops the table process
|
% @doc stops the table process
|
||||||
|
@ -65,9 +66,10 @@ notify_game(TablePid, From, Message) ->
|
||||||
|
|
||||||
%% @doc Notify a player sitting at this table with the message supplied.
|
%% @doc Notify a player sitting at this table with the message supplied.
|
||||||
%% Player, Table and From are in token form.
|
%% Player, Table and From are in token form.
|
||||||
notify_player(TableToken, Player, From, Message) ->
|
notify_player(TableToken, PlayerToken, From, Message) ->
|
||||||
TablePid = ggs_coordinator:table_token_to_pid(TableToken),
|
TablePid = ggs_coordinator:table_token_to_pid(TableToken),
|
||||||
gen_server:cast(TablePid, {notify_player, Player, From, Message}).
|
%PlayerPid = ggs_coordinator:player_token_to_pid(PlayerToken),
|
||||||
|
gen_server:cast(TablePid, {notify_player, PlayerToken, From, Message}).
|
||||||
|
|
||||||
send_command(TableToken, PlayerToken, Command, Args) ->
|
send_command(TableToken, PlayerToken, Command, Args) ->
|
||||||
gen_logger:not_implemented().
|
gen_logger:not_implemented().
|
||||||
|
@ -94,7 +96,10 @@ handle_call({remove_player, Player}, _From, #state { players = Players } = State
|
||||||
{reply, ok, State#state { players = Players -- [Player] }};
|
{reply, ok, State#state { players = Players -- [Player] }};
|
||||||
|
|
||||||
handle_call(get_player_list, _From, #state { players = Players } = State) ->
|
handle_call(get_player_list, _From, #state { players = Players } = State) ->
|
||||||
{reply, {ok, Players}, State};
|
io:format("Players: ~p~n", [Players]),
|
||||||
|
TokenPlayers = lists:map(
|
||||||
|
fun (Pid) -> ggs_coordinator:player_pid_to_token(Pid) end, Players),
|
||||||
|
{reply, {ok, TokenPlayers}, 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]),
|
||||||
|
@ -102,11 +107,12 @@ handle_call(Msg, _From, State) ->
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
handle_cast({notify, Player, Message}, #state { game_vm = GameVM } = State) ->
|
handle_cast({notify, Player, Message}, #state { game_vm = GameVM } = State) ->
|
||||||
|
PlayerToken = ggs_coordinator:player_pid_to_token(Player),
|
||||||
case Message of
|
case Message of
|
||||||
{server, define, Args} ->
|
{server, define, Args} ->
|
||||||
ggs_gamevm_e:define(GameVM, Args);
|
ggs_gamevm_e:define(GameVM, Args);
|
||||||
{game, Command, Args} ->
|
{game, Command, Args} ->
|
||||||
ggs_gamevm_e:player_command(GameVM, Player, Command, Args)
|
ggs_gamevm_e:player_command(GameVM, PlayerToken, Command, Args)
|
||||||
end,
|
end,
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
|
|
||||||
|
@ -121,8 +127,9 @@ handle_cast({notify_all_players, Message}, #state{players = Players} = State) ->
|
||||||
),
|
),
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
|
|
||||||
handle_cast({notify_player, Player, From, Message}, State) ->
|
handle_cast({notify_player, PlayerToken, From, Message}, State) ->
|
||||||
ggs_player:notify(Player, From, Message),
|
PlayerPid = ggs_coordinator:player_token_to_pid(PlayerToken),
|
||||||
|
ggs_player:notify(PlayerPid, From, Message),
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
|
|
||||||
handle_cast(stop, State) ->
|
handle_cast(stop, State) ->
|
||||||
|
|
Reference in a new issue