From 4be808049e0de12cd0a2cb5ea08353bd251884f0 Mon Sep 17 00:00:00 2001 From: Jeena Paradies Date: Wed, 16 Feb 2011 20:53:36 +0100 Subject: [PATCH] added ggs_logger module and implemented some ggs_table stuff --- src/ggs_coordinator.erl | 16 ++++++++-------- src/ggs_dispatcher.erl | 4 ++-- src/ggs_logger.erl | 8 ++++++++ src/ggs_player.erl | 8 ++++---- src/ggs_table.erl | 39 ++++++++++++++++++++++++++++++--------- 5 files changed, 52 insertions(+), 23 deletions(-) create mode 100644 src/ggs_logger.erl diff --git a/src/ggs_coordinator.erl b/src/ggs_coordinator.erl index 1081077..21bb641 100644 --- a/src/ggs_coordinator.erl +++ b/src/ggs_coordinator.erl @@ -12,36 +12,36 @@ %% @doc Starts the coordinator process. start_link() -> - not_implemented(). + ggs_logger:not_implemented(). %% @doc Terminates the coordinator process. stop(_Reason) -> - not_implemented(). + ggs_logger:not_implemented(). %% @doc Joins table with specified token join_table(_Token) -> - not_implemented(). + ggs_logger:not_implemented(). %% @doc Create a new table create_table(_Params) -> - not_implemented(). + ggs_logger:not_implemented(). %% @doc This is the first function run by a newly created players. %% Generates a unique token that we use to identify the player. join_lobby() -> - not_implemented(). + ggs_logger:not_implemented(). %% @doc Act as a supervisor to player and respawns player when it gets bad data. respawn_player(_Player, _Socket) -> - not_implemented(). + ggs_logger:not_implemented(). %% @doc Act as a supervisor to table and respawns table when it gets bad data. respawn_table(_Token) -> - not_implemented(). + ggs_logger:not_implemented(). %% @doc Removes a player from coordinator. remove_player(From, Player) -> - not_implemented(). + ggs_logger:not_implemented(). %% gen_server callbacks diff --git a/src/ggs_dispatcher.erl b/src/ggs_dispatcher.erl index 69d5481..fadb96d 100644 --- a/src/ggs_dispatcher.erl +++ b/src/ggs_dispatcher.erl @@ -20,12 +20,12 @@ %% Port = Integer %% Pid = # start_link(Port) -> - not_implemented(). + ggs_logger:not_implemented(). %% @doc Stops the dispatcher with the specified reason. %% @spec stop(Reason) -> ok. %% Reason = String -stop(Reason) -> not_implemented(). +stop(Reason) -> ggs_logger:not_implemented(). %% gen_server callbacks diff --git a/src/ggs_logger.erl b/src/ggs_logger.erl new file mode 100644 index 0000000..40a9595 --- /dev/null +++ b/src/ggs_logger.erl @@ -0,0 +1,8 @@ +-module(ggs_logger). +-export(not_implemented/0, log/2). + +not_implemented() -> + exit(not_implemented). + +log(Format, Args) -> + error_logger:info_msg(Format, Args). \ No newline at end of file diff --git a/src/ggs_player.erl b/src/ggs_player.erl index 6eb31c9..ae20ae8 100644 --- a/src/ggs_player.erl +++ b/src/ggs_player.erl @@ -13,7 +13,7 @@ %% identifying the player. %% @spec start_link(Socket::socket()) -> ok start_link(Socket) -> - not_implemented(). + ggs_logger:not_implemented(). %% @doc Handles incoming messages from the GGS and forwards them through the player @@ -21,16 +21,16 @@ start_link(Socket) -> %% @spec notify(Player::Pid(), From::Pid(), %% {Command::String(), Message::string()}) -> ok notify(Player, From, Message) -> - not_implemented(). + ggs_logger:not_implemented(). %% @doc Get the player token uniquely representing the player. %% @spec get_token() -> string() get_token() -> - not_implemented(). + ggs_logger:not_implemented(). %% @doc Properly terminates the player process. The player token will be destroyed. %% Makes table token unreferenced and destroys the process in the end. %% @spec stop(Table::pid()) -> Reason::string() stop(Table) -> - not_implemented(). + ggs_logger:not_implemented(). diff --git a/src/ggs_table.erl b/src/ggs_table.erl index 87a623a..5c0cbf0 100644 --- a/src/ggs_table.erl +++ b/src/ggs_table.erl @@ -1,5 +1,3 @@ -%% @doc This module represents a Player with a Socket and a Token - -module(ggs_table). -export([ start_link/1, @@ -9,28 +7,51 @@ notify/3 ]). +%% @doc This module represents a Player with a Socket and a Token + % @doc returns a new table -start_link(Token) -> - not_implemented(). +start_link(Token, Socket) -> + spawn_link(fun(Token, Socket) -> + GameVM = ggs_gamevm:start_link(); + loop(Token, Socket, GameVM, []) + ). % @doc adds a player to a table add_player(Table, Player) -> - not_implemented(). + ggs_logger:not_implemented(). % @doc removes player form a table remove_player(Table, Player) -> - not_implemented(). + ggs_logger:not_implemented(). % @doc stops the table process stop(Table) -> - not_implemented(). + Table ! {'EXIT', self(), normal} % @doc notifies the table with a message from a player notify(Table, Player, Message) -> - not_implemented(). + Table ! {notify, Player, Message}. % loop - +loop(Token, Socket, GameVM, PlayerList) -> + receive + {add_player, Player} -> + NewPlayerList = list:append(PlayerList, [Player]), + loop(Token, Socket, GameVM, NewPlayerList); + {remove_player, Player} -> + NewPlayerList = list:delete(Player, PlayerList), + loop(Token, Socket, GameVM, NewPlayerList); + {notify, Player, Message} -> + case Message of + {server, define, Args} -> + ggs_gamevm:define(GameVM, Args), + loop(Token, Socket, GameVM, PlayerList); + {game, Command, Args} -> + ggs_gamevm:user_command(GameVM, Player, Command, Args), + loop(Token, Socket, GameVM, PlayerList); + end + end. + % private helpers \ No newline at end of file