tetris: poop69
This commit is contained in:
parent
882a416831
commit
7815ae3c57
@ -312,7 +312,7 @@ ws_tetris2(Sock, Request, Received) ->
|
|||||||
case fd_ws:handshake(Request) of
|
case fd_ws:handshake(Request) of
|
||||||
{ok, Response} ->
|
{ok, Response} ->
|
||||||
respond(Sock, Response),
|
respond(Sock, Response),
|
||||||
tetris_init(Sock),
|
{ok, _} = fd_tetris:start_link(),
|
||||||
ws_tetris_loop(Sock, [], Received);
|
ws_tetris_loop(Sock, [], Received);
|
||||||
Error ->
|
Error ->
|
||||||
tell("ws_tetris: error: ~tp", [Error]),
|
tell("ws_tetris: error: ~tp", [Error]),
|
||||||
@ -334,11 +334,11 @@ ws_tetris_loop(Sock, Frames, Received) ->
|
|||||||
receive
|
receive
|
||||||
{tcp, Sock, Bin} ->
|
{tcp, Sock, Bin} ->
|
||||||
Rcv1 = <<Received/binary, Bin/binary>>,
|
Rcv1 = <<Received/binary, Bin/binary>>,
|
||||||
tell("~p:~p rcv1: ~tp", [?MODULE, ?LINE, Rcv1]),
|
ok = tell("~p:~p rcv1: ~tp", [?MODULE, ?LINE, Rcv1]),
|
||||||
ws_tetris_loop(Sock, Frames, <<>>);
|
ws_tetris_loop(Sock, Frames, <<>>);
|
||||||
{tetris, State} ->
|
{tetris, Message} ->
|
||||||
tell("tetris: ~p", [State]),
|
ok = tell("tetris: ~p", [Message]),
|
||||||
fd_ws:send(Sock, {text, State}),
|
ok = fd_ws:send(Sock, {text, Message}),
|
||||||
ws_tetris_loop(Sock, Frames, Received);
|
ws_tetris_loop(Sock, Frames, Received);
|
||||||
{tcp_closed, Sock} -> {error, tcp_closed};
|
{tcp_closed, Sock} -> {error, tcp_closed};
|
||||||
{tcp_error, Sock, Reason} -> {error, {tcp_error, Reason}}
|
{tcp_error, Sock, Reason} -> {error, {tcp_error, Reason}}
|
||||||
@ -349,18 +349,6 @@ ws_tetris_loop(Sock, Frames, Received) ->
|
|||||||
{error, {inet, Reason}}
|
{error, {inet, Reason}}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
tetris_init(_Sock) ->
|
|
||||||
tell("~p tetris_init", [self()]),
|
|
||||||
Parent = self(),
|
|
||||||
_Child = spawn_link(fun() -> poop(Parent) end),
|
|
||||||
ok.
|
|
||||||
|
|
||||||
poop(Parent) ->
|
|
||||||
tell("~p poop(~p)", [self(), Parent]),
|
|
||||||
Parent ! {tetris, "poop\n"},
|
|
||||||
timer:sleep(3_000),
|
|
||||||
poop(Parent).
|
|
||||||
|
|
||||||
%% ------------------------------
|
%% ------------------------------
|
||||||
%% echo
|
%% echo
|
||||||
%% ------------------------------
|
%% ------------------------------
|
||||||
@ -390,7 +378,7 @@ ws_echo_loop(Sock) ->
|
|||||||
ws_echo_loop(Sock, Frames, Received) ->
|
ws_echo_loop(Sock, Frames, Received) ->
|
||||||
tell("~p ws_echo_loop(Sock, ~tp, ~tp)", [self(), Frames, Received]),
|
tell("~p ws_echo_loop(Sock, ~tp, ~tp)", [self(), Frames, Received]),
|
||||||
case fd_ws:recv(Sock, Received, 5*fd_ws:min(), Frames) of
|
case fd_ws:recv(Sock, Received, 5*fd_ws:min(), Frames) of
|
||||||
Result = {ok, Message, NewFrames, NewReceived} ->
|
{ok, Message, NewFrames, NewReceived} ->
|
||||||
tell("~p echo message: ~tp", [self(), Message]),
|
tell("~p echo message: ~tp", [self(), Message]),
|
||||||
% send the same message back
|
% send the same message back
|
||||||
ok = fd_ws:send(Sock, Message),
|
ok = fd_ws:send(Sock, Message),
|
||||||
|
|||||||
@ -62,7 +62,7 @@ start_link() ->
|
|||||||
%% gen_server callbacks
|
%% gen_server callbacks
|
||||||
|
|
||||||
init(none) ->
|
init(none) ->
|
||||||
log(info, "starting fd_cache"),
|
tell("starting fd_sfc"),
|
||||||
InitState = #s{},
|
InitState = #s{},
|
||||||
erlang:send_after(InitState#s.auto_renew, self(), auto_renew),
|
erlang:send_after(InitState#s.auto_renew, self(), auto_renew),
|
||||||
{ok, InitState}.
|
{ok, InitState}.
|
||||||
|
|||||||
76
src/fd_tetris.erl
Normal file
76
src/fd_tetris.erl
Normal 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.
|
||||||
Loading…
x
Reference in New Issue
Block a user