tetris: poop69

This commit is contained in:
Peter Harpending
2025-10-26 23:50:50 -07:00
parent 882a416831
commit 7815ae3c57
3 changed files with 83 additions and 19 deletions
+76
View File
@@ -0,0 +1,76 @@
% @doc tetris
%
% manages state for a single game of tetris
%
% 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 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.