Created
February 16, 2011 22:25
-
-
Save jeena/830405 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(ggs_table). | |
-export([ | |
start_link/2, | |
add_player/2, | |
remove_player/2, | |
stop/1, | |
notify/3, | |
loop/4, | |
foo/0 | |
]). | |
-include_lib("eunit/include/eunit.hrl"). | |
%% @doc This module represents a Player with a Socket and a Token | |
% @doc returns a new table | |
start_link(Token, Socket) -> | |
GameVM = ggs_gamevm:start_link(), | |
Pid = spawn_link(?MODULE, loop, [Token, Socket, GameVM, []]), | |
Pid. | |
% @doc adds a player to a table | |
add_player(Table, Player) -> | |
Table ! {add_player, Player}, | |
ok. | |
% @doc removes player form a table | |
remove_player(Table, Player) -> | |
Table ! {remove_player, Player}, | |
ok. | |
% @doc stops the table process | |
stop(Table) -> | |
Table ! {'EXIT', self(), normal}, | |
ok. | |
% @doc notifies the table with a message from a player | |
notify(Table, Player, Message) -> | |
Table ! {notify, Player, Message}, | |
ok. | |
% 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); | |
Any -> | |
error_logger:info_msg("~p: Unknown message ~p~n", [?MODULE, Any]), | |
loop(Token, Socket, GameVM, PlayerList) | |
end | |
end. | |
% Tests | |
start_link_test() -> | |
{ok, LSocket} = gen_tcp:listen(7000, [binary, {packet, 0}, {active, false}]), | |
{ok, Socket} = gen_tcp:accept(LSocket), | |
ClientToken = "123", | |
Pid = start_link(ClientToken, Socket), | |
?assertNot(Pid =:= undefined). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment