Added some more ggsvm_e functionality and also a chat client
This commit is contained in:
parent
883cf9e9d7
commit
67567fe263
15 changed files with 1058 additions and 45 deletions
Binary file not shown.
Binary file not shown.
|
@ -49,7 +49,8 @@ respawn_table(_Token) ->
|
|||
ggs_logger:not_implemented().
|
||||
|
||||
%% @doc Removes a player from coordinator.
|
||||
remove_player(_From, _Player) ->
|
||||
remove_player(_From, Player) ->
|
||||
%gen_server:cast(ggs_coordinator, {remove_player, Player}).
|
||||
ggs_logger:not_implemented().
|
||||
|
||||
%% gen_server callbacks
|
||||
|
@ -87,6 +88,11 @@ handle_call(_Message, _From, State) ->
|
|||
handle_cast({stop, Reason}, State) ->
|
||||
{stop, normal, state};
|
||||
|
||||
%% @TODO: Implement me
|
||||
%handle_cast({remove_player, Player}) ->
|
||||
% {noreply, State#co_state{
|
||||
|
||||
|
||||
handle_cast(_Message, State) ->
|
||||
{noreply, State}.
|
||||
|
||||
|
|
|
@ -33,9 +33,24 @@ user_command(GameVM, Player, Command, Args) ->
|
|||
loop(Table) ->
|
||||
receive
|
||||
{define, SourceCode} ->
|
||||
io:format("GameVM_e can't define functions, sorry!~n"),
|
||||
loop(Table);
|
||||
{user_command, _User, Command, _Args, _From, _Ref} ->
|
||||
io:format("GameVM received a message~n"),
|
||||
ggs_table:notify_all_players(Table, Command),
|
||||
{user_command, Player, Command, Args, From, _Ref} ->
|
||||
erlang:display(Player),
|
||||
do_stuff(Command, Args, Player, Table),
|
||||
loop(Table)
|
||||
end.
|
||||
|
||||
do_stuff(Command, Args, Player, Table) ->
|
||||
case Command of
|
||||
"greet" ->
|
||||
ggs_player:notify(Player, server, "Hello there!\n");
|
||||
"chat" ->
|
||||
ggs_table:notify_all_players(Table, Args ++ "\n");
|
||||
"uname" ->
|
||||
Uname = os:cmd("uname -a"),
|
||||
ggs_player:notify(Player, server, Uname);
|
||||
|
||||
Other ->
|
||||
ggs_player:notify(Player, server, "I don't know that command..\n")
|
||||
end.
|
||||
|
|
|
@ -26,10 +26,12 @@ start_link(Socket) ->
|
|||
TableStatus = ggs_coordinator:join_table(1337),
|
||||
case TableStatus of
|
||||
{ok, Table} ->
|
||||
notify(self(), self(), Token),
|
||||
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),
|
||||
notify(self(), self(), Token),
|
||||
loop(#pl_state{socket = Socket, token = Token, table = Table})
|
||||
end.
|
||||
|
||||
|
@ -57,10 +59,25 @@ stop(_Player,_Table) ->
|
|||
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),
|
||||
io:format("Parsing via protocol module..~n"),
|
||||
Parsed = ggs_protocol:parse(Data),
|
||||
self() ! Parsed,
|
||||
loop(State);
|
||||
{notify, From, Message} ->
|
||||
gen_tcp:send(Socket, Message),
|
||||
loop(State)
|
||||
loop(State);
|
||||
% Below are messages generated by the parser
|
||||
{game_cmd,Cmd, Headers, Data} ->
|
||||
ggs_table:notify(Table, self(), {game, Cmd, Data}),
|
||||
loop(State);
|
||||
{srv_cmd,"define", Headers, Data} ->
|
||||
ggs_table:notify(Table, self(), {server, define, Data}),
|
||||
loop(State);
|
||||
{tcp_closed, _Socket} ->
|
||||
io:format("Client disconnected, but THIS IS NOT SUPPORTED YET!~n"),
|
||||
loop(State);
|
||||
Other ->
|
||||
io:format("Got UNKNOWN message: "),
|
||||
erlang:display(Other),
|
||||
io:format("~n")
|
||||
end.
|
||||
|
|
61
src/ggs_protocol.erl
Normal file
61
src/ggs_protocol.erl
Normal file
|
@ -0,0 +1,61 @@
|
|||
-module(ggs_protocol).
|
||||
-export([parse/1, getToken/1]).
|
||||
|
||||
%% API Functions
|
||||
parse(Data) ->
|
||||
Parsed = do_parse(Data, []),
|
||||
prettify(Parsed).
|
||||
|
||||
getToken(Parsed) ->
|
||||
case lists:keyfind(token, 1, Parsed) of
|
||||
{_, Value} ->
|
||||
Value;
|
||||
false ->
|
||||
false
|
||||
end.
|
||||
|
||||
%% Internal helpers
|
||||
do_parse(Data, ParsedMessage) ->
|
||||
NewLinePos = string:chr(Data, $\n),
|
||||
Line = string:substr(Data, 1, NewLinePos-1),
|
||||
Tokens = re:split(Line, ": ", [{return, list}]),
|
||||
case handle(Tokens) of
|
||||
{Command, more} ->
|
||||
do_parse(string:substr(Data, NewLinePos+1), ParsedMessage ++ [Command]);
|
||||
{separator, data_next} ->
|
||||
{_, Value} = lists:keyfind(content_len, 1, ParsedMessage),
|
||||
{ContentLength, []} = string:to_integer(Value),
|
||||
{data, ArgumentData} = handle_data(string:substr(Data, NewLinePos+1), ContentLength),
|
||||
{ParsedMessage, ArgumentData}
|
||||
end.
|
||||
|
||||
handle([[]]) ->
|
||||
{separator, data_next};
|
||||
handle(["Server-Command", Param]) ->
|
||||
{{srv_cmd, Param}, more};
|
||||
handle(["Game-Command", Param]) ->
|
||||
{{game_cmd, Param}, more};
|
||||
handle(["Content-Length", Param]) ->
|
||||
{{content_len, Param}, more};
|
||||
handle(["Token", Param]) ->
|
||||
{{token, Param}, more};
|
||||
handle(["Content-Type", Param]) ->
|
||||
{{content_type, Param}, more}.
|
||||
|
||||
handle_data(Data, Length) ->
|
||||
{data, string:substr(Data,1,Length)}.
|
||||
|
||||
|
||||
prettify({Args, Data}) ->
|
||||
case lists:keyfind(srv_cmd, 1, Args) of
|
||||
{_, Value} ->
|
||||
{srv_cmd, Value, Args, Data};
|
||||
_Other ->
|
||||
case lists:keyfind(game_cmd, 1, Args) of
|
||||
{_, Value} ->
|
||||
{game_cmd, Value, Args, Data};
|
||||
_ ->
|
||||
ok
|
||||
end
|
||||
end.
|
||||
|
|
@ -51,7 +51,6 @@ 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}).
|
||||
|
@ -82,22 +81,19 @@ handle_call(Msg, _From, State) ->
|
|||
%% @private
|
||||
handle_cast({notify, Player, Message}, #state { game_vm = GameVM } = State) ->
|
||||
case Message of
|
||||
{server, define, Args} ->
|
||||
ggs_gamevm_e:define(GameVM, Args);
|
||||
{game, Command, Args} ->
|
||||
ggs_gamevm_e:user_command(GameVM, Player, Command, Args)
|
||||
{server, define, Args} ->
|
||||
ggs_gamevm_e:define(GameVM, Args);
|
||||
{game, 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};
|
||||
|
|
Reference in a new issue