ton of refactoring, dropped lustre for a wasm frontend in rust
This commit is contained in:
parent
76522cc053
commit
91547b9c2e
1
backend/.gitignore
vendored
1
backend/.gitignore
vendored
@ -2,3 +2,4 @@
|
||||
*.ez
|
||||
/build
|
||||
erl_crash.dump
|
||||
config.toml
|
||||
|
@ -14,6 +14,15 @@ version = "1.0.0"
|
||||
|
||||
[dependencies]
|
||||
gleam_stdlib = ">= 0.44.0 and < 2.0.0"
|
||||
storail = ">= 3.0.0 and < 4.0.0"
|
||||
tom = ">= 1.1.1 and < 2.0.0"
|
||||
simplifile = ">= 2.2.1 and < 3.0.0"
|
||||
glint = ">= 1.2.1 and < 2.0.0"
|
||||
shared = { path = "../shared" }
|
||||
mist = ">= 4.0.7 and < 5.0.0"
|
||||
argv = ">= 1.0.2 and < 2.0.0"
|
||||
gleam_erlang = ">= 0.34.0 and < 1.0.0"
|
||||
gleam_http = ">= 4.0.0 and < 5.0.0"
|
||||
|
||||
[dev-dependencies]
|
||||
gleeunit = ">= 1.0.0 and < 2.0.0"
|
||||
|
@ -5,21 +5,14 @@ packages = [
|
||||
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
|
||||
{ name = "directories", version = "1.2.0", build_tools = ["gleam"], requirements = ["envoy", "gleam_stdlib", "platform", "simplifile"], otp_app = "directories", source = "hex", outer_checksum = "D13090CFCDF6759B87217E8DDD73A75903A700148A82C1D33799F333E249BF9E" },
|
||||
{ name = "envoy", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "95FD059345AA982E89A0B6E2A3BF1CF43E17A7048DCD85B5B65D3B9E4E39D359" },
|
||||
{ name = "exception", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "F5580D584F16A20B7FCDCABF9E9BE9A2C1F6AC4F9176FA6DD0B63E3B20D450AA" },
|
||||
{ name = "filepath", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "B06A9AF0BF10E51401D64B98E4B627F1D2E48C154967DA7AF4D0914780A6D40A" },
|
||||
{ name = "fs", version = "11.4.1", build_tools = ["rebar3"], requirements = [], otp_app = "fs", source = "hex", outer_checksum = "DD00A61D89EAC01D16D3FC51D5B0EB5F0722EF8E3C1A3A547CD086957F3260A9" },
|
||||
{ name = "gleam_community_ansi", version = "1.4.3", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_regexp", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "8A62AE9CC6EA65BEA630D95016D6C07E4F9973565FA3D0DE68DC4200D8E0DD27" },
|
||||
{ name = "gleam_community_colour", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "FDD6AC62C6EC8506C005949A4FCEF032038191D5EAAEC3C9A203CD53AE956ACA" },
|
||||
{ name = "gleam_crypto", version = "1.5.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "917BC8B87DBD584830E3B389CBCAB140FFE7CB27866D27C6D0FB87A9ECF35602" },
|
||||
{ name = "gleam_deque", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_deque", source = "hex", outer_checksum = "64D77068931338CF0D0CB5D37522C3E3CCA7CB7D6C5BACB41648B519CC0133C7" },
|
||||
{ name = "gleam_erlang", version = "0.34.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "0C38F2A128BAA0CEF17C3000BD2097EB80634E239CE31A86400C4416A5D0FDCC" },
|
||||
{ name = "gleam_fetch", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_http", "gleam_javascript", "gleam_stdlib"], otp_app = "gleam_fetch", source = "hex", outer_checksum = "2CBF9F2E1C71AEBBFB13A9D5720CD8DB4263EB02FE60C5A7A1C6E17B0151C20C" },
|
||||
{ name = "gleam_http", version = "4.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "0A62451FC85B98062E0907659D92E6A89F5F3C0FBE4AB8046C99936BF6F91DBC" },
|
||||
{ name = "gleam_httpc", version = "4.1.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "gleam_httpc", source = "hex", outer_checksum = "1A38507AF26CACA145248733688703EADCB734EA971D4E34FB97B7613DECF132" },
|
||||
{ name = "gleam_javascript", version = "0.13.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_javascript", source = "hex", outer_checksum = "F98328FCF573DA6F3A35D7F6CB3F9FF19FD5224CCBA9151FCBEAA0B983AF2F58" },
|
||||
{ name = "gleam_json", version = "2.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "C55C5C2B318533A8072D221C5E06E5A75711C129E420DD1CE463342106012E5D" },
|
||||
{ name = "gleam_otp", version = "0.16.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "50DA1539FC8E8FA09924EB36A67A2BBB0AD6B27BCDED5A7EF627057CF69D035E" },
|
||||
{ name = "gleam_package_interface", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_package_interface", source = "hex", outer_checksum = "C2D2CA097831D27A20DAFA62D44F5D1B12E8470272337FD133368ACA4969A317" },
|
||||
{ name = "gleam_regexp", version = "1.1.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "9C215C6CA84A5B35BB934A9B61A9A306EC743153BE2B0425A0D032E477B062A9" },
|
||||
{ name = "gleam_stdlib", version = "0.59.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "F8FEE9B35797301994B81AF75508CF87C328FE1585558B0FFD188DC2B32EAA95" },
|
||||
{ name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" },
|
||||
@ -27,30 +20,27 @@ packages = [
|
||||
{ name = "glint", version = "1.2.1", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_community_colour", "gleam_stdlib", "snag"], otp_app = "glint", source = "hex", outer_checksum = "2214C7CEFDE457CEE62140C3D4899B964E05236DA74E4243DFADF4AF29C382BB" },
|
||||
{ name = "glisten", version = "7.0.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib", "logging", "telemetry"], otp_app = "glisten", source = "hex", outer_checksum = "1A53CF9FB3231A93FF7F1BD519A43DC968C1722F126CDD278403A78725FC5189" },
|
||||
{ name = "gramps", version = "3.0.1", build_tools = ["gleam"], requirements = ["gleam_crypto", "gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "gramps", source = "hex", outer_checksum = "59194B3980110B403EE6B75330DB82CDE05FC8138491C2EAEACBC7AAEF30B2E8" },
|
||||
{ name = "houdini", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "houdini", source = "hex", outer_checksum = "5BA517E5179F132F0471CB314F27FE210A10407387DA1EA4F6FD084F74469FC2" },
|
||||
{ name = "hpack_erl", version = "0.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "hpack", source = "hex", outer_checksum = "D6137D7079169D8C485C6962DFE261AF5B9EF60FBC557344511C1E65E3D95FB0" },
|
||||
{ name = "logging", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "1098FBF10B54B44C2C7FDF0B01C1253CAFACDACABEFB4B0D027803246753E06D" },
|
||||
{ name = "lustre", version = "5.0.2", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_json", "gleam_otp", "gleam_stdlib", "houdini"], otp_app = "lustre", source = "hex", outer_checksum = "ED46F0CA5BA61067DDC2CEDEA9906AC99E88F49918EFDC58283A531F0A14F042" },
|
||||
{ name = "lustre_dev_tools", version = "1.7.1", build_tools = ["gleam"], requirements = ["argv", "filepath", "fs", "gleam_community_ansi", "gleam_crypto", "gleam_deque", "gleam_erlang", "gleam_http", "gleam_httpc", "gleam_json", "gleam_otp", "gleam_package_interface", "gleam_regexp", "gleam_stdlib", "glint", "glisten", "lustre", "mist", "repeatedly", "simplifile", "term_size", "tom", "wisp"], otp_app = "lustre_dev_tools", source = "hex", outer_checksum = "B426F3E518B44144643CAE956D072E3ADAA9BBC71ECE08CD559CA0276A74C167" },
|
||||
{ name = "marceau", version = "1.3.0", build_tools = ["gleam"], requirements = [], otp_app = "marceau", source = "hex", outer_checksum = "2D1C27504BEF45005F5DFB18591F8610FB4BFA91744878210BDC464412EC44E9" },
|
||||
{ name = "mist", version = "4.0.7", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_http", "gleam_otp", "gleam_stdlib", "gleam_yielder", "glisten", "gramps", "hpack_erl", "logging"], otp_app = "mist", source = "hex", outer_checksum = "F7D15A1E3232E124C7CE31900253633434E59B34ED0E99F273DEE61CDB573CDD" },
|
||||
{ name = "platform", version = "1.0.0", build_tools = ["gleam"], requirements = [], otp_app = "platform", source = "hex", outer_checksum = "8339420A95AD89AAC0F82F4C3DB8DD401041742D6C3F46132A8739F6AEB75391" },
|
||||
{ name = "repeatedly", version = "2.1.2", build_tools = ["gleam"], requirements = [], otp_app = "repeatedly", source = "hex", outer_checksum = "93AE1938DDE0DC0F7034F32C1BF0D4E89ACEBA82198A1FE21F604E849DA5F589" },
|
||||
{ name = "rsvp", version = "1.0.3", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_fetch", "gleam_http", "gleam_httpc", "gleam_javascript", "gleam_json", "gleam_stdlib", "lustre"], otp_app = "rsvp", source = "hex", outer_checksum = "91DBD068A315E78250B5C76DB55B27F7E03400862EEED55A43FD0AC0A6966EB5" },
|
||||
{ name = "shared", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], source = "local", path = "../shared" },
|
||||
{ name = "simplifile", version = "2.2.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "C88E0EE2D509F6D86EB55161D631657675AA7684DAB83822F7E59EB93D9A60E3" },
|
||||
{ name = "snag", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "7E9F06390040EB5FAB392CE642771484136F2EC103A92AE11BA898C8167E6E17" },
|
||||
{ name = "storail", version = "3.0.0", build_tools = ["gleam"], requirements = ["directories", "filepath", "gleam_crypto", "gleam_json", "gleam_stdlib", "simplifile"], otp_app = "storail", source = "hex", outer_checksum = "D032EE5C89AA4B6306FF81929BF9B5DD2583E9F743F0047788AE5F1F52AFE3B4" },
|
||||
{ name = "telemetry", version = "1.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "telemetry", source = "hex", outer_checksum = "7015FC8919DBE63764F4B4B87A95B7C0996BD539E0D499BE6EC9D7F3875B79E6" },
|
||||
{ name = "term_size", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "term_size", source = "hex", outer_checksum = "D00BD2BC8FB3EBB7E6AE076F3F1FF2AC9D5ED1805F004D0896C784D06C6645F1" },
|
||||
{ name = "tom", version = "1.1.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "tom", source = "hex", outer_checksum = "0910EE688A713994515ACAF1F486A4F05752E585B9E3209D8F35A85B234C2719" },
|
||||
{ name = "wisp", version = "1.6.0", build_tools = ["gleam"], requirements = ["directories", "exception", "gleam_crypto", "gleam_erlang", "gleam_http", "gleam_json", "gleam_stdlib", "logging", "marceau", "mist", "simplifile"], otp_app = "wisp", source = "hex", outer_checksum = "AE1C568FE30718C358D3B37666DF0A0743ECD96094AD98C9F4921475075F660A" },
|
||||
]
|
||||
|
||||
[requirements]
|
||||
gleam_json = { version = ">= 2.3.0 and < 3.0.0" }
|
||||
argv = { version = ">= 1.0.2 and < 2.0.0" }
|
||||
gleam_erlang = { version = ">= 0.34.0 and < 1.0.0" }
|
||||
gleam_http = { version = ">= 4.0.0 and < 5.0.0" }
|
||||
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
|
||||
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
|
||||
lustre = { version = ">= 5.0.2 and < 6.0.0" }
|
||||
lustre_dev_tools = { version = ">= 1.7.1 and < 2.0.0" }
|
||||
rsvp = { version = ">= 1.0.3 and < 2.0.0" }
|
||||
glint = { version = ">= 1.2.1 and < 2.0.0" }
|
||||
mist = { version = ">= 4.0.7 and < 5.0.0" }
|
||||
shared = { path = "../shared" }
|
||||
simplifile = { version = ">= 2.2.1 and < 3.0.0" }
|
||||
storail = { version = ">= 3.0.0 and < 4.0.0" }
|
||||
tom = { version = ">= 1.1.1 and < 2.0.0" }
|
71
backend/src/app/router.gleam
Normal file
71
backend/src/app/router.gleam
Normal file
@ -0,0 +1,71 @@
|
||||
import app/web
|
||||
import gleam/bytes_tree
|
||||
import gleam/http/request.{type Request}
|
||||
import gleam/http/response.{type Response}
|
||||
import gleam/option.{None}
|
||||
import gleam/result
|
||||
import gleam/string
|
||||
import mist.{type Connection, type ResponseData}
|
||||
|
||||
pub fn handle_request(
|
||||
request: Request(Connection),
|
||||
ctx: web.Context,
|
||||
) -> Response(ResponseData) {
|
||||
case request.path_segments(request) {
|
||||
[] -> serve_frontend()
|
||||
["static", "frontend.wasm"] -> serve_wasm(ctx)
|
||||
_ -> response.set_body(response.new(404), mist.Bytes(bytes_tree.new()))
|
||||
}
|
||||
}
|
||||
|
||||
const game_body = "
|
||||
<html lang=\"en\">
|
||||
|
||||
<head>
|
||||
<meta charset=\"utf-8\">
|
||||
<title>TITLE</title>
|
||||
<style>
|
||||
html,
|
||||
body,
|
||||
canvas {
|
||||
margin: 0px;
|
||||
padding: 0px;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
overflow: hidden;
|
||||
position: absolute;
|
||||
background: black;
|
||||
z-index: 0;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<canvas id=\"glcanvas\" tabindex='1'></canvas>
|
||||
<!-- Minified and statically hosted version of https://github.com/not-fl3/macroquad/blob/master/js/mq_js_bundle.js -->
|
||||
<script src=\"https://not-fl3.github.io/miniquad-samples/mq_js_bundle.js\"></script>
|
||||
<script>load(\"static/frontend.wasm\");</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
"
|
||||
|
||||
fn serve_frontend() -> Response(ResponseData) {
|
||||
response.new(200)
|
||||
|> response.set_body(mist.Bytes(bytes_tree.from_string(game_body)))
|
||||
|> response.set_header("content-type", "text/html")
|
||||
}
|
||||
|
||||
fn serve_wasm(ctx: web.Context) -> Response(ResponseData) {
|
||||
let wasm_path = string.join([ctx.config.static_path, "frontend.wasm"], "/")
|
||||
mist.send_file(wasm_path, offset: 0, limit: None)
|
||||
|> result.map(fn(file) {
|
||||
response.new(200)
|
||||
|> response.prepend_header("content-type", "application/wasm")
|
||||
|> response.set_body(file)
|
||||
})
|
||||
|> result.lazy_unwrap(fn() {
|
||||
response.new(404)
|
||||
|> response.set_body(mist.Bytes(bytes_tree.new()))
|
||||
})
|
||||
}
|
7
backend/src/app/web.gleam
Normal file
7
backend/src/app/web.gleam
Normal file
@ -0,0 +1,7 @@
|
||||
import config
|
||||
import session
|
||||
import storail
|
||||
|
||||
pub type Context {
|
||||
Context(config: config.Config, sessions: storail.Collection(session.Session))
|
||||
}
|
@ -1,5 +1,74 @@
|
||||
import gleam/io
|
||||
import app/router
|
||||
import app/web
|
||||
import argv
|
||||
import config
|
||||
import gleam/erlang/process
|
||||
import gleam/result
|
||||
import glint
|
||||
import mist
|
||||
import session
|
||||
import storail
|
||||
|
||||
pub type Error {
|
||||
ConfigError(config.ConfigError)
|
||||
}
|
||||
|
||||
pub fn error_string(err: Error) -> String {
|
||||
case err {
|
||||
ConfigError(err) -> config.error_to_string(err)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn setup_session_collection(
|
||||
config: storail.Config,
|
||||
) -> storail.Collection(session.Session) {
|
||||
storail.Collection(
|
||||
name: "Sessions",
|
||||
to_json: session.serialize,
|
||||
decoder: session.decoder(),
|
||||
config:,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn run_server() -> glint.Command(Result(Nil, Error)) {
|
||||
use <- glint.command_help("Runs the backend of Play of the Game")
|
||||
use _, args, _ <- glint.command()
|
||||
|
||||
let cfg_path = case args {
|
||||
[] -> "config.toml"
|
||||
[cfg_path, ..] -> cfg_path
|
||||
}
|
||||
|
||||
use cfg <- result.try(
|
||||
result.map_error(config.from_file(cfg_path), fn(err) {
|
||||
let err = ConfigError(err)
|
||||
err
|
||||
}),
|
||||
)
|
||||
|
||||
let storail_cfg = storail.Config(cfg.storage_path)
|
||||
|
||||
let ctx =
|
||||
web.Context(config: cfg, sessions: setup_session_collection(storail_cfg))
|
||||
|
||||
let handler = router.handle_request(_, ctx)
|
||||
|
||||
let assert Ok(_) =
|
||||
handler
|
||||
|> mist.new
|
||||
|> mist.bind(cfg.host)
|
||||
|> mist.port(cfg.port)
|
||||
|> mist.start_http
|
||||
|
||||
process.sleep_forever()
|
||||
|
||||
Ok(Nil)
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
io.println("Hello from backend!")
|
||||
glint.new()
|
||||
|> glint.with_name("Play Of The Game")
|
||||
|> glint.pretty_help(glint.default_pretty_help())
|
||||
|> glint.add(at: [], do: run_server())
|
||||
|> glint.run(argv.load().arguments)
|
||||
}
|
||||
|
71
backend/src/config.gleam
Normal file
71
backend/src/config.gleam
Normal file
@ -0,0 +1,71 @@
|
||||
import gleam/result
|
||||
import gleam/string
|
||||
import simplifile
|
||||
import tom
|
||||
|
||||
pub type ConfigError {
|
||||
IOError(simplifile.FileError)
|
||||
TomlError(tom.ParseError)
|
||||
MissingKey(tom.GetError)
|
||||
}
|
||||
|
||||
pub type Config {
|
||||
Config(storage_path: String, host: String, port: Int, static_path: String)
|
||||
}
|
||||
|
||||
pub fn error_to_string(err: ConfigError) -> String {
|
||||
let msg = case err {
|
||||
IOError(err) -> simplifile.describe_error(err)
|
||||
TomlError(err) -> {
|
||||
case err {
|
||||
tom.Unexpected(got, expected) ->
|
||||
"Unexpected: Got: " <> got <> "Expected: " <> expected
|
||||
tom.KeyAlreadyInUse(path) ->
|
||||
"Key already used at: " <> string.join(path, ".")
|
||||
}
|
||||
}
|
||||
MissingKey(err) -> {
|
||||
case err {
|
||||
tom.NotFound(path) -> "Missing Key: " <> string.join(path, ".")
|
||||
tom.WrongType(path, expected, got) ->
|
||||
"Wrong type at: "
|
||||
<> string.join(path, ".")
|
||||
<> ", got: "
|
||||
<> got
|
||||
<> "expected: "
|
||||
<> expected
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
"Config error: " <> msg
|
||||
}
|
||||
|
||||
pub fn from_file(path: String) -> Result(Config, ConfigError) {
|
||||
use config_str <- result.try(result.map_error(
|
||||
simplifile.read(from: path),
|
||||
IOError,
|
||||
))
|
||||
use config_tom <- result.try(result.map_error(
|
||||
tom.parse(config_str),
|
||||
TomlError,
|
||||
))
|
||||
use storage_path <- result.try(result.map_error(
|
||||
tom.get_string(config_tom, ["storage_path"]),
|
||||
MissingKey,
|
||||
))
|
||||
use port <- result.try(result.map_error(
|
||||
tom.get_int(config_tom, ["port"]),
|
||||
MissingKey,
|
||||
))
|
||||
use host <- result.try(result.map_error(
|
||||
tom.get_string(config_tom, ["host"]),
|
||||
MissingKey,
|
||||
))
|
||||
use static_path <- result.try(result.map_error(
|
||||
tom.get_string(config_tom, ["static_path"]),
|
||||
MissingKey,
|
||||
))
|
||||
|
||||
Ok(Config(storage_path:, port:, host:, static_path:))
|
||||
}
|
1
frontend/.gitignore
vendored
Normal file
1
frontend/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
272
frontend/Cargo.lock
generated
Normal file
272
frontend/Cargo.lock
generated
Normal file
@ -0,0 +1,272 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "adler2"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627"
|
||||
|
||||
[[package]]
|
||||
name = "allocator-api2"
|
||||
version = "0.2.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||
|
||||
[[package]]
|
||||
name = "bytemuck"
|
||||
version = "1.23.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9134a6ef01ce4b366b50689c94f82c14bc72bc5d0386829828a2e2752ef7958c"
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "color_quant"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
|
||||
|
||||
[[package]]
|
||||
name = "crc32fast"
|
||||
version = "1.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "equivalent"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
|
||||
|
||||
[[package]]
|
||||
name = "fdeflate"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e6853b52649d4ac5c0bd02320cddc5ba956bdb407c4b75a2c6b75bf51500f8c"
|
||||
dependencies = [
|
||||
"simd-adler32",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "flate2"
|
||||
version = "1.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7ced92e76e966ca2fd84c8f7aa01a4aea65b0eb6648d72f7c8f3e2764a67fece"
|
||||
dependencies = [
|
||||
"crc32fast",
|
||||
"miniz_oxide",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "foldhash"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
|
||||
|
||||
[[package]]
|
||||
name = "fontdue"
|
||||
version = "0.9.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2e57e16b3fe8ff4364c0661fdaac543fb38b29ea9bc9c2f45612d90adf931d2b"
|
||||
dependencies = [
|
||||
"hashbrown",
|
||||
"ttf-parser",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "frontend"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"macroquad",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "glam"
|
||||
version = "0.27.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9e05e7e6723e3455f4818c7b26e855439f7546cf617ef669d1adedb8669e5cb9"
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.15.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "84b26c544d002229e640969970a2e74021aadf6e2f96372b9c58eff97de08eb3"
|
||||
dependencies = [
|
||||
"allocator-api2",
|
||||
"equivalent",
|
||||
"foldhash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "image"
|
||||
version = "0.24.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5690139d2f55868e080017335e4b94cb7414274c74f1669c84fb5feba2c9f69d"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"byteorder",
|
||||
"color_quant",
|
||||
"num-traits",
|
||||
"png",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.172"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa"
|
||||
|
||||
[[package]]
|
||||
name = "macroquad"
|
||||
version = "0.4.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d2befbae373456143ef55aa93a73594d080adfb111dc32ec96a1123a3e4ff4ae"
|
||||
dependencies = [
|
||||
"fontdue",
|
||||
"glam",
|
||||
"image",
|
||||
"macroquad_macro",
|
||||
"miniquad",
|
||||
"quad-rand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "macroquad_macro"
|
||||
version = "0.1.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "64b1d96218903768c1ce078b657c0d5965465c95a60d2682fd97443c9d2483dd"
|
||||
|
||||
[[package]]
|
||||
name = "malloc_buf"
|
||||
version = "0.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "miniquad"
|
||||
version = "0.4.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2fb3e758e46dbc45716a8a49ca9edc54b15bcca826277e80b1f690708f67f9e3"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"ndk-sys",
|
||||
"objc-rs",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "miniz_oxide"
|
||||
version = "0.8.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3be647b768db090acb35d5ec5db2b0e1f1de11133ca123b9eacf5137868f892a"
|
||||
dependencies = [
|
||||
"adler2",
|
||||
"simd-adler32",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ndk-sys"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e1bcdd74c20ad5d95aacd60ef9ba40fdf77f767051040541df557b7a9b2a2121"
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "objc-rs"
|
||||
version = "0.2.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "64a1e7069a2525126bf12a9f1f7916835fafade384fb27cabf698e745e2a1eb8"
|
||||
dependencies = [
|
||||
"malloc_buf",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "png"
|
||||
version = "0.17.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "82151a2fc869e011c153adc57cf2789ccb8d9906ce52c0b39a6b5697749d7526"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"crc32fast",
|
||||
"fdeflate",
|
||||
"flate2",
|
||||
"miniz_oxide",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quad-rand"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a651516ddc9168ebd67b24afd085a718be02f8858fe406591b013d101ce2f40"
|
||||
|
||||
[[package]]
|
||||
name = "simd-adler32"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe"
|
||||
|
||||
[[package]]
|
||||
name = "ttf-parser"
|
||||
version = "0.21.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2c591d83f69777866b9126b24c6dd9a18351f177e49d625920d19f989fd31cf8"
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
7
frontend/Cargo.toml
Normal file
7
frontend/Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "frontend"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
macroquad = "0.4.14"
|
16
frontend/src/main.rs
Normal file
16
frontend/src/main.rs
Normal file
@ -0,0 +1,16 @@
|
||||
use macroquad::prelude::*;
|
||||
|
||||
#[macroquad::main("BasicShapes")]
|
||||
async fn main() {
|
||||
loop {
|
||||
clear_background(RED);
|
||||
|
||||
draw_line(40.0, 40.0, 100.0, 200.0, 15.0, BLUE);
|
||||
draw_rectangle(screen_width() / 2.0 - 60.0, 100.0, 120.0, 60.0, GREEN);
|
||||
draw_circle(screen_width() - 30.0, screen_height() - 30.0, 15.0, YELLOW);
|
||||
|
||||
draw_text("IT WORKS!", 20.0, 20.0, 30.0, DARKGRAY);
|
||||
|
||||
next_frame().await
|
||||
}
|
||||
}
|
5
game_client/.gitignore
vendored
5
game_client/.gitignore
vendored
@ -1,5 +0,0 @@
|
||||
*.beam
|
||||
*.ez
|
||||
/build
|
||||
erl_crash.dump
|
||||
/priv/static
|
@ -1,24 +0,0 @@
|
||||
# game_client
|
||||
|
||||
[](https://hex.pm/packages/game_client)
|
||||
[](https://hexdocs.pm/game_client/)
|
||||
|
||||
```sh
|
||||
gleam add game_client@1
|
||||
```
|
||||
```gleam
|
||||
import game_client
|
||||
|
||||
pub fn main() {
|
||||
// TODO: An example of the project in use
|
||||
}
|
||||
```
|
||||
|
||||
Further documentation can be found at <https://hexdocs.pm/game_client>.
|
||||
|
||||
## Development
|
||||
|
||||
```sh
|
||||
gleam run # Run the project
|
||||
gleam test # Run the tests
|
||||
```
|
@ -1,25 +0,0 @@
|
||||
name = "game_client"
|
||||
version = "1.0.0"
|
||||
target = "javascript"
|
||||
|
||||
# Fill out these fields if you intend to generate HTML documentation or publish
|
||||
# your project to the Hex package manager.
|
||||
#
|
||||
# description = ""
|
||||
# licences = ["Apache-2.0"]
|
||||
# repository = { type = "github", user = "", repo = "" }
|
||||
# links = [{ title = "Website", href = "" }]
|
||||
#
|
||||
# For a full reference of all the available options, you can have a look at
|
||||
# https://gleam.run/writing-gleam/gleam-toml/.
|
||||
|
||||
[dependencies]
|
||||
gleam_stdlib = ">= 0.44.0 and < 2.0.0"
|
||||
lustre = ">= 5.0.2 and < 6.0.0"
|
||||
rsvp = ">= 1.0.3 and < 2.0.0"
|
||||
gleam_json = ">= 2.3.0 and < 3.0.0"
|
||||
shared = { path = "../shared" }
|
||||
|
||||
[dev-dependencies]
|
||||
gleeunit = ">= 1.0.0 and < 2.0.0"
|
||||
lustre_dev_tools = ">= 1.7.1 and < 2.0.0"
|
@ -1,17 +0,0 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
|
||||
<title>🚧 game_client</title>
|
||||
|
||||
<!-- Uncomment this if you add the TailwindCSS integration -->
|
||||
<!-- <link rel="stylesheet" href="/priv/static/game_client.css"> -->
|
||||
<script type="module" src="/priv/static/game_client.mjs"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
</body>
|
||||
</html>
|
@ -1,112 +0,0 @@
|
||||
import gleam/dynamic
|
||||
import gleam/int
|
||||
import gleam/json
|
||||
import gleam/list
|
||||
import gleam/option
|
||||
import gleam/result
|
||||
import lustre
|
||||
import lustre/attribute
|
||||
import lustre/effect.{type Effect}
|
||||
import lustre/element.{type Element}
|
||||
import lustre/element/html
|
||||
import lustre/event
|
||||
import rsvp
|
||||
import user_state
|
||||
|
||||
pub fn main() {
|
||||
// For hydration, we'll read the initial model from the window
|
||||
let flags = get_initial_state()
|
||||
|
||||
let app = lustre.application(init, update, view)
|
||||
let assert Ok(_) = lustre.start(app, "#app", flags)
|
||||
|
||||
Nil
|
||||
}
|
||||
|
||||
fn get_initial_state() -> user_state.UserState {
|
||||
user_state.UserState(0, option.Some(1))
|
||||
}
|
||||
|
||||
type Screen {
|
||||
Title
|
||||
HostControl
|
||||
Display
|
||||
Player
|
||||
}
|
||||
|
||||
type ApplicationState {
|
||||
ApplicationState(user: user_state.UserState, new_game: String, screen: Screen)
|
||||
}
|
||||
|
||||
fn init(user: user_state.UserState) -> #(ApplicationState, Effect(Msg)) {
|
||||
let application_state = ApplicationState(user:, new_game: "", screen: Title)
|
||||
#(application_state, effect.none())
|
||||
}
|
||||
|
||||
type Msg {
|
||||
HostGame
|
||||
JoinGame
|
||||
UserTypedGameID(String)
|
||||
Quit
|
||||
}
|
||||
|
||||
fn update(state: ApplicationState, msg: Msg) -> #(ApplicationState, Effect(Msg)) {
|
||||
case msg {
|
||||
JoinGame -> #(
|
||||
ApplicationState(
|
||||
user: user_state.UserState(..state.user, game_id: option.Some(0)),
|
||||
new_game: "",
|
||||
screen: Player,
|
||||
),
|
||||
effect.none(),
|
||||
)
|
||||
HostGame -> #(ApplicationState(..state, screen: Display), effect.none())
|
||||
UserTypedGameID(id) -> #(
|
||||
ApplicationState(..state, new_game: id),
|
||||
effect.none(),
|
||||
)
|
||||
Quit -> #(ApplicationState(..state, screen: Title), effect.none())
|
||||
}
|
||||
}
|
||||
|
||||
fn view(state: ApplicationState) -> Element(Msg) {
|
||||
let styles = [
|
||||
#("max-width", "30ch"),
|
||||
#("margin", "0 auto"),
|
||||
#("display", "flex"),
|
||||
#("flex-direction", "column"),
|
||||
#("gap", "1em"),
|
||||
]
|
||||
|
||||
case state.screen {
|
||||
Title ->
|
||||
html.div([attribute.styles(styles)], [
|
||||
html.h1([], [html.text("Title")]),
|
||||
view_title(state.new_game),
|
||||
])
|
||||
Player ->
|
||||
html.div([attribute.styles(styles)], [
|
||||
html.h1([], [html.text("Game")]),
|
||||
view_quit_button(),
|
||||
])
|
||||
_ ->
|
||||
html.div([attribute.styles(styles)], [
|
||||
html.h1([], [html.text("Lol LMAO")]),
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
fn view_title(game_name: String) -> Element(Msg) {
|
||||
html.div([], [
|
||||
html.input([
|
||||
attribute.placeholder("Enter game id to join:"),
|
||||
attribute.value(game_name),
|
||||
event.on_input(UserTypedGameID),
|
||||
]),
|
||||
html.button([event.on_click(JoinGame)], [html.text("Join")]),
|
||||
])
|
||||
}
|
||||
|
||||
fn view_quit_button() -> Element(Msg) {
|
||||
html.div([], [html.button([event.on_click(Quit)], [html.text("Quit")])])
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
import gleeunit
|
||||
import gleeunit/should
|
||||
|
||||
pub fn main() {
|
||||
gleeunit.main()
|
||||
}
|
||||
|
||||
// gleeunit test functions end in `_test`
|
||||
pub fn hello_world_test() {
|
||||
1
|
||||
|> should.equal(1)
|
||||
}
|
@ -21,14 +21,14 @@ pub fn serialize(session: Session) -> json.Json {
|
||||
])
|
||||
}
|
||||
|
||||
pub fn deseralize(json_string: String) -> Result(Session, json.DecodeError) {
|
||||
let session_decoder = {
|
||||
pub fn decoder() -> decode.Decoder(Session) {
|
||||
use id <- decode.field("id", decode.int)
|
||||
use host_user_id <- decode.field("host_user_id", decode.int)
|
||||
use players <- decode.field("players", decode.list(player.decoder()))
|
||||
|
||||
decode.success(Session(id:, host_user_id:, players:))
|
||||
}
|
||||
|
||||
json.parse(from: json_string, using: session_decoder)
|
||||
}
|
||||
|
||||
pub fn deseralize(json_string: String) -> Result(Session, json.DecodeError) {
|
||||
json.parse(from: json_string, using: decoder())
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user