Merge branch 'master' of github.com:jeena/GGS

This commit is contained in:
Jeena Paradies 2011-04-26 16:56:04 +02:00
commit 6271af5777
9 changed files with 76 additions and 44 deletions

@ -1 +1 @@
Subproject commit 00312859714bef6e9a4fdb9931a41fef56eeb89a
Subproject commit 030e0ecafff27fff8e6918add47dfcdeec363e7c

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

@ -4,6 +4,7 @@
connect() ->
{ok,Socket} = gen_tcp:connect("ggs.jeena.net", 9000,[{active, false}]),
%{ok,Socket} = gen_tcp:connect("localhost", 9000,[{active, false}]),
Socket.
read(Socket, Ref) ->

View file

@ -61,21 +61,24 @@ ggsNetworkReceivedCommandWithArgs(Command, Args, Ref) ->
"game" ->
game(Args, Ref);
"player1_points" ->
%io:format("Player1 win~n"),
new_round(Ref);
"player2_points" ->
%io:format("Player2 win~n"),
new_round(Ref);
_ -> ok
end.
welcome(Who_am_I, Ref) ->
case Who_am_I of
"1" ->
Me = gen_server:call({global, {pong_bot, Ref}}, player1),
gen_server:cast({global, {pong_bot, Ref}}, {me, Me});
"2" ->
Me = gen_server:call({global, {pong_bot, Ref}}, player2),
gen_server:cast({global, {pong_bot, Ref}}, {me, Me})
end.
gen_server:cast({global, {pong_bot, Ref}}, {me, Who_am_I}).
%case Who_am_I of
% "1" ->
% Me = gen_server:call({global, {pong_bot, Ref}}, player1),
% gen_server:cast({global, {pong_bot, Ref}}, {me, Me});
% "2" ->
% Me = gen_server:call({global, {pong_bot, Ref}}, player2),
% gen_server:cast({global, {pong_bot, Ref}}, {me, Me})
%end.
@ -101,14 +104,27 @@ gameTick(Ref) ->
Ball = gen_server:call({global, {pong_bot, Ref}}, ball),
{_, BallY} = Ball,
Me = gen_server:call({global, {pong_bot, Ref}}, me),
{_, MeY} = Me,
case Me of
"1" ->
PlayerMe = gen_server:call({global, {pong_bot, Ref}}, player1);
"2" ->
PlayerMe = gen_server:call({global, {pong_bot, Ref}}, player2)
end,
{_, MeY} = PlayerMe,
case BallY < (MeY - 5) of
case ((BallY - MeY) < 0) of
true ->
ggs_network:send_command("up", "", Ref);
_ -> case BallY > ( MeY - 5) of
%io:format("Player down sent to server~n"),
%io:format("Ball: ~B~n", [BallY]),
%io:format("Player: ~B~n", [MeY]);
_ -> case ((BallY - MeY) > 0) of
true ->
ggs_network:send_command("down", "", Ref);
%io:format("Player up sent to server~n"),
%io:format("Ball: ~B~n", [BallY]),
%io:format("Player: ~B~n", [MeY]);
_ -> ok
end
end
@ -126,10 +142,12 @@ ball(Pos_s, Ref) ->
player1_y(YStr, Ref) ->
Y = list_to_integer(YStr),
%io:format("Player1_y~n~n~n~n"),
gen_server:cast({global, {pong_bot, Ref}}, {player1_y, Y}).
player2_y(YStr, Ref) ->
Y = list_to_integer(YStr),
%io:format("Player2_y~n~n~n~n"),
gen_server:cast({global, {pong_bot, Ref}}, {player2_y, Y}).
game(WaitOrStart, Ref) ->

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

@ -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).