Skip to content

Commit

Permalink
fix: improve input validation for tree editing
Browse files Browse the repository at this point in the history
  • Loading branch information
b-inary committed Oct 1, 2023
1 parent 7a9054b commit d640a3b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 36 deletions.
3 changes: 3 additions & 0 deletions src/components/RunSolver.vue
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ import {
import {
MAX_AMOUNT,
convertBetString,
ROOT_LINE_STRING,
INVALID_LINE_STRING,
readableLineString,
} from "../utils";
Expand Down Expand Up @@ -425,7 +426,9 @@ const checkConfig = (
: config.removedLines.split(",").map(readableLineString);
if (
addedLinesArray.includes(ROOT_LINE_STRING) ||
addedLinesArray.includes(INVALID_LINE_STRING) ||
removedLinesArray.includes(ROOT_LINE_STRING) ||
removedLinesArray.includes(INVALID_LINE_STRING)
) {
return "Invalid line found (loaded broken configurations?)";
Expand Down
3 changes: 3 additions & 0 deletions src/components/TreeConfig.vue
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,7 @@ import { useStore, useConfigStore } from "../store";
import {
MAX_AMOUNT,
sanitizeBetString,
ROOT_LINE_STRING,
INVALID_LINE_STRING,
readableLineString,
} from "../utils";
Expand Down Expand Up @@ -904,7 +905,9 @@ const warningMisc = computed(() => {
const errorLines = computed(() => {
const errors: string[] = [];
if (
addedLinesArray.value.includes(ROOT_LINE_STRING) ||
addedLinesArray.value.includes(INVALID_LINE_STRING) ||
removedLinesArray.value.includes(ROOT_LINE_STRING) ||
removedLinesArray.value.includes(INVALID_LINE_STRING)
) {
errors.push("Invalid line found (loaded broken configurations?)");
Expand Down
6 changes: 3 additions & 3 deletions src/components/TreeEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,9 @@ const existingAmounts = computed(() => {
return ret;
});
const addedLines = ref(config.addedLines);
const removedLines = ref(config.removedLines);
const invalidLines = ref("");
const addedLines = ref(await invokes.treeAddedLines());
const removedLines = ref(await invokes.treeRemovedLines());
const invalidLines = ref(await invokes.treeInvalidTerminals());
const addedLinesArray = computed(() =>
addedLines.value === ""
Expand Down
74 changes: 41 additions & 33 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,54 +280,62 @@ export const convertBetString = (s: string): string => {
.join(",");
};

const parseBetAmount = (
s: string,
index: number
): { amount: number; indexEnd: number } => {
const parseBetAmount = (s: string, index: number) => {
let indexEnd = index;
while (indexEnd < s.length && /\d/.test(s[indexEnd])) indexEnd++;
const amount = Number(s.slice(index, indexEnd));
return { amount, indexEnd };
};

export const ROOT_LINE_STRING = "(Root)";
export const INVALID_LINE_STRING = "Invalid line string";

export const readableLineString = (s: string): string => {
if (s === "(Root)") return s;
if (s === ROOT_LINE_STRING) return s;

let ret = "";
let index = 0;
let isSeparatorExpected = false;

while (index < s.length) {
if (s[index] === "-") {
ret += " - ";
index += 1;
} else if (s[index] === "|") {
ret += ", ";
index += 1;
} else if (s[index] === "F") {
ret += "Fold";
index += 1;
} else if (s[index] === "X") {
ret += "Check";
index += 1;
} else if (s[index] === "C") {
ret += "Call";
index += 1;
} else if (s[index] === "B") {
const { amount, indexEnd } = parseBetAmount(s, index + 1);
ret += `Bet ${amount}`;
index = indexEnd;
} else if (s[index] === "R") {
const { amount, indexEnd } = parseBetAmount(s, index + 1);
ret += `Raise ${amount}`;
index = indexEnd;
} else if (s[index] === "A") {
const { amount, indexEnd } = parseBetAmount(s, index + 1);
ret += `All-in ${amount}`;
index = indexEnd;
} else {
let isOk = false;

switch (s[index]) {
case "-":
case "|": {
ret += s[index] === "-" ? " - " : ", ";
index += 1;
isOk = isSeparatorExpected;
break;
}
case "F":
case "X":
case "C": {
ret += { F: "Fold", X: "Check", C: "Call" }[s[index]];
index += 1;
isOk = !isSeparatorExpected;
break;
}
case "B":
case "R":
case "A": {
const { amount, indexEnd } = parseBetAmount(s, index + 1);
ret += { B: "Bet", R: "Raise", A: "All-in" }[s[index]] + ` ${amount}`;
index = indexEnd;
isOk = !isSeparatorExpected && amount > 0;
break;
}
}

if (!isOk) {
return INVALID_LINE_STRING;
}

isSeparatorExpected = !isSeparatorExpected;
}

if (!isSeparatorExpected) {
return INVALID_LINE_STRING;
}

return ret;
Expand Down

0 comments on commit d640a3b

Please sign in to comment.