aboutsummaryrefslogtreecommitdiff
path: root/calculette_aoo/js/main.ml
diff options
context:
space:
mode:
Diffstat (limited to 'calculette_aoo/js/main.ml')
-rw-r--r--calculette_aoo/js/main.ml97
1 files changed, 97 insertions, 0 deletions
diff --git a/calculette_aoo/js/main.ml b/calculette_aoo/js/main.ml
new file mode 100644
index 0000000..9a63d12
--- /dev/null
+++ b/calculette_aoo/js/main.ml
@@ -0,0 +1,97 @@
+open Brr
+
+let ( let=? ) : 'a option -> ('a -> unit) -> unit =
+ fun f opt -> Option.iter opt f
+
+let get_int_value element =
+ let value = El.prop El.Prop.value element in
+ match Jstr.to_int value with
+ | Some v -> v
+ | None -> 0
+
+let cout_a = (800, 200, 100)
+let cout_m = (110, 55, 35)
+let cout_rm = (50, 40, 20)
+let cout_pm = (5, 3, 1)
+let cout_fm = (100, 50, 30)
+
+let get_element_by_id id =
+ Jstr.of_string id |> Brr.Document.find_el_by_id Brr.G.document
+
+let get_int_element id =
+ Jstr.of_string id
+ |> Brr.Document.find_el_by_id Brr.G.document
+ |> Option.map get_int_value |> Option.value ~default:0
+
+let eval _ =
+ let a_element = get_int_element "a" in
+ let m_element = get_int_element "m" in
+ let rm_element = get_int_element "rm" in
+ let fm_element = get_int_element "fm" in
+ let pm_element = get_int_element "pm" in
+
+ let a_bonus = get_int_element "a_bonus" in
+ let m_bonus = get_int_element "m_bonus" in
+ let rm_bonus = get_int_element "rm_bonus" in
+ let fm_bonus = get_int_element "fm_bonus" in
+ let pm_bonus = get_int_element "pm_bonus" in
+
+ let a = Aoo.Carac.create ~bonus:a_bonus a_element cout_a in
+ let m = Aoo.Carac.create ~bonus:m_bonus m_element cout_m in
+ let rm = Aoo.Carac.create ~bonus:rm_bonus rm_element cout_rm in
+ let fm = Aoo.Carac.create ~bonus:fm_bonus fm_element cout_fm in
+ let pm = Aoo.Carac.create ~bonus:pm_bonus pm_element cout_pm in
+
+ let build = Aoo.Build.{ a; m; rm; fm; pm } in
+
+ (* Evaluate the cost for the given upgrades *)
+ let cost = Aoo.Build.cost build in
+
+ let fm_oponent = get_int_element "fm_oponent" in
+ let env : Aoo.Build.env =
+ {
+ cost_max = get_int_element "xp" + cost
+ ; max_tours = float (get_int_element "tours")
+ ; cout_sort = get_int_element "cost"
+ ; degat_sort = get_int_element "dammage"
+ ; fm_oponent
+ ; frequencies = Aoo.Build.buil_freq_table fm_element fm_oponent
+ }
+ in
+ let upgrades =
+ match Aoo.Build.upgrade env build with
+ | [] ->
+ (* If there is no upgrade available, return the current build as a
+ solution instead of nothing. *)
+ [ build ]
+ | upgrades -> upgrades
+ in
+ let build =
+ Aoo.Build.traverse env
+ (Aoo.Build.cost build, Aoo.Build.score env build)
+ upgrades
+ in
+
+ let () = Aoo.Build.repr env Format.str_formatter build in
+ let result = Format.flush_str_formatter () in
+ let=? result_element = get_element_by_id "result" in
+
+ El.set_children result_element [ El.txt' result ];
+ ()
+
+let main () =
+ let=? btn = get_element_by_id "form" in
+ let _ =
+ Ev.listen Brr_io.Form.Ev.submit
+ (fun ev ->
+ Ev.prevent_default ev;
+ eval ev)
+ (El.as_target btn)
+ in
+
+ eval ()
+
+let () =
+ let open Jv in
+ let main = obj [| ("run", repr main) |] in
+ set global "lib" main