merge
This commit is contained in:
commit
b7fc68a010
8 changed files with 94 additions and 32 deletions
|
@ -1,5 +1,5 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
for (( i = 0; i < 2; i++ )); do
|
for (( i = 0; i < 2; i++ )); do
|
||||||
./pong-bot.rb &
|
ruby pong-bot.rb &
|
||||||
done
|
done
|
||||||
|
|
|
@ -23,6 +23,7 @@ class GGSNetwork
|
||||||
|
|
||||||
def connect(host='localhost', port=9000)
|
def connect(host='localhost', port=9000)
|
||||||
@socket = TCPSocket.new(host, port)
|
@socket = TCPSocket.new(host, port)
|
||||||
|
sprintf(@socket)
|
||||||
read
|
read
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -47,7 +48,7 @@ class GGSNetwork
|
||||||
key, value = line.split(": ")
|
key, value = line.split(": ")
|
||||||
headers[key] = value.strip
|
headers[key] = value.strip
|
||||||
end
|
end
|
||||||
|
|
||||||
if headers.has_key?("Content-Size")
|
if headers.has_key?("Content-Size")
|
||||||
args = @socket.read(headers["Content-Size"].to_i)
|
args = @socket.read(headers["Content-Size"].to_i)
|
||||||
end
|
end
|
||||||
|
|
|
@ -40,9 +40,9 @@ class PongBot
|
||||||
when "player2_points" then new_round()
|
when "player2_points" then new_round()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def gameTick()
|
def gameTick()
|
||||||
if @game_paused
|
if @game_paused
|
||||||
unless @send_start
|
unless @send_start
|
||||||
|
|
|
@ -51,7 +51,7 @@ Content-Type: text\n\
|
||||||
Content-Length: 23\n\
|
Content-Length: 23\n\
|
||||||
\n\
|
\n\
|
||||||
Hello guys, what's up?\n" % token)
|
Hello guys, what's up?\n" % token)
|
||||||
time.sleep(1)
|
time.sleep(3)
|
||||||
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
|
|
@ -8,10 +8,10 @@
|
||||||
-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, { port, table } ).
|
-record(state, { port, table, global } ).
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([start_link/1, define/2, player_command/4, stop/1, call_js/2]).
|
-export([start_link/1, define/3, player_command/5, stop/1, call_js/2]).
|
||||||
|
|
||||||
|
|
||||||
%% ----------------------------------------------------------------------
|
%% ----------------------------------------------------------------------
|
||||||
|
@ -20,13 +20,15 @@
|
||||||
%% @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(Table) ->
|
start_link(Table) ->
|
||||||
erlang_js:start(), %% @TODO: should only be done once
|
%erlv8_vm:start(), %c NEW
|
||||||
|
application:start(erlv8),
|
||||||
|
%erlang_js:start(), %c OLD %% @TODO: should only be done once
|
||||||
{ok, Pid} = gen_server:start_link(?MODULE, [Table], []),
|
{ok, Pid} = gen_server:start_link(?MODULE, [Table], []),
|
||||||
Pid.
|
Pid.
|
||||||
|
|
||||||
%% @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, Key, SourceCode) ->
|
||||||
gen_server:cast(GameVM, {define, SourceCode}).
|
gen_server:cast(GameVM, {define, Key, SourceCode}). %c Mod
|
||||||
|
|
||||||
%% @doc Execute a player command on the specified VM. This function is
|
%% @doc Execute a player command on the specified VM. This function is
|
||||||
%% asynchronous, and returns ok.
|
%% asynchronous, and returns ok.
|
||||||
|
@ -35,8 +37,8 @@ define(GameVM, SourceCode) ->
|
||||||
%% Player = the player running the command
|
%% Player = the player running the command
|
||||||
%% Command = a game command to run
|
%% Command = a game command to run
|
||||||
%% Args = arguments for the Command parameter
|
%% Args = arguments for the Command parameter
|
||||||
player_command(GameVM, Player, Command, Args) ->
|
player_command(GameVM, Key, Player, Command, Args) ->
|
||||||
gen_server:cast(GameVM, {player_command, Player, Command, Args}).
|
gen_server:cast(GameVM, {player_command, Key, Player, Command, Args}).
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
% only for tests
|
% only for tests
|
||||||
|
@ -53,33 +55,43 @@ stop(GameVM) ->
|
||||||
%% @private
|
%% @private
|
||||||
init([Table]) ->
|
init([Table]) ->
|
||||||
process_flag(trap_exit, true),
|
process_flag(trap_exit, true),
|
||||||
{ok, Port} = js_driver:new(),
|
%{ok, Port} = js_driver:new(), c Old
|
||||||
|
{ok, Port} = erlv8_vm:start(), %c New
|
||||||
|
Global = erlv8_vm:global(Port), %c New
|
||||||
{ok, JSAPISourceCode} = file:read_file("src/ggs_api.js"),
|
{ok, JSAPISourceCode} = file:read_file("src/ggs_api.js"),
|
||||||
ok = js:define(Port, JSAPISourceCode),
|
%ok = js:define(Port, JSAPISourceCode), c Old
|
||||||
|
Global:set_value("src", JSAPISourceCode), %c New
|
||||||
InitGGSJSString = "var GGS = new _GGS(" ++ Table ++ ");",
|
InitGGSJSString = "var GGS = new _GGS(" ++ Table ++ ");",
|
||||||
ok = js:define(Port, list_to_binary(InitGGSJSString)),
|
%ok = js:define(Port, list_to_binary(InitGGSJSString)), c Old
|
||||||
{ok, #state { port = Port, table = Table }}.
|
Global:set_value("ggs", InitGGSJSString), %c New
|
||||||
|
{ok, #state { port = Port, table = Table, global = Global }}. %c Mod
|
||||||
|
|
||||||
%% private
|
%% private
|
||||||
% only needed for the tests
|
% only needed for the tests
|
||||||
handle_call({eval, SourceCode}, _From, #state { port = Port } = State) ->
|
handle_call({eval, SourceCode}, _From, #state { port = Port } = State) ->
|
||||||
{ok, Ret} = js:eval(Port, list_to_binary(SourceCode)),
|
%{ok, Ret} = js:eval(Port, list_to_binary(SourceCode)), c Old
|
||||||
|
{ok, Ret} = erlv8_vm:run(Port, SourceCode), %c New
|
||||||
{reply, Ret, State}.
|
{reply, Ret, State}.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
handle_cast({define, SourceCode}, #state { port = Port, table = Table } = State) ->
|
handle_cast({define, Key, SourceCode}, #state { port = Port, table = Table, global = Global } = State) -> %c Mod
|
||||||
Ret = js:define(Port, list_to_binary(SourceCode)),
|
%Ret = js:define(Port, list_to_binary(SourceCode)), %c old
|
||||||
case Ret of
|
Global:set_value(Key, SourceCode),
|
||||||
ok ->
|
ggs_table:notify_all_players(Table, {"defined", "ok"}), %c ok
|
||||||
ggs_table:notify_all_players(Table, {"defined", "ok"}),
|
{noreply, State}; %c New
|
||||||
{noreply, State};
|
%case Ret of %c Old
|
||||||
Other ->
|
% ok ->
|
||||||
ggs_table:notify_all_players(Table, {"defined", "error " ++ Other}),
|
% ggs_table:notify_all_players(Table, {"defined", "ok"}),
|
||||||
{noreply, State}
|
% {noreply, State};
|
||||||
end;
|
% Other ->
|
||||||
handle_cast({player_command, Player, Command, Args}, #state { port = Port } = State) ->
|
% ggs_table:notify_all_players(Table, {"defined", "error " ++ Other}),
|
||||||
Js = list_to_binary("playerCommand(new Player('" ++ Player ++ "'), '" ++ js_escape(Command) ++ "', '" ++ js_escape(Args) ++ "');"),
|
% {noreply, State}
|
||||||
js_driver:define_js(Port, Js),
|
%end;
|
||||||
|
handle_cast({player_command, Key, Player, Command, Args}, #state { port = Port, global = Global } = State) ->
|
||||||
|
%Js = list_to_binary("playerCommand(new Player('" ++ Player ++ "'), '" ++ js_escape(Command) ++ "', '" ++ js_escape(Args) ++ "');"), %c Old
|
||||||
|
Js = "playerCommand(new Player('" ++ Player ++ "'), '" ++ js_escape(Command) ++ "', '" ++ js_escape(Args) ++ "');", %c New
|
||||||
|
%%js_driver:define_js(Port, Js), %c old
|
||||||
|
Global:set_value(Key, Js), %c new
|
||||||
erlang:display(binary_to_list(Js)),
|
erlang:display(binary_to_list(Js)),
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
handle_cast(stop, State) ->
|
handle_cast(stop, State) ->
|
||||||
|
|
|
@ -43,6 +43,7 @@ create_message({Command, Data}) ->
|
||||||
%% Assemble a message which can b
|
%% Assemble a message which can b
|
||||||
%e used as a reply to a client
|
%e used as a reply to a client
|
||||||
create_message(Cmd, Enc, Acc, Data) ->
|
create_message(Cmd, Enc, Acc, Data) ->
|
||||||
|
ggs_stats:message(),
|
||||||
Length = integer_to_list(string:len(Data)),
|
Length = integer_to_list(string:len(Data)),
|
||||||
Msg = "Client-Command: " ++ Cmd ++ "\n" ++
|
Msg = "Client-Command: " ++ Cmd ++ "\n" ++
|
||||||
"Client-Encoding: " ++ Enc ++ "\n" ++
|
"Client-Encoding: " ++ Enc ++ "\n" ++
|
||||||
|
@ -109,6 +110,7 @@ code_change(_OldVsn, State, _Extra) ->
|
||||||
|
|
||||||
|
|
||||||
prettify(Args, Data) ->
|
prettify(Args, Data) ->
|
||||||
|
ggs_stats:message(),
|
||||||
case lists:keyfind("Server-Command", 1, Args) of
|
case lists:keyfind("Server-Command", 1, Args) of
|
||||||
{_, Value} ->
|
{_, Value} ->
|
||||||
{srv_cmd, Value, Args, Data};
|
{srv_cmd, Value, Args, Data};
|
||||||
|
|
47
src/ggs_stats.erl
Normal file
47
src/ggs_stats.erl
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
-module(ggs_stats).
|
||||||
|
-export([start_link/0, message/0, print/0, tick/0]).
|
||||||
|
-behaviour(gen_server).
|
||||||
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
||||||
|
terminate/2, code_change/3]).
|
||||||
|
-vsn(0).
|
||||||
|
|
||||||
|
-record(ate, { count = 0 }).
|
||||||
|
-define(SERVER, ?MODULE).
|
||||||
|
|
||||||
|
message() ->
|
||||||
|
gen_server:cast(ggs_stats, add_one).
|
||||||
|
|
||||||
|
print() ->
|
||||||
|
gen_server:cast(ggs_stats, print).
|
||||||
|
|
||||||
|
tick() ->
|
||||||
|
print(),
|
||||||
|
gen_server:cast(ggs_stats, tick),
|
||||||
|
timer:apply_after(1000, ggs_stats, tick, []).
|
||||||
|
|
||||||
|
start_link() ->
|
||||||
|
gen_server:start_link({local, ?SERVER}, ?MODULE, [0], []).
|
||||||
|
|
||||||
|
|
||||||
|
init([Count]) ->
|
||||||
|
St = #ate{ count = Count },
|
||||||
|
{ok, St}.
|
||||||
|
|
||||||
|
handle_cast(add_one, St) ->
|
||||||
|
NewSt = #ate { count = St#ate.count + 1},
|
||||||
|
{noreply, NewSt};
|
||||||
|
|
||||||
|
handle_cast(print, St) ->
|
||||||
|
erlang:display(St#ate.count),
|
||||||
|
{noreply, St};
|
||||||
|
|
||||||
|
handle_cast(tick, _St) ->
|
||||||
|
NewSt = #ate { count = 0 },
|
||||||
|
{noreply, NewSt}.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
handle_call(_Request, _From, St) -> {stop, unimplemented, St}.
|
||||||
|
handle_info(_Info, St) -> {stop, unimplemented, St}.
|
||||||
|
terminate(_Reason, _St) -> ok.
|
||||||
|
code_change(_OldVsn, St, _Extra) -> {ok, St}.
|
|
@ -4,6 +4,6 @@
|
||||||
start() ->
|
start() ->
|
||||||
application:start(inets),
|
application:start(inets),
|
||||||
application:start(erlang_js),
|
application:start(erlang_js),
|
||||||
ggs_stats:start_link(),
|
%ggs_stats:start_link(),
|
||||||
ggs_db:init(),
|
ggs_db:init(),
|
||||||
application:start(ggs).
|
application:start(ggs).
|
||||||
|
|
Reference in a new issue