From: MA Beaudet Date: Fri, 5 Nov 2021 14:50:11 +0000 (+0100) Subject: feat(web): add frequency, parse_cards and eval_hand functions X-Git-Url: https://git.beaudet.xyz/?a=commitdiff_plain;h=fc2e651c563bd214f50e7eb26906ad83c3203e2c;p=poker-eval.git feat(web): add frequency, parse_cards and eval_hand functions Does not use deprecated `eval_from_str` exported wasm functions anymore. Updated the wasm_bindgen generated files --- diff --git a/static/index.html b/static/index.html index 3d33e65..22a8867 100644 --- a/static/index.html +++ b/static/index.html @@ -21,8 +21,8 @@ -
Score:
Hand value:
+
Score:
Hand value:
Frequency:

Based on Kevin L. Suffecool poker hand evaluator.

diff --git a/static/index.js b/static/index.js index c2510e0..efe1a32 100644 --- a/static/index.js +++ b/static/index.js @@ -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 + "
"; resultDiv.innerHTML += "Hand value: " + VALUE_STR[rank] + "
"; + resultDiv.innerHTML += "Frequency: " + freq; warningDiv.style.visibility = 'hidden'; warningDiv.innerHTML = ""; diff --git a/static/poker_eval.js b/static/poker_eval.js index e3c245f..e3c2c68 100644 --- a/static/poker_eval.js +++ b/static/poker_eval.js @@ -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) { diff --git a/static/poker_eval_bg.wasm b/static/poker_eval_bg.wasm index dae367c..b3d8695 100644 Binary files a/static/poker_eval_bg.wasm and b/static/poker_eval_bg.wasm differ