feat(web): add frequency, parse_cards and eval_hand functions
authorMA Beaudet <ma@beaudet.xyz>
Fri, 5 Nov 2021 14:50:11 +0000 (15:50 +0100)
committerMA Beaudet <ma@beaudet.xyz>
Fri, 5 Nov 2021 14:50:11 +0000 (15:50 +0100)
Does not use deprecated `eval_from_str` exported wasm functions anymore.
Updated the wasm_bindgen generated files

static/index.html
static/index.js
static/poker_eval.js
static/poker_eval_bg.wasm

index 3d33e65..22a8867 100644 (file)
@@ -21,8 +21,8 @@
     <label for="eval-input">Cards</label>
     <input class="input" id="eval-input" placeholder="2s 3d Td Kc 2d" type="text" />
     <button class="input" id="eval-button" type="submit">Evaluate</button>
-    <div id="result">Score:<br />Hand value:</div>
     <div id="warning"></div>
+    <div id="result">Score:<br />Hand value:<br />Frequency:</div>
     <p>Based on Kevin L. Suffecool <a href="http://suffe.cool/poker/evaluator.html">poker hand evaluator</a>.</p>
     <footer>Code available on my <a href="https://git.beaudet.xyz/?p=poker-eval.git">git repository</a></footer>
     <script src="index.js" type="module" defer></script>
index c2510e0..efe1a32 100644 (file)
@@ -1,4 +1,4 @@
-import init, { eval_from_str, hand_rank } from './poker_eval.js';
+import init, { hand_rank, frequency, parse_cards, eval_hand } from './poker_eval.js';
 async function run() {
   await init();
 }
@@ -14,8 +14,10 @@ evalButton.addEventListener("click", _ => {
     const cards = parse_cards(inputText);
     const result = eval_hand(cards);
     const rank = hand_rank(result);
+    const freq = frequency(rank);
     resultDiv.innerHTML = "Score: " + result + "<br />";
     resultDiv.innerHTML += "Hand value: " + VALUE_STR[rank] + "<br />";
+    resultDiv.innerHTML += "Frequency: " + freq;
     warningDiv.style.visibility = 'hidden';
     warningDiv.innerHTML = "";
 
index e3c245f..e3c2c68 100644 (file)
@@ -63,27 +63,73 @@ function passStringToWasm0(arg, malloc, realloc) {
     WASM_VECTOR_LEN = offset;
     return ptr;
 }
+
+let cachegetInt32Memory0 = null;
+function getInt32Memory0() {
+    if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) {
+        cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer);
+    }
+    return cachegetInt32Memory0;
+}
+
+let cachegetUint32Memory0 = null;
+function getUint32Memory0() {
+    if (cachegetUint32Memory0 === null || cachegetUint32Memory0.buffer !== wasm.memory.buffer) {
+        cachegetUint32Memory0 = new Uint32Array(wasm.memory.buffer);
+    }
+    return cachegetUint32Memory0;
+}
+
+function getArrayU32FromWasm0(ptr, len) {
+    return getUint32Memory0().subarray(ptr / 4, ptr / 4 + len);
+}
 /**
-* Wrapper over both five-card and seven-card hand evaluator from string.
+* @param {string} s
+* @returns {Uint32Array}
+*/
+export function parse_cards(s) {
+    try {
+        const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
+        var ptr0 = passStringToWasm0(s, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
+        var len0 = WASM_VECTOR_LEN;
+        wasm.parse_cards(retptr, ptr0, len0);
+        var r0 = getInt32Memory0()[retptr / 4 + 0];
+        var r1 = getInt32Memory0()[retptr / 4 + 1];
+        var v1 = getArrayU32FromWasm0(r0, r1).slice();
+        wasm.__wbindgen_free(r0, r1 * 4);
+        return v1;
+    } finally {
+        wasm.__wbindgen_add_to_stack_pointer(16);
+    }
+}
+
+function passArray32ToWasm0(arg, malloc) {
+    const ptr = malloc(arg.length * 4);
+    getUint32Memory0().set(arg, ptr / 4);
+    WASM_VECTOR_LEN = arg.length;
+    return ptr;
+}
+/**
+* Wrapper over both five-card and seven-card hand evaluator from a vector of cards.
 *
 * The evaluator orders hands from 1 to 7462.
 *
 * # Example
 *
 * ```
-* use poker_eval::{eval_from_str};
+* use poker_eval::{eval_hand, parse_cards};
 *
-* assert_eq!(eval_from_str("4s 4d 4h 4c 5d"), 140);
-* assert_eq!(eval_from_str("Ts 9s 8s 7s 6s 7h 6d"), 5);
+* assert_eq!(eval_hand(parse_cards("4s 4d 4h 4c 5d")), 140);
+* assert_eq!(eval_hand(parse_cards("Ts 9s 8s 7s 6s 7h 6d")), 5);
 * ```
-* @param {string} s
+* @param {Uint32Array} cards
 * @returns {number}
 */
-export function eval_from_str(s) {
-    var ptr0 = passStringToWasm0(s, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
+export function eval_hand(cards) {
+    var ptr0 = passArray32ToWasm0(cards, wasm.__wbindgen_malloc);
     var len0 = WASM_VECTOR_LEN;
-    var ret = wasm.eval_from_str(ptr0, len0);
-    return ret >>> 0;
+    var ret = wasm.eval_hand(ptr0, len0);
+    return ret;
 }
 
 /**
@@ -92,9 +138,9 @@ export function eval_from_str(s) {
 * # Example
 *
 * ```
-* use poker_eval::{eval_from_str, hand_rank, constants::FLUSH};
+* use poker_eval::{eval_hand, hand_rank, parse_cards, constants::FLUSH};
 *
-* let eval = eval_from_str("Js 9s 7s 3s 2s");
+* let eval = eval_hand(parse_cards("Js 9s 7s 3s 2s"));
 * assert_eq!(eval, 1433);
 * assert_eq!(hand_rank(eval), FLUSH);
 * ```
@@ -103,7 +149,16 @@ export function eval_from_str(s) {
 */
 export function hand_rank(i) {
     var ret = wasm.hand_rank(i);
-    return ret >>> 0;
+    return ret;
+}
+
+/**
+* @param {number} i
+* @returns {number}
+*/
+export function frequency(i) {
+    var ret = wasm.frequency(i);
+    return ret;
 }
 
 async function load(module, imports) {
index dae367c..b3d8695 100644 (file)
Binary files a/static/poker_eval_bg.wasm and b/static/poker_eval_bg.wasm differ