Displaying Chip value contribution #6 #33

Merged
djwesty merged 10 commits from vutukuri15/6 into main 2025-02-25 11:00:20 -08:00
3 changed files with 110 additions and 76 deletions
Showing only changes of commit f2710588ea - Show all commits

View File

@ -6,7 +6,7 @@ import ChipsSelector from "@/components/ChipsSelector";
import ChipDistributionSummary from "@/components/ChipDistributionSummary"; import ChipDistributionSummary from "@/components/ChipDistributionSummary";
const IndexScreen = () => { const IndexScreen = () => {
const [playerCount, setPlayerCount] = useState(2); const [playerCount, setPlayerCount] = useState(2);
const [buyInAmount, setBuyInAmount] = useState<number | null>(null); const [buyInAmount, setBuyInAmount] = useState<number>(20);
const [numberOfChips, setNumberOfChips] = useState<number>(5); const [numberOfChips, setNumberOfChips] = useState<number>(5);
const [totalChipsCount, setTotalChipsCount] = useState<number[]>([]); const [totalChipsCount, setTotalChipsCount] = useState<number[]>([]);
const handleSave = () => { const handleSave = () => {

View File

@ -9,7 +9,7 @@ import {
import { MaterialIcons } from "@expo/vector-icons"; import { MaterialIcons } from "@expo/vector-icons";
interface BuyInSelectorProps { interface BuyInSelectorProps {
setBuyInAmount: React.Dispatch<React.SetStateAction<number | null>>; setBuyInAmount: React.Dispatch<React.SetStateAction<number>>;
} }
const defaultBuyInOptions = [10, 25, 50]; const defaultBuyInOptions = [10, 25, 50];
@ -26,8 +26,8 @@ const BuyInSelector: React.FC<BuyInSelectorProps> = ({ setBuyInAmount }) => {
setBuyInAmount(numericValue); setBuyInAmount(numericValue);
} else { } else {
setCustomAmount(""); setCustomAmount("");
setBuyInAmountState(null); setBuyInAmountState(25);
setBuyInAmount(null); setBuyInAmount(25);
} }
}; };

View File

@ -1,10 +1,10 @@
import React, { useEffect, useState } from "react"; import React, { 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";
interface ChipDistributionSummaryProps { interface ChipDistributionSummaryProps {
playerCount: number; playerCount: number;
buyInAmount: number | null; buyInAmount: number;
totalChipsCount: number[]; totalChipsCount: number[];
colors?: ColorValue[]; colors?: ColorValue[];
} }
@ -15,92 +15,126 @@ const ChipDistributionSummary = ({
totalChipsCount, totalChipsCount,
colors = ["white", "red", "green", "blue", "black"], colors = ["white", "red", "green", "blue", "black"],
}: ChipDistributionSummaryProps) => { }: ChipDistributionSummaryProps) => {
const [chipCountPerPlayer, setChipCountPerPlayer] = useState< // const [chipCountPerPlayer, setChipCountPerPlayer] = useState<
Record<string, { count: number; value: number }> // Record<string, { count: number; value: number }>
>({}); // >({});
const validDenominations = [
0.05, 0.1, 0.25, 0.5, 1, 2, 2.5, 5, 10, 20, 50, 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]);
// Helper function to return the closest (but lower) valid denomination to the target
const findFloorDenomination = (target: number) => {
let current: number = validDenominations[0];
validDenominations.forEach((value, index) => {
if (value < target) current = value;
});
return current;
};
const maxDenomination = useMemo(() => {
if (chipMap.size > 3) {
return findFloorDenomination(buyInAmount / 2);
} else {
return findFloorDenomination(buyInAmount / 4);
}
}, [chipMap]);
useEffect(() => { useEffect(() => {
if ( const testDistribution: Map<ColorValue, number> = new Map();
buyInAmount !== null && const numColors = chipMap.size;
playerCount > 0 && testDistribution.set(colors[numColors - 1], maxDenomination);
totalChipsCount.every((chips) => chips > 0) const maxDenominationIndex: number =
) { validDenominations.indexOf(maxDenomination);
const validDenominations = [ console.log("test distribution", testDistribution);
0.05, 0.1, 0.25, 0.5, 1, 2, 5, 10, 20, 50, 100, }, [chipMap, maxDenomination]);
];
const availableColors = Math.min(colors.length, 5); // useEffect(() => {
let selectedChips: number[] = []; // if (
let maxDenomination = buyInAmount / 2; // buyInAmount !== null &&
// playerCount > 0 &&
// totalChipsCount.every((chips) => chips > 0)
// ) {
// const availableColors = Math.min(colors.length, 5);
// let selectedChips: number[] = [];
// let maxDenomination = buyInAmount / 2;
// Select the denominations for available colors (up to 5 colors) // // Select the denominations for available colors (up to 5 colors)
for (let i = validDenominations.length - 1; i >= 0; i--) { // for (let i = validDenominations.length - 1; i >= 0; i--) {
if ( // if (
validDenominations[i] <= maxDenomination && // validDenominations[i] <= maxDenomination &&
selectedChips.length < availableColors // selectedChips.length < availableColors
) { // ) {
selectedChips.unshift(validDenominations[i]); // selectedChips.unshift(validDenominations[i]);
} // }
} // }
// Ensure the selected chips are sorted from low to high denomination // // Ensure the selected chips are sorted from low to high denomination
selectedChips = selectedChips.sort((a, b) => a - b); // selectedChips = selectedChips.sort((a, b) => a - b);
let distribution = new Array(selectedChips.length).fill(0); // let distribution = new Array(selectedChips.length).fill(0);
let remainingValue = buyInAmount; // let remainingValue = buyInAmount;
let remainingChips = [...totalChipsCount.slice(0, selectedChips.length)]; // let remainingChips = [...totalChipsCount.slice(0, selectedChips.length)];
// First pass: Distribute at least one chip of each selected denomination per player // // First pass: Distribute at least one chip of each selected denomination per player
for (let i = 0; i < selectedChips.length; i++) { // for (let i = 0; i < selectedChips.length; i++) {
const chipValue = selectedChips[i]; // const chipValue = selectedChips[i];
if (remainingValue >= chipValue && remainingChips[i] >= playerCount) { // if (remainingValue >= chipValue && remainingChips[i] >= playerCount) {
distribution[i] = 1; // distribution[i] = 1;
remainingValue -= chipValue; // remainingValue -= chipValue;
remainingChips[i] -= playerCount; // remainingChips[i] -= playerCount;
} // }
} // }
// Second pass: Distribute remaining buy-in amount fairly across chip colors // // Second pass: Distribute remaining buy-in amount fairly across chip colors
while (remainingValue > 0) { // while (remainingValue > 0) {
let allocatedInRound = false; // let allocatedInRound = false;
for (let i = 0; i < selectedChips.length; i++) { // for (let i = 0; i < selectedChips.length; i++) {
if (remainingValue <= 0) break; // if (remainingValue <= 0) break;
const chipValue = selectedChips[i]; // const chipValue = selectedChips[i];
if (remainingChips[i] >= playerCount && remainingValue >= chipValue) { // if (remainingChips[i] >= playerCount && remainingValue >= chipValue) {
distribution[i] += 1; // distribution[i] += 1;
remainingValue -= chipValue; // remainingValue -= chipValue;
remainingChips[i] -= playerCount; // remainingChips[i] -= playerCount;
allocatedInRound = true; // allocatedInRound = true;
} // }
} // }
if (!allocatedInRound) break; // Prevent infinite loops // if (!allocatedInRound) break; // Prevent infinite loops
} // }
// Create a mapping from chip color names to chip counts and denominations // // Create a mapping from chip color names to chip counts and denominations
let chipMap: Record<string, { count: number; value: number }> = {}; // let chipMap: Record<string, { count: number; value: number }> = {};
for (let i = 0; i < selectedChips.length; i++) { // for (let i = 0; i < selectedChips.length; i++) {
if (distribution[i] > 0) { // if (distribution[i] > 0) {
// Map denomination and color to chip count // // Map denomination and color to chip count
chipMap[colors[i]] = { // chipMap[colors[i]] = {
count: distribution[i], // count: distribution[i],
value: selectedChips[i], // value: selectedChips[i],
}; // };
} // }
} // }
setChipCountPerPlayer(chipMap); // setChipCountPerPlayer(chipMap);
} else { // } else {
setChipCountPerPlayer({}); // setChipCountPerPlayer({});
} // }
}, [buyInAmount, playerCount, totalChipsCount, colors]); // }, [buyInAmount, playerCount, totalChipsCount, colors]);
return ( return (
<View style={styles.container}> <View style={styles.container}>
<Text style={styles.title}>Chip Distribution Summary:</Text> <Text style={styles.title}>Chip Distribution Summary:</Text>
{Object.keys(chipCountPerPlayer).length > 0 ? ( {/**Object.keys(chipCountPerPlayer).length > 0 ? (
<View style={styles.chipContainer}> <View style={styles.chipContainer}>
{Object.entries(chipCountPerPlayer).map( {Object.entries(chipCountPerPlayer).map(
([color, { count, value }]) => ( ([color, { count, value }]) => (
@ -115,7 +149,7 @@ const ChipDistributionSummary = ({
<Text style={styles.noDataText}> <Text style={styles.noDataText}>
No valid distribution calculated yet. No valid distribution calculated yet.
</Text> </Text>
)} )**/}
</View> </View>
); );
}; };