now sending table token and if client is the first or not
This commit is contained in:
parent
de171a9d9d
commit
726528985b
3 changed files with 34 additions and 17 deletions
|
@ -106,12 +106,8 @@ handle_call({eval, SourceCode}, _From, #state { vm = VM } = State) ->
|
||||||
{reply, Ret, State}.
|
{reply, Ret, State}.
|
||||||
|
|
||||||
handle_cast({define, SourceCode}, #state { table = Table, vm = VM } = State) ->
|
handle_cast({define, SourceCode}, #state { table = Table, vm = VM } = State) ->
|
||||||
R = erlv8_vm:run(VM, "playerCommand"),
|
{ok, _Ret} = erlv8_vm:run(VM, SourceCode),
|
||||||
case R of
|
ggs_table:notify_all_players(Table, {"defined", "ok"}),
|
||||||
{throw, _} -> {ok, Ret} = erlv8_vm:run(VM, SourceCode),
|
|
||||||
ggs_table:notify_all_players(Table, {"defined", "ok"});
|
|
||||||
_ -> ok
|
|
||||||
end
|
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
|
|
||||||
handle_cast({player_command, Player, Command, Args}, #state { vm = VM } = State) ->
|
handle_cast({player_command, Player, Command, Args}, #state { vm = VM } = State) ->
|
||||||
|
|
|
@ -62,7 +62,12 @@ init([Socket]) ->
|
||||||
},
|
},
|
||||||
|
|
||||||
%ggs_protocol:parse(Protocol, Data),
|
%ggs_protocol:parse(Protocol, Data),
|
||||||
ggs_player:notify(self(), self(), {"hello", Token}), % send hello to the client
|
TableToken = ggs_coordinator:table_pid_to_token(Table),
|
||||||
|
ShallDefine = case ggs_table:already_defined(Table) of
|
||||||
|
true -> "true";
|
||||||
|
false -> "false"
|
||||||
|
end,
|
||||||
|
ggs_player:notify(self(), self(), {"hello", Token ++ "," ++ ShallDefine ++ "," ++ TableToken}), % send hello to the client
|
||||||
{ok, State}.
|
{ok, State}.
|
||||||
|
|
||||||
%% @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
|
||||||
|
@ -92,11 +97,11 @@ stop(Player) ->
|
||||||
%% Internals
|
%% Internals
|
||||||
handle_call(_Request, _From, St) -> {stop, unimplemented, St}.
|
handle_call(_Request, _From, St) -> {stop, unimplemented, St}.
|
||||||
|
|
||||||
handle_cast({tcp, _Socket, Data}, #state { protocol = Protocol } = _State) ->
|
handle_cast({tcp, _Socket, Data}, #state { protocol = Protocol } = State) ->
|
||||||
ggs_protocol:parse(Protocol, Data),
|
ggs_protocol:parse(Protocol, Data),
|
||||||
{noreply, State}
|
{noreply, State};
|
||||||
|
|
||||||
handle_cast({tcp_closed, _Socket}, _State) ->
|
handle_cast({tcp_closed, _Socket}, State) ->
|
||||||
erlang:display("Client disconnected, but THIS IS NOT SUPPORTED YET!~n"),
|
erlang:display("Client disconnected, but THIS IS NOT SUPPORTED YET!~n"),
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
|
|
||||||
|
@ -104,8 +109,14 @@ handle_cast({notify, Message}, #state { socket = Socket } = State) ->
|
||||||
gen_tcp:send(Socket, ggs_protocol:create_message(Message)),
|
gen_tcp:send(Socket, ggs_protocol:create_message(Message)),
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
|
|
||||||
handle_cast({srv_cmd, "hello", _Headers, _Data}, #state { token = Token } = State) ->
|
handle_cast({srv_cmd, "hello", _Headers, _Data}, #state { token = Token, table = Table } = State) ->
|
||||||
ggs_player:notify(self(), self(), {"hello", Token}),
|
ShallDefine = case ggs_table:already_defined(Table) of
|
||||||
|
true -> "true";
|
||||||
|
false -> "false"
|
||||||
|
end,
|
||||||
|
TableToken = ggs_coordinator:table_pid_to_token(Table),
|
||||||
|
erlang:display("hello"),
|
||||||
|
ggs_player:notify(self(), self(), {"hello", "token="++ Token ++ "&define=" ++ ShallDefine ++ "&table_token=" ++ TableToken}),
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
|
|
||||||
handle_cast({srv_cmd, "define", _Headers, Data}, #state { table = Table } = State) ->
|
handle_cast({srv_cmd, "define", _Headers, Data}, #state { table = Table } = State) ->
|
||||||
|
|
|
@ -7,11 +7,12 @@
|
||||||
-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]).
|
||||||
|
|
||||||
-record(state, { players, game_vm } ).
|
-record(state, { players, game_vm, already_defined } ).
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([start/1,
|
-export([start/1,
|
||||||
add_player/2,
|
add_player/2,
|
||||||
|
already_defined/1,
|
||||||
remove_player/2,
|
remove_player/2,
|
||||||
stop/1,
|
stop/1,
|
||||||
notify/3,
|
notify/3,
|
||||||
|
@ -69,6 +70,9 @@ send_command(TableToken, PlayerToken, Message) ->
|
||||||
TablePid = ggs_coordinator:table_token_to_pid(TableToken),
|
TablePid = ggs_coordinator:table_token_to_pid(TableToken),
|
||||||
gen_server:cast(TablePid, {notify_player, PlayerToken, self(), Message}).
|
gen_server:cast(TablePid, {notify_player, PlayerToken, self(), Message}).
|
||||||
|
|
||||||
|
already_defined(TablePid) ->
|
||||||
|
gen_server:call(TablePid, already_defined).
|
||||||
|
|
||||||
%% ----------------------------------------------------------------------
|
%% ----------------------------------------------------------------------
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
|
@ -77,13 +81,17 @@ init([TableToken]) ->
|
||||||
GameVM = ggs_gamevm:start_link(TableToken),
|
GameVM = ggs_gamevm:start_link(TableToken),
|
||||||
{ok, #state {
|
{ok, #state {
|
||||||
game_vm = GameVM,
|
game_vm = GameVM,
|
||||||
players = [] }}.
|
players = [],
|
||||||
|
already_defined = false }}.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
|
|
||||||
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(already_defined, _From, #state { already_defined = AlreadyDefined} = State) ->
|
||||||
|
{reply, AlreadyDefined, State};
|
||||||
|
|
||||||
handle_call(get_player_list, _From, #state { players = Players } = State) ->
|
handle_call(get_player_list, _From, #state { players = Players } = State) ->
|
||||||
TokenPlayers = lists:map(
|
TokenPlayers = lists:map(
|
||||||
fun (Pid) -> ggs_coordinator:player_pid_to_token(Pid) end, Players),
|
fun (Pid) -> ggs_coordinator:player_pid_to_token(Pid) end, Players),
|
||||||
|
@ -101,11 +109,13 @@ handle_cast({notify, Player, Message}, #state { game_vm = GameVM } = State) ->
|
||||||
PlayerToken = ggs_coordinator:player_pid_to_token(Player),
|
PlayerToken = ggs_coordinator:player_pid_to_token(Player),
|
||||||
case Message of
|
case Message of
|
||||||
{server, define, Args} ->
|
{server, define, Args} ->
|
||||||
ggs_gamevm:define(GameVM, Args);
|
ggs_gamevm:define(GameVM, Args),
|
||||||
|
NewState = State#state{ already_defined = true };
|
||||||
{game, Command, Args} ->
|
{game, Command, Args} ->
|
||||||
ggs_gamevm:player_command(GameVM, PlayerToken, Command, Args)
|
ggs_gamevm:player_command(GameVM, PlayerToken, Command, Args),
|
||||||
|
NewState = State
|
||||||
end,
|
end,
|
||||||
{noreply, State};
|
{noreply, NewState};
|
||||||
|
|
||||||
handle_cast({add_player, Player}, #state { players = Players } = State) ->
|
handle_cast({add_player, Player}, #state { players = Players } = State) ->
|
||||||
{noreply, State#state { players = [Player | Players] }};
|
{noreply, State#state { players = [Player | Players] }};
|
||||||
|
|
Reference in a new issue