Compare commits
No commits in common. "4e48d6b40b1b8065956ccfa379b2784c41987768" and "7815ae3c5768f6c7525e8499c1a5315dd231f14e" have entirely different histories.
4e48d6b40b
...
7815ae3c57
@ -6,12 +6,6 @@
|
||||
<link rel="stylesheet" href="/css/default.css">
|
||||
</head>
|
||||
<body>
|
||||
<div id="titlebar">
|
||||
<div class="content">
|
||||
<a href="/" class="tb-home">Home</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="content">
|
||||
<h1 class="content-title">Chat with websockets</h1>
|
||||
|
||||
|
||||
@ -25,19 +25,6 @@ body {
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
/* titlebar */
|
||||
#titlebar {
|
||||
background: var(--lgray2);
|
||||
height: 40px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
a.tb-home{
|
||||
font-size: 30px;
|
||||
text-decoration: none;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
|
||||
.content {
|
||||
max-width: 800px;
|
||||
|
||||
@ -6,21 +6,37 @@
|
||||
<link rel="stylesheet" href="/css/default.css">
|
||||
</head>
|
||||
<body>
|
||||
<div id="titlebar">
|
||||
<div class="content">
|
||||
<a href="/" class="tb-home">Home</a>
|
||||
<div class="content">
|
||||
<h1 class="content-title">WFC Demo</h1>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
Websocket demos that work
|
||||
|
||||
<ul>
|
||||
<li><a href="/ws-test-echo.html">Echo</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>
|
||||
Don't work
|
||||
|
||||
<ul>
|
||||
<li><a href="/chat.html">Chatroom</a></li>
|
||||
<li><a href="/tetris.html">Tetris</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<div class="content-body">
|
||||
<textarea disabled id="wfc-output"></textarea>
|
||||
<input autofocus id="wfc-input"></input>
|
||||
|
||||
<h2>Settings</h2>
|
||||
<input type="checkbox" checked id="auto-resize-output">Auto-resize output</input> <br>
|
||||
<input type="checkbox" checked id="auto-scroll" >Auto-scroll output to bottom</input>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="content">
|
||||
<h1 class="content-title">FEWD: index</h1>
|
||||
|
||||
<ul>
|
||||
<li><a href="/chat.html">Chatroom</a></li>
|
||||
<li><a href="/echo.html">Echo</a></li>
|
||||
<li><a href="/tetris.html">Tetris</a></li>
|
||||
<li><a href="/wfc.html">WFC</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
<script type="module" src="./js/dist/index.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
22
priv/static/js/dist/index.d.ts
vendored
Normal file
22
priv/static/js/dist/index.d.ts
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Home page ts/js
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
declare function main(): void;
|
||||
declare function on_input_key(evt: KeyboardEvent, ielt: HTMLInputElement, oelt: HTMLTextAreaElement, cb_resize: HTMLInputElement, cb_scroll: HTMLInputElement, max_height: number): Promise<void>;
|
||||
declare function auto_resize_output(checkbox_element: HTMLInputElement, target_element: HTMLTextAreaElement, max_height: number): void;
|
||||
declare function auto_scroll_to_bottom(checkbox_element: HTMLInputElement, target_element: HTMLTextAreaElement): void;
|
||||
type ok_err<t> = {
|
||||
ok: true;
|
||||
result: t;
|
||||
} | {
|
||||
ok: false;
|
||||
error: string;
|
||||
};
|
||||
type wfcin = {
|
||||
wfcin: string;
|
||||
};
|
||||
type wfcout = ok_err<string>;
|
||||
declare function assert(condition: boolean, fail_msg: string): void;
|
||||
declare function fetch_wfcin(user_line: string): Promise<wfcout>;
|
||||
@ -1,9 +1,9 @@
|
||||
"use strict";
|
||||
/**
|
||||
* Home page ts/js
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
import * as libfewd from './libfewd.js';
|
||||
//------------------------------------------------------------------
|
||||
// page element stuff
|
||||
//------------------------------------------------------------------
|
||||
@ -42,11 +42,28 @@ async function on_input_key(evt, ielt, oelt, cb_resize, cb_scroll, max_height) {
|
||||
oelt.value += result.error;
|
||||
oelt.value += '\n';
|
||||
// auto-resize
|
||||
libfewd.auto_resize(cb_resize, oelt, max_height);
|
||||
libfewd.auto_scroll_to_bottom(cb_scroll, oelt);
|
||||
auto_resize_output(cb_resize, oelt, max_height);
|
||||
auto_scroll_to_bottom(cb_scroll, oelt);
|
||||
}
|
||||
}
|
||||
}
|
||||
function auto_resize_output(checkbox_element, target_element, max_height) {
|
||||
// if the user has manually resized their output, we do nothing
|
||||
if (checkbox_element.checked) {
|
||||
let target_height = target_element.scrollHeight;
|
||||
// resize it automagically up to 500px
|
||||
if (target_height < max_height)
|
||||
target_element.style.height = String(target_height) + 'px';
|
||||
else
|
||||
target_element.style.height = String(max_height) + 'px';
|
||||
}
|
||||
}
|
||||
function auto_scroll_to_bottom(checkbox_element, target_element) {
|
||||
if (checkbox_element.checked) {
|
||||
// scroll to bottom
|
||||
target_element.scrollTop = target_element.scrollHeight;
|
||||
}
|
||||
}
|
||||
function assert(condition, fail_msg) {
|
||||
if (!condition)
|
||||
throw new Error(fail_msg);
|
||||
@ -76,4 +93,4 @@ async function fetch_wfcin(user_line) {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
//# sourceMappingURL=wfc.js.map
|
||||
//# sourceMappingURL=index.js.map
|
||||
1
priv/static/js/dist/index.js.map
vendored
Normal file
1
priv/static/js/dist/index.js.map
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":";AAAA;;;;GAIG;AAGH,oEAAoE;AACpE,qBAAqB;AACrB,oEAAoE;AAEpE,IAAI,EAAE,CAAC;AAEP,SACA,IAAI;IAIA,IAAI,IAAI,GAAoC,QAAQ,CAAC,cAAc,CAAC,WAAW,CAA8B,CAAK;IAClH,IAAI,IAAI,GAAoC,QAAQ,CAAC,cAAc,CAAC,YAAY,CAAgC,CAAE;IAClH,IAAI,SAAS,GAA+B,QAAQ,CAAC,cAAc,CAAC,oBA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KAAK;QACb,KAAK,EAAG,kCAAkC,EAAC,CAAC;IAElE,IAAI,CAAC;QACD,IAAI,QAAQ,GAAc,MAAM,KAAK,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC;QAC7D,IAAI,QAAQ,CAAC,EAAE;YACX,MAAM,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAY,CAAC;aACxC,CAAC;YACF,OAAO,CAAC,GAAG,CAAC,oBAAoB,EAAE,QAAQ,CAAC,CAAC;YAC5C,MAAM,GAAG,EAAC,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,mBAAmB,EAAC,CAAC;QACrD,CAAC;IACL,CAAC;IACD,OAAO,CAAM,EAAE,CAAC;QACZ,OAAO,CAAC,GAAG,CAAC,gBAAgB,EAAE,CAAC,CAAC,CAAC;QACjC,MAAM,GAAG,EAAC,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,eAAe,EAAC,CAAC;IACjD,CAAC;IAED,OAAO,MAAM,CAAC;AAClB,CAAC"}
|
||||
6
priv/static/js/dist/wfc.d.ts
vendored
6
priv/static/js/dist/wfc.d.ts
vendored
@ -1,6 +0,0 @@
|
||||
/**
|
||||
* Home page ts/js
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
export {};
|
||||
1
priv/static/js/dist/wfc.js.map
vendored
1
priv/static/js/dist/wfc.js.map
vendored
@ -1 +0,0 @@
|
||||
{"version":3,"file":"wfc.js","sourceRoot":"","sources":["../ts/wfc.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,OAAO,MAAM,cAAc,CAAA;AAEvC,oEAAoE;AACpE,qBAAqB;AACrB,oEAAoE;AAEpE,IAAI,EAAE,CAAC;AAEP,SACA,IAAI;IAIA,IAAI,IAAI,GAAoC,QAAQ,CAAC,cAAc,CAAC,WAAW,CAA8B,CAAK;IAClH,IAAI,IAAI,GAAoC,QAAQ,CAAC,cAAc,CAAC,YAAY,CAAgC,CAAE;IAClH,IAAI,SAAS,GAA+B,QAAQ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E,eAAe,EAAC,CAAC;IACjD,CAAC;IAED,OAAO,MAAM,CAAC;AAClB,CAAC"}
|
||||
@ -4,7 +4,6 @@
|
||||
* @module
|
||||
*/
|
||||
|
||||
import * as libfewd from './libfewd.js'
|
||||
|
||||
//------------------------------------------------------------------
|
||||
// page element stuff
|
||||
@ -69,13 +68,47 @@ on_input_key
|
||||
oelt.value += '\n';
|
||||
|
||||
// auto-resize
|
||||
libfewd.auto_resize(cb_resize, oelt, max_height);
|
||||
libfewd.auto_scroll_to_bottom(cb_scroll, oelt);
|
||||
auto_resize_output(cb_resize, oelt, max_height);
|
||||
auto_scroll_to_bottom(cb_scroll, oelt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
function
|
||||
auto_resize_output
|
||||
(checkbox_element : HTMLInputElement,
|
||||
target_element : HTMLTextAreaElement,
|
||||
max_height : number)
|
||||
: void
|
||||
{
|
||||
// if the user has manually resized their output, we do nothing
|
||||
if (checkbox_element.checked) {
|
||||
let target_height: number = target_element.scrollHeight;
|
||||
// resize it automagically up to 500px
|
||||
if (target_height < max_height)
|
||||
target_element.style.height = String(target_height) + 'px';
|
||||
else
|
||||
target_element.style.height = String(max_height) + 'px';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function
|
||||
auto_scroll_to_bottom
|
||||
(checkbox_element : HTMLInputElement,
|
||||
target_element : HTMLTextAreaElement)
|
||||
: void
|
||||
{
|
||||
if (checkbox_element.checked) {
|
||||
// scroll to bottom
|
||||
target_element.scrollTop = target_element.scrollHeight;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//------------------------------------------------------------------
|
||||
// wfc api
|
||||
//------------------------------------------------------------------
|
||||
@ -7,12 +7,6 @@
|
||||
<link rel="stylesheet" href="/css/tetris.css">
|
||||
</head>
|
||||
<body>
|
||||
<div id="titlebar">
|
||||
<div class="content">
|
||||
<a href="/" class="tb-home">Home</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="content">
|
||||
<h1 class="content-title">Tetris</h1>
|
||||
|
||||
|
||||
@ -1,30 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>FEWD: WF Compiler Demo</title>
|
||||
<link rel="stylesheet" href="/css/default.css">
|
||||
</head>
|
||||
<body>
|
||||
<div id="titlebar">
|
||||
<div class="content">
|
||||
<a href="/" class="tb-home">Home</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="content">
|
||||
<h1 class="content-title">WFC Demo</h1>
|
||||
|
||||
<div class="content-body">
|
||||
<textarea disabled id="wfc-output"></textarea>
|
||||
<input autofocus id="wfc-input"></input>
|
||||
|
||||
<h2>Settings</h2>
|
||||
<input type="checkbox" checked id="auto-resize-output">Auto-resize output</input> <br>
|
||||
<input type="checkbox" checked id="auto-scroll" >Auto-scroll output to bottom</input>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script type="module" src="./js/dist/wfc.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@ -6,12 +6,6 @@
|
||||
<link rel="stylesheet" href="/css/default.css">
|
||||
</head>
|
||||
<body>
|
||||
<div id="titlebar">
|
||||
<div class="content">
|
||||
<a href="/" class="tb-home">Home</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="content">
|
||||
<h1 class="content-title">Websockets echo test</h1>
|
||||
|
||||
105
src/et_piece.erl
105
src/et_piece.erl
@ -1,105 +0,0 @@
|
||||
-module(et_piece).
|
||||
-vsn("1.0.4").
|
||||
-author("Craig Everett <zxq9@zxq9.com>").
|
||||
-copyright("Craig Everett <zxq9@zxq9.com>").
|
||||
-license("MIT").
|
||||
|
||||
-export([rand/0, new/1, flip/2, points/2, points/1, type/1, sides/1]).
|
||||
|
||||
-export_type([data/0]).
|
||||
|
||||
-record(p,
|
||||
{flip = 1 :: 1..4,
|
||||
type = i :: erltris:type()}).
|
||||
|
||||
-opaque data() :: #p{}.
|
||||
|
||||
rand() ->
|
||||
case rand:uniform(7) of
|
||||
1 -> new(i);
|
||||
2 -> new(o);
|
||||
3 -> new(t);
|
||||
4 -> new(s);
|
||||
5 -> new(z);
|
||||
6 -> new(j);
|
||||
7 -> new(l)
|
||||
end.
|
||||
|
||||
|
||||
new(T) -> #p{type = T}.
|
||||
|
||||
|
||||
flip(r, Piece = #p{flip = 4}) -> Piece#p{flip = 1};
|
||||
flip(r, Piece = #p{flip = F}) -> Piece#p{flip = F + 1};
|
||||
flip(l, Piece = #p{flip = 1}) -> Piece#p{flip = 4};
|
||||
flip(l, Piece = #p{flip = F}) -> Piece#p{flip = F - 1}.
|
||||
|
||||
|
||||
points(Piece, {LX, LY}) ->
|
||||
Offsets = points(Piece),
|
||||
Translate = fun({OX, OY}) -> {LX + OX, LY + OY} end,
|
||||
lists:map(Translate, Offsets).
|
||||
|
||||
|
||||
points(#p{flip = F, type = T}) ->
|
||||
offset(T, F).
|
||||
|
||||
offset(i, 1) -> [{0, 2}, {1, 2}, {2, 2}, {3, 2}];
|
||||
offset(i, 2) -> [{2, 3}, {2, 2}, {2, 1}, {2, 0}];
|
||||
offset(i, 3) -> [{0, 1}, {1, 1}, {2, 1}, {3, 1}];
|
||||
offset(i, 4) -> [{1, 3}, {1, 2}, {1, 1}, {1, 0}];
|
||||
offset(o, _) -> [{1, 1}, {1, 2}, {2, 1}, {2, 2}];
|
||||
offset(t, 1) -> [{0, 1}, {1, 1}, {2, 1}, {1, 2}];
|
||||
offset(t, 2) -> [{1, 2}, {1, 1}, {1, 0}, {2, 1}];
|
||||
offset(t, 3) -> [{0, 1}, {1, 1}, {2, 1}, {1, 0}];
|
||||
offset(t, 4) -> [{1, 2}, {1, 1}, {1, 0}, {0, 1}];
|
||||
offset(s, 1) -> [{0, 1}, {1, 1}, {1, 2}, {2, 2}];
|
||||
offset(s, 2) -> [{1, 2}, {1, 1}, {2, 1}, {2, 0}];
|
||||
offset(s, 3) -> [{0, 0}, {1, 0}, {1, 1}, {2, 1}];
|
||||
offset(s, 4) -> [{0, 2}, {0, 1}, {1, 1}, {1, 0}];
|
||||
offset(z, 1) -> [{0, 2}, {1, 2}, {1, 1}, {2, 1}];
|
||||
offset(z, 2) -> [{1, 0}, {1, 1}, {2, 1}, {2, 2}];
|
||||
offset(z, 3) -> [{0, 1}, {1, 1}, {1, 0}, {2, 0}];
|
||||
offset(z, 4) -> [{0, 0}, {0, 1}, {1, 1}, {1, 2}];
|
||||
offset(j, 1) -> [{0, 2}, {0, 1}, {1, 1}, {2, 1}];
|
||||
offset(j, 2) -> [{1, 0}, {1, 1}, {1, 2}, {2, 2}];
|
||||
offset(j, 3) -> [{0, 1}, {1, 1}, {2, 1}, {2, 0}];
|
||||
offset(j, 4) -> [{0, 0}, {1, 0}, {1, 1}, {1, 2}];
|
||||
offset(l, 1) -> [{0, 1}, {1, 1}, {2, 1}, {2, 2}];
|
||||
offset(l, 2) -> [{1, 2}, {1, 1}, {1, 0}, {2, 0}];
|
||||
offset(l, 3) -> [{0, 0}, {0, 1}, {1, 1}, {2, 1}];
|
||||
offset(l, 4) -> [{0, 2}, {1, 2}, {1, 1}, {1, 0}].
|
||||
|
||||
|
||||
type(#p{type = T}) -> T.
|
||||
|
||||
|
||||
sides(#p{type = T, flip = F}) ->
|
||||
sides(T, F).
|
||||
|
||||
|
||||
sides(i, 1) -> {0, 3, 2};
|
||||
sides(i, 2) -> {2, 2, 3};
|
||||
sides(i, 3) -> {0, 3, 1};
|
||||
sides(i, 4) -> {1, 1, 3};
|
||||
sides(o, _) -> {1, 2, 2};
|
||||
sides(t, 1) -> {0, 2, 2};
|
||||
sides(t, 2) -> {1, 2, 2};
|
||||
sides(t, 3) -> {0, 2, 1};
|
||||
sides(t, 4) -> {0, 1, 2};
|
||||
sides(s, 1) -> {0, 2, 2};
|
||||
sides(s, 2) -> {1, 2, 2};
|
||||
sides(s, 3) -> {0, 2, 1};
|
||||
sides(s, 4) -> {0, 1, 2};
|
||||
sides(z, 1) -> {0 ,2, 2};
|
||||
sides(z, 2) -> {1, 2, 2};
|
||||
sides(z, 3) -> {0 ,2, 1};
|
||||
sides(z, 4) -> {0, 1, 2};
|
||||
sides(j, 1) -> {0, 2, 2};
|
||||
sides(j, 2) -> {1, 2, 2};
|
||||
sides(j, 3) -> {0, 2, 1};
|
||||
sides(j, 4) -> {0, 1, 2};
|
||||
sides(l, 1) -> {0, 2, 2};
|
||||
sides(l, 2) -> {1, 2, 2};
|
||||
sides(l, 3) -> {0, 2, 1};
|
||||
sides(l, 4) -> {0, 1, 2}.
|
||||
@ -1,79 +0,0 @@
|
||||
-module(et_well).
|
||||
-vsn("1.0.4").
|
||||
-author("Craig Everett <zxq9@zxq9.com>").
|
||||
-copyright("Craig Everett <zxq9@zxq9.com>").
|
||||
-license("MIT").
|
||||
|
||||
-export([new/0, new/2,
|
||||
dimensions/1, height/1, width/1,
|
||||
fetch/3, store/4, complete/1, collapse/2]).
|
||||
|
||||
-export_type([playfield/0]).
|
||||
|
||||
|
||||
-opaque playfield() :: tuple().
|
||||
|
||||
|
||||
new() ->
|
||||
new(10, 20).
|
||||
|
||||
|
||||
new(W, H) ->
|
||||
erlang:make_tuple(H, row(W)).
|
||||
|
||||
|
||||
row(W) ->
|
||||
erlang:make_tuple(W, x).
|
||||
|
||||
|
||||
dimensions(Well) ->
|
||||
H = size(Well),
|
||||
W = size(element(1, Well)),
|
||||
{W, H}.
|
||||
|
||||
|
||||
height(Well) ->
|
||||
size(Well).
|
||||
|
||||
|
||||
width(Well) ->
|
||||
size(element(1, Well)).
|
||||
|
||||
|
||||
fetch(Well, X, Y) ->
|
||||
element(X, element(Y, Well)).
|
||||
|
||||
|
||||
store(Well, Value, X, Y) ->
|
||||
setelement(Y, Well, setelement(X, element(Y, Well), Value)).
|
||||
|
||||
|
||||
complete(Well) ->
|
||||
{W, H} = dimensions(Well),
|
||||
complete(H, W, Well, []).
|
||||
|
||||
complete(Y, W, Well, Lines) when Y >= 1 ->
|
||||
case line_complete(W, element(Y, Well)) of
|
||||
true -> complete(Y - 1, W, Well, [Y | Lines]);
|
||||
false -> complete(Y - 1, W, Well, Lines)
|
||||
end;
|
||||
complete(_, _, _, Lines) ->
|
||||
Lines.
|
||||
|
||||
line_complete(X, Line) when X >= 1 ->
|
||||
case element(X, Line) of
|
||||
x -> false;
|
||||
_ -> line_complete(X - 1, Line)
|
||||
end;
|
||||
line_complete(_, _) ->
|
||||
true.
|
||||
|
||||
|
||||
collapse(Well, Lines) ->
|
||||
Blank = row(width(Well)),
|
||||
Crunch =
|
||||
fun(L, {W, Count}) ->
|
||||
Crunched = erlang:insert_element(1, erlang:delete_element(L, W), Blank),
|
||||
{Crunched, Count + 1}
|
||||
end,
|
||||
lists:foldl(Crunch, {Well, 0}, Lines).
|
||||
@ -142,7 +142,7 @@ loop(Parent, Debug, State = #s{socket = Socket, next = Next0}) ->
|
||||
%% should trigger bad request
|
||||
tell(error, "~p QHL parse error: ~tp", [?LINE, Error]),
|
||||
tell(error, "~p bad request:~n~ts", [?LINE, Received]),
|
||||
fd_http_utils:http_err(Socket, 400),
|
||||
http_err(Socket, 400),
|
||||
gen_tcp:shutdown(Socket, read_write),
|
||||
exit(normal)
|
||||
end;
|
||||
@ -240,10 +240,10 @@ route(Sock, get, Route, Request, Received) ->
|
||||
route(Sock, post, Route, Request, Received) ->
|
||||
case Route of
|
||||
<<"/wfcin">> -> wfcin(Sock, Request) , Received;
|
||||
_ -> fd_http_utils:http_err(Sock, 404) , Received
|
||||
_ -> http_err(Sock, 404) , Received
|
||||
end;
|
||||
route(Sock, _, _, _, Received) ->
|
||||
fd_http_utils:http_err(Sock, 404),
|
||||
http_err(Sock, 404),
|
||||
Received.
|
||||
|
||||
|
||||
@ -275,9 +275,9 @@ respond_static(Sock, {found, Entry}) ->
|
||||
Headers1 = [{"content-type", fd_sfc_entry:mime_type(Entry)} | Headers0],
|
||||
Response = #response{headers = Headers1,
|
||||
body = fd_sfc_entry:contents(Entry)},
|
||||
fd_http_utils:respond(Sock, Response);
|
||||
respond(Sock, Response);
|
||||
respond_static(Sock, not_found) ->
|
||||
fd_http_utils:http_err(Sock, 404).
|
||||
http_err(Sock, 404).
|
||||
|
||||
|
||||
%% ------------------------------
|
||||
@ -291,7 +291,13 @@ respond_static(Sock, not_found) ->
|
||||
NewReceived :: binary().
|
||||
|
||||
ws_tetris(Sock, Request, Received) ->
|
||||
.
|
||||
try
|
||||
ws_tetris2(Sock, Request, Received)
|
||||
catch
|
||||
X:Y:Z ->
|
||||
tell(error, "CRASH ws_tetris: ~tp:~tp:~tp", [X, Y, Z]),
|
||||
http_err(Sock, 500)
|
||||
end.
|
||||
|
||||
|
||||
|
||||
@ -305,41 +311,35 @@ ws_tetris2(Sock, Request, Received) ->
|
||||
%tell("~p: ws_tetris request: ~tp", [?LINE, Request]),
|
||||
case fd_ws:handshake(Request) of
|
||||
{ok, Response} ->
|
||||
fd_http_utils:respond(Sock, Response),
|
||||
{ok, TetrisPid} = fd_tetris:start_link(),
|
||||
ws_tetris_loop(Sock, TetrisPid, [], Received);
|
||||
respond(Sock, Response),
|
||||
{ok, _} = fd_tetris:start_link(),
|
||||
ws_tetris_loop(Sock, [], Received);
|
||||
Error ->
|
||||
tell("ws_tetris: error: ~tp", [Error]),
|
||||
fd_http_utils:http_err(Sock, 400)
|
||||
http_err(Sock, 400)
|
||||
end.
|
||||
|
||||
|
||||
-spec ws_tetris_loop(Sock, Tetris, Frames, Received) -> NewReceived
|
||||
-spec ws_tetris_loop(Sock, Frames, Received) -> NewReceived
|
||||
when Sock :: gen_tcp:socket(),
|
||||
Tetris :: pid(),
|
||||
Frames :: [fd_ws:frame()],
|
||||
Received :: binary(),
|
||||
NewReceived :: binary().
|
||||
|
||||
ws_tetris_loop(Sock, Tetris, Frames, Received) ->
|
||||
tell("~p:ws_tetris_loop(Sock, ~p, ~p, ~p)", [?MODULE, Tetris, Frames, Received]),
|
||||
ws_tetris_loop(Sock, Frames, Received) ->
|
||||
tell("~p:ws_tetris_loop(Sock, ~p, ~p)", [?MODULE, Frames, Received]),
|
||||
%% create tetris state
|
||||
case inet:setopts(Sock, [{active, once}]) of
|
||||
ok ->
|
||||
receive
|
||||
{tcp, Sock, Bin} ->
|
||||
Rcv1 = <<Received/binary, Bin/binary>>,
|
||||
case fd_ws:recv(Sock, Rcv1, 3_000, Frames) of
|
||||
{ok, WsMsg, NewFrames, Rcv2} ->
|
||||
ok = fd_tetris:ws_msg(Tetris, WsMsg),
|
||||
ws_tetris_loop(Sock, Tetris, NewFrames, Rcv2);
|
||||
Error ->
|
||||
error(Error)
|
||||
end;
|
||||
ok = tell("~p:~p rcv1: ~tp", [?MODULE, ?LINE, Rcv1]),
|
||||
ws_tetris_loop(Sock, Frames, <<>>);
|
||||
{tetris, Message} ->
|
||||
ok = log(info, "~p tetris: ~p", [self(), Message]),
|
||||
ok = tell("tetris: ~p", [Message]),
|
||||
ok = fd_ws:send(Sock, {text, Message}),
|
||||
ws_tetris_loop(Sock, Tetris, Frames, Received);
|
||||
ws_tetris_loop(Sock, Frames, Received);
|
||||
{tcp_closed, Sock} -> {error, tcp_closed};
|
||||
{tcp_error, Sock, Reason} -> {error, {tcp_error, Reason}}
|
||||
after 30_000 ->
|
||||
@ -359,17 +359,17 @@ ws_echo(Sock, Request) ->
|
||||
catch
|
||||
X:Y:Z ->
|
||||
tell(error, "CRASH ws_echo: ~tp:~tp:~tp", [X, Y, Z]),
|
||||
fd_http_utils:http_err(Sock, 500)
|
||||
http_err(Sock, 500)
|
||||
end.
|
||||
|
||||
ws_echo2(Sock, Request) ->
|
||||
case fd_ws:handshake(Request) of
|
||||
{ok, Response} ->
|
||||
fd_http_utils:respond(Sock, Response),
|
||||
respond(Sock, Response),
|
||||
ws_echo_loop(Sock);
|
||||
Error ->
|
||||
tell("ws_echo: error: ~tp", [Error]),
|
||||
fd_http_utils:http_err(Sock, 400)
|
||||
http_err(Sock, 400)
|
||||
end.
|
||||
|
||||
ws_echo_loop(Sock) ->
|
||||
@ -389,11 +389,6 @@ ws_echo_loop(Sock, Frames, Received) ->
|
||||
error(Error)
|
||||
end.
|
||||
|
||||
|
||||
%% ------------------------------
|
||||
%% wfc
|
||||
%% ------------------------------
|
||||
|
||||
wfcin(Sock, #request{enctype = json,
|
||||
cookies = Cookies,
|
||||
body = #{"wfcin" := Input}}) ->
|
||||
@ -405,17 +400,17 @@ wfcin(Sock, #request{enctype = json,
|
||||
case wfc_read:expr(Input) of
|
||||
{ok, Expr, _Rest} ->
|
||||
case wfc_eval:eval(Expr, Ctx0) of
|
||||
{ok, noop, Ctx1} -> {fd_http_utils:jsgud("<noop>"), Ctx1};
|
||||
{ok, Sentence, Ctx1} -> {fd_http_utils:jsgud(wfc_pp:sentence(Sentence)), Ctx1};
|
||||
{error, Message} -> {fd_http_utils:jsbad(Message), Ctx0}
|
||||
{ok, noop, Ctx1} -> {jsgud("<noop>"), Ctx1};
|
||||
{ok, Sentence, Ctx1} -> {jsgud(wfc_pp:sentence(Sentence)), Ctx1};
|
||||
{error, Message} -> {jsbad(Message), Ctx0}
|
||||
end;
|
||||
{error, Message} ->
|
||||
{fd_http_utils:jsbad(Message), Ctx0}
|
||||
{jsbad(Message), Ctx0}
|
||||
end
|
||||
catch
|
||||
error:E:S ->
|
||||
ErrorMessage = unicode:characters_to_list(io_lib:format("parser crashed: ~p:~p", [E, S])),
|
||||
{fd_http_utils:jsbad(ErrorMessage), Ctx0}
|
||||
{jsbad(ErrorMessage), Ctx0}
|
||||
end,
|
||||
% update cache with new context
|
||||
ok = fd_cache:set(Cookie, NewCtx),
|
||||
@ -423,10 +418,10 @@ wfcin(Sock, #request{enctype = json,
|
||||
Response = #response{headers = [{"content-type", "application/json"},
|
||||
{"set-cookie", ["wfc=", Cookie]}],
|
||||
body = Body},
|
||||
fd_http_utils:respond(Sock, Response);
|
||||
respond(Sock, Response);
|
||||
wfcin(Sock, Request) ->
|
||||
tell("wfcin: bad request: ~tp", [Request]),
|
||||
fd_http_utils:http_err(Sock, 400).
|
||||
http_err(Sock, 400).
|
||||
|
||||
|
||||
|
||||
@ -441,12 +436,110 @@ ctx(#{<<"wfc">> := Cookie}) ->
|
||||
error -> {Cookie, wfc_eval_context:default()}
|
||||
end;
|
||||
ctx(_) ->
|
||||
{fd_http_utils:new_cookie(), wfc_eval_context:default()}.
|
||||
{new_cookie(), wfc_eval_context:default()}.
|
||||
|
||||
|
||||
|
||||
-spec new_cookie() -> Cookie
|
||||
when Cookie :: binary().
|
||||
|
||||
new_cookie() ->
|
||||
binary:encode_hex(crypto:strong_rand_bytes(8)).
|
||||
|
||||
|
||||
|
||||
-spec jsgud(JSON) -> Encodable
|
||||
when JSON :: zj:value(),
|
||||
Encodable :: JSON.
|
||||
|
||||
jsgud(X) ->
|
||||
#{"ok" => true,
|
||||
"result" => X}.
|
||||
|
||||
|
||||
|
||||
-spec jsbad(JSON) -> JSBad
|
||||
when JSON :: zj:value(),
|
||||
JSBad :: zj:value().
|
||||
|
||||
jsbad(X) ->
|
||||
#{"ok" => false,
|
||||
"error" => X}.
|
||||
|
||||
|
||||
|
||||
-spec http_err(Socket, ErrorCode) -> ok
|
||||
when Socket :: gen_tcp:socket(),
|
||||
ErrorCode :: integer().
|
||||
|
||||
http_err(Sock, N) ->
|
||||
Slogan = qhl:slogan(N),
|
||||
Body = ["<!doctype html>"
|
||||
"<html lang=\"en\">"
|
||||
"<head>"
|
||||
"<meta charset=\"utf-8\">"
|
||||
"<title>QHL: ", integer_to_list(N), " ", Slogan, "</title>"
|
||||
"</head>"
|
||||
"<body>"
|
||||
"<h1>"
|
||||
"QHL: ", integer_to_list(N), " ", Slogan,
|
||||
"</h1>"
|
||||
"</body>"
|
||||
"</html>"],
|
||||
Resp = #response{code = N,
|
||||
headers = [{"content/type", "text/html"}],
|
||||
body = Body},
|
||||
respond(Sock, Resp).
|
||||
|
||||
|
||||
|
||||
-spec respond(Sock, Response) -> ok
|
||||
when Sock :: gen_tcp:socket(),
|
||||
Response :: response().
|
||||
|
||||
respond(Sock, Response = #response{code = Code}) ->
|
||||
tell("~tp ~tp ~ts", [self(), Code, qhl:slogan(Code)]),
|
||||
gen_tcp:send(Sock, fmtresp(Response)).
|
||||
|
||||
|
||||
|
||||
-spec fmtresp(Response) -> Formatted
|
||||
when Response :: response(),
|
||||
Formatted :: iolist().
|
||||
|
||||
fmtresp(#response{type = page, %% no idea what {data, String} is
|
||||
version = http11,
|
||||
code = Code,
|
||||
headers = Hs,
|
||||
body = Body}) ->
|
||||
%% need byte size for binary
|
||||
Headers = add_headers(Hs, Body),
|
||||
[io_lib:format("HTTP/1.1 ~tp ~ts", [Code, qhl:slogan(Code)]), "\r\n",
|
||||
[io_lib:format("~ts: ~ts\r\n", [K, V]) || {K, V} <- Headers],
|
||||
"\r\n",
|
||||
Body].
|
||||
|
||||
|
||||
|
||||
-spec add_headers(Existing, Body) -> Hdrs
|
||||
when Existing :: [{iolist(), iolist()}],
|
||||
Body :: iolist(),
|
||||
Hdrs :: [{iolist(), iolist()}].
|
||||
|
||||
%% body needed just for size
|
||||
add_headers(Hs, Body) ->
|
||||
Defaults = default_headers(Body),
|
||||
Hs2 = proplists:to_map(Hs),
|
||||
proplists:from_map(maps:merge(Defaults, Hs2)).
|
||||
|
||||
|
||||
|
||||
-spec default_headers(Body) -> HdrsMap
|
||||
when Body :: iolist(),
|
||||
HdrsMap :: #{iolist() := iolist()}.
|
||||
|
||||
default_headers(Body) ->
|
||||
BodySize = byte_size(iolist_to_binary(Body)),
|
||||
#{"Server" => "fewd 0.1.0",
|
||||
"Date" => qhl:ridiculous_web_date(),
|
||||
"Content-Length" => io_lib:format("~p", [BodySize])}.
|
||||
|
||||
@ -1,115 +0,0 @@
|
||||
% @doc http utility functions
|
||||
-module(fd_http_utils).
|
||||
|
||||
-export([
|
||||
new_cookie/0,
|
||||
jsgud/1, jsbad/1,
|
||||
http_err/2,
|
||||
respond/2,
|
||||
fmtresp/1
|
||||
])
|
||||
|
||||
|
||||
-spec new_cookie() -> Cookie
|
||||
when Cookie :: binary().
|
||||
|
||||
new_cookie() ->
|
||||
binary:encode_hex(crypto:strong_rand_bytes(8)).
|
||||
|
||||
|
||||
|
||||
-spec jsgud(JSON) -> Encodable
|
||||
when JSON :: zj:value(),
|
||||
Encodable :: JSON.
|
||||
|
||||
jsgud(X) ->
|
||||
#{"ok" => true,
|
||||
"result" => X}.
|
||||
|
||||
|
||||
|
||||
-spec jsbad(JSON) -> JSBad
|
||||
when JSON :: zj:value(),
|
||||
JSBad :: zj:value().
|
||||
|
||||
jsbad(X) ->
|
||||
#{"ok" => false,
|
||||
"error" => X}.
|
||||
|
||||
|
||||
|
||||
-spec http_err(Socket, ErrorCode) -> ok
|
||||
when Socket :: gen_tcp:socket(),
|
||||
ErrorCode :: integer().
|
||||
|
||||
http_err(Sock, N) ->
|
||||
Slogan = qhl:slogan(N),
|
||||
Body = ["<!doctype html>"
|
||||
"<html lang=\"en\">"
|
||||
"<head>"
|
||||
"<meta charset=\"utf-8\">"
|
||||
"<title>QHL: ", integer_to_list(N), " ", Slogan, "</title>"
|
||||
"</head>"
|
||||
"<body>"
|
||||
"<h1>"
|
||||
"QHL: ", integer_to_list(N), " ", Slogan,
|
||||
"</h1>"
|
||||
"</body>"
|
||||
"</html>"],
|
||||
Resp = #response{code = N,
|
||||
headers = [{"content/type", "text/html"}],
|
||||
body = Body},
|
||||
respond(Sock, Resp).
|
||||
|
||||
|
||||
|
||||
-spec respond(Sock, Response) -> ok
|
||||
when Sock :: gen_tcp:socket(),
|
||||
Response :: response().
|
||||
|
||||
respond(Sock, Response = #response{code = Code}) ->
|
||||
tell("~tp ~tp ~ts", [self(), Code, qhl:slogan(Code)]),
|
||||
gen_tcp:send(Sock, fmtresp(Response)).
|
||||
|
||||
|
||||
|
||||
-spec fmtresp(Response) -> Formatted
|
||||
when Response :: response(),
|
||||
Formatted :: iolist().
|
||||
|
||||
fmtresp(#response{type = page, %% no idea what {data, String} is
|
||||
version = http11,
|
||||
code = Code,
|
||||
headers = Hs,
|
||||
body = Body}) ->
|
||||
%% need byte size for binary
|
||||
Headers = add_headers(Hs, Body),
|
||||
[io_lib:format("HTTP/1.1 ~tp ~ts", [Code, qhl:slogan(Code)]), "\r\n",
|
||||
[io_lib:format("~ts: ~ts\r\n", [K, V]) || {K, V} <- Headers],
|
||||
"\r\n",
|
||||
Body].
|
||||
|
||||
|
||||
|
||||
-spec add_headers(Existing, Body) -> Hdrs
|
||||
when Existing :: [{iolist(), iolist()}],
|
||||
Body :: iolist(),
|
||||
Hdrs :: [{iolist(), iolist()}].
|
||||
|
||||
%% body needed just for size
|
||||
add_headers(Hs, Body) ->
|
||||
Defaults = default_headers(Body),
|
||||
Hs2 = proplists:to_map(Hs),
|
||||
proplists:from_map(maps:merge(Defaults, Hs2)).
|
||||
|
||||
|
||||
|
||||
-spec default_headers(Body) -> HdrsMap
|
||||
when Body :: iolist(),
|
||||
HdrsMap :: #{iolist() := iolist()}.
|
||||
|
||||
default_headers(Body) ->
|
||||
BodySize = byte_size(iolist_to_binary(Body)),
|
||||
#{"Server" => "fewd 0.1.0",
|
||||
"Date" => qhl:ridiculous_web_date(),
|
||||
"Content-Length" => io_lib:format("~p", [BodySize])}.
|
||||
@ -27,7 +27,7 @@
|
||||
|
||||
-record(s, {base_path = base_path() :: file:filename(),
|
||||
cache = fd_sfc_cache:new(base_path()) :: fd_sfc_cache:cache(),
|
||||
auto_renew = 0_500 :: pos_integer()}).
|
||||
auto_renew = 5_000 :: pos_integer()}).
|
||||
%-type state() :: #s{}.
|
||||
|
||||
|
||||
|
||||
@ -2,11 +2,7 @@
|
||||
%
|
||||
% 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
|
||||
% https://www.erlang.org/docs/24/man/gen_server
|
||||
-module(fd_tetris).
|
||||
|
||||
-behavior(gen_server).
|
||||
@ -31,14 +27,6 @@
|
||||
%% 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()], []).
|
||||
|
||||
@ -65,8 +65,10 @@ day() -> 24*hr().
|
||||
|
||||
-spec handshake(Req) -> Result
|
||||
when Req :: request(),
|
||||
Result :: {ok, DraftResponse}
|
||||
Result :: {ok, ClientProtocols, ClientExtensions, DraftResponse}
|
||||
| {error, Reason},
|
||||
ClientProtocols :: [binary()],
|
||||
ClientExtensions :: binary(),
|
||||
DraftResponse :: response(),
|
||||
Reason :: any().
|
||||
% @doc
|
||||
@ -632,11 +634,11 @@ recv_frame_await(Frame, Sock, Received, Timeout) ->
|
||||
% @end
|
||||
|
||||
send(Socket, {Type, Payload}) ->
|
||||
log(info, "fd_ws: send(~tp, {~tp, ~tp})", [Socket, Type, Payload]),
|
||||
tell("fd_ws: send(~tp, {~tp, ~tp})", [Socket, Type, Payload]),
|
||||
BPayload = payload_to_binary(Payload),
|
||||
log(info, "fd_ws: BPayload = ~tp", [BPayload]),
|
||||
tell("fd_ws: BPayload = ~tp", [BPayload]),
|
||||
Frame = message_to_frame(Type, BPayload),
|
||||
log(info, "fd_ws: Frame = ~tp", [Frame]),
|
||||
tell("fd_ws: Frame = ~tp", [Frame]),
|
||||
send_frame(Socket, Frame).
|
||||
|
||||
payload_to_binary(Bin) when is_binary(Bin) -> Bin;
|
||||
@ -675,7 +677,7 @@ message_to_frame(Control, Payload)
|
||||
|
||||
send_frame(Sock, Frame) ->
|
||||
Binary = render_frame(Frame),
|
||||
log(info, "send_frame: rendered frame: ~tp", [Binary]),
|
||||
tell("send_frame: rendered frame: ~tp", [Binary]),
|
||||
gen_tcp:send(Sock, Binary).
|
||||
|
||||
|
||||
|
||||
102
src/fd_wsp.erl
102
src/fd_wsp.erl
@ -1,102 +0,0 @@
|
||||
% @doc Abstracts a web socket into a process
|
||||
%
|
||||
% hands the TCP socket over to this process, also this process does the
|
||||
% handshake.
|
||||
%
|
||||
% this process sends back `{ws, self(), Message: fd_ws:ws_msg()}'
|
||||
%
|
||||
% for each websocket message it gets
|
||||
-module(fd_wsp).
|
||||
|
||||
-behavior(gen_server).
|
||||
|
||||
-export_type([
|
||||
|
||||
]).
|
||||
|
||||
-export([
|
||||
%% caller context
|
||||
handshake/0,
|
||||
start_link/0,
|
||||
|
||||
%% process context
|
||||
init/1, handle_call/3, handle_cast/2, handle_info/2,
|
||||
code_change/3, terminate/2
|
||||
]).
|
||||
|
||||
-include("http.hrl").
|
||||
-include("$zx_include/zx_logger.hrl").
|
||||
|
||||
-record(s, {socket :: gen_tcp:socket()})
|
||||
-type state() :: #s{}.
|
||||
|
||||
|
||||
%%-----------------------------------------------------------------------------
|
||||
%% caller context
|
||||
%%-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
-spec start_link(Socket, HandshakeReq, Received) -> Result
|
||||
when Socket :: gen_tcp:socket(),
|
||||
HandshakeReq :: request(),
|
||||
Received :: binary(),
|
||||
Result :: {ok, pid()}
|
||||
| {error, term()}.
|
||||
% @doc
|
||||
% starts a websocket and hands control of socket over to child process
|
||||
|
||||
start_link(Socket, HandshakeReq, Received) ->
|
||||
case gen_server:start_link(?MODULE, [Socket, HandshakeReq, Received], []) of
|
||||
{ok, PID} ->
|
||||
gen_tcp:controlling_process(Socket, PID),
|
||||
{ok, PID};
|
||||
Error ->
|
||||
Error
|
||||
end.
|
||||
|
||||
|
||||
%%-----------------------------------------------------------------------------
|
||||
%% process context below this line
|
||||
%%-----------------------------------------------------------------------------
|
||||
|
||||
%% gen_server callbacks
|
||||
|
||||
init([Socket, HandshakeReq, Received]) ->
|
||||
log("~p:~p init", [?MODULE, self()]),
|
||||
case fd_ws:handshake(HandshakeReq) of
|
||||
{ok, Response} ->
|
||||
ok = fd_http_utils:respond(Sock, Response),
|
||||
InitState = #s{socket = Socket},
|
||||
Error ->
|
||||
tell("~p:~p websocket handshake err: ~p", [?MODULE, self(), Error]),
|
||||
fd_http_utils:http_err(Socket, 400)
|
||||
Error
|
||||
end.
|
||||
|
||||
|
||||
handle_call(Unexpected, From, State) ->
|
||||
tell("~tp: unexpected call from ~tp: ~tp", [?MODULE, Unexpected, From]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
handle_cast(Unexpected, State) ->
|
||||
tell("~tp: unexpected cast: ~tp", [?MODULE, Unexpected]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
handle_info({tcp, Sock, Bytes}, State = #s{socket = Sock}) ->
|
||||
handle_info(Unexpected, State) ->
|
||||
tell("~tp: unexpected info: ~tp", [?MODULE, Unexpected]),
|
||||
{noreply, State}.
|
||||
|
||||
|
||||
code_change(_, State, _) ->
|
||||
{ok, State}.
|
||||
|
||||
terminate(_, _) ->
|
||||
ok.
|
||||
|
||||
|
||||
%%-----------------------------------------------------------------------------
|
||||
%% internals
|
||||
%%-----------------------------------------------------------------------------
|
||||
Loading…
x
Reference in New Issue
Block a user