optimizations and comments
This commit is contained in:
parent
4cc8db3e9c
commit
0f5d7316de
@ -1,4 +1,4 @@
|
|||||||
import React, { useEffect, useMemo, useState } from "react";
|
import React, { useCallback, useEffect, useMemo, useState } from "react";
|
||||||
import { View, Text, StyleSheet } from "react-native";
|
import { View, Text, StyleSheet } from "react-native";
|
||||||
import { ColorValue } from "react-native";
|
import { ColorValue } from "react-native";
|
||||||
|
|
||||||
@ -35,15 +35,6 @@ const ChipDistributionSummary = ({
|
|||||||
| 50
|
| 50
|
||||||
| 100;
|
| 100;
|
||||||
|
|
||||||
// Re-organize the inputs in a map for convience
|
|
||||||
const chipMap: Map<ColorValue, number> = useMemo(() => {
|
|
||||||
const m: Map<ColorValue, number> = new Map<ColorValue, number>();
|
|
||||||
totalChipsCount.map((v, i) => {
|
|
||||||
m.set(colors[i], v);
|
|
||||||
});
|
|
||||||
return m;
|
|
||||||
}, [playerCount, buyInAmount, totalChipsCount]);
|
|
||||||
|
|
||||||
// Return the closest (but lower) valid denomination to the target
|
// Return the closest (but lower) valid denomination to the target
|
||||||
const findFloorDenomination = (target: number): validDenomination => {
|
const findFloorDenomination = (target: number): validDenomination => {
|
||||||
let current: validDenomination = validDenominations[0];
|
let current: validDenomination = validDenominations[0];
|
||||||
@ -56,12 +47,12 @@ const ChipDistributionSummary = ({
|
|||||||
// Bound for the value of the highest chip
|
// Bound for the value of the highest chip
|
||||||
// This is somewhat arbitray, but 1/3 to 1/4 is reasonable depending on the number of colors.
|
// This is somewhat arbitray, but 1/3 to 1/4 is reasonable depending on the number of colors.
|
||||||
const maxDenomination = useMemo(() => {
|
const maxDenomination = useMemo(() => {
|
||||||
if (chipMap.size > 3) {
|
if (totalChipsCount.length > 3) {
|
||||||
return findFloorDenomination(buyInAmount / 3);
|
return findFloorDenomination(buyInAmount / 3);
|
||||||
} else {
|
} else {
|
||||||
return findFloorDenomination(buyInAmount / 4);
|
return findFloorDenomination(buyInAmount / 4);
|
||||||
}
|
}
|
||||||
}, [chipMap]);
|
}, [totalChipsCount]);
|
||||||
|
|
||||||
// Total value of the pot
|
// Total value of the pot
|
||||||
const potValue = useMemo(
|
const potValue = useMemo(
|
||||||
@ -86,56 +77,60 @@ const ChipDistributionSummary = ({
|
|||||||
|
|
||||||
// Redenominate the chips in case of failure to properly distribute.
|
// Redenominate the chips in case of failure to properly distribute.
|
||||||
// Move the shuffle index to the next lowest denomination, and keep all else same
|
// Move the shuffle index to the next lowest denomination, and keep all else same
|
||||||
const redenominate = (
|
const redenominate = useCallback(
|
||||||
invalidDenomination: validDenomination[],
|
(
|
||||||
shuffleIndex: number
|
invalidDenomination: validDenomination[],
|
||||||
): validDenomination[] => {
|
shuffleIndex: number
|
||||||
let moved = false;
|
): validDenomination[] => {
|
||||||
const newDenomination: validDenomination[] = [];
|
let moved = false;
|
||||||
for (let i = invalidDenomination.length - 1; i >= 0; i--) {
|
const newDenomination: validDenomination[] = [];
|
||||||
if (i > shuffleIndex) {
|
for (let i = invalidDenomination.length - 1; i >= 0; i--) {
|
||||||
newDenomination.push(invalidDenomination[i]);
|
if (i > shuffleIndex) {
|
||||||
} else if (i == shuffleIndex) {
|
newDenomination.push(invalidDenomination[i]);
|
||||||
newDenomination.push(invalidDenomination[i]);
|
} else if (i == shuffleIndex) {
|
||||||
} else if (i < shuffleIndex && !moved) {
|
newDenomination.push(invalidDenomination[i]);
|
||||||
const nextLowestDenominationIndex = Math.max(
|
} else if (i < shuffleIndex && !moved) {
|
||||||
validDenominations.indexOf(invalidDenomination[i]) - 1,
|
const nextLowestDenominationIndex = Math.max(
|
||||||
0
|
validDenominations.indexOf(invalidDenomination[i]) - 1,
|
||||||
);
|
0
|
||||||
newDenomination.push(validDenominations[nextLowestDenominationIndex]);
|
);
|
||||||
moved = true;
|
newDenomination.push(validDenominations[nextLowestDenominationIndex]);
|
||||||
} else {
|
moved = true;
|
||||||
newDenomination.push(invalidDenomination[i]);
|
} else {
|
||||||
|
newDenomination.push(invalidDenomination[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
newDenomination.reverse();
|
||||||
newDenomination.reverse();
|
return newDenomination;
|
||||||
return newDenomination;
|
},
|
||||||
};
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Dynamically set denominations and distributions from changing inputs
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let testDenomination: validDenomination[] = [];
|
let testDenomination: validDenomination[] = [];
|
||||||
|
|
||||||
const numColors = chipMap.size;
|
const numColors = totalChipsCount.length;
|
||||||
const testDistribution: number[] = [];
|
const testDistribution: number[] = [];
|
||||||
for (let i = 0; i < numColors; ++i) {
|
for (let i = 0; i < numColors; ++i) {
|
||||||
testDistribution.push(0);
|
testDistribution.push(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
let numColorsRemaining = numColors;
|
// Start with max denominations, then push on the next adjacent lower denomination
|
||||||
testDenomination.push(maxDenomination);
|
testDenomination.push(maxDenomination);
|
||||||
numColorsRemaining -= 1;
|
|
||||||
let currentDenominationIndex: number =
|
let currentDenominationIndex: number =
|
||||||
validDenominations.indexOf(maxDenomination);
|
validDenominations.indexOf(maxDenomination);
|
||||||
while (numColorsRemaining > 0) {
|
for (let i = numColors - 2; i >= 0; i = i - 1) {
|
||||||
numColorsRemaining -= 1;
|
|
||||||
currentDenominationIndex -= 1;
|
currentDenominationIndex -= 1;
|
||||||
const currentDemoniation = validDenominations[currentDenominationIndex];
|
const currentDemoniation = validDenominations[currentDenominationIndex];
|
||||||
testDenomination.push(currentDemoniation);
|
testDenomination.push(currentDemoniation);
|
||||||
}
|
}
|
||||||
testDenomination.reverse();
|
testDenomination.reverse();
|
||||||
|
|
||||||
|
// Distribute the chips using the test denomination
|
||||||
|
// If distribution fails to equal the buy-in, redenominate and re-try
|
||||||
|
// Algorithm could be improved with more complexity and optimization
|
||||||
let remainingValue = buyInAmount;
|
let remainingValue = buyInAmount;
|
||||||
|
|
||||||
let fail = true;
|
let fail = true;
|
||||||
let failCount = 0;
|
let failCount = 0;
|
||||||
while (fail && failCount < 1) {
|
while (fail && failCount < 1) {
|
||||||
@ -167,7 +162,7 @@ const ChipDistributionSummary = ({
|
|||||||
|
|
||||||
setDenominations(testDenomination);
|
setDenominations(testDenomination);
|
||||||
setDistributions(testDistribution);
|
setDistributions(testDistribution);
|
||||||
}, [chipMap, maxDenomination, buyInAmount, playerCount]);
|
}, [totalChipsCount, maxDenomination, buyInAmount, playerCount]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View style={styles.container}>
|
<View style={styles.container}>
|
||||||
|
Loading…
Reference in New Issue
Block a user