Johann Woelper 5 years ago
commit
5667f6d602
5 changed files with 270 additions and 0 deletions
  1. 4 0
      .gitignore
  2. 15 0
      Cargo.toml
  3. 99 0
      Makefile.toml
  4. 24 0
      index.html
  5. 128 0
      src/lib.rs

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
+/target
+**/*.rs.bk
+Cargo.lock
+.history

+ 15 - 0
Cargo.toml

@@ -0,0 +1,15 @@
+[package]
+name = "webdrop"
+version = "0.1.0"
+authors = ["Johann Woelper <woelper@gmail.com>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+seed = "^0.5.0"
+wasm-bindgen = "^0.2.50"
+drop = { path = "../drop" }
+
+[lib]
+crate-type = ["cdylib"]

+ 99 - 0
Makefile.toml

@@ -0,0 +1,99 @@
+[env]
+# all workspace members can use this Makefile
+CARGO_MAKE_EXTEND_WORKSPACE_MAKEFILE = "true"
+PORT = "8000"
+
+# ---- BUILD & CREATE WASMS ----
+[tasks.compile]
+description = "Build"
+workspace = false
+command = "cargo"
+args = ["build"]
+
+[tasks.compile_release]
+description = "Build, with the --release flag"
+workspace = false
+command = "cargo"
+args = ["build", "--release"]
+
+[tasks.create_wasm]
+description = "Build with wasm-pack"
+install_crate = { crate_name = "wasm-pack", binary = "wasm-pack", test_arg = "-V", min_version = "0.8.0" }
+command = "wasm-pack"
+args = ["build", "--target", "web", "--out-name", "package", "--dev"]
+
+[tasks.create_wasm_release]
+description = "Build with wasm-pack"
+install_crate = { crate_name = "wasm-pack", binary = "wasm-pack", test_arg = "-V", min_version = "0.8.0" }
+command = "wasm-pack"
+args = ["build", "--target", "web", "--out-name", "package"]
+
+[tasks.build]
+description = "Build, and create wasms"
+workspace = false
+dependencies = ["compile", "create_wasm"]
+
+[tasks.build_release]
+description = "Build, and create wasms, with the release flag"
+workspace = false
+dependencies = ["compile_release", "create_wasm_release"]
+
+[tasks.watch]
+description = "Build, create wasms, and watch/recompile files for changes"
+workspace = false
+dependencies = ["build", "create_wasm"]
+watch = { ignore_pattern="pkg/*" }
+
+[tasks.serve]
+description = "Start server"
+install_crate = { crate_name = "microserver", binary = "microserver", test_arg = "-h" }
+workspace = false
+command = "microserver"
+args = ["--port", "${PORT}"]
+
+[tasks.start]
+description = "Combine the build and serve tasks"
+workspace = false
+dependencies = ["build", "serve"]
+
+
+# ---- LINT ----
+
+[tasks.clippy]
+description = "Lint with Clippy"
+clear = true
+workspace = false
+install_crate = { rustup_component_name = "clippy", binary = "cargo-clippy", test_arg = "--help" }
+command = "cargo"
+args = ["clippy", "--all-features", "--", "--deny", "warnings", "--deny", "clippy::pedantic", "--deny", "clippy::nursery"]
+
+[tasks.fmt]
+description = "Format with rustfmt"
+workspace = false
+dependencies = ["fmt"]
+
+
+# ---- TEST ----
+
+[tasks.test]
+description = "Run tests. Ex: 'cargo make test firefox'. Test envs: [chrome, firefox, safari]"
+clear = true
+workspace = false
+install_crate = { crate_name = "wasm-pack", binary = "wasm-pack", test_arg = "-V", min_version = "0.8.0" }
+command = "wasm-pack"
+args = ["test", "--${@}"]
+
+[tasks.test_release]
+extend = "test"
+description = "Run tests in release mode. Ex: 'cargo make test firefox'. Test envs: [chrome, firefox, safari]"
+args = ["test", "--${@}", "--release"]
+
+[tasks.test_h]
+description = "Run headless tests. Ex: 'cargo make test_h firefox'. Test envs: [chrome, firefox, safari]"
+extend = "test"
+args = ["test", "--headless", "--${@}"]
+
+[tasks.test_h_release]
+extend = "test_h"
+description = "Run headless tests in release mode. Ex: 'cargo make test_h firefox'. Test envs: [chrome, firefox, safari]"
+args = ["test", "--headless", "--${@}", "--release"]

+ 24 - 0
index.html

@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="utf-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
+
+    <meta name="description" content="">
+
+    <link rel="icon" type="image/png" href="/public/favicon.png">
+
+    <title>Drop</title>
+
+    </head>
+
+    <body>
+        <section id="app"></section>
+        <script type="module">
+            // https://rustwasm.github.io/docs/wasm-bindgen/examples/without-a-bundler.html
+            import init from '/pkg/package.js';
+            init('/pkg/package_bg.wasm');
+        </script>
+
+    </body>
+</html>

+ 128 - 0
src/lib.rs

@@ -0,0 +1,128 @@
+use seed::{*, prelude::*};
+use drop;
+
+
+fn calc_ballistics(dist: u32, zero_dist: u32, cartridge: &str) -> String {
+    match drop::load_cartridges_from_string(&CARTRIDGES.to_string()) {
+        Ok(cartridges) => {
+            format!("{:?}", cartridges);
+            match cartridges.get(cartridge) {
+                Some(cartridge) => {
+                    
+                    format!("{:?}", cartridge.drop_zeroed(dist, zero_dist))
+                },
+
+
+                None => format!("Your cartridge is not available.")
+
+            }
+        },
+        Err(e) => format!("{:?}", e)
+    }
+}
+
+// Model
+
+const CARTRIDGES: &str = include_str!("../../drop/loading_data.json");
+
+struct Model {
+    pub val: i32,
+    pub distance: String,
+    pub zero_distance: String,
+    pub cartridge: String
+}
+
+impl Default for Model {
+    fn default() -> Self {
+        Self {
+            val: 10,
+            distance: "250".to_string(),
+            zero_distance: "100".to_string(),
+            cartridge: ".30-06 Hammerhead 14.3g".to_string(),
+        }
+    }
+}
+
+
+// Update
+
+#[derive(Clone)]
+enum Msg {
+    Distance(String),
+    ZeroDistance(String),
+    Cartridge(String)
+
+}
+
+fn update(msg: Msg, model: &mut Model, _: &mut impl Orders<Msg>) {
+    match msg {
+        Msg::Distance(d) => model.distance = d,
+        Msg::ZeroDistance(zd) => model.zero_distance = zd,
+        Msg::Cartridge(c) => model.cartridge = c
+    }
+
+}
+
+
+// View
+
+fn view(model: &Model) -> impl View<Msg> {
+    vec![
+        h3![
+            {
+
+            format!(
+                "dist:{} zero:{} cartridge:{} ballistics:{}",
+                model.distance,
+                model.zero_distance,
+                model.cartridge,
+                calc_ballistics(model.distance.parse().unwrap_or(50), model.zero_distance.parse().unwrap_or(50), &model.cartridge)
+            )
+            }
+        ],
+        label!["Distance",
+            attrs! {
+                At::For => "distance"
+            }
+        ],
+        input![
+            attrs! {
+                At::Value => model.distance;
+                At::Id => "distance"
+            },
+            input_ev(Ev::Input, Msg::Distance),
+
+        ],
+        label!["Zero distance",
+            attrs! {
+                At::For => "zero_distance"
+            }
+        ],
+        input![
+            attrs! {
+                At::Value => model.zero_distance;
+                At::Id => "zero_distance"
+            },
+            input_ev(Ev::Input, Msg::ZeroDistance),
+
+        ],
+        select![
+            attrs!{At::Value => "0"},
+            option![attrs!{At::Value => ".30-06 Hammerhead 14.3g"}, ".30-06 Hammerhead 14.3g"],
+            option![attrs!{At::Value => ".30-06 Hammerhead 11g"}, ".30-06 Hammerhead 11g"],
+            option![attrs!{At::Value => ".300 WM Barnes VOR-TX 11g"}, ".300 WM Barnes VOR-TX 11g"],
+            input_ev(Ev::Input, Msg::Cartridge)
+        ]
+        // button![
+        //     simple_ev(Ev::Click, Msg::Increment),
+            
+        //     format!("Hello, World × {}", model.val)
+        // ]
+    ]
+}
+
+#[wasm_bindgen(start)]
+pub fn render() {
+    App::builder(update, view)
+        .build_and_start();
+}