added pong vm
This commit is contained in:
parent
2eaa278517
commit
c7919309e9
13 changed files with 567 additions and 2348 deletions
|
@ -1,36 +1,35 @@
|
|||
function Storage(type) {
|
||||
if (type == "world" || type == "localStorage" || type == "players") {
|
||||
this.type = type;
|
||||
this.tableToken = tableToken;
|
||||
var self = this;
|
||||
|
||||
return {
|
||||
setItem: function(key, value) {
|
||||
if(this.type != "players")
|
||||
callErlang("ggs_db setItem " + escapeErlang([self.tableToken, self.type, key, value]));
|
||||
callErlang("ggs_db setItem " + escapeErlang([GGS.tableToken, self.type, key, value]));
|
||||
else
|
||||
throw "No such method setItem()";
|
||||
},
|
||||
getItem: function(key) {
|
||||
return callErlang("ggs_db getItem " + escapeErlang([self.tableToken, self.type, key]));
|
||||
return callErlang("ggs_db getItem " + escapeErlang([GGS.tableToken, self.type, key]));
|
||||
},
|
||||
key: function(position) {
|
||||
return callErlang("ggs_db key " + escapeErlang([self.tableToken, self.type, position]));
|
||||
return callErlang("ggs_db key " + escapeErlang([GGS.tableToken, self.type, position]));
|
||||
},
|
||||
length: {
|
||||
get: function() {
|
||||
return callErlang("ggs_db length " + escapeErlang([self.tableToken, self.type]));
|
||||
return callErlang("ggs_db length " + escapeErlang([GGS.tableToken, self.type]));
|
||||
}
|
||||
},
|
||||
removeItem: function(key) {
|
||||
if(this.type != "players")
|
||||
callErlang("ggs_db removeItem " + escapeErlang([self.tableToken, self.type, key]));
|
||||
callErlang("ggs_db removeItem " + escapeErlang([GGS.tableToken, self.type, key]));
|
||||
else
|
||||
throw "No such method removeItem()";
|
||||
},
|
||||
clear: function() {
|
||||
if(this.type != "players")
|
||||
callErlang("ggs_db clear " + escapeErlang([self.tableToken, self.type]));
|
||||
callErlang("ggs_db clear " + escapeErlang([GGS.tableToken, self.type]));
|
||||
else
|
||||
throw "No such method clear()";
|
||||
}
|
||||
|
@ -71,13 +70,13 @@ _GGS.prototype.sendCommandToAll = function(command, args) {
|
|||
}
|
||||
|
||||
_GGS.prototype.serverLog = function(message) {
|
||||
callErlang("error_logger info_msg " + escapeErlang([message]))
|
||||
callErlang("'error_logger info_msg " + escapeErlang([message]) + "'");
|
||||
}
|
||||
|
||||
function escapeErlang(args) {
|
||||
var str = JSON.stringify(args);
|
||||
str = str.replace("'", "\\\'");
|
||||
return "'" + str + "'";
|
||||
return str;
|
||||
}
|
||||
|
||||
function Player(token) {
|
||||
|
@ -89,7 +88,9 @@ function Player(token) {
|
|||
|
||||
return {
|
||||
sendCommand: function(command, args) {
|
||||
callErlang("ggs_table send_command " + escapeErlang(GGS.tableToken, command, args));
|
||||
ejsLog("/tmp/ggs-test.txt", "'ggs_table send_command " + escapeErlang([GGS.tableToken+ "", playerToken, command, args])+"'");
|
||||
//callErlang("'ggs_table send_command " + escapeErlang([GGS.tableToken+ "", playerToken, command, args]) + "'");
|
||||
ejsLog("/tmp/ggs-test.txt", "done");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -103,7 +103,7 @@ handle_call(join_lobby, From, State) ->
|
|||
Token = helpers:get_new_token(),
|
||||
Players = State#co_state.players,
|
||||
io:format("join_lobby from: ~p~n", [From]),
|
||||
{Pid, Sock} = From,
|
||||
{Pid, _Sock} = From,
|
||||
NewState = State#co_state{players = [{Pid, Token} | Players]},
|
||||
back_up(NewState),
|
||||
{reply, {ok, Token}, NewState};
|
||||
|
|
|
@ -54,9 +54,10 @@ stop(GameVM) ->
|
|||
init([Table]) ->
|
||||
process_flag(trap_exit, true),
|
||||
{ok, Port} = js_driver:new(),
|
||||
% %% @TODO: add here default JS API instead
|
||||
{ok, JSAPISourceCode} = file:read_file("src/ggs_api.js"),
|
||||
ok = js:define(Port, JSAPISourceCode),
|
||||
InitGGSJSString = "var GGS = new _GGS(" ++ Table ++ ");",
|
||||
ok = js:define(Port, list_to_binary(InitGGSJSString)),
|
||||
{ok, #state { port = Port, table = Table }}.
|
||||
|
||||
%% private
|
||||
|
@ -66,14 +67,20 @@ handle_call({eval, SourceCode}, _From, #state { port = Port } = State) ->
|
|||
{reply, Ret, State}.
|
||||
|
||||
%% @private
|
||||
handle_cast({define, SourceCode}, #state { port = Port } = State) ->
|
||||
ok = js:define(Port, list_to_binary(SourceCode)),
|
||||
{noreply, State};
|
||||
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) ->
|
||||
Arguments = string:concat("'", string:concat(
|
||||
string:join([js_escape(Player), js_escape(Command), js_escape(Args)], "','"), "'")),
|
||||
Js = list_to_binary(string:concat(string:concat("playerCommand(", Arguments), ");")),
|
||||
Js = list_to_binary("playerCommand(new Player('" ++ Player ++ "'), '" ++ js_escape(Command) ++ "', '" ++ js_escape(Args) ++ "');"),
|
||||
js_driver:define_js(Port, Js),
|
||||
erlang:display(binary_to_list(Js)),
|
||||
{noreply, State};
|
||||
handle_cast(stop, State) ->
|
||||
{stop, normal, State};
|
||||
|
|
279
src/ggs_gamevm_p.erl
Normal file
279
src/ggs_gamevm_p.erl
Normal file
|
@ -0,0 +1,279 @@
|
|||
%% @doc This module is responsible for running the game VM:s. You can issue
|
||||
%% commands to a vm using this module.
|
||||
|
||||
-module(ggs_gamevm_p).
|
||||
-behaviour(gen_server).
|
||||
|
||||
%% gen_server callbacks
|
||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
||||
terminate/2, code_change/3]).
|
||||
|
||||
-record(state, { port, table } ).
|
||||
|
||||
%% API
|
||||
-export([start_link/1, define/2, player_command/4, stop/1, call_js/2]).
|
||||
|
||||
|
||||
%% ----------------------------------------------------------------------
|
||||
% API implementation
|
||||
|
||||
%% @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
|
||||
{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}).
|
||||
|
||||
%% @doc Execute a player command on the specified VM. This function is
|
||||
%% asynchronous, and returns ok.
|
||||
%% @spec player_command(GameVM, User, Command, Args) -> ok
|
||||
%% GameVM = process IS of VM
|
||||
%% 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}).
|
||||
|
||||
%% @private
|
||||
% only for tests
|
||||
call_js(GameVM, SourceCode) ->
|
||||
gen_server:call(GameVM, {eval, SourceCode}).
|
||||
|
||||
% @doc stops the gamevm process
|
||||
stop(GameVM) ->
|
||||
gen_server:cast(GameVM, stop).
|
||||
|
||||
|
||||
%% ----------------------------------------------------------------------
|
||||
|
||||
%% @private
|
||||
init([Table]) ->
|
||||
process_flag(trap_exit, true),
|
||||
{ok, Port} = js_driver:new(),
|
||||
{ok, JSAPISourceCode} = file:read_file("src/ggs_api.js"),
|
||||
ok = js:define(Port, JSAPISourceCode),
|
||||
InitGGSJSString = "var GGS = new _GGS(" ++ Table ++ ");",
|
||||
ok = js:define(Port, list_to_binary(InitGGSJSString)),
|
||||
{ok, #state { port = Port, table = Table }}.
|
||||
|
||||
%% private
|
||||
% only needed for the tests
|
||||
handle_call({eval, SourceCode}, _From, #state { port = Port } = State) ->
|
||||
{ok, Ret} = js:eval(Port, list_to_binary(SourceCode)),
|
||||
{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, table = Table } = State) ->
|
||||
intern_player_command(Table, Player, Command, Args),
|
||||
{noreply, State};
|
||||
handle_cast(stop, State) ->
|
||||
{stop, normal, State};
|
||||
handle_cast(Msg, S) ->
|
||||
error_logger:error_report([unknown_msg, Msg]),
|
||||
{noreply, S}.
|
||||
|
||||
%% @private
|
||||
handle_info(Msg, S) ->
|
||||
error_logger:error_report([unknown_msg, Msg]),
|
||||
{noreply, S}.
|
||||
|
||||
%% @private
|
||||
terminate(_Reason, _State) ->
|
||||
ok.
|
||||
|
||||
%% @private
|
||||
code_change(_OldVsn, State, _Extra) ->
|
||||
{ok, State}.
|
||||
|
||||
%js_escape(S) ->
|
||||
% lists:flatmap(fun($\') -> [$\\, $\']; (X) -> [X] end, S).
|
||||
|
||||
|
||||
intern_player_command(Table, Player, Command, _Args) ->
|
||||
case Command of
|
||||
"ready" ->
|
||||
intern_add_player(Table, Player);
|
||||
"up" ->
|
||||
intern_up(Table, Player);
|
||||
"down" ->
|
||||
intern_down(Table, Player);
|
||||
"start" ->
|
||||
intern_start(Table, Player)
|
||||
end.
|
||||
|
||||
intern_add_player(Table, Player) ->
|
||||
{ok, PlayerList} = ggs_table:get_player_list(Table),
|
||||
case length(PlayerList) of
|
||||
1 ->
|
||||
ggs_db:setItem(Table, local_storage, Player, player1),
|
||||
ggs_db:setItem(Table, local_storage, player1_y, 50),
|
||||
ggs_table:send_command(Table, Player, {"welcome", int2str(1)}),
|
||||
ggs_table:notify_all_players(Table, {"player1_y", int2str(50)});
|
||||
2 ->
|
||||
ggs_db:setItem(Table, local_storage, Player, player2),
|
||||
ggs_db:setItem(Table, local_storage, player2_y, 50),
|
||||
ggs_table:send_command(Table, Player, {"welcome", int2str(2)}),
|
||||
ggs_table:send_command(Table, Player, {"player1_y", int2str(50)}),
|
||||
ggs_table:notify_all_players(Table, {"player2_y", int2str(50)});
|
||||
_Other ->
|
||||
ggs_table:send_command(Table, Player, {"not_welcome", ""})
|
||||
end.
|
||||
|
||||
|
||||
intern_up(Table, Player) ->
|
||||
case ggs_db:getItem(Table, local_storage, Player) of
|
||||
player1 ->
|
||||
Y = ggs_db:getItem(Table, local_storage, player1_y),
|
||||
NewY = Y - 10,
|
||||
case NewY >= 0 of
|
||||
true ->
|
||||
ggs_db:setItem(Table, local_storage, player1_y, NewY),
|
||||
ggs_table:notify_all_players(Table, {"player1_y", int2str(NewY)});
|
||||
_Other ->
|
||||
ggs_table:send_command(Table, Player, {"notice", "Already on top"})
|
||||
end;
|
||||
player2 ->
|
||||
Y = ggs_db:getItem(Table, local_storage, player2_y),
|
||||
NewY = Y - 10,
|
||||
case NewY >= 0 of
|
||||
true ->
|
||||
ggs_db:setItem(Table, local_storage, player2_y, NewY),
|
||||
ggs_table:notify_all_players(Table, {"player2_y", int2str(NewY)});
|
||||
_Other ->
|
||||
ggs_table:send_command(Table, Player, {"notice", "Already on top"})
|
||||
end
|
||||
end.
|
||||
|
||||
intern_down(Table, Player) ->
|
||||
case ggs_db:getItem(Table, local_storage, Player) of
|
||||
player1 ->
|
||||
Y = ggs_db:getItem(Table, local_storage, player1_y),
|
||||
NewY = Y + 10,
|
||||
case NewY =< 100 of
|
||||
true ->
|
||||
ggs_db:setItem(Table, local_storage, player1_y, NewY),
|
||||
ggs_table:notify_all_players(Table, {"player1_y", int2str(NewY)});
|
||||
_Other ->
|
||||
ggs_table:send_command(Table, Player, {"notice", "Already on bottom"})
|
||||
end;
|
||||
player2 ->
|
||||
Y = ggs_db:getItem(Table, local_storage, player2_y),
|
||||
NewY = Y + 10,
|
||||
case NewY =< 100 of
|
||||
true ->
|
||||
ggs_db:setItem(Table, local_storage, player2_y, NewY),
|
||||
ggs_table:notify_all_players(Table, {"player2_y", int2str(NewY)});
|
||||
_Other ->
|
||||
ggs_table:send_command(Table, Player, {"notice", "Already on bottom"})
|
||||
end
|
||||
end.
|
||||
|
||||
intern_start(Table, Player) ->
|
||||
case ggs_db:getItem(Table, local_storage, Player) of
|
||||
player1 ->
|
||||
ggs_db:setItem(Table, local_storage, player1_ready, true),
|
||||
ggs_db:setItem(Table, local_storage, player1_points, 0),
|
||||
case ggs_db:getItem(Table, local_storage, player2_ready) of
|
||||
true ->
|
||||
ggs_table:notify_all_players(Table, {"game", "start"}),
|
||||
ggs_db:setItem(Table, local_storage, ball, {50,50,1,1}),
|
||||
spawn(fun() -> game_loop([Table]) end);
|
||||
false ->
|
||||
ggs_table:send_command(Table, Player, {"game", "wait"})
|
||||
end;
|
||||
player2 ->
|
||||
ggs_db:setItem(Table, local_storage, player2_ready, true),
|
||||
ggs_db:setItem(Table, local_storage, player2_points, 0),
|
||||
case ggs_db:getItem(Table, local_storage, player1_ready) of
|
||||
true ->
|
||||
ggs_table:notify_all_players(Table, {"game", "start"}),
|
||||
ggs_db:setItem(Table, local_storage, ball, {50,50,-1,-1}),
|
||||
spawn(fun() -> game_loop([Table]) end);
|
||||
false ->
|
||||
ggs_table:send_command(Table, Player, {"game", "wait"})
|
||||
end
|
||||
end.
|
||||
|
||||
game_loop([Table]) ->
|
||||
receive
|
||||
tick ->
|
||||
{BX, BY, SX, SY} = step_ball(ggs_db:getItem(Table, local_storage, ball)),
|
||||
Ball = {BX, BY, SX, SY},
|
||||
ggs_db:setItem(Table, local_storage, ball, Ball),
|
||||
ggs_table:notify_all_players(Table, {"ball", int2str(BX) ++ "," ++ int2str(BY)}),
|
||||
check_ball(Table, Ball);
|
||||
'EXIT' ->
|
||||
exit(normal)
|
||||
after 5000 ->
|
||||
self() ! tick
|
||||
end.
|
||||
|
||||
int2str(Int) ->
|
||||
lists:flatten(io_lib:format("~p", [Int])).
|
||||
|
||||
step_ball({BX, BY, SX, SY}) ->
|
||||
{BX + SX, BY + SY, SX, BY}.
|
||||
|
||||
check_ball(Table, {BX, BY, SX, SY}) ->
|
||||
% check up and down bounds
|
||||
case (BY > 90) or (BY < 0) of
|
||||
true ->
|
||||
NewSY = -SY;
|
||||
false ->
|
||||
NewSY = SY
|
||||
end,
|
||||
|
||||
% check intersection with player1
|
||||
P1Y = ggs_db:getItem(Table, local_storage, player1_y),
|
||||
case check_intersect({0, P1Y, 10, 30}, {BX, BY, 10, 10}) of
|
||||
true ->
|
||||
SX1 = -SX;
|
||||
false ->
|
||||
SX1 = SX
|
||||
end,
|
||||
|
||||
% check intersection with player2
|
||||
P2Y = ggs_db:getItem(Table, local_storage, player2_y),
|
||||
case check_intersect({90, P2Y, 10, 30}, {BX, BY, 10, 10}) of
|
||||
true ->
|
||||
SX2 = - SX1;
|
||||
false ->
|
||||
SX2 = SX1
|
||||
end,
|
||||
ggs_db:setItem(Table, local_storage, ball, {BX, BY , SX2, NewSY}),
|
||||
|
||||
% check for point player1
|
||||
if BX > 90 ->
|
||||
Player1Points = ggs_db:getItem(Table, local_storage, player1_points),
|
||||
NewPlayer1Points = Player1Points + 1,
|
||||
ggs_db:setItem(Table, local_storage, player1_points, NewPlayer1Points),
|
||||
ggs_table:notify_all_players(Table, {"player1_points", int2str(NewPlayer1Points)}),
|
||||
exit(normal)
|
||||
end,
|
||||
|
||||
% check for point player2
|
||||
if BX < 0 ->
|
||||
Player2Points = ggs_db:getItem(Table, local_storage, player2_points),
|
||||
NewPlayer2Points = Player2Points + 1,
|
||||
ggs_db:setItem(Table, local_storage, player2_points, NewPlayer2Points),
|
||||
ggs_table:notify_all_players(Table, {"player2_points", int2str(NewPlayer2Points)}),
|
||||
exit(normal)
|
||||
end.
|
||||
|
||||
|
||||
check_intersect({AX, AY, AW, AH}, {BX, BY, BW, BH}) ->
|
||||
not (BX > (AX + AW)) or ((BX + BW) < AX) or (BY > (AY + AH)) or ((BY + BH) < AY).
|
|
@ -23,14 +23,14 @@ start_link(Socket) ->
|
|||
% us, otherwise these messages end up in our parent.
|
||||
erlang:port_connect(Socket, self()),
|
||||
{ok, Token} = ggs_coordinator:join_lobby(),
|
||||
TableStatus = ggs_coordinator:join_table(1337),
|
||||
TableStatus = ggs_coordinator:join_table("1337"),
|
||||
case TableStatus of
|
||||
{ok, Table} ->
|
||||
notify(self(), self(), {"hello", 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),
|
||||
ggs_coordinator:create_table({force, "1337"}),
|
||||
{ok, Table} = ggs_coordinator:join_table("1337"),
|
||||
notify(self(), self(), {"hello", Token}),
|
||||
loop(#pl_state{socket = Socket, token = Token, table = Table})
|
||||
end.
|
||||
|
@ -42,7 +42,7 @@ start_link(Socket) ->
|
|||
notify(Player, From, Message) ->
|
||||
erlang:display(Message),
|
||||
{Cmd, Data} = Message,
|
||||
Parsed = ggs_protocol:create_message(Cmd, "text","text", Data),
|
||||
Parsed = ggs_protocol:create_message(Cmd, "text","text", Data),
|
||||
Player ! {notify, From, Parsed}.
|
||||
|
||||
%% @doc Get the player token uniquely representing the player.
|
||||
|
@ -69,6 +69,7 @@ loop(#pl_state{token = _Token, socket = Socket, table = Table} = State) ->
|
|||
self() ! Parsed,
|
||||
loop(State);
|
||||
{notify, _From, Message} ->
|
||||
erlang:display(Message),
|
||||
gen_tcp:send(Socket, Message),
|
||||
loop(State);
|
||||
% Below are messages generated by the parser
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
notify_all_players/2,
|
||||
notify_game/3,
|
||||
get_player_list/1,
|
||||
notify_player/4]).
|
||||
send_command/3]).
|
||||
|
||||
|
||||
%% ----------------------------------------------------------------------
|
||||
|
@ -66,24 +66,16 @@ notify_game(TablePid, From, Message) ->
|
|||
|
||||
%% @doc Notify a player sitting at this table with the message supplied.
|
||||
%% Player, Table and From are in token form.
|
||||
notify_player(TableToken, PlayerToken, From, Message) ->
|
||||
send_command(TableToken, PlayerToken, Message) ->
|
||||
TablePid = ggs_coordinator:table_token_to_pid(TableToken),
|
||||
%PlayerPid = ggs_coordinator:player_token_to_pid(PlayerToken),
|
||||
gen_server:cast(TablePid, {notify_player, PlayerToken, From, Message}).
|
||||
|
||||
send_command(TableToken, PlayerToken, Command, Args) ->
|
||||
gen_logger:not_implemented().
|
||||
|
||||
send_command_to_all(TableToken, Command, Args) ->
|
||||
gen_logger:not_implemented().
|
||||
|
||||
gen_server:cast(TablePid, {notify_player, PlayerToken, self(), Message}).
|
||||
|
||||
%% ----------------------------------------------------------------------
|
||||
|
||||
%% @private
|
||||
init([TableToken]) ->
|
||||
process_flag(trap_exit, true),
|
||||
GameVM = ggs_gamevm:start_link(TableToken),
|
||||
GameVM = ggs_gamevm_p:start_link(TableToken),
|
||||
{ok, #state {
|
||||
game_vm = GameVM,
|
||||
players = [] }}.
|
||||
|
@ -110,14 +102,14 @@ handle_cast({notify, Player, Message}, #state { game_vm = GameVM } = State) ->
|
|||
PlayerToken = ggs_coordinator:player_pid_to_token(Player),
|
||||
case Message of
|
||||
{server, define, Args} ->
|
||||
ggs_gamevm:define(GameVM, Args);
|
||||
ggs_gamevm_p:define(GameVM, Args);
|
||||
{game, Command, Args} ->
|
||||
ggs_gamevm:player_command(GameVM, PlayerToken, Command, Args)
|
||||
ggs_gamevm_p:player_command(GameVM, PlayerToken, Command, Args)
|
||||
end,
|
||||
{noreply, State};
|
||||
|
||||
handle_cast({notify_game, Message, From}, #state { game_vm = GameVM } = State) ->
|
||||
ggs_gamevm:player_command(GameVM, From, Message, ""),
|
||||
ggs_gamevm_p:player_command(GameVM, From, Message, ""),
|
||||
{noreply, State};
|
||||
|
||||
handle_cast({notify_all_players, Message}, #state{players = Players} = State) ->
|
||||
|
|
Reference in a new issue