Start multiple times on same terminal
This commit is contained in:
parent
c8e6bebce7
commit
762698945d
2 changed files with 68 additions and 71 deletions
|
@ -1,20 +1,20 @@
|
||||||
-module(ggs_network).
|
-module(ggs_network).
|
||||||
-export([connect/0,append_key_value_strings_to_dict/2,key_value_string_to_list/1]).
|
-export([connect/0,append_key_value_strings_to_dict/2,key_value_string_to_list/1]).
|
||||||
-export([read/1, send_command/2]).
|
-export([read/2, send_command/3]).
|
||||||
|
|
||||||
connect() ->
|
connect() ->
|
||||||
{ok,Socket} = gen_tcp:connect("localhost", 9000,[{active, false}]),
|
{ok,Socket} = gen_tcp:connect("localhost", 9000,[{active, false}]),
|
||||||
Socket.
|
Socket.
|
||||||
|
|
||||||
read(Socket) ->
|
read(Socket, Ref) ->
|
||||||
Content = receive_content(Socket),
|
Content = receive_content(Socket),
|
||||||
Headers = extract_headers(Content),
|
Headers = extract_headers(Content),
|
||||||
ContentSize = dict:fetch("Content-Size", Headers),
|
ContentSize = dict:fetch("Content-Size", Headers),
|
||||||
ContentSizeI = list_to_integer(lists:nth(1, ContentSize)),
|
ContentSizeI = list_to_integer(lists:nth(1, ContentSize)),
|
||||||
Data = receive_data(Socket, ContentSizeI, ""),
|
Data = receive_data(Socket, ContentSizeI, ""),
|
||||||
io:format("Headers: ~s~n", [Content]),
|
%io:format("Headers: ~s~n", [Content]),
|
||||||
io:format("Data: ~s~n", [Data]),
|
%io:format("Data: ~s~n", [Data]),
|
||||||
received_command(Headers, Data).
|
received_command(Headers, Data, Ref).
|
||||||
|
|
||||||
receive_content(Socket) ->
|
receive_content(Socket) ->
|
||||||
receive_content_(0, "", Socket).
|
receive_content_(0, "", Socket).
|
||||||
|
@ -37,21 +37,21 @@ receive_data(Socket, ContentSize, Headers) ->
|
||||||
{ok, Char} = gen_tcp:recv(Socket, 1),
|
{ok, Char} = gen_tcp:recv(Socket, 1),
|
||||||
receive_data(Socket, ContentSize - 1, Headers ++ Char).
|
receive_data(Socket, ContentSize - 1, Headers ++ Char).
|
||||||
|
|
||||||
received_command(Headers, Data) ->
|
received_command(Headers, Data, Ref) ->
|
||||||
{ok, CommandList} = dict:find("Client-Command", Headers),
|
{ok, CommandList} = dict:find("Client-Command", Headers),
|
||||||
Command = lists:nth(1, CommandList),
|
Command = lists:nth(1, CommandList),
|
||||||
case Command of
|
case Command of
|
||||||
"hello" ->
|
"hello" ->
|
||||||
pong_bot:set_game_token(Data),
|
pong_bot:set_game_token(Data, Ref),
|
||||||
send_command("ready", "");
|
send_command("ready", "", Ref);
|
||||||
"defined" ->
|
"defined" ->
|
||||||
ok;
|
ok;
|
||||||
Command ->
|
Command ->
|
||||||
pong_bot:ggsNetworkReceivedCommandWithArgs(Command, Data)
|
pong_bot:ggsNetworkReceivedCommandWithArgs(Command, Data, Ref)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
make_message(ServerOrGame, Command, Args) ->
|
make_message(ServerOrGame, Command, Args, Ref) ->
|
||||||
GameToken = pong_bot:get_game_token(),
|
GameToken = pong_bot:get_game_token(Ref),
|
||||||
StrGameToken = string:concat("Token: ", GameToken),
|
StrGameToken = string:concat("Token: ", GameToken),
|
||||||
StrGameTokenln = string:concat(StrGameToken, "\n"),
|
StrGameTokenln = string:concat(StrGameToken, "\n"),
|
||||||
StrCommand = string:concat("-Command: ", Command),
|
StrCommand = string:concat("-Command: ", Command),
|
||||||
|
@ -64,11 +64,11 @@ make_message(ServerOrGame, Command, Args) ->
|
||||||
MessageWithArgs = string:concat(Message, list_concat(Args,[])),
|
MessageWithArgs = string:concat(Message, list_concat(Args,[])),
|
||||||
MessageWithArgs.
|
MessageWithArgs.
|
||||||
|
|
||||||
send_command(Command, Args) ->
|
send_command(Command, Args, Ref) ->
|
||||||
write(make_message("Game", Command, Args)).
|
write(make_message("Game", Command, Args, Ref), Ref).
|
||||||
|
|
||||||
write(Message) ->
|
write(Message, Ref) ->
|
||||||
Socket = gen_server:call({global, pong_bot}, socket),
|
Socket = gen_server:call({global, {pong_bot, Ref}}, socket),
|
||||||
gen_tcp:send(Socket, Message).
|
gen_tcp:send(Socket, Message).
|
||||||
|
|
||||||
list_concat([],Ret) ->
|
list_concat([],Ret) ->
|
||||||
|
@ -76,8 +76,6 @@ list_concat([],Ret) ->
|
||||||
list_concat([E|ES],Ret) ->
|
list_concat([E|ES],Ret) ->
|
||||||
NewRet = string:concat(Ret,E),
|
NewRet = string:concat(Ret,E),
|
||||||
list_concat(ES,NewRet).
|
list_concat(ES,NewRet).
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
%%%Packet parsing.%%%
|
%%%Packet parsing.%%%
|
||||||
|
|
||||||
|
|
|
@ -2,23 +2,24 @@
|
||||||
-behaviour(gen_server).
|
-behaviour(gen_server).
|
||||||
-export([start_link/0]).
|
-export([start_link/0]).
|
||||||
-export([init/1, handle_call/3, handle_cast/2]).
|
-export([init/1, handle_call/3, handle_cast/2]).
|
||||||
-export([ggsNetworkReceivedCommandWithArgs/2,set_game_token/1,get_game_token/0]).
|
-export([ggsNetworkReceivedCommandWithArgs/3,set_game_token/2,get_game_token/1]).
|
||||||
-export([view/0, peek_socket/0]).
|
-export([view/1, peek_socket/1]).
|
||||||
|
|
||||||
|
|
||||||
start_link() ->
|
start_link() ->
|
||||||
gen_server:start_link({global, pong_bot}, pong_bot, [], []),
|
Ref = make_ref(),
|
||||||
Socket = peek_socket(),
|
gen_server:start_link({global, {pong_bot, Ref}}, pong_bot, [], []),
|
||||||
spawn(fun() -> communication_loop(Socket) end),
|
Socket = peek_socket(Ref),
|
||||||
spawn(fun() -> game_loop() end ).
|
spawn(fun() -> communication_loop(Socket, Ref) end),
|
||||||
|
spawn(fun() -> game_loop(Ref) end ).
|
||||||
|
|
||||||
communication_loop(Socket) ->
|
communication_loop(Socket, Ref) ->
|
||||||
ggs_network:read(Socket),
|
ggs_network:read(Socket, Ref),
|
||||||
communication_loop(Socket).
|
communication_loop(Socket, Ref).
|
||||||
|
|
||||||
|
|
||||||
peek_socket() ->
|
peek_socket(Ref) ->
|
||||||
gen_server:call({global, pong_bot}, socket).
|
gen_server:call({global, {pong_bot, Ref}}, socket).
|
||||||
|
|
||||||
|
|
||||||
init(_Args) ->
|
init(_Args) ->
|
||||||
|
@ -41,115 +42,113 @@ new_pos() ->
|
||||||
{0, 0}.
|
{0, 0}.
|
||||||
|
|
||||||
|
|
||||||
ggsNetworkReceivedCommandWithArgs(Command, Args) ->
|
ggsNetworkReceivedCommandWithArgs(Command, Args, Ref) ->
|
||||||
case Command of
|
case Command of
|
||||||
"welcome" ->
|
"welcome" ->
|
||||||
welcome(Args);
|
welcome(Args, Ref);
|
||||||
"ball" ->
|
"ball" ->
|
||||||
ball(Args);
|
ball(Args, Ref);
|
||||||
"player1_y" ->
|
"player1_y" ->
|
||||||
player1_y(Args);
|
player1_y(Args, Ref);
|
||||||
"player2_y" ->
|
"player2_y" ->
|
||||||
player2_y(Args);
|
player2_y(Args, Ref);
|
||||||
"game" ->
|
"game" ->
|
||||||
game(Args);
|
game(Args, Ref);
|
||||||
"player1_points" ->
|
"player1_points" ->
|
||||||
new_round();
|
new_round(Ref);
|
||||||
"player2_points" ->
|
"player2_points" ->
|
||||||
new_round();
|
new_round(Ref);
|
||||||
_ -> ok
|
_ -> ok
|
||||||
end.
|
end.
|
||||||
|
|
||||||
welcome(Who_am_I) ->
|
welcome(Who_am_I, Ref) ->
|
||||||
case Who_am_I of
|
case Who_am_I of
|
||||||
"1" ->
|
"1" ->
|
||||||
Me = gen_server:call({global, pong_bot}, player1),
|
Me = gen_server:call({global, {pong_bot, Ref}}, player1),
|
||||||
gen_server:cast({global, pong_bot}, {me, Me});
|
gen_server:cast({global, {pong_bot, Ref}}, {me, Me});
|
||||||
"2" ->
|
"2" ->
|
||||||
Me = gen_server:call({global, pong_bot}, player2),
|
Me = gen_server:call({global, {pong_bot, Ref}}, player2),
|
||||||
gen_server:cast({global, pong_bot}, {me, Me})
|
gen_server:cast({global, {pong_bot, Ref}}, {me, Me})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
game_loop() ->
|
game_loop(Ref) ->
|
||||||
timer:sleep(300),
|
timer:sleep(300),
|
||||||
gameTick(),
|
gameTick(Ref),
|
||||||
game_loop().
|
game_loop(Ref).
|
||||||
|
|
||||||
gameTick() ->
|
gameTick(Ref) ->
|
||||||
GamePaused = gen_server:call({global, pong_bot}, paused),
|
GamePaused = gen_server:call({global, {pong_bot, Ref}}, paused),
|
||||||
SendStart = gen_server:call({global, pong_bot}, start),
|
SendStart = gen_server:call({global, {pong_bot, Ref}}, start),
|
||||||
|
|
||||||
case GamePaused of
|
case GamePaused of
|
||||||
true ->
|
true ->
|
||||||
case SendStart of
|
case SendStart of
|
||||||
false ->
|
false ->
|
||||||
ggs_network:send_command("start", ""),
|
ggs_network:send_command("start", "", Ref),
|
||||||
gen_server:cast({global, pong_bot}, {start, true});
|
gen_server:cast({global, {pong_bot, Ref}}, {start, true});
|
||||||
true ->
|
true ->
|
||||||
ok
|
ok
|
||||||
end;
|
end;
|
||||||
false ->
|
false ->
|
||||||
Ball = gen_server:call({global, pong_bot}, ball),
|
Ball = gen_server:call({global, {pong_bot, Ref}}, ball),
|
||||||
{_, BallY} = Ball,
|
{_, BallY} = Ball,
|
||||||
Me = gen_server:call({global, pong_bot}, me),
|
Me = gen_server:call({global, {pong_bot, Ref}}, me),
|
||||||
{_, MeY} = Me,
|
{_, MeY} = Me,
|
||||||
|
|
||||||
case BallY < (MeY - 5) of
|
case BallY < (MeY - 5) of
|
||||||
true ->
|
true ->
|
||||||
ggs_network:send_command("up", "");
|
ggs_network:send_command("up", "", Ref);
|
||||||
_ -> case BallY > ( MeY - 5) of
|
_ -> case BallY > ( MeY - 5) of
|
||||||
true ->
|
true ->
|
||||||
ggs_network:send_command("down", "");
|
ggs_network:send_command("down", "", Ref);
|
||||||
_ -> ok
|
_ -> ok
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
ball(Pos_s) ->
|
ball(Pos_s, Ref) ->
|
||||||
PosList = string:tokens(Pos_s, ","),
|
PosList = string:tokens(Pos_s, ","),
|
||||||
XStr = lists:nth(1,PosList),
|
XStr = lists:nth(1,PosList),
|
||||||
YStr = lists:nth(2,PosList),
|
YStr = lists:nth(2,PosList),
|
||||||
X = list_to_integer(XStr),
|
X = list_to_integer(XStr),
|
||||||
Y = list_to_integer(YStr),
|
Y = list_to_integer(YStr),
|
||||||
Pos = {X, Y},
|
Pos = {X, Y},
|
||||||
gen_server:cast({global, pong_bot}, {ball, Pos}).
|
gen_server:cast({global, {pong_bot, Ref}}, {ball, Pos}).
|
||||||
|
|
||||||
player1_y(YStr) ->
|
player1_y(YStr, Ref) ->
|
||||||
Y = list_to_integer(YStr),
|
Y = list_to_integer(YStr),
|
||||||
gen_server:cast({global, pong_bot}, {player1_y, Y}).
|
gen_server:cast({global, {pong_bot, Ref}}, {player1_y, Y}).
|
||||||
|
|
||||||
player2_y(YStr) ->
|
player2_y(YStr, Ref) ->
|
||||||
Y = list_to_integer(YStr),
|
Y = list_to_integer(YStr),
|
||||||
gen_server:cast({global, pong_bot}, {player2_y, Y}).
|
gen_server:cast({global, {pong_bot, Ref}}, {player2_y, Y}).
|
||||||
|
|
||||||
game(WaitOrStart) ->
|
game(WaitOrStart, Ref) ->
|
||||||
case WaitOrStart of
|
case WaitOrStart of
|
||||||
"wait" ->
|
"wait" ->
|
||||||
ok;
|
ok;
|
||||||
_ ->
|
_ ->
|
||||||
gen_server:cast({global, pong_bot}, {paused, false})
|
gen_server:cast({global, {pong_bot, Ref}}, {paused, false})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
new_round() ->
|
new_round(Ref) ->
|
||||||
Paused = true,
|
Paused = true,
|
||||||
Start = false,
|
Start = false,
|
||||||
gen_server:cast({global, pong_bot}, {new_round, Paused, Start}).
|
gen_server:cast({global, {pong_bot, Ref}}, {new_round, Paused, Start}).
|
||||||
|
|
||||||
|
|
||||||
set_game_token(GameToken) ->
|
set_game_token(GameToken, Ref) ->
|
||||||
gen_server:cast({global, pong_bot}, {game_token, GameToken}).
|
gen_server:cast({global, {pong_bot, Ref}}, {game_token, GameToken}).
|
||||||
|
|
||||||
get_game_token() ->
|
get_game_token(Ref) ->
|
||||||
gen_server:call({global, pong_bot}, game_token).
|
gen_server:call({global, {pong_bot, Ref}}, game_token).
|
||||||
|
|
||||||
view() ->
|
view(Ref) ->
|
||||||
gen_server:call({global, pong_bot}, game_token).
|
gen_server:call({global, {pong_bot, Ref}}, game_token).
|
||||||
|
|
||||||
handle_call(player1, _From, State) ->
|
handle_call(player1, _From, State) ->
|
||||||
Player1 = dict:fetch(player1, State),
|
Player1 = dict:fetch(player1, State),
|
||||||
|
|
Reference in a new issue