Now gga_gamevm_e uses tokens to identify tables

This commit is contained in:
Jonatan Pålsson 2011-02-26 00:01:49 +01:00
parent a20f9351d4
commit 83cddf1c09
5 changed files with 77 additions and 35 deletions

View file

@ -61,15 +61,15 @@ function _GGS(tableToken) {
var tableToken = this.tableToken; var tableToken = this.tableToken;
this.__defineGetter__("tableToken", function() { this.__defineGetter__("tableToken", function() {
return tableToken; return tableToken;
}) });
} }
/*
function _GGS.prototype.sendCommandToAll(command, args) { function _GGS.prototype.sendCommandToAll(command, args) {
var message = "{" + command + "," + args + "}"; var message = "{" + command + "," + args + "}";
callErlang("ggs_table send_command_to_all " + escapeErlang([this.tableToken, message])); callErlang("ggs_table send_command_to_all " + escapeErlang([this.tableToken, message]));
} }
*/
function _GGS.prototype.serverLog(message) { function _GGS.prototype.serverLog(message) {
callErlang("error_logger info_msg " + escapeErlang([message])) callErlang("error_logger info_msg " + escapeErlang([message]))
} }

View file

@ -1,8 +1,17 @@
-module(ggs_coordinator). -module(ggs_coordinator).
%% API Exports %% API Exports
-export([start_link/0, stop/1, join_table/1, create_table/1, join_lobby/0, -export([ start_link/0,
respawn_player/2, respawn_table/1, remove_player/2, get_all_players/0]). stop/1,
join_table/1,
create_table/1,
join_lobby/0,
respawn_player/2,
respawn_table/1,
remove_player/2,
get_all_players/0,
table_token_to_pid/1,
table_pid_to_token/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,
@ -56,6 +65,15 @@ remove_player(_From, _Player) ->
get_all_players() -> get_all_players() ->
gen_server:call(?SERVER, get_all_players). gen_server:call(?SERVER, get_all_players).
%% Conversion tools
table_token_to_pid(Token) ->
gen_server:call(?SERVER, {table_token_to_pid, Token}).
table_pid_to_token(Pid) ->
gen_server:call(?SERVER, {table_pid_to_token, Pid}).
%% 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,20 +109,31 @@ handle_call({join_table, Table}, From, State) ->
{reply, {error, no_such_table}, State} {reply, {error, no_such_table}, State}
end; end;
handle_call({create_table, {force, TableID}}, From, State) -> handle_call({create_table, {force, TableToken}}, From, State) ->
TableIDMap = State#co_state.player_table_map, TableIDMap = State#co_state.player_table_map,
Tables = State#co_state.tables, Tables = State#co_state.tables,
NewTableProc = ggs_table:start(), % With start_link, the table dies with the coordinator NewTableProc = ggs_table:start(TableToken), % With start_link, the table dies with the coordinator
NewState = State#co_state{ NewState = State#co_state{
player_table_map = [{From, TableID} | TableIDMap], player_table_map = [{From, TableToken} | TableIDMap],
tables = [{TableID, NewTableProc} | Tables] tables = [{TableToken, NewTableProc} | Tables]
}, },
back_up(NewState), back_up(NewState),
{reply, {ok, TableID}, NewState}; {reply, {ok, TableToken}, NewState};
handle_call(get_all_players, _From, State) -> handle_call(get_all_players, _From, State) ->
{reply, State#co_state.players, State}; {reply, State#co_state.players, State};
%% Conversion tools
handle_call({table_token_to_pid, Token}, _From, State) ->
Tables = State#co_state.tables,
{_, Pid} = lists:keyfind(Token, 1, Tables),
{reply, Pid, State};
handle_call({table_pid_to_token, Pid}, _From, State) ->
Tables = State#co_state.tables,
{Token, _} = lists:keyfind(Pid, 2, Tables),
{reply, Token, State};
handle_call(_Message, _From, State) -> handle_call(_Message, _From, State) ->
{noreply, State}. {noreply, State}.

View file

@ -13,8 +13,6 @@
%% API %% API
-export([start_link/1, define/2, player_command/4, stop/1, call_js/2]). -export([start_link/1, define/2, player_command/4, stop/1, call_js/2]).
-include_lib("eunit/include/eunit.hrl").
%% ---------------------------------------------------------------------- %% ----------------------------------------------------------------------
% API implementation % API implementation
@ -56,8 +54,8 @@ stop(GameVM) ->
init([Table]) -> init([Table]) ->
process_flag(trap_exit, true), process_flag(trap_exit, true),
{ok, Port} = js_driver:new(), {ok, Port} = js_driver:new(),
%% @TODO: add here default JS API instead % %% @TODO: add here default JS API instead
{ok, JSAPISourceCode} = file:read_file("ggs_api.js"), {ok, JSAPISourceCode} = file:read_file("src/ggs_api.js"),
ok = js:define(Port, JSAPISourceCode), ok = js:define(Port, JSAPISourceCode),
{ok, #state { port = Port, table = Table }}. {ok, #state { port = Port, table = Table }}.

View file

@ -44,20 +44,20 @@ loop(Table) ->
do_stuff(Command, Args, Player, Table) -> do_stuff(Command, Args, Player, Table) ->
case Command of case Command of
"greet" -> "greet" ->
ggs_player:notify(Player, server, "Hello there!\n"); ggs_table:notify_player(Table, Player, server, "Hello there!\n");
"chat" -> "chat" ->
Nick = ggs_db:getItem(Table, nicks, Player), Nick = ggs_db:getItem(Table, nicks, Player),
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_player:notify(Player, server, Uname); ggs_table:notify_player(Table, Player, 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_player:notify(Player, server,io_lib:format("LUSERS ~p\n",[Nicks])); ggs_table:notify_player(Table, Player, server,io_lib:format("LUSERS ~p\n",[Nicks]));
"nick" -> "nick" ->
ggs_db:setItem(Table,nicks,Player,Args), ggs_db:setItem(Table,nicks,Player,Args),
io:format("Changing nickname of ~p to ~p.", [Player, Args]); io:format("Changing nickname of ~p to ~p.", [Player, Args]);
_Other -> _Other ->
ggs_player:notify(Player, server, "I don't know that command..\n") ggs_table:notify_player(Table, Player, server, "I don't know that command..\n")
end. end.

View file

@ -10,22 +10,23 @@
-record(state, { players, game_vm } ). -record(state, { players, game_vm } ).
%% API %% API
-export([start/0, -export([start/1,
add_player/2, add_player/2,
remove_player/2, remove_player/2,
stop/1, stop/1,
notify/3, notify/3,
notify_all_players/2, notify_all_players/2,
notify_game/3, notify_game/3,
get_player_list/1]). get_player_list/1,
notify_player/4]).
%% ---------------------------------------------------------------------- %% ----------------------------------------------------------------------
% API implementation % API implementation
% @doc returns a new table % @doc returns a new table
start() -> start(Token) ->
{ok, Pid} = gen_server:start(?MODULE, [], []), {ok, Pid} = gen_server:start(?MODULE, [Token], []),
Pid. Pid.
%% @private %% @private
@ -41,22 +42,32 @@ remove_player(Table, Player) ->
call(Table, {remove_player, Player}). call(Table, {remove_player, 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(Table) -> get_player_list(TableToken) ->
gen_server:call(Table, get_player_list). TablePid = ggs_coordinator:table_token_to_pid(TableToken),
gen_server:call(TablePid, get_player_list).
% @doc stops the table process % @doc stops the table process
stop(Table) -> stop(Table) ->
gen_server:cast(Table, stop). gen_server:cast(Table, stop).
% @doc notifies the table with a message from a player % @doc notifies the table with a message from a player
notify(Table, Player, Message) -> notify(TablePid, Player, Message) ->
gen_server:cast(Table, {notify, Player, Message}). %TablePid = ggs_coordinator:table_token_to_pid(TableToken),
gen_server:cast(TablePid, {notify, Player, Message}).
notify_all_players(Table, Message) -> notify_all_players(TableToken, Message) ->
gen_server:cast(Table, {notify_all_players, Message}). TablePid = ggs_coordinator:table_token_to_pid(TableToken),
gen_server:cast(TablePid, {notify_all_players, Message}).
notify_game(Table, From, Message) -> notify_game(TablePid, From, Message) ->
gen_server:cast(Table, {notify_game, Message, From}). TableToken = ggs_coordinator:table_pid_to_token(TablePid),
gen_server:cast(TableToken, {notify_game, Message, From}).
%% @doc Notify a player sitting at this table with the message supplied.
%% Player, Table and From are in token form.
notify_player(TableToken, Player, From, Message) ->
TablePid = ggs_coordinator:table_token_to_pid(TableToken),
gen_server:cast(TablePid, {notify_player, Player, From, Message}).
send_command(TableToken, PlayerToken, Command, Args) -> send_command(TableToken, PlayerToken, Command, Args) ->
gen_logger:not_implemented(). gen_logger:not_implemented().
@ -68,9 +79,9 @@ send_command_to_all(TableToken, Command, Args) ->
%% ---------------------------------------------------------------------- %% ----------------------------------------------------------------------
%% @private %% @private
init([]) -> init([TableToken]) ->
process_flag(trap_exit, true), process_flag(trap_exit, true),
GameVM = ggs_gamevm:start_link(self()), GameVM = ggs_gamevm_e:start_link(TableToken),
{ok, #state { {ok, #state {
game_vm = GameVM, game_vm = GameVM,
players = [] }}. players = [] }}.
@ -93,14 +104,14 @@ handle_call(Msg, _From, State) ->
handle_cast({notify, Player, Message}, #state { game_vm = GameVM } = State) -> handle_cast({notify, Player, Message}, #state { game_vm = GameVM } = State) ->
case Message of case Message of
{server, define, Args} -> {server, define, Args} ->
ggs_gamevm:define(GameVM, Args); ggs_gamevm_e:define(GameVM, Args);
{game, Command, Args} -> {game, Command, Args} ->
ggs_gamevm:player_command(GameVM, Player, Command, Args) ggs_gamevm_e:player_command(GameVM, Player, Command, Args)
end, end,
{noreply, State}; {noreply, State};
handle_cast({notify_game, Message, From}, #state { game_vm = GameVM } = State) -> handle_cast({notify_game, Message, From}, #state { game_vm = GameVM } = State) ->
ggs_gamevm:player_command(GameVM, From, Message, ""), ggs_gamevm_e:player_command(GameVM, From, Message, ""),
{noreply, State}; {noreply, State};
handle_cast({notify_all_players, Message}, #state{players = Players} = State) -> handle_cast({notify_all_players, Message}, #state{players = Players} = State) ->
@ -110,6 +121,10 @@ handle_cast({notify_all_players, Message}, #state{players = Players} = State) ->
), ),
{noreply, State}; {noreply, State};
handle_cast({notify_player, Player, From, Message}, State) ->
ggs_player:notify(Player, From, Message),
{noreply, State};
handle_cast(stop, State) -> handle_cast(stop, State) ->
{stop, normal, State}; {stop, normal, State};
handle_cast(Msg, S) -> handle_cast(Msg, S) ->