From d9a835c31506f0e65c0a8beb315f9d9f9c2a9bd2 Mon Sep 17 00:00:00 2001 From: vutukuri15 Date: Sat, 15 Feb 2025 19:56:41 -0800 Subject: [PATCH] Displaying Chip value contribution --- components/ChipDistributionSummary.tsx | 140 ++++++++++++++++++++----- 1 file changed, 116 insertions(+), 24 deletions(-) diff --git a/components/ChipDistributionSummary.tsx b/components/ChipDistributionSummary.tsx index 19fd70d..4b78b5d 100644 --- a/components/ChipDistributionSummary.tsx +++ b/components/ChipDistributionSummary.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useMemo, useState } from "react"; +import React, { useEffect, useState } from "react"; import { View, Text, StyleSheet } from "react-native"; import { ColorValue } from "react-native"; @@ -9,8 +9,6 @@ interface ChipDistributionSummaryProps { colors?: ColorValue[]; } -const MAX_CHIPS = 500; - const ChipDistributionSummary = ({ playerCount, buyInAmount, @@ -20,41 +18,135 @@ const ChipDistributionSummary = ({ const [chipDistribution, setChipDistribution] = useState([]); useEffect(() => { - if (buyInAmount !== null && playerCount > 0) { - let totalChips = totalChipsCount.reduce((sum, count) => sum + count, 0); - - if (totalChips > MAX_CHIPS) { - const scaleFactor = MAX_CHIPS / totalChips; - totalChipsCount = totalChipsCount.map((count) => Math.floor(count * scaleFactor)); - totalChips = MAX_CHIPS; + if ( + buyInAmount !== null && + playerCount > 0 && + totalChipsCount.every((chips) => chips > 0) + ) { + const chipValues = [0.05, 0.25, 1, 2.5, 5]; // Chip values: white, red, green, blue, black + let distribution = [0, 0, 0, 0, 0]; // Number of chips per player for each color + let remainingValue = buyInAmount; + let remainingChips = [...totalChipsCount]; // Make a copy of the available chips + + console.log("Starting distribution with buy-in amount:", buyInAmount); + console.log("Player count:", playerCount); + console.log("Remaining value to distribute:", remainingValue); + console.log("Available chips:", remainingChips); + + // Step 1: Distribute chips from highest to lowest denomination + for (let i = chipValues.length - 1; i >= 0; i--) { + const chipValue = chipValues[i]; + const availableChips = remainingChips[i]; + + console.log(`Attempting to distribute ${chipValue} chips`); + + // Calculate how many chips we can distribute to each player + const maxChipsPerPlayer = Math.min( + Math.floor(remainingValue / chipValue), // Max chips based on remaining value + Math.floor(availableChips / playerCount) // Max chips based on availability + ); + + console.log( + `Max chips per player for ${chipValue} value: ${maxChipsPerPlayer}` + ); + + if (maxChipsPerPlayer > 0) { + // Distribute the chips + const chipsToDistribute = Math.min( + maxChipsPerPlayer, + Math.floor(remainingValue / chipValue) + ); + distribution[i] = chipsToDistribute; + + remainingValue -= chipsToDistribute * chipValue; // Subtract the value of these chips + remainingChips[i] -= chipsToDistribute * playerCount; // Update remaining chips + + console.log( + `Distributed ${chipsToDistribute} chips of ${chipValue} value` + ); + } + + if (remainingValue <= 0) break; // Stop once the required value is met } - const distribution = totalChipsCount.map((chipCount) => - Math.floor(chipCount / playerCount) + + console.log("Remaining value after distribution:", remainingValue); + console.log("Remaining chips:", remainingChips); + + // Step 2: Handle the remaining value with smaller denominations if necessary + if (remainingValue > 0) { + for (let i = 0; i < chipValues.length; i++) { + const chipValue = chipValues[i]; + const availableChips = remainingChips[i]; + + const maxChipsPerPlayer = Math.min( + Math.floor(remainingValue / chipValue), // Max chips based on remaining value + Math.floor(availableChips / playerCount) // Max chips based on availability + ); + + console.log( + `Attempting to distribute ${chipValue} chips (remaining value: ${remainingValue})` + ); + + if (maxChipsPerPlayer > 0) { + const chipsToDistribute = Math.min( + maxChipsPerPlayer, + Math.floor(remainingValue / chipValue) + ); + distribution[i] += chipsToDistribute; + + remainingValue -= chipsToDistribute * chipValue; // Subtract the value of these chips + remainingChips[i] -= chipsToDistribute * playerCount; // Update remaining chips + + console.log( + `Distributed ${chipsToDistribute} chips of ${chipValue} value` + ); + } + + if (remainingValue <= 0) break; // Stop if the remaining value is fulfilled + } + } + + console.log("Remaining value after distribution:", remainingValue); + console.log("Final chip distribution:", distribution); + + // Step 3: Adjust distribution to ensure no player gets more chips than available + distribution = distribution.map((chipCount, i) => + Math.min(chipCount, Math.floor(totalChipsCount[i] / playerCount)) ); - + + // Step 4: Check if total value distributed per player matches the buy-in + const totalDistributedValue = distribution.reduce( + (total, chips, index) => total + chips * chipValues[index], + 0 + ); + + console.log("Total distributed value:", totalDistributedValue); + + // If total distributed value doesn't match, reset distribution + if (totalDistributedValue !== buyInAmount) { + console.log("Mismatch in distributed value, resetting distribution."); + distribution = [0, 0, 0, 0, 0]; + } + setChipDistribution(distribution); } else { setChipDistribution([]); } }, [buyInAmount, playerCount, totalChipsCount]); - const hasValidDistribution = useMemo( - () => - buyInAmount !== null && playerCount > 0 && chipDistribution.length > 0, - [buyInAmount, playerCount, chipDistribution] - ); - return ( Chip Distribution Summary: - {hasValidDistribution ? ( + {chipDistribution.length > 0 ? ( chipDistribution.map((count, index) => ( - - {`${colors[index]?.toString().toUpperCase()} Chips: ${count} per player`} + + {`${colors[index]?.toString().toUpperCase()} Chips: ${count} per player`} )) ) : ( - No valid distribution calculated yet. + + No valid distribution calculated yet. + )} ); @@ -82,4 +174,4 @@ const styles = StyleSheet.create({ }, }); -export default ChipDistributionSummary; \ No newline at end of file +export default ChipDistributionSummary;