Now we have some myltiplayer action!
This commit is contained in:
parent
3f41769233
commit
883cf9e9d7
4 changed files with 151 additions and 63 deletions
|
@ -1,5 +1,9 @@
|
|||
-module(ggs_player).
|
||||
-export([start_link/1, notify/3, get_token/1, stop/2]).
|
||||
-record(pl_state,
|
||||
{token, % Player's token
|
||||
socket, % Player's socket
|
||||
table}). % Player's table
|
||||
|
||||
%% @doc This module handles communication between a player and GGS. This module is
|
||||
%%responsible for:
|
||||
|
@ -13,14 +17,28 @@
|
|||
%% identifying the player.
|
||||
%% @spec start_link(Socket::socket()) -> {ok, Pid} | {error, Reason}
|
||||
start_link(Socket) ->
|
||||
loop(Socket).
|
||||
% The socket is in 'active' mode, and that means we are pushed any data
|
||||
% that arrives on it, we do not need to recv() manually. Since the socket
|
||||
% was opened in our parent process, we need to change the owner of it to
|
||||
% 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),
|
||||
case TableStatus of
|
||||
{ok, Table} ->
|
||||
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),
|
||||
loop(#pl_state{socket = Socket, token = Token, table = Table})
|
||||
end.
|
||||
|
||||
%% @doc Handles incoming messages from the GGS and forwards them through the player
|
||||
%% socket to the player.
|
||||
%% @spec notify(Player::Pid(), From::Pid(),
|
||||
%% {Command::String(), Message::string()}) -> ok
|
||||
notify(Player, From, Message) ->
|
||||
ggs_logger:not_implemented().
|
||||
Player ! {notify, From, Message}.
|
||||
|
||||
%% @doc Get the player token uniquely representing the player.
|
||||
%% @spec get_token() -> string()
|
||||
|
@ -36,13 +54,13 @@ stop(_Player,_Table) ->
|
|||
|
||||
%% Internals
|
||||
|
||||
loop(Socket) ->
|
||||
% The socket is in 'active' mode, and that means we are pushed any data
|
||||
% that arrives on it, we do not need to recv() manually. Since the socket
|
||||
% was opened in our parent process, we need to change the owner of it to
|
||||
% us, otherwise these messages end up in our parent.
|
||||
erlang:port_connect(Socket, self()),
|
||||
receive {tcp, Socket, Data} -> % Just echo for now..
|
||||
gen_tcp:send(Socket,Data),
|
||||
loop(Socket)
|
||||
loop(#pl_state{token = Token, socket = Socket, table = Table} = State) ->
|
||||
receive
|
||||
{tcp, Socket, Data} -> % Just echo for now..
|
||||
io:format("Notifying table..~n"),
|
||||
ggs_table:notify_game(Table, Token, Data),
|
||||
loop(State);
|
||||
{notify, From, Message} ->
|
||||
gen_tcp:send(Socket, Message),
|
||||
loop(State)
|
||||
end.
|
||||
|
|
Reference in a new issue