]> git.beaudet.xyz Git - poker-eval.git/commitdiff
feat(web): add warning handling for faillible wasm functions
authorMA Beaudet <ma@beaudet.xyz>
Fri, 5 Nov 2021 14:45:59 +0000 (15:45 +0100)
committerMA Beaudet <ma@beaudet.xyz>
Fri, 5 Nov 2021 14:45:59 +0000 (15:45 +0100)
static/index.html
static/index.js
static/styles.css

index 107ab1ae09e0d3a2e2e6f0cbe46f9b4b29c50b73..3d33e650d93adb8e44355c2721b2e63166b85687 100644 (file)
@@ -22,6 +22,7 @@
     <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>
     <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 4f6d712d2f7c3b58377222ac9cb933e72d6c7d10..c2510e0948f2d24c91ce501fc0a91b1df511ffa1 100644 (file)
@@ -8,11 +8,22 @@ const evalButton = document.getElementById("eval-button");
 evalButton.addEventListener("click", _ => {
   const inputText = document.getElementById("eval-input").value;
   const resultDiv = document.getElementById("result");
+  const warningDiv = document.getElementById('warning');
 
-  const result = eval_from_str(inputText);
-  const rank = hand_rank(result);
-  resultDiv.innerHTML = "Score: " + result + "<br />";
-  resultDiv.innerHTML += "Hand value: " + VALUE_STR[rank];
+  try {
+    const cards = parse_cards(inputText);
+    const result = eval_hand(cards);
+    const rank = hand_rank(result);
+    resultDiv.innerHTML = "Score: " + result + "<br />";
+    resultDiv.innerHTML += "Hand value: " + VALUE_STR[rank] + "<br />";
+    warningDiv.style.visibility = 'hidden';
+    warningDiv.innerHTML = "";
+
+  } catch (e) {
+    warningDiv.innerHTML = "😵 Wrong cards provided. Try something like this:<br />2s 3s 4s 5s 6s";
+    warningDiv.style.visibility = 'visible';
+    console.error(e);
+  }
 });
 
 const VALUE_STR = [
@@ -21,7 +32,7 @@ const VALUE_STR = [
   "Four of a Kind",
   "Full House",
   "Flush",
-  "Staight",
+  "Straight",
   "Three of a Kind",
   "Two Pair",
   "One Pair",
index d2abf4176a489628943a744600a0d39f8bc4a562..e9535a82131c36ce62746446dbea2a984c9944cb 100644 (file)
@@ -73,3 +73,8 @@ label {
     );
   outline: 3px solid transparent;
 }
+
+#warning {
+  visibility: hidden;
+  color: red;
+}