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 #!/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

View file

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

View file

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

View file

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

View file

@ -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:

View file

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

View file

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