From a8df60044993cd5b03b172dd99ef16cbb438b338 Mon Sep 17 00:00:00 2001 From: MA Beaudet Date: Wed, 3 Nov 2021 22:34:36 +0100 Subject: [PATCH] feat: add wasm-bindgen build option and live example --- Cargo.toml | 4 ++++ README.md | 6 ++++-- src/constants.rs | 2 +- src/lib.rs | 4 ++++ 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index aa4afb0..3275a2f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,8 +3,12 @@ name = "poker-eval" version = "0.1.0" edition = "2021" +[lib] +crate-type = ["cdylib", "rlib"] + [features] parallel = ["rayon"] [dependencies] rayon = { version = "1.5.1", optional = true } +wasm-bindgen = "0.2.78" diff --git a/README.md b/README.md index 3613140..fbeadbe 100644 --- a/README.md +++ b/README.md @@ -2,5 +2,7 @@ Poker hand evaluator -Based on Kevin L. Suffecool poker hand evaluator. - +This project can be built using wasm-pack.\ +An example is available [here](https://poker-eval.beaudet.xyz). + +Based on Kevin L. Suffecool [poker hand evaluator](http://suffe.cool/poker/evaluator.html) diff --git a/src/constants.rs b/src/constants.rs index 3b54d48..419a1c5 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -16,7 +16,7 @@ pub const VALUE_STR: [&str; 10] = [ "Four of a Kind", "Full House", "Flush", - "Staight", + "Straight", "Three of a Kind", "Two Pair", "One Pair", diff --git a/src/lib.rs b/src/lib.rs index d2e951d..99e31af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,6 +15,8 @@ use constants::{ PERM7, PRIMES, STRAIGHT, STRAIGHT_FLUSH, THREE_OF_A_KIND, TWO_PAIR, UNIQUE5, }; +use wasm_bindgen::prelude::*; + /// Initializes a deck of 52 cards as an integer vector of lenght 52. /// /// The order in which the cards are returned is not random. @@ -132,6 +134,7 @@ fn find_fast(mut u: u32) -> u32 { /// assert_eq!(eval_from_str("4s 4d 4h 4c 5d"), 140); /// assert_eq!(eval_from_str("Ts 9s 8s 7s 6s 7h 6d"), 5); /// ``` +#[wasm_bindgen] pub fn eval_from_str(s: &str) -> u32 { let cards = s.split(' ').map(|c| card_from_str(c)).collect::>(); match cards.len() { @@ -187,6 +190,7 @@ pub fn eval_7hand(hand: &[u32; 7]) -> u32 { /// assert_eq!(eval, 1433); /// assert_eq!(hand_rank(eval), FLUSH); /// ``` +#[wasm_bindgen] pub fn hand_rank(i: u32) -> u32 { match i { 6186..=7462 => HIGH_CARD, -- 2.20.1