From 05363500ec555ee62fff980c3c44a29c0413d4d8 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Mon, 31 Dec 2012 19:39:22 -0500 Subject: [PATCH] Dummy player and game lobby --- src/player_dummy.erl | 36 ++++++++++++++++++++++++++++++++++++ src/server_lobby.erl | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/player_dummy.erl create mode 100644 src/server_lobby.erl diff --git a/src/player_dummy.erl b/src/player_dummy.erl new file mode 100644 index 0000000..d3cace5 --- /dev/null +++ b/src/player_dummy.erl @@ -0,0 +1,36 @@ +-module(player_dummy). +-behaviour(gen_server). + +-export([start_link/1]). +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, + code_change/3]). + +-record(state, {name}). + +start_link(Name) -> + gen_server:start_link(?MODULE, [Name], []). + +init([Name]) -> + {ok, #state{name=Name}}. + +handle_call(_Msg, _From, State) -> + {noreply, State}. + +handle_cast({message, _From, Body}, State) -> + error_logger:info_report({message_received, + {self, self()}, + {state, State}, + {body, Body}}), + {noreply, State}; + +handle_cast(_Msg, State) -> + {noreply, State}. + +handle_info(_Msg, State) -> + {noreply, State}. + +terminate(_Reason, _State) -> + ok. + +code_change(_OldVsn, State, _Extra) -> + {ok, State}. \ No newline at end of file diff --git a/src/server_lobby.erl b/src/server_lobby.erl new file mode 100644 index 0000000..0df52da --- /dev/null +++ b/src/server_lobby.erl @@ -0,0 +1,40 @@ +-module(server_lobby). +-behaviour(gen_server). + +-export([start_link/0]). +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, + code_change/3]). + +-record(state, {players=[]}). + +start_link() -> + gen_server:start_link(?MODULE, [], []). + +init([]) -> + {ok, #state{}}. + +handle_call({add_player, Player}, _From, State) -> + {reply, ok, State#state{players=[Player|State#state.players]}}; + +handle_call(get_players, _From, State) -> + {reply, State#state.players, State}; + +handle_call(_Msg, _From, State) -> + {noreply, State}. + +handle_cast({send, {message, From, Body}=Message}, State) -> + Players = State#state.players, + [gen_server:cast(Pid, Message) || Pid <- Players], + {noreply, State}; + +handle_cast(_Msg, State) -> + {noreply, State}. + +handle_info(_Msg, State) -> + {noreply, State}. + +terminate(_Reason, _State) -> + ok. + +code_change(_OldVsn, State, _Extra) -> + {ok, State}. \ No newline at end of file