hello
This commit is contained in:
parent
77c6038ec8
commit
fb6457660b
2 changed files with 62 additions and 40 deletions
|
@ -1,6 +1,6 @@
|
||||||
-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]).
|
-export([read/1, send_command/2]).
|
||||||
|
|
||||||
connect() ->
|
connect() ->
|
||||||
{ok,Socket} = gen_tcp:connect("localhost", 9000,[{active, false}]),
|
{ok,Socket} = gen_tcp:connect("localhost", 9000,[{active, false}]),
|
||||||
|
@ -10,9 +10,11 @@ connect() ->
|
||||||
read(Message) ->
|
read(Message) ->
|
||||||
case Message of
|
case Message of
|
||||||
{ok, M} ->
|
{ok, M} ->
|
||||||
|
io:format("Message: ~n~n~s~n", [M]),
|
||||||
HeaderList = string:tokens(M, "\n"),
|
HeaderList = string:tokens(M, "\n"),
|
||||||
Headers = extract_headers(HeaderList),
|
Headers = extract_headers(HeaderList),
|
||||||
Data = extract_data(HeaderList),
|
Data = extract_data(HeaderList),
|
||||||
|
io:format("Data: ~s~n", [Data]),
|
||||||
received_command(Headers, Data)
|
received_command(Headers, Data)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
@ -21,17 +23,13 @@ received_command(Headers, Data) ->
|
||||||
Command = lists:nth(1, CommandList),
|
Command = lists:nth(1, CommandList),
|
||||||
case Command of
|
case Command of
|
||||||
"hello" ->
|
"hello" ->
|
||||||
io:format("Received command 'hello'~n"),
|
|
||||||
io:format("Game token: ~s~n", [Data]),
|
|
||||||
pong_bot:set_game_token(Data),
|
pong_bot:set_game_token(Data),
|
||||||
%gen_server:cast({global, pong_bot}, {game_token, Data}),
|
|
||||||
send_command("ready", "");
|
send_command("ready", "");
|
||||||
%pong_bot:ggsNetworkReady(); Unneccessary
|
|
||||||
"defined" ->
|
"defined" ->
|
||||||
|
io:format("Defined~n"),
|
||||||
ok;
|
ok;
|
||||||
%pong_bot:ggsNetworkDefined(); Unneccessary
|
|
||||||
Command ->
|
Command ->
|
||||||
gen_server:ggsNetworkReceivedCommandWithArgs(Command, Data)
|
pong_bot:ggsNetworkReceivedCommandWithArgs(Command, Data)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
make_message(ServerOrGame, Command, Args) ->
|
make_message(ServerOrGame, Command, Args) ->
|
||||||
|
@ -46,11 +44,10 @@ make_message(ServerOrGame, Command, Args) ->
|
||||||
StrTokenCommand = string:concat(StrGameTokenln, StrFullCommand),
|
StrTokenCommand = string:concat(StrGameTokenln, StrFullCommand),
|
||||||
Message = string:concat(StrTokenCommand, StrContentLengthln),
|
Message = string:concat(StrTokenCommand, StrContentLengthln),
|
||||||
MessageWithArgs = string:concat(Message, list_concat(Args,[])),
|
MessageWithArgs = string:concat(Message, list_concat(Args,[])),
|
||||||
io:format("Make message: ~n~n~s",[MessageWithArgs]),
|
|
||||||
MessageWithArgs.
|
MessageWithArgs.
|
||||||
|
|
||||||
send_command(Command, Args) ->
|
send_command(Command, Args) ->
|
||||||
write(make_message("Client", Command, Args)).
|
write(make_message("Game", Command, Args)).
|
||||||
|
|
||||||
write(Message) ->
|
write(Message) ->
|
||||||
Socket = gen_server:call({global, pong_bot}, socket),
|
Socket = gen_server:call({global, pong_bot}, socket),
|
||||||
|
@ -74,6 +71,7 @@ extract_headers(Source) ->
|
||||||
extract_data([]) ->
|
extract_data([]) ->
|
||||||
[];
|
[];
|
||||||
extract_data([E|ES]) ->
|
extract_data([E|ES]) ->
|
||||||
|
io:format("~n~s~n", [E]),
|
||||||
KeyValueList = key_value_string_to_list(E),
|
KeyValueList = key_value_string_to_list(E),
|
||||||
case length(KeyValueList) of
|
case length(KeyValueList) of
|
||||||
2 ->
|
2 ->
|
||||||
|
|
|
@ -9,29 +9,33 @@
|
||||||
start_link() ->
|
start_link() ->
|
||||||
gen_server:start_link({global, pong_bot}, pong_bot, [], []),
|
gen_server:start_link({global, pong_bot}, pong_bot, [], []),
|
||||||
Socket = peek_socket(),
|
Socket = peek_socket(),
|
||||||
|
spawn(fun() -> communication_loop(Socket) end),
|
||||||
|
spawn(fun() -> game_loop() end ).
|
||||||
|
|
||||||
|
communication_loop(Socket) ->
|
||||||
A = gen_tcp:recv(Socket, 0),
|
A = gen_tcp:recv(Socket, 0),
|
||||||
ggs_network:read(A).
|
ggs_network:read(A),
|
||||||
|
communication_loop(Socket).
|
||||||
|
|
||||||
|
|
||||||
peek_socket() ->
|
peek_socket() ->
|
||||||
gen_server:call({global, pong_bot}, socket).
|
gen_server:call({global, pong_bot}, socket).
|
||||||
|
|
||||||
|
|
||||||
init(_Args) ->
|
init(_Args) ->
|
||||||
io:format("State initialization.~n"),
|
|
||||||
Player1 = new_pos(),
|
Player1 = new_pos(),
|
||||||
Player2 = new_pos(),
|
Player2 = new_pos(),
|
||||||
Ball = new_pos(),
|
Ball = new_pos(),
|
||||||
Paused = true,
|
Paused = true,
|
||||||
SendStart = false,
|
Start = false,
|
||||||
Socket = ggs_network:connect(), %Localhost is set internally inside ggs_network.
|
Socket = ggs_network:connect(), %Localhost is set internally inside ggs_network.
|
||||||
State1 = dict:new(),
|
State1 = dict:new(),
|
||||||
State2 = dict:store(player1, Player1, State1),
|
State2 = dict:store(player1, Player1, State1),
|
||||||
State3 = dict:store(player2, Player2, State2),
|
State3 = dict:store(player2, Player2, State2),
|
||||||
State4 = dict:store(ball, Ball, State3),
|
State4 = dict:store(ball, Ball, State3),
|
||||||
State5 = dict:store(paused, Paused, State4),
|
State5 = dict:store(paused, Paused, State4),
|
||||||
State6 = dict:store(send_start, SendStart, State5),
|
State6 = dict:store(start, Start, State5),
|
||||||
State = dict:store(socket, Socket, State6),
|
State = dict:store(socket, Socket, State6),
|
||||||
io:format("End State initialization.~n"),
|
|
||||||
{ok, State}.
|
{ok, State}.
|
||||||
|
|
||||||
new_pos() ->
|
new_pos() ->
|
||||||
|
@ -58,44 +62,47 @@ ggsNetworkReceivedCommandWithArgs(Command, Args) ->
|
||||||
|
|
||||||
welcome(Who_am_I) ->
|
welcome(Who_am_I) ->
|
||||||
io:format("Welcome begin~n"),
|
io:format("Welcome begin~n"),
|
||||||
|
io:format("I am player: ~s~n", [Who_am_I]),
|
||||||
case Who_am_I of
|
case Who_am_I of
|
||||||
1 ->
|
"1" ->
|
||||||
Me = gen_server:call(pong_bot, player1),
|
Me = gen_server:call({global, pong_bot}, player1),
|
||||||
gen_server:cast(pong_bot, {me, Me});
|
gen_server:cast({global, pong_bot}, {me, Me});
|
||||||
2 ->
|
"2" ->
|
||||||
Me = gen_server:call(pong_bot, player2),
|
Me = gen_server:call({global, pong_bot}, player2),
|
||||||
gen_server:cast(pong_bot, {me, Me})
|
gen_server:cast({global, pong_bot}, {me, Me})
|
||||||
end,
|
end.
|
||||||
|
|
||||||
loop().
|
|
||||||
|
|
||||||
loop() ->
|
|
||||||
|
game_loop() ->
|
||||||
timer:sleep(300),
|
timer:sleep(300),
|
||||||
gameTick(),
|
gameTick(),
|
||||||
loop().
|
game_loop().
|
||||||
|
|
||||||
gameTick() ->
|
gameTick() ->
|
||||||
GamePaused = gen_server:call(pong_bot, paused),
|
GamePaused = gen_server:call({global, pong_bot}, paused),
|
||||||
SendStart = gen_server:call(pong_bot, send_start),
|
SendStart = gen_server:call({global, pong_bot}, 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", ""),
|
||||||
gen_server:cast(pong_bot, {send_start, true})
|
gen_server:cast({global, pong_bot}, {start, true});
|
||||||
|
true ->
|
||||||
|
ok
|
||||||
end;
|
end;
|
||||||
false ->
|
false ->
|
||||||
Ball = gen_server:call(pong_bot, ball),
|
Ball = gen_server:call({global, pong_bot}, ball),
|
||||||
{_, BallY} = Ball,
|
{_, BallY} = Ball,
|
||||||
Me = gen_server:call(pong_bot, me),
|
Me = gen_server:call({global, pong_bot}, 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", "");
|
||||||
false ->
|
false ->
|
||||||
ggs_network:send_command("down")
|
ggs_network:send_command("down", "")
|
||||||
end
|
end
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
@ -109,29 +116,29 @@ ball(Pos_s) ->
|
||||||
X = string:to_integer(XStr),
|
X = string:to_integer(XStr),
|
||||||
Y = string:to_integer(YStr),
|
Y = string:to_integer(YStr),
|
||||||
Pos = {X, Y},
|
Pos = {X, Y},
|
||||||
gen_server:cast(pong_bot, {ball, Pos}).
|
gen_server:cast({global, pong_bot}, {ball, Pos}).
|
||||||
|
|
||||||
player1_y(YStr) ->
|
player1_y(YStr) ->
|
||||||
Y = string:to_integer(YStr),
|
Y = string:to_integer(YStr),
|
||||||
gen_server:cast(pong_bot, {player1_y, Y}).
|
gen_server:cast({global, pong_bot}, {player1_y, Y}).
|
||||||
|
|
||||||
player2_y(YStr) ->
|
player2_y(YStr) ->
|
||||||
Y = string:to_integer(YStr),
|
Y = string:to_integer(YStr),
|
||||||
gen_server:cast(pong_bot, {player2_y, Y}).
|
gen_server:cast({global, pong_bot}, {player2_y, Y}).
|
||||||
|
|
||||||
game(WaitOrStart) ->
|
game(WaitOrStart) ->
|
||||||
case WaitOrStart of
|
case WaitOrStart of
|
||||||
"wait" ->
|
"wait" ->
|
||||||
ok;
|
ok;
|
||||||
_ ->
|
_ ->
|
||||||
gen_server:cast(pong_bot, {paused, false})
|
gen_server:cast({global, pong_bot}, {paused, false})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
new_round() ->
|
new_round() ->
|
||||||
Paused = true,
|
Paused = true,
|
||||||
SendStart = false,
|
SendStart = false,
|
||||||
gen_server:cast(pong_bot, {new_round, Paused, SendStart}).
|
gen_server:cast({global, pong_bot}, {new_round, Paused, SendStart}).
|
||||||
|
|
||||||
|
|
||||||
set_game_token(GameToken) ->
|
set_game_token(GameToken) ->
|
||||||
|
@ -144,9 +151,15 @@ view() ->
|
||||||
gen_server:call({global, pong_bot}, game_token).
|
gen_server:call({global, pong_bot}, game_token).
|
||||||
|
|
||||||
handle_call(player1, _From, State) ->
|
handle_call(player1, _From, State) ->
|
||||||
|
io:format("Player1 before~n"),
|
||||||
Player1 = dict:fetch(player1, State),
|
Player1 = dict:fetch(player1, State),
|
||||||
|
io:format("Player1 after~n"),
|
||||||
{reply, Player1, State};
|
{reply, Player1, State};
|
||||||
|
|
||||||
|
handle_call(player2, _From, State) ->
|
||||||
|
Player2 = dict:fetch(player2, State),
|
||||||
|
{reply, Player2, State};
|
||||||
|
|
||||||
handle_call(player1_y, _From, State) ->
|
handle_call(player1_y, _From, State) ->
|
||||||
{_,Y} = dict:fetch(player1, State),
|
{_,Y} = dict:fetch(player1, State),
|
||||||
{reply, Y, State};
|
{reply, Y, State};
|
||||||
|
@ -167,7 +180,15 @@ handle_call(view, _From, State) ->
|
||||||
handle_call(socket, _From, State) ->
|
handle_call(socket, _From, State) ->
|
||||||
Socket = dict:fetch(socket, State),
|
Socket = dict:fetch(socket, State),
|
||||||
%Socket = lists:nth(1, SocketInList),
|
%Socket = lists:nth(1, SocketInList),
|
||||||
{reply, Socket, State}.
|
{reply, Socket, State};
|
||||||
|
|
||||||
|
handle_call(paused, _From, State) ->
|
||||||
|
Paused = dict:fetch(paused, State),
|
||||||
|
{reply, Paused, State};
|
||||||
|
|
||||||
|
handle_call(start, _From, State) ->
|
||||||
|
Start = dict:fetch(start, State),
|
||||||
|
{reply, Start, State}.
|
||||||
|
|
||||||
handle_cast({game_token, GameToken}, State) ->
|
handle_cast({game_token, GameToken}, State) ->
|
||||||
NewState = dict:store(game_token, GameToken, State),
|
NewState = dict:store(game_token, GameToken, State),
|
||||||
|
@ -200,5 +221,8 @@ handle_cast({paused, Paused}, State) ->
|
||||||
handle_cast({new_rouned, Paused, SendStart}, State) ->
|
handle_cast({new_rouned, Paused, SendStart}, State) ->
|
||||||
State1 = dict:store(paused, Paused, State),
|
State1 = dict:store(paused, Paused, State),
|
||||||
NewState = dict:store(send_start, SendStart, State1),
|
NewState = dict:store(send_start, SendStart, State1),
|
||||||
{noreply, NewState}.
|
{noreply, NewState};
|
||||||
|
|
||||||
|
handle_cast({start, Start}, State) ->
|
||||||
|
NewState = dict:store(start, Start, State),
|
||||||
|
{noreply, NewState}.
|
||||||
|
|
Reference in a new issue