Now we have some myltiplayer action!
This commit is contained in:
parent
3f41769233
commit
883cf9e9d7
4 changed files with 151 additions and 63 deletions
|
@ -61,21 +61,24 @@ handle_call(join_lobby, _From, State) ->
|
||||||
Token = helpers:get_new_token(),
|
Token = helpers:get_new_token(),
|
||||||
{reply, {ok, Token}, State};
|
{reply, {ok, Token}, State};
|
||||||
|
|
||||||
handle_call({join_table, Table}, _From, State) ->
|
handle_call({join_table, Table}, From, State) ->
|
||||||
|
{FromPlayer, _Ref} = From,
|
||||||
Tables = State#co_state.tables,
|
Tables = State#co_state.tables,
|
||||||
case lists:keyfind(Table, 1, Tables) of
|
case lists:keyfind(Table, 1, Tables) of
|
||||||
{Table} ->
|
{TableID, TablePID} ->
|
||||||
{reply, {ok, Table}, State}; %% @TODO: Also add player to table
|
ggs_table:add_player(TablePID, FromPlayer),
|
||||||
|
{reply, {ok, TablePID}, State};
|
||||||
false ->
|
false ->
|
||||||
{reply, {error, no_such_table}, State}
|
{reply, {error, no_such_table}, State}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_call({create_table, {force, TID}}, From, State) ->
|
handle_call({create_table, {force, TableID}}, From, State) ->
|
||||||
TIDMap = State#co_state.player_table_map,
|
TableIDMap = State#co_state.player_table_map,
|
||||||
Tables = State#co_state.tables,
|
Tables = State#co_state.tables,
|
||||||
{reply, {ok, TID}, State#co_state{
|
NewTableProc = ggs_table:start_link(),
|
||||||
player_table_map = [{From, TID} | TIDMap],
|
{reply, {ok, TableID}, State#co_state{
|
||||||
tables = [{TID} | TID]
|
player_table_map = [{From, TableID} | TableIDMap],
|
||||||
|
tables = [{TableID, NewTableProc} | Tables]
|
||||||
}};
|
}};
|
||||||
|
|
||||||
handle_call(_Message, _From, State) ->
|
handle_call(_Message, _From, State) ->
|
||||||
|
|
41
src/ggs_gamevm_e.erl
Normal file
41
src/ggs_gamevm_e.erl
Normal file
|
@ -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.
|
|
@ -1,5 +1,9 @@
|
||||||
-module(ggs_player).
|
-module(ggs_player).
|
||||||
-export([start_link/1, notify/3, get_token/1, stop/2]).
|
-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
|
%% @doc This module handles communication between a player and GGS. This module is
|
||||||
%%responsible for:
|
%%responsible for:
|
||||||
|
@ -13,14 +17,28 @@
|
||||||
%% identifying the player.
|
%% identifying the player.
|
||||||
%% @spec start_link(Socket::socket()) -> {ok, Pid} | {error, Reason}
|
%% @spec start_link(Socket::socket()) -> {ok, Pid} | {error, Reason}
|
||||||
start_link(Socket) ->
|
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
|
%% @doc Handles incoming messages from the GGS and forwards them through the player
|
||||||
%% socket to the player.
|
%% socket to the player.
|
||||||
%% @spec notify(Player::Pid(), From::Pid(),
|
%% @spec notify(Player::Pid(), From::Pid(),
|
||||||
%% {Command::String(), Message::string()}) -> ok
|
%% {Command::String(), Message::string()}) -> ok
|
||||||
notify(Player, From, Message) ->
|
notify(Player, From, Message) ->
|
||||||
ggs_logger:not_implemented().
|
Player ! {notify, From, Message}.
|
||||||
|
|
||||||
%% @doc Get the player token uniquely representing the player.
|
%% @doc Get the player token uniquely representing the player.
|
||||||
%% @spec get_token() -> string()
|
%% @spec get_token() -> string()
|
||||||
|
@ -36,13 +54,13 @@ stop(_Player,_Table) ->
|
||||||
|
|
||||||
%% Internals
|
%% Internals
|
||||||
|
|
||||||
loop(Socket) ->
|
loop(#pl_state{token = Token, socket = Socket, table = Table} = State) ->
|
||||||
% The socket is in 'active' mode, and that means we are pushed any data
|
receive
|
||||||
% that arrives on it, we do not need to recv() manually. Since the socket
|
{tcp, Socket, Data} -> % Just echo for now..
|
||||||
% was opened in our parent process, we need to change the owner of it to
|
io:format("Notifying table..~n"),
|
||||||
% us, otherwise these messages end up in our parent.
|
ggs_table:notify_game(Table, Token, Data),
|
||||||
erlang:port_connect(Socket, self()),
|
loop(State);
|
||||||
receive {tcp, Socket, Data} -> % Just echo for now..
|
{notify, From, Message} ->
|
||||||
gen_tcp:send(Socket,Data),
|
gen_tcp:send(Socket, Message),
|
||||||
loop(Socket)
|
loop(State)
|
||||||
end.
|
end.
|
||||||
|
|
|
@ -5,12 +5,13 @@
|
||||||
|
|
||||||
%% gen_server callbacks
|
%% gen_server callbacks
|
||||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
-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, { token, players, socket, game_vm } ).
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([start_link/2,
|
-export([start_link/0,
|
||||||
add_player/2,
|
add_player/2,
|
||||||
remove_player/2,
|
remove_player/2,
|
||||||
stop/1,
|
stop/1,
|
||||||
|
@ -22,9 +23,8 @@
|
||||||
% API implementation
|
% API implementation
|
||||||
|
|
||||||
% @doc returns a new table
|
% @doc returns a new table
|
||||||
start_link(Token, Socket) ->
|
start_link() ->
|
||||||
GameVM = ggs_gamevm:start_link(),
|
{ok, Pid} = gen_server:start_link(?MODULE, [], []),
|
||||||
{ok, Pid} = gen_server:start_link(?MODULE, [Token, Socket, GameVM], []),
|
|
||||||
Pid.
|
Pid.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
|
@ -47,22 +47,34 @@ stop(Table) ->
|
||||||
notify(Table, Player, Message) ->
|
notify(Table, Player, Message) ->
|
||||||
gen_server:cast(Table, {notify, 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
|
%% @private
|
||||||
init([Token, Socket, GameVM]) ->
|
init([]) ->
|
||||||
{ok, #state { token = Token,
|
GameVM = ggs_gamevm_e:start_link(self()),
|
||||||
socket = Socket,
|
{ok, #state {
|
||||||
game_vm = GameVM,
|
game_vm = GameVM,
|
||||||
players = [] }}.
|
players = [] }}.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
handle_call({add_player, Player}, _From, #state { players = Players } = State) ->
|
handle_call({add_player, Player}, _From, #state { players = Players } = State) ->
|
||||||
{reply, ok, State#state { players = [Player | Players] }};
|
{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] }};
|
||||||
|
|
||||||
handle_call(get_player_list, _From, #state { players = Players } = State) ->
|
handle_call(get_player_list, _From, #state { players = Players } = State) ->
|
||||||
{reply, {ok, Players}, State};
|
{reply, {ok, Players}, 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]),
|
||||||
{reply, ok, State}.
|
{reply, ok, State}.
|
||||||
|
@ -71,11 +83,25 @@ 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:user_command(GameVM, Player, Command, Args)
|
ggs_gamevm_e:user_command(GameVM, Player, Command, Args)
|
||||||
end,
|
end,
|
||||||
{noreply, State};
|
{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) ->
|
handle_cast(stop, State) ->
|
||||||
{stop, normal, State};
|
{stop, normal, State};
|
||||||
handle_cast(Msg, S) ->
|
handle_cast(Msg, S) ->
|
||||||
|
@ -100,39 +126,39 @@ code_change(_OldVsn, State, _Extra) ->
|
||||||
|
|
||||||
% Tests
|
% Tests
|
||||||
|
|
||||||
start_link_test() ->
|
%start_link_test() ->
|
||||||
Table = start_link("123", none),
|
% Table = start_link("123", none),
|
||||||
?assertNot(Table =:= undefined).
|
% ?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).
|
||||||
|
|
||||||
add_player_test() ->
|
%remove_player_test() ->
|
||||||
Table = start_link("123", none),
|
% Table = start_link("123", none),
|
||||||
Player = test_player,
|
% Player = test_player,
|
||||||
add_player(Table, Player),
|
% Player2 = test_player2,
|
||||||
{ok, [Player]} = gen_server:call(Table, get_player_list).
|
% add_player(Table, Player),
|
||||||
|
% {ok, [Player]} = gen_server:call(Table, get_player_list),
|
||||||
remove_player_test() ->
|
% add_player(Table, Player2),
|
||||||
Table = start_link("123", none),
|
% {ok, [Player2, Player]} = gen_server:call(Table, get_player_list),
|
||||||
Player = test_player,
|
% remove_player(Table, Player),
|
||||||
Player2 = test_player2,
|
% {ok, [Player2]} = gen_server:call(Table, get_player_list),
|
||||||
add_player(Table, Player),
|
% remove_player(Table, Player2),
|
||||||
{ok, [Player]} = gen_server:call(Table, get_player_list),
|
% {ok, []} = gen_server:call(Table, get_player_list).
|
||||||
add_player(Table, Player2),
|
%
|
||||||
{ok, [Player2, Player]} = gen_server:call(Table, get_player_list),
|
%stop_test() ->
|
||||||
remove_player(Table, Player),
|
% Table = start_link("123", none),
|
||||||
{ok, [Player2]} = gen_server:call(Table, get_player_list),
|
% ok = stop(Table).
|
||||||
remove_player(Table, Player2),
|
%
|
||||||
{ok, []} = gen_server:call(Table, get_player_list).
|
%% @private
|
||||||
|
%notify_test() ->
|
||||||
stop_test() ->
|
% Table = start_link("123", none),
|
||||||
Table = start_link("123", none),
|
% Player = test_player,
|
||||||
ok = stop(Table).
|
% Message = {server, define, "function helloWorld(x) { }"},
|
||||||
|
% ok = notify(Table, Player, Message).
|
||||||
% @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"},
|
%Message2 = {game, "helloWorld", "test"},
|
||||||
%ok = notify(Table, Player, Message2).
|
%ok = notify(Table, Player, Message2).
|
||||||
|
|
Reference in a new issue