Merge branch 'rewrite' into jonte_rewrite

Conflicts:
	src/ggs_gamevm.erl
This commit is contained in:
Jonatan Pålsson 2011-02-21 15:12:27 +01:00
commit daf48fd606
3 changed files with 154 additions and 44 deletions

View file

@ -1,23 +1,34 @@
-module(ggs_gamevm).
-export([start_link/0, define/2, user_command/4]).
%% @doc This module is responsible for running the game VM:s. You can issue %% @doc This module is responsible for running the game VM:s. You can issue
%% commands to a vm using this module. %% commands to a vm using this module.
-module(ggs_gamevm).
-behaviour(gen_server).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-record(state, { port, table } ).
%% API
-export([start_link/1, define/2, user_command/4, stop/1, call_js/2]).
-include_lib("eunit/include/eunit.hrl").
%% ----------------------------------------------------------------------
% API implementation
%% @doc Create a new VM process. The process ID is returned and can be used %% @doc Create a new VM process. The process ID is returned and can be used
%% with for example the define method of this module. %% with for example the define method of this module.
start_link() -> start_link(Table) ->
PortPid = spawn_link( fun() -> erlang_js:start(), %% @TODO: should only be done once
process_flag(trap_exit, true), {ok, Pid} = gen_server:start_link(?MODULE, [Table], []),
{ok, Port} = js_driver:new(), Pid.
js:define(Port, <<"function userCommand(user, command, args){return 'Hello world';}">>),
loop(Port)
end ),
PortPid.
%% @doc Define some new code on the specified VM, returns the atom ok. %% @doc Define some new code on the specified VM, returns the atom ok.
define(GameVM, SourceCode) -> define(GameVM, SourceCode) ->
GameVM ! {define,SourceCode}, gen_server:cast(GameVM, {define, SourceCode}).
ok.
%% @doc Execute a user command on the specified VM. This function is %% @doc Execute a user command on the specified VM. This function is
%% asynchronous, and returns ok. %% asynchronous, and returns ok.
@ -27,25 +38,85 @@ define(GameVM, SourceCode) ->
%% Command = a game command to run %% Command = a game command to run
%% Args = arguments for the Command parameter %% Args = arguments for the Command parameter
user_command(GameVM, Player, Command, Args) -> user_command(GameVM, Player, Command, Args) ->
Ref = make_ref(), gen_server:cast(GameVM, {user_command, Player, Command, Args}).
GameVM ! {user_command, Player, Command, Args, self(), Ref},
%% @private
% only for tests
call_js(GameVM, SourceCode) ->
gen_server:call(GameVM, {eval, SourceCode}).
% @doc stops the gamevm process
stop(GameVM) ->
gen_server:cast(GameVM, stop).
%% ----------------------------------------------------------------------
%% @private
init([Table]) ->
process_flag(trap_exit, true),
{ok, Port} = js_driver:new(),
%% @TODO: add here default JS API instead
{ok, #state { port = Port, table = Table }}.
%% private
% only needed for the tests
handle_call({eval, SourceCode}, _From, #state { port = Port } = State) ->
{ok, Ret} = js:eval(Port, list_to_binary(SourceCode)),
{reply, Ret, State}.
%% @private
handle_cast({define, SourceCode}, #state { port = Port } = State) ->
ok = js:define(Port, list_to_binary(SourceCode)),
{noreply, State};
handle_cast({user_command, Player, Command, Args}, #state { port = Port } = State) ->
Arguments = string:concat("'", string:concat(
string:join([js_escape(Player), js_escape(Command), js_escape(Args)], "','"), "'")),
Js = list_to_binary(string:concat(string:concat("userCommand(", Arguments), ");")),
js_driver:define_js(Port, Js),
{noreply, State};
handle_cast(stop, State) ->
{stop, normal, State};
handle_cast(Msg, S) ->
error_logger:error_report([unknown_msg, Msg]),
{noreply, S}.
%% @private
handle_info(Msg, S) ->
error_logger:error_report([unknown_msg, Msg]),
{noreply, S}.
%% @private
terminate(_Reason, _State) ->
ok. ok.
%% Helper functions %% @private
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
loop(Port) -> js_escape(S) ->
receive lists:flatmap(fun($\') -> [$\\, $\']; (X) -> [X] end, S).
{define, SourceCode} ->
ok = js:define(Port, list_to_binary(SourceCode)), %% ----------------------------------------------------------------------
loop(Port); % Tests
{user_command, User, Command, Args, From, Ref} ->
{ok, Ret} = js:call(Port, <<"userCommand">>, start_link_test() ->
[ list_to_binary(User), erlang_js:start(), %% @TODO: should only be done once
list_to_binary(Command), GameVM = start_link(test_table),
list_to_binary(Args) ?assertNot(GameVM =:= undefined).
]),
From ! {Ref, Ret}, define_test() ->
loop(Port); GameVM = start_link(test_table),
{eval, JS} -> define(GameVM, "function hello(test) { return test; }"),
?assertMatch(<<"jeena">>, gen_server:call(GameVM, {eval, "hello('jeena')"})).
stop_test() ->
GameVM = start_link(test_table),
ok = stop(GameVM).
user_command_test() ->
GameVM = start_link(test_table),
define(GameVM, "var t = '';\nfunction userCommand(user, command, args) { t = user + command + args; }\n"),
user_command(GameVM, "'jeena", "thecommand", "theargs'"),
?assertMatch(<<"'jeenathecommandtheargs'">>, gen_server:call(GameVM, {eval, "t;"})).
end.

View file

@ -1,4 +1,4 @@
%% @doc This module represents a Player with a Socket and a Token %% @doc This module represents a table with players
-module(ggs_table). -module(ggs_table).
-behaviour(gen_server). -behaviour(gen_server).
@ -8,7 +8,7 @@
terminate/2, code_change/3, notify_all_players/2, notify_game/3, terminate/2, code_change/3, notify_all_players/2, notify_game/3,
get_player_list/1]). get_player_list/1]).
-record(state, { token, players, socket, game_vm } ). -record(state, { players, game_vm } ).
%% API %% API
-export([start_link/0, -export([start_link/0,
@ -25,7 +25,6 @@
% @doc returns a new table % @doc returns a new table
start_link() -> start_link() ->
{ok, Pid} = gen_server:start_link(?MODULE, [], []), {ok, Pid} = gen_server:start_link(?MODULE, [], []),
Pid.
%% @private %% @private
call(Pid, Msg) -> call(Pid, Msg) ->
@ -63,7 +62,7 @@ notify_game(Table, From, Message) ->
%% @private %% @private
init([]) -> init([]) ->
GameVM = ggs_gamevm_e:start_link(self()), GameVM = ggs_gamevm_e:start_link(self()), %% @TODO: Temporary erlang gamevm
{ok, #state { {ok, #state {
game_vm = GameVM, game_vm = GameVM,
players = [] }}. players = [] }}.
@ -121,12 +120,47 @@ terminate(_Reason, _State) ->
code_change(_OldVsn, State, _Extra) -> code_change(_OldVsn, State, _Extra) ->
{ok, State}. {ok, State}.
%% @TODO: Please put these tests in a separate file. We can't compile this file if
%% they contain errors from switching vms
%% ---------------------------------------------------------------------- %% ----------------------------------------------------------------------
% Tests % Tests
%<<<<<<< HEAD
%start_link_test() -> %start_link_test() ->
% Table = start_link(),
% ?assertNot(Table =:= undefined).
%add_player_test() ->
% Table = start_link(),
% Player = test_player,
% add_player(Table, Player),
% {ok, [Player]} = gen_server:call(Table, get_player_list).
%remove_player_test() ->
% Table = start_link(),
% Player = test_player,
% Player2 = test_player2,
% add_player(Table, Player),
% {ok, [Player]} = gen_server:call(Table, get_player_list),
% add_player(Table, Player2),
% {ok, [Player2, Player]} = gen_server:call(Table, get_player_list),
% remove_player(Table, Player),
% {ok, [Player2]} = gen_server:call(Table, get_player_list),
% remove_player(Table, Player2),
% {ok, []} = gen_server:call(Table, get_player_list).
%
%stop_test() ->
% Table = start_link(),
% ok = stop(Table).
% @private
%notify_test() ->
% Table = start_link(),
% Player = test_player,
% Message = {server, define, "function helloWorld(x) { }"},
% ok = notify(Table, Player, Message).
%=======
%%start_link_test() ->
% Table = start_link("123", none), % Table = start_link("123", none),
% ?assertNot(Table =:= undefined). % ?assertNot(Table =:= undefined).
% %
@ -159,6 +193,7 @@ code_change(_OldVsn, State, _Extra) ->
% Player = test_player, % Player = test_player,
% Message = {server, define, "function helloWorld(x) { }"}, % Message = {server, define, "function helloWorld(x) { }"},
% ok = notify(Table, Player, Message). % ok = notify(Table, Player, Message).
%>>>>>>> jonte_rewrite
%Message2 = {game, "helloWorld", "test"}, %Message2 = {game, "helloWorld", "test"},
%ok = notify(Table, Player, Message2). %ok = notify(Table, Player, Message2).

View file

@ -9,7 +9,10 @@ start_link_test() ->
%% @doc Given that start_link returned {ok, Player}. Notify shall always return ok and %% @doc Given that start_link returned {ok, Player}. Notify shall always return ok and
%% deliver a specified message through the socket. %% deliver a specified message through the socket.
notify_test() -> notify_test() ->
ggs_logger:not_implemented(). Player = start_link("bad arg"),
Message = {"something", ""},
Ret = ggs_player:notify(Player, self(), Message)
?assertNot(ok =:= Ret).
%% @doc Given that start_link returned {ok, Player}. get_token shall always return a valid %% @doc Given that start_link returned {ok, Player}. get_token shall always return a valid
%% player token. a valid token should be unique. %% player token. a valid token should be unique.
@ -19,5 +22,6 @@ get_token_test() ->
%% @doc Given that start_link returned {ok, Pid}. There shouldn't be possible to %% @doc Given that start_link returned {ok, Pid}. There shouldn't be possible to
%% execute this function with the same Player and Table arguments twice. %% execute this function with the same Player and Table arguments twice.
stop_test() -> stop_test() ->
ggs_logger:not_implemented(). Player = start_link(something),
Table = test,
ok = stop(Player, Table).