diff --git a/.gitignore b/.gitignore index d176978..f6b93e9 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.dump *.beam Mnesia.* +*.swo diff --git a/erlang_js b/erlang_js index 5350ed2..709b568 160000 --- a/erlang_js +++ b/erlang_js @@ -1 +1 @@ -Subproject commit 5350ed21606606dbee5ecb07e974f2abb9106270 +Subproject commit 709b568efbc99c954507d1593bc5633f900bc5dc diff --git a/mnesia/gamedb.erl b/mnesia/gamedb.erl deleted file mode 100644 index 751eb92..0000000 --- a/mnesia/gamedb.erl +++ /dev/null @@ -1,50 +0,0 @@ -%%%%---------------------------------------------------- -%%% @author Mattias Pettersson -%%% @copyright 2011 Mattias Pettersson -%%% @doc Database for runtime game variable storage. -%%% @end - - Test Mnesia --module(gamedb). --import(mnesia). --export([init/0,insert_player/1,example_player/0,read_player/1,test_player/0]). --include("gamedb.hrl"). - -%%----------------------------------------------------- -%% Creation -%%----------------------------------------------------- -init() -> - mnesia:create_table(player, [{attributes, record_info(fields, player)}]). - -%%----------------------------------------------------- -%% Test -%%----------------------------------------------------- -test_player() -> - insert_player(example_player()), - read_player(0001). - -example_player() -> - #player{id = 0001, - name = "Tux"}. - -%%----------------------------------------------------- -%% Insertions -%%----------------------------------------------------- -insert_player(Player) -> - Fun = fun() -> - mnesia:write(Player) - end, - mnesia:transaction(Fun). - - -%%----------------------------------------------------- -%% Querries -%%----------------------------------------------------- -read_player(Player_Key) -> - Fun = fun() -> - [P] = mnesia:read(player, Player_Key), - Name = P#player.name, - io:format("Player name: ~s~n",[Name]) - end, - mnesia:transaction(Fun). - diff --git a/src/.ggs_connection.erl.swp b/src/.ggs_connection.erl.swp deleted file mode 100644 index 0c009f8..0000000 Binary files a/src/.ggs_connection.erl.swp and /dev/null differ diff --git a/src/.ggs_server.erl.swo b/src/.ggs_server.erl.swo deleted file mode 100644 index 3048659..0000000 Binary files a/src/.ggs_server.erl.swo and /dev/null differ diff --git a/src/ggs_coordinator.erl b/src/ggs_coordinator.erl index 4e6b623..733ff8d 100644 --- a/src/ggs_coordinator.erl +++ b/src/ggs_coordinator.erl @@ -1,13 +1,20 @@ -module(ggs_coordinator). %% API Exports --export([start_link/0, stop/1]). +-export([start_link/0, stop/1, join_table/1, create_table/1, join_lobby/0, + respawn_player/2, respawn_table/1, remove_player/2]). %% gen_server callback exports -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). -define(SERVER, ?MODULE). +-record(co_state, + {players = [], % List of all player processes + player_table_map = [], % Players <-> Table map + table_state_map = [], + tables = []}). % Table <-> Table state map + %% @doc This module act as "the man in the middle". %% Creates the starting connection between table and players. @@ -16,21 +23,22 @@ start_link() -> gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). %% @doc Terminates the coordinator process. -stop(_Reason) -> - ggs_logger:not_implemented(). +stop(Reason) -> + gen_server:cast(ggs_coordinator, {stop, Reason}). -%% @doc Joins table with specified token -join_table(_Token) -> - ggs_logger:not_implemented(). +%% @doc Joins table with specified token, returns {error, no_such_table} +%% if the specified table token does not exist +join_table(Token) -> + gen_server:call(ggs_coordinator, {join_table, Token}). -%% @doc Create a new table -create_table(_Params) -> - ggs_logger:not_implemented(). +%% @doc Create a new table, return {error, Reason} or {ok, TableToken} +create_table(Params) -> + gen_server:call(ggs_coordinator, {create_table, Params}). %% @doc This is the first function run by a newly created players. %% Generates a unique token that we use to identify the player. join_lobby() -> - ggs_logger:not_implemented(). + gen_server:call(ggs_coordinator, join_lobby). %% @doc Act as a supervisor to player and respawns player when it gets bad data. respawn_player(_Player, _Socket) -> @@ -47,11 +55,38 @@ remove_player(_From, _Player) -> %% gen_server callbacks init([]) -> - {ok, ok}. + {ok, #co_state{}}. + +handle_call(join_lobby, _From, State) -> + Token = helpers:get_new_token(), + {reply, {ok, Token}, State}; + +handle_call({join_table, Table}, From, State) -> + {FromPlayer, _Ref} = From, + Tables = State#co_state.tables, + case lists:keyfind(Table, 1, Tables) of + {TableID, TablePID} -> + ggs_table:add_player(TablePID, FromPlayer), + {reply, {ok, TablePID}, State}; + false -> + {reply, {error, no_such_table}, State} + end; + +handle_call({create_table, {force, TableID}}, From, State) -> + TableIDMap = State#co_state.player_table_map, + Tables = State#co_state.tables, + NewTableProc = ggs_table:start_link(), + {reply, {ok, TableID}, State#co_state{ + player_table_map = [{From, TableID} | TableIDMap], + tables = [{TableID, NewTableProc} | Tables] + }}; handle_call(_Message, _From, State) -> {noreply, State}. +handle_cast({stop, Reason}, State) -> + {stop, normal, state}; + handle_cast(_Message, State) -> {noreply, State}. diff --git a/src/ggs_gamevm.erl b/src/ggs_gamevm.erl index 1a8f547..6dd19a7 100644 --- a/src/ggs_gamevm.erl +++ b/src/ggs_gamevm.erl @@ -1,24 +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 %% 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 %% with for example the define method of this module. -start_link() -> +start_link(Table) -> erlang_js:start(), %% @TODO: should only be done once - PortPid = spawn_link( fun() -> - process_flag(trap_exit, true), - {ok, Port} = js_driver:new(), - js:define(Port, <<"function userCommand(user, command, args){return 'Hello world';}">>), - loop(Port) - end ), - PortPid. + {ok, Pid} = gen_server:start_link(?MODULE, [Table], []), + Pid. %% @doc Define some new code on the specified VM, returns the atom ok. define(GameVM, SourceCode) -> - GameVM ! {define,SourceCode}, - ok. + gen_server:cast(GameVM, {define, SourceCode}). %% @doc Execute a user command on the specified VM. This function is %% asynchronous, and returns ok. @@ -28,23 +38,93 @@ define(GameVM, SourceCode) -> %% Command = a game command to run %% Args = arguments for the Command parameter user_command(GameVM, Player, Command, Args) -> - Ref = make_ref(), - GameVM ! {user_command, Player, Command, Args, self(), Ref}, + gen_server:cast(GameVM, {user_command, Player, Command, Args}). + +%% @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. -%% Helper functions +%% @private +code_change(_OldVsn, State, _Extra) -> + {ok, State}. + +js_escape(S) -> + lists:flatmap(fun($\') -> [$\\, $\']; (X) -> [X] end, S). + +%% ---------------------------------------------------------------------- +% Tests + +start_link_test() -> + erlang_js:start(), %% @TODO: should only be done once + GameVM = start_link(test_table), + ?assertNot(GameVM =:= undefined). + +define_test() -> + GameVM = start_link(test_table), + 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;"})). + +js_erlang_test() -> + GameVM = start_link(test_table), + define(GameVM, "var t = '';\nfunction userCommand(user, command, args) { t = callErlang('erlang time') + ''; }\n"), + user_command(GameVM, "", "", ""), + {A, B, C} = erlang:time(), + T = "{" ++ integer_to_list(A) ++ ", " ++ integer_to_list(B) ++ ", " ++ integer_to_list(C) ++ "}", + ?assertMatch(T, binary_to_list(gen_server:call(GameVM, {eval, "t;"}))). -loop(Port) -> - receive - {define, SourceCode} -> - ok = js:define(Port, list_to_binary(SourceCode)), - loop(Port); - {user_command, User, Command, Args, From, Ref} -> - {ok, Ret} = js:call(Port, <<"userCommand">>, - [ list_to_binary(User), - list_to_binary(Command), - list_to_binary(Args) - ]), - From ! {Ref, Ret}, - loop(Port) - end. diff --git a/src/ggs_gamevm_e.erl b/src/ggs_gamevm_e.erl new file mode 100644 index 0000000..76b9350 --- /dev/null +++ b/src/ggs_gamevm_e.erl @@ -0,0 +1,41 @@ +-module(ggs_gamevm_e). +-export([start_link/1, define/2, user_command/4]). +%% @doc This module is responsible for running the game VM:s. You can issue +%% commands to a vm using this module. + +%% @doc Create a new VM process. The process ID is returned and can be used +%% with for example the define method of this module. +start_link(Table) -> + PortPid = spawn( fun() -> + loop(Table) + end ), + PortPid. + +%% @doc Define some new code on the specified VM, returns the atom ok. +define(GameVM, SourceCode) -> + GameVM ! {define,SourceCode}, + ok. + +%% @doc Execute a user command on the specified VM. This function is +%% asynchronous, and returns ok. +%% @spec user_command(GameVM, User, Command, Args) -> ok +%% GameVM = process IS of VM +%% Player = the player running the command +%% Command = a game command to run +%% Args = arguments for the Command parameter +user_command(GameVM, Player, Command, Args) -> + Ref = make_ref(), + GameVM ! {user_command, Player, Command, Args, self(), Ref}, + ok. + +%% Helper functions + +loop(Table) -> + receive + {define, SourceCode} -> + loop(Table); + {user_command, _User, Command, _Args, _From, _Ref} -> + io:format("GameVM received a message~n"), + ggs_table:notify_all_players(Table, Command), + loop(Table) + end. diff --git a/src/ggs_player.erl b/src/ggs_player.erl index 37c8496..af92ad9 100644 --- a/src/ggs_player.erl +++ b/src/ggs_player.erl @@ -1,5 +1,9 @@ -module(ggs_player). -export([start_link/1, notify/3, get_token/1, stop/2]). +-record(pl_state, + {token, % Player's token + socket, % Player's socket + table}). % Player's table %% @doc This module handles communication between a player and GGS. This module is %%responsible for: @@ -13,14 +17,28 @@ %% identifying the player. %% @spec start_link(Socket::socket()) -> {ok, Pid} | {error, Reason} start_link(Socket) -> - loop(Socket). + % The socket is in 'active' mode, and that means we are pushed any data + % that arrives on it, we do not need to recv() manually. Since the socket + % was opened in our parent process, we need to change the owner of it to + % us, otherwise these messages end up in our parent. + erlang:port_connect(Socket, self()), + {ok, Token} = ggs_coordinator:join_lobby(), + TableStatus = ggs_coordinator:join_table(1337), + case TableStatus of + {ok, Table} -> + loop(#pl_state{socket = Socket, token = Token, table = Table}); + {error, no_such_table} -> + ggs_coordinator:create_table({force, 1337}), + {ok, Table} = ggs_coordinator:join_table(1337), + loop(#pl_state{socket = Socket, token = Token, table = Table}) + end. %% @doc Handles incoming messages from the GGS and forwards them through the player %% socket to the player. %% @spec notify(Player::Pid(), From::Pid(), %% {Command::String(), Message::string()}) -> ok notify(Player, From, Message) -> - ggs_logger:not_implemented(). + Player ! {notify, From, Message}. %% @doc Get the player token uniquely representing the player. %% @spec get_token() -> string() @@ -36,13 +54,13 @@ stop(_Player,_Table) -> %% Internals -loop(Socket) -> - % The socket is in 'active' mode, and that means we are pushed any data - % that arrives on it, we do not need to recv() manually. Since the socket - % was opened in our parent process, we need to change the owner of it to - % us, otherwise these messages end up in our parent. - erlang:port_connect(Socket, self()), - receive {tcp, Socket, Data} -> % Just echo for now.. - gen_tcp:send(Socket,Data), - loop(Socket) +loop(#pl_state{token = Token, socket = Socket, table = Table} = State) -> + receive + {tcp, Socket, Data} -> % Just echo for now.. + io:format("Notifying table..~n"), + ggs_table:notify_game(Table, Token, Data), + loop(State); + {notify, From, Message} -> + gen_tcp:send(Socket, Message), + loop(State) end. diff --git a/src/ggs_table.erl b/src/ggs_table.erl index 1e4f323..4715e79 100644 --- a/src/ggs_table.erl +++ b/src/ggs_table.erl @@ -1,31 +1,29 @@ -%% @doc This module represents a Player with a Socket and a Token +%% @doc This module represents a table with players -module(ggs_table). -behaviour(gen_server). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, - terminate/2, code_change/3]). + terminate/2, code_change/3, notify_all_players/2, notify_game/3, + add_player/2]). --record(state, { token, players, socket, game_vm } ). +-record(state, { players, game_vm } ). %% API --export([start_link/2, +-export([start_link/0, add_player/2, remove_player/2, stop/1, notify/3]). --include_lib("eunit/include/eunit.hrl"). %% ---------------------------------------------------------------------- % API implementation % @doc returns a new table -start_link(Token, Socket) -> - GameVM = ggs_gamevm:start_link(), - {ok, Pid} = gen_server:start_link(?MODULE, [Token, Socket, GameVM], []), - Pid. +start_link() -> + {ok, Pid} = gen_server:start_link(?MODULE, [], []), %% @private call(Pid, Msg) -> @@ -47,22 +45,34 @@ stop(Table) -> notify(Table, Player, Message) -> gen_server:cast(Table, {notify, Player, Message}). +notify_all_players(Table, Message) -> + gen_server:cast(Table, {notify_all_players, Message}). + +notify_game(Table, From, Message) -> + io:format("Notify game called on"), + erlang:display(Table), + io:format("~n"), + gen_server:cast(Table, {notify_game, Message, From}). + %% ---------------------------------------------------------------------- %% @private -init([Token, Socket, GameVM]) -> - {ok, #state { token = Token, - socket = Socket, +init([]) -> + GameVM = ggs_gamevm_e:start_link(self()), %% @TODO: Temporary erlang gamevm + {ok, #state { game_vm = GameVM, 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] }}; + handle_call(get_player_list, _From, #state { players = Players } = State) -> {reply, {ok, Players}, State}; + handle_call(Msg, _From, State) -> error_logger:error_report([unknown_msg, Msg]), {reply, ok, State}. @@ -71,11 +81,25 @@ handle_call(Msg, _From, State) -> handle_cast({notify, Player, Message}, #state { game_vm = GameVM } = State) -> case Message of {server, define, Args} -> - ggs_gamevm:define(GameVM, Args); + ggs_gamevm_e:define(GameVM, Args); {game, Command, Args} -> - ggs_gamevm:user_command(GameVM, Player, Command, Args) + ggs_gamevm_e:user_command(GameVM, Player, Command, Args) end, {noreply, State}; + +handle_cast({notify_game, Message, From}, #state { game_vm = GameVM } = State) -> + io:format("notify_game message received~n"), + ggs_gamevm_e:user_command(GameVM, From, Message, ""), + {noreply, State}; + +handle_cast({notify_all_players, Message}, #state{players = Players} = State) -> + io:format("Notifying all players... ~p~n", [Players]), + lists:foreach(fun(P) -> + io:format("Notifying ~p~n", [P]), + ggs_player:notify(P, "Server", Message) + end, Players), + {noreply, State}; + handle_cast(stop, State) -> {stop, normal, State}; handle_cast(Msg, S) -> @@ -95,44 +119,3 @@ terminate(_Reason, _State) -> code_change(_OldVsn, State, _Extra) -> {ok, State}. - -%% ---------------------------------------------------------------------- - -% Tests - -start_link_test() -> - Table = start_link("123", none), - ?assertNot(Table =:= undefined). - -add_player_test() -> - Table = start_link("123", none), - Player = test_player, - add_player(Table, Player), - {ok, [Player]} = gen_server:call(Table, get_player_list). - -remove_player_test() -> - Table = start_link("123", none), - 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("123", none), - ok = stop(Table). - -% @private -notify_test() -> - Table = start_link("123", none), - Player = test_player, - Message = {server, define, "function helloWorld(x) { }"}, - ok = notify(Table, Player, Message). - %Message2 = {game, "helloWorld", "test"}, - %ok = notify(Table, Player, Message2). - \ No newline at end of file diff --git a/src/helpers.erl b/src/helpers.erl index 1bf7825..3a42fbf 100644 --- a/src/helpers.erl +++ b/src/helpers.erl @@ -1,4 +1,8 @@ -module(helpers). +-export([not_implemented/0, get_new_token/0]). not_implemented() -> exit("Not implemented"). + +get_new_token() -> + string:strip(os:cmd("uuidgen"), right, $\n ). diff --git a/start_test b/start_test index 5920151..76050c1 100755 --- a/start_test +++ b/start_test @@ -1,3 +1,3 @@ #!/usr/bin/env bash -erl -boot start_sasl -pa ebin_test -pa erlang_js/ebin/ -pa erlv8/ebin -pa ebin -pa src -eval 'ggs_protocol_test:test_parse().' +erl -boot start_sasl -pa ebin_test -pa erlang_js/ebin/ -pa ebin -pa src -eval 'ggs_coordinator_test:test().' diff --git a/tests/ggs_coordinator_test.erl b/tests/ggs_coordinator_test.erl new file mode 100644 index 0000000..6ec41c6 --- /dev/null +++ b/tests/ggs_coordinator_test.erl @@ -0,0 +1,53 @@ +-module(ggs_coordinator_test). +-include_lib("eunit/include/eunit.hrl"). + +coordinator_test_() -> + {foreach, + fun() -> + {ok, _Coord} = ggs_coordinator:start_link(), + timer:sleep(100) + end, + fun(_X) -> + ggs_coordinator:stop("End of test"), + timer:sleep(100) + end, + [ + fun test_start_link/0, + fun test_stop/0, + fun test_join_bad_table/0, + fun test_join_lobby/0 + ] + }. + +test_start_link() -> + % Check process info + PInfo = whereis(ggs_coordinator), + ?assert((PInfo /= undefined)). % Did the server start? + +test_stop() -> + ok = ggs_coordinator:stop(""), % Extra cleaning + timer:sleep(100), + % Did it stop? + ?assert((whereis(ggs_coordinator)) == undefined). + +test_join_bad_table() -> + Response = ggs_coordinator:join_table("Nonexistant table"), + ?assert(Response == {error, no_such_table}). + +test_join_lobby() -> + {Response, _} = ggs_coordinator:join_lobby(), + ?assert(Response /= error). + +%% 'Manual' tests + +create_table_test() -> + {ok, _Coord} = ggs_coordinator:start_link(), + timer:sleep(100), + % Forcibly create a table. This functionality should be disabled + % in the production system, but is pretty nice for testing. + Response = ggs_coordinator:create_table({force, 1337}), + ?assert(Response == {ok, 1337}). + +join_good_table_test() -> + Response = ggs_coordinator:join_table(1337), + ?assert(Response == {ok, 1337}). diff --git a/tests/ggs_player_test.erl b/tests/ggs_player_test.erl index be38135..66900d6 100644 --- a/tests/ggs_player_test.erl +++ b/tests/ggs_player_test.erl @@ -1,3 +1,4 @@ +-module(ggs_player_test). -include_lib("eunit/include/eunit.hrl"). -import(ggs_player). diff --git a/tests/ggs_table_test.erl b/tests/ggs_table_test.erl new file mode 100644 index 0000000..7a270ed --- /dev/null +++ b/tests/ggs_table_test.erl @@ -0,0 +1,45 @@ +-module(ggs_table_test). +-include_lib("eunit/include/eunit.hrl"). +-import(ggs_table). + + +% @private +start_link_test() -> + Table = start_link(), + ?assertNot(Table =:= undefined). + +% @private +add_player_test() -> + Table = start_link(), + Player = test_player, + add_player(Table, Player), + {ok, [Player]} = gen_server:call(Table, get_player_list). + +% @private +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). + +% @private +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). + Message2 = {game, "helloWorld", "test"}, + ok = notify(Table, Player, Message2). +