% @doc tetris % % manages state for a single game of tetris % % sends parent process messages `{tetris, String}` where String is an encoded % JSON blob meant to be sent to the page script in /priv/static/js/ts/tetris.ts % % Refs: % 1. https://www.erlang.org/docs/24/man/gen_server -module(fd_tetris). -behavior(gen_server). -export([ %% caller context start_link/0, %% process context %% gen_server callbacks init/1, handle_call/3, handle_cast/2, handle_info/2, code_change/3, terminate/2 ]). -include("$zx_include/zx_logger.hrl"). -record(s, {parent :: pid()}). -type state() :: #s{}. %%----------------------------------------------------------------------------- %% caller context below this line %%----------------------------------------------------------------------------- -spec ws_msg(Tetris, Message) -> ok when Tetris :: pid(), Message :: fd_ws:ws_msg(). ws_msg(Tetris, Msg) -> gen_server:cast(Tetris, {ws_msg, Msg}). -spec start_link() -> {ok, pid()} | {error, term()}. start_link() -> gen_server:start_link(?MODULE, [self()], []). %%----------------------------------------------------------------------------- %% process context below this line %%----------------------------------------------------------------------------- %% gen_server callbacks -spec init(Args) -> {ok, State} when Args :: [Parent], Parent :: pid(), State :: state(). init([Parent]) -> tell("~tp:~tp starting fd_tetris with parent ~p", [?MODULE, self(), Parent]), self() ! {poop, 0}, InitState = #s{parent = Parent}, {ok, InitState}. handle_call(Unexpected, From, State) -> tell("~tp:~tp unexpected call from ~tp: ~tp", [?MODULE, self(), From, Unexpected]), {noreply, State}. handle_cast(Unexpected, State) -> tell("~tp:~tp unexpected cast: ~tp", [?MODULE, self(), Unexpected]), {noreply, State}. handle_info({poop, N}, State = #s{parent = Parent}) -> Parent ! {tetris, io_lib:format("poop~p", [N])}, erlang:send_after(1_000, self(), {poop, N+1}), {noreply, State}; handle_info(Unexpected, State) -> tell("~tp:~tp unexpected info: ~tp", [?MODULE, self(), Unexpected]), {noreply, State}. code_change(_, State, _) -> {ok, State}. terminate(_, _) -> ok.