Results
Welcome to the documentation for the Results 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: Results
Description
The Results
component represents the results stage in the game. It manages player feedback, brand switching decisions, and timer-based warnings for user actions.
Key Features
-
Timer Integration:
- Notifies players when only 15 seconds remain in the stage using toast notifications.
-
Brand Switching:
- Allows players to switch brands, reset reputation, or keep their current brand.
-
Results Display:
- Includes child components like
<ResultsCard />
to display detailed feedback for producers and consumers.
- Includes child components like
-
Data Persistence:
- Updates game state with player decisions such as brand changes and stage submissions.
Key Variables
player
: Represents the current player.consumers
: Array of all players who are not the current player.cheated
: Boolean indicating whether the player's current quality is "low."warrants
: Array tracking the player's warranty status.reviews
: Reviews received by the producer.timer
: Tracks the remaining time for the stage.toastShown
: State to track whether the warning toast has already been displayed.brandStatus
: State representing whether the player has decided to switch brands or not.
Function: handleSubmit()
Description
Handles the submission of the player's decisions at the end of the results stage.
Parameters
None
Returns
None
Code
const handleSubmit = () => {
player.set("boughtStock", false);
player.stage.set("submit", true);
};
Logic Explanation
-
Reset Stock Purchase:
- Sets the player's
boughtStock
attribute tofalse
.
- Sets the player's
-
Submit Stage:
- Marks the stage as submitted using
player.stage.set("submit", true)
.
- Marks the stage as submitted using
Function: switchBrand()
Description
Allows the player to switch their brand, resetting their reputation.
Parameters
None
Returns
None
Code
const switchBrand = () => {
setBrandStatus(true);
let brandArray = player.get("changedBrand");
brandArray.pop();
brandArray.push(true);
player.set("changedBrand", brandArray);
console.log('before toast');
toast.success("You changed brands! Your reputation will be reset.")
};
Logic Explanation
-
Update Brand Status:
- Sets the
brandStatus
state totrue
.
- Sets the
-
Modify Brand Array:
- Updates the player's
changedBrand
array to reflect the brand change.
- Updates the player's
-
Display Feedback:
- Shows a toast message to confirm the brand switch.
Function: keepBrand()
Description
Allows the player to retain their current brand and reputation.
Parameters
None
Returns
None
Code
const keepBrand = () => {
setBrandStatus(false);
let brandArray = player.get("changedBrand");
brandArray.pop();
brandArray.push(false);
player.set("changedBrand", brandArray);
toast.success("You will keep your current brand and reputation.");
};
Logic Explanation
-
Update Brand Status:
- Sets the
brandStatus
state tofalse
.
- Sets the
-
Modify Brand Array:
- Updates the player's
changedBrand
array to reflect no change in brand.
- Updates the player's
-
Display Feedback:
- Shows a toast message to confirm the decision to keep the current brand.
useEffect Hook
Description
Monitors the remaining time in the stage and triggers a warning toast when 15 seconds remain.
Parameters
None
Returns
None
Code
useEffect(() => {
if (timer?.remaining || timer?.remaining === 0) {
const remainingSeconds = Math.round(timer.remaining / 1000);
if (remainingSeconds <= 15 && !toastShown) {
toast.warning("Hurry up! Only 15 seconds left!");
setToastShown(true);
}
}
}, [timer, toastShown]);
Logic Explanation
-
Monitor Timer:
- Checks the remaining time in the stage using the
timer.remaining
property.
- Checks the remaining time in the stage using the
-
Trigger Warning:
- If 15 seconds or less remain and a warning toast hasn’t been shown, displays a warning using
toast.warning
.
- If 15 seconds or less remain and a warning toast hasn’t been shown, displays a warning using
-
Prevent Duplicate Warnings:
- Updates
toastShown
totrue
to avoid multiple warnings.
- Updates