Producer Choice
Welcome to the documentation for the Producer Choice stage! This guide will help understand the code and its features. This page is verified by: Vedant Kejariwal
Functions
This is the list of functions that are defined on this page.
Component: ProducerChoice
Description
The ProducerChoice
component manages the producer's decision-making stage in the game. It allows producers to choose product quality, manage warranties, and update game state accordingly.
Key Features
-
Product Quality Selection:
- Producers can choose between "low" or "high" quality products.
-
Warranty Management:
- Producers can opt to warrant their product's quality and incur additional costs.
-
Capital Management:
- Ensures that producers cannot produce products or warranties without sufficient capital.
-
Feedback and UI Integration:
- Provides toast notifications for user actions and errors.
-
Dynamic Updates:
- Updates the producer's data, including product quality, warranty status, and capital.
Function: addItem()
Description
Adds the selected product quality to the producer's product history and updates the game state.
Parameters
newItem
(string): The quality of the product ("low" or "high").
Returns
None
Code
/**
This function adds in the current round's produced item's quality into an array of strings.
@returns {[string]} The list of producer's product qualities with the appended string.
@example
addItem("low") -> ["low"];
// Returns the list of the producer's product qualities with the appended product.
*/
const addItem = (newItem) => {
setItems((items) => [...items, newItem]);
player.set("productQuality", [...items, newItem]);
};
Logic Explanation
-
Add to State:
- Updates the
items
state to include the new product quality.
- Updates the
-
Persist Data:
- Updates the producer's product quality in the game state.
Function: chooseLow()
Description
Selects "low" as the product quality and updates the game state.
Parameters
None
Returns
None
Code
const chooseLow = () => {
if (currentQuality !== "low") {
toast.info("You chose to produce a low quality product!");
}
setCurrentQuality("low");
setClearChoice(false);
};
Logic Explanation
-
Update Product Quality:
- Sets
currentQuality
to "low".
- Sets
-
Clear Previous Choices:
- Resets the
clearChoice
state to false.
- Resets the
Function: chooseHigh()
Description
Selects "high" as the product quality and updates the game state.
Parameters
None
Returns
None
Code
const chooseHigh = () => {
if (currentQuality !== "high") {
toast.info("You chose to produce a high quality product!");
}
setCurrentQuality("high");
setClearChoice(false);
};
Logic Explanation
-
Update Product Quality:
- Sets
currentQuality
to "high".
- Sets
-
Clear Previous Choices:
- Resets the
clearChoice
state to false.
- Resets the
Function: selectWarrant()
Description
Enables the warranty for the selected product.
Parameters
None
Returns
None
Code
const selectWarrant = () => {
if (!warrantStatus) {
toast.info(
"You chose to warrant the claim of your high product quality!"
);
}
setWarrantStatus(true);
setClickCounter(clickCounter + 1);
};
Logic Explanation
-
Enable Warranty:
- Sets
warrantStatus
to true.
- Sets
-
Track Changes:
- Increments
clickCounter
to track user interactions.
- Increments
Function: deselectWarrant()
Description
Disables the warranty for the selected product.
Parameters
None
Returns
None
Code
const deselectWarrant = () => {
if (warrantStatus) {
toast.info(
"You chose to not warrant the claim of your high product quality!"
);
}
setWarrantStatus(false);
setClickCounter(clickCounter + 1);
};
Logic Explanation
-
Disable Warranty:
- Sets
warrantStatus
to false.
- Sets
-
Track Changes:
- Increments
clickCounter
to track user interactions.
- Increments
Function: clearButton()
Description
Clears all selections made by the producer and allows the producer to go to the next stage without producing any products in the current stage
Parameters
None
Returns
None
Code
const clearButton = () => {
/*
Clear Button to clear all the seletions that the producer made and allow the producer
to go to the next stage without producing any products in the current stage
*/
toast.info(
"Warning: This yields 0 points!"
)
setClearChoice(true);
setCurrentQuality("");
setWarrantStatus(false);
}
Logic Explanation
-
Reset Choices:
- Sets
currentQuality
to an empty string andwarrantStatus
to false.
- Sets
-
Enable Clear Choice:
- Sets
clearChoice
to true.
- Sets
Function: handleSubmit()
Description
Handles the submission of the producer's choices, updating relevant game data.
Parameters
None
Returns
None
Code
function handleSubmit() {
if (player.get("role") === "consumer") {
// console.log(
// `Saving ${consumerReviews} to consumer ${player.get("name")}`
// );
// console.log("conbut sumerReviews in handleSubmit", consumerReviews);
player.set("consumerReviews", consumerReviews);
}
player.stage.set("submit", true);
}
Logic Explanation
-
Validate Capital:
- Ensures the producer has sufficient capital for the selected product and warranty.
-
Calculate Costs:
- Determines production cost based on product quality and warranty status.
-
Update Capital:
- Deducts the production cost from the producer's capital.
-
Persist Data:
- Updates the producer's product quality and warranty status in the game state.
-
Submit Stage:
- Marks the stage as submitted using
player.stage.set("submit", true)
.
- Marks the stage as submitted using
If the clearChoice variable is true then the producer is prompted on the screen to select a product quality.
if (currentQuality === "" && clearChoice === false) {
toast.error("Please select a product quality!");
player.stage.set("submit", false);
return;
}