Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | 305x 305x 305x 305x 305x 305x 305x 8x 305x 7x 305x 305x 305x 498x 305x 305x 305x 305x 305x 45x 43x 305x 305x 220x 220x 304x 305x 305x 305x 305x 305x 305x 305x 305x 305x 305x 305x 305x 2x 305x 305x 305x 2x 305x 305x 3x 305x 305x 305x 305x | import React, { useEffect, useState } from "react";
import { Container, CardGroup, Button } from "react-bootstrap";
import { useParams } from "react-router";
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import GameOverview from "main/components/Game/GameOverview";
import GamePlay from "main/components/Game/GamePlay";
import FarmStats from "main/components/Game/FarmStats";
import HerdList from "main/components/Game/HerdList";
import { useQueryClient } from "react-query";
import ManageCows from "main/components/Game/ManageCows";
import Profits from "main/components/Game/Profits";
import { useBackend, useBackendMutation } from "main/utils/useBackend";
import { hasRole } from "main/utils/currentUser";
import { useCurrentUser } from "main/utils/currentUser";
import {
getPlayPageBackground,
PLAY_PAGE_GRASS_GREEN,
} from "main/utils/playPageBackgroundUtils";
import ChatPanel from "main/components/Chat/ChatPanel";
import ManageCowsModal from "main/components/Game/ManageCowsModal";
import SellHerdsModal from "main/components/Game/SellHerdsModal";
export default function PlayPage() {
const { gameId } = useParams();
const queryClient = useQueryClient();
const { data: currentUser } = useCurrentUser();
// Both windows also need a buy/sell message before they open, so starting with this true would show nothing either.
// Stryker disable next-line BooleanLiteral
const [isModalOpen, setIsModalOpen] = useState(false);
const [message, setMessage] = useState();
const [numCows, setNumCows] = useState(1);
const openModal = () => {
setIsModalOpen(true);
};
const closeModal = () => {
setIsModalOpen(false);
};
// Same interval as the herds query below, so Farm Stats and the cow
// picture pick up background health updates along with the herd table.
const PLAY_PAGE_REFRESH_MS = 10000;
// Stryker disable all
const farmerAxiosParams = {
method: "GET",
url: "/api/farmer/forcurrentuser",
params: {
gameId: gameId,
},
};
// Stryker restore all
const { data: farmer, error: farmerError } = useBackend(
[`/api/farmer/forcurrentuser?gameId=${gameId}`],
farmerAxiosParams,
undefined,
{
// Only poll once the farmer has loaded, so a failed load (e.g. not
// enrolled) doesn't repeat its error toast every 10 seconds.
refetchInterval: (data) => Boolean(data) && PLAY_PAGE_REFRESH_MS,
},
);
// Stryker disable all
const { data: gamePlus } = useBackend([`/api/game/plus?id=${gameId}`], {
method: "GET",
url: "/api/game/plus",
params: {
id: gameId,
},
});
// Stryker restore all
// Stryker disable all
const { data: currentAnnouncements } = useBackend(
[`/api/announcements/current?gameId=${gameId}`],
{
method: "GET",
url: "/api/announcements/current",
params: {
gameId: gameId,
},
},
);
// Stryker restore all
// Records a play-page-view activity row (see issue #291). The backend
// silently no-ops unless this game is linked to a course and the current
// user's email matches a student on that course's roster, so it's safe to
// fire on every navigation here without checking eligibility client-side.
// Only PlayPage does this - AdminViewPlayPage (an admin emulating a
// farmer's view) must not count as a visit.
// Stryker disable all: a background tracking call with no observable UI
// outcome to assert on.
const objectToAxiosParamsPageView = () => ({
url: "/api/farmeractivity/pageview",
method: "POST",
params: {
gameId: gameId,
},
});
const mutationPageView = useBackendMutation(objectToAxiosParamsPageView);
useEffect(() => {
if (gameId) {
mutationPageView.mutate();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [gameId]);
// Stryker restore all
const gamePlusExists = !(typeof gamePlus == "undefined");
let gameforuser;
let matched;
if (gamePlusExists) {
gameforuser = currentUser.root.user.game;
matched = gameforuser.some((com) => com.id === gamePlus.game.id);
}
const farmerErrorResponse = farmerError?.response ?? {};
const farmerErrorData = farmerErrorResponse.data ?? {};
const deniedAccess =
farmerErrorResponse.status === 403 &&
farmerErrorData.type === "NotEnrolledInCourseAssociatedWithGameException";
const allowed =
gamePlusExists && matched && !gamePlus.game.hidden && !deniedAccess;
const notallowed = gamePlusExists && !matched && !deniedAccess;
const hidden = gamePlusExists && gamePlus.game.hidden;
const herdsQueryKey = [`/api/herds/forcurrentuser?gameId=${gameId}`];
const {
data: herds,
isLoading: herdsLoading,
isError: herdsError,
refetch: refetchHerds,
} = useBackend(
herdsQueryKey,
{
// Stryker disable next-line StringLiteral: Axios treats an empty method as GET too.
method: "GET",
url: "/api/herds/forcurrentuser",
params: { gameId },
},
undefined,
{
enabled: Boolean(allowed && farmer),
retry: false,
// Keep daily health updates visible while the play page is open.
refetchInterval: PLAY_PAGE_REFRESH_MS,
},
);
const refreshHerds = () => queryClient.invalidateQueries(herdsQueryKey);
// Stryker disable all
const { data: farmerProfits } = useBackend(
[`/api/profits/all/gameid?gameId=${gameId}`],
{
method: "GET",
url: "/api/profits/all/gameid",
params: {
gameId: gameId,
},
},
);
// Stryker restore all
// Stryker disable all
const objectToAxiosParamsBuy = (newFarmer) => ({
url: "/api/farmer/buy",
method: "PUT",
data: newFarmer,
params: {
gameId: gameId,
numCows: numCows,
},
});
// Stryker restore all
// Stryker disable all
const mutationbuy = useBackendMutation(
objectToAxiosParamsBuy,
{ onSuccess: refreshHerds },
[`/api/farmer/forcurrentuser?gameId=${gameId}`],
);
// Stryker restore all
const onBuy = (farmer, numCows) => {
mutationbuy.mutate(farmer, numCows);
};
// Stryker disable all
// herdCounts maps each herd id to the number of cows to sell from it.
const objectToAxiosParamsSell = (herdCounts) => ({
url: "/api/farmer/sell",
method: "PUT",
data: herdCounts,
params: {
gameId: gameId,
},
});
// Stryker restore all
// Stryker disable all
const mutationsell = useBackendMutation(
objectToAxiosParamsSell,
{ onSuccess: refreshHerds },
[`/api/farmer/forcurrentuser?gameId=${gameId}`],
);
// Stryker restore all
const onSell = (herdCounts) => {
mutationsell.mutate(herdCounts);
};
const [isChatOpen, setIsChatOpen] = useState(false);
const toggleChatWindow = () => {
setIsChatOpen((prevState) => !prevState);
};
const chatButtonStyle = {
width: "60px",
height: "60px",
borderRadius: "25%",
backgroundColor: "lightblue",
color: "black",
position: "fixed",
bottom: "30px",
right: "30px",
};
const chatContainerStyle = {
width: "550px",
position: "fixed",
bottom: "100px",
right: "20px",
};
const emojiStyle = {
fontFamily: "Arial, sans-serif",
fontSize: "30px",
};
return (
<div
style={{
// The picture keeps its size at the top of the page; as the herd list
// grows, the page below it is filled with the same green as the grass
// at the bottom of the picture instead of zooming the picture in.
backgroundColor: PLAY_PAGE_GRASS_GREEN,
backgroundImage: `url(${getPlayPageBackground(farmer?.cowHealth)})`,
backgroundSize: "100%", // full width, height keeps the picture's shape
backgroundRepeat: "no-repeat",
backgroundPosition: "center top",
minHeight: "100vh",
}}
data-testid="playpage-div"
>
<BasicLayout>
<Container>
{!gamePlus && <h1>This game does not exist!</h1>}
{deniedAccess && (
<h1>
You do not currently have access to this Game. Please contact your
instructor if you think this is an error
</h1>
)}
{notallowed && <h1>You have yet to join this game!</h1>}
{hidden && (
<h1>This game has been hidden by the site administrator.</h1>
)}
{allowed && !!currentUser && <GamePlay currentUser={currentUser} />}
{allowed && !!gamePlus && (
<GameOverview
gamePlus={gamePlus}
currentUser={currentUser}
currentAnnouncements={currentAnnouncements}
/>
)}
<br />
{allowed && !!farmer && !!gamePlus && (
<CardGroup>
<ManageCows
farmer={farmer}
game={gamePlus.game}
setMessage={setMessage}
openModal={openModal}
/>
<FarmStats farmer={farmer} />
<Profits farmer={farmer} profits={farmerProfits} />
<ManageCowsModal
number={numCows}
setNumber={setNumCows}
farmer={farmer}
game={gamePlus.game}
isOpen={isModalOpen && message === "buy"}
message={message}
onClose={closeModal}
onBuy={onBuy}
/>
<SellHerdsModal
isOpen={isModalOpen && message === "sell"}
onClose={closeModal}
herds={herds}
game={gamePlus.game}
onSell={onSell}
/>
</CardGroup>
)}
{allowed && !!farmer && (
<HerdList
herds={herds}
isLoading={herdsLoading}
isError={herdsError}
onRetry={refetchHerds}
/>
)}
</Container>
</BasicLayout>
{(hasRole(currentUser, "ROLE_ADMIN") ||
(allowed && !!gamePlus && gamePlus.game.showChat)) && (
<div style={chatContainerStyle} data-testid="playpage-chat-div">
{!!isChatOpen && <ChatPanel gameId={gameId} />}
<Button
style={chatButtonStyle}
onClick={toggleChatWindow}
data-testid="playpage-chat-toggle"
>
{isChatOpen ? (
<span style={emojiStyle} data-testid="close-icon">
❌
</span>
) : (
<span style={emojiStyle} data-testid="message-icon">
💬
</span>
)}
</Button>
</div>
)}
</div>
);
}
|