This commit is contained in:
Kallfaktorn 2011-04-26 16:16:20 +02:00
commit b7fc68a010
8 changed files with 94 additions and 32 deletions

View file

@ -1,5 +1,5 @@
#!/bin/sh
for (( i = 0; i < 2; i++ )); do
./pong-bot.rb &
done
ruby pong-bot.rb &
done

View file

@ -23,6 +23,7 @@ class GGSNetwork
def connect(host='localhost', port=9000)
@socket = TCPSocket.new(host, port)
sprintf(@socket)
read
end
@ -47,7 +48,7 @@ class GGSNetwork
key, value = line.split(": ")
headers[key] = value.strip
end
if headers.has_key?("Content-Size")
args = @socket.read(headers["Content-Size"].to_i)
end

View file

@ -40,9 +40,9 @@ class PongBot
when "player2_points" then new_round()
end
end
protected
def gameTick()
if @game_paused
unless @send_start

View file

@ -51,7 +51,7 @@ Content-Type: text\n\
Content-Length: 23\n\
\n\
Hello guys, what's up?\n" % token)
time.sleep(1)
time.sleep(3)
while True:

View file

@ -8,10 +8,10 @@
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-record(state, { port, table } ).
-record(state, { port, table, global } ).
%% 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
%% with for example the define method of this module.
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], []),
Pid.
%% @doc Define some new code on the specified VM, returns the atom ok.
define(GameVM, SourceCode) ->
gen_server:cast(GameVM, {define, SourceCode}).
define(GameVM, Key, SourceCode) ->
gen_server:cast(GameVM, {define, Key, SourceCode}). %c Mod
%% @doc Execute a player command on the specified VM. This function is
%% asynchronous, and returns ok.
@ -35,8 +37,8 @@ define(GameVM, SourceCode) ->
%% Player = the player running the command
%% Command = a game command to run
%% Args = arguments for the Command parameter
player_command(GameVM, Player, Command, Args) ->
gen_server:cast(GameVM, {player_command, Player, Command, Args}).
player_command(GameVM, Key, Player, Command, Args) ->
gen_server:cast(GameVM, {player_command, Key, Player, Command, Args}).
%% @private
% only for tests
@ -53,33 +55,43 @@ stop(GameVM) ->
%% @private
init([Table]) ->
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 = js:define(Port, JSAPISourceCode),
%ok = js:define(Port, JSAPISourceCode), c Old
Global:set_value("src", JSAPISourceCode), %c New
InitGGSJSString = "var GGS = new _GGS(" ++ Table ++ ");",
ok = js:define(Port, list_to_binary(InitGGSJSString)),
{ok, #state { port = Port, table = Table }}.
%ok = js:define(Port, list_to_binary(InitGGSJSString)), c Old
Global:set_value("ggs", InitGGSJSString), %c New
{ok, #state { port = Port, table = Table, global = Global }}. %c Mod
%% private
% only needed for the tests
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}.
%% @private
handle_cast({define, SourceCode}, #state { port = Port, table = Table } = State) ->
Ret = js:define(Port, list_to_binary(SourceCode)),
case Ret of
ok ->
ggs_table:notify_all_players(Table, {"defined", "ok"}),
{noreply, State};
Other ->
ggs_table:notify_all_players(Table, {"defined", "error " ++ Other}),
{noreply, State}
end;
handle_cast({player_command, Player, Command, Args}, #state { port = Port } = State) ->
Js = list_to_binary("playerCommand(new Player('" ++ Player ++ "'), '" ++ js_escape(Command) ++ "', '" ++ js_escape(Args) ++ "');"),
js_driver:define_js(Port, Js),
handle_cast({define, Key, SourceCode}, #state { port = Port, table = Table, global = Global } = State) -> %c Mod
%Ret = js:define(Port, list_to_binary(SourceCode)), %c old
Global:set_value(Key, SourceCode),
ggs_table:notify_all_players(Table, {"defined", "ok"}), %c ok
{noreply, State}; %c New
%case Ret of %c Old
% ok ->
% ggs_table:notify_all_players(Table, {"defined", "ok"}),
% {noreply, State};
% Other ->
% ggs_table:notify_all_players(Table, {"defined", "error " ++ Other}),
% {noreply, State}
%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)),
{noreply, State};
handle_cast(stop, State) ->

View file

@ -43,6 +43,7 @@ create_message({Command, Data}) ->
%% Assemble a message which can b
%e used as a reply to a client
create_message(Cmd, Enc, Acc, Data) ->
ggs_stats:message(),
Length = integer_to_list(string:len(Data)),
Msg = "Client-Command: " ++ Cmd ++ "\n" ++
"Client-Encoding: " ++ Enc ++ "\n" ++
@ -109,6 +110,7 @@ code_change(_OldVsn, State, _Extra) ->
prettify(Args, Data) ->
ggs_stats:message(),
case lists:keyfind("Server-Command", 1, Args) of
{_, Value} ->
{srv_cmd, Value, Args, Data};

47
src/ggs_stats.erl Normal file
View 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}.

View file

@ -4,6 +4,6 @@
start() ->
application:start(inets),
application:start(erlang_js),
ggs_stats:start_link(),
%ggs_stats:start_link(),
ggs_db:init(),
application:start(ggs).