Cleanup & Confetti ❤️

This commit is contained in:
oitsjustjose 2023-01-17 17:22:03 -05:00
commit 2db581d9e7
7 changed files with 138 additions and 63 deletions

View file

@ -4,6 +4,7 @@
"private": true, "private": true,
"dependencies": { "dependencies": {
"react": "^18.2.0", "react": "^18.2.0",
"react-confetti": "^6.1.0",
"react-dom": "^18.2.0", "react-dom": "^18.2.0",
"react-router-dom": "^6.3.0", "react-router-dom": "^6.3.0",
"react-scripts": "5.0.1", "react-scripts": "5.0.1",

View file

@ -1,4 +1,6 @@
import React from 'react'; import React from 'react';
import Routes from './Routes'; import Routes from './Routes';
export const GridSize = 7;
export default () => (<Routes />); export default () => (<Routes />);

View file

@ -1,4 +1,7 @@
/* eslint-disable no-plusplus */
import GetWeapons from './weapons/Weapons'; import GetWeapons from './weapons/Weapons';
import { GridSize } from '../App';
import Freespace from './weapons/freespaec.png';
export const Shuffle = (arr) => { export const Shuffle = (arr) => {
const arrayIndices = new Set(); const arrayIndices = new Set();
@ -11,18 +14,44 @@ export const Shuffle = (arr) => {
}; };
export const LoadDataOrFallback = () => { export const LoadDataOrFallback = () => {
const stored = window.localStorage.getItem('splatoon_card'); const stored = window.localStorage.getItem('splingo_state');
return stored ? JSON.parse(stored) : Shuffle(GetWeapons()).map((x) => ({ if (stored) {
src: x, // Need to convert obj to 2d arr
hit: false, const as2d = [];
})); Object.values(JSON.parse(stored)).forEach((x) => as2d.push(x));
return as2d;
}
const as2dArray = [];
const weaponArray = Shuffle(GetWeapons()).slice(0, GridSize * GridSize);
const midSize = Math.floor(GridSize / 2);
let idx = 0;
for (let row = 0; row < GridSize; row++) {
const column = [];
for (let col = 0; col < GridSize; col++) {
column.push({
src: (row === midSize && col === midSize)
? Freespace : weaponArray[idx],
hit: false,
});
idx++;
}
as2dArray[row] = column;
}
return as2dArray;
}; };
export const SaveDataToAppStorage = (bingoCard) => { export const SaveDataToAppStorage = (data) => {
window.localStorage.setItem('splatoon_card', JSON.stringify(bingoCard)); const asObj = {};
data.forEach((row, idx) => {
asObj[idx] = row;
});
window.localStorage.setItem('splingo_state', JSON.stringify(asObj));
}; };
export const ClearSavedData = () => { export const ClearSavedData = () => {
window.localStorage.removeItem('splatoon_card'); window.localStorage.removeItem('splingo_state');
window.location.reload(); window.location.reload();
}; };

View file

@ -1,82 +1,111 @@
/* eslint-disable no-plusplus */ /* eslint-disable no-plusplus */
import React from 'react'; import React, { useState } from 'react';
import Confetti from 'react-confetti';
import { Button, Popup } from 'semantic-ui-react'; import { Button, Popup } from 'semantic-ui-react';
import { GridSize } from '../App';
import BingoSlot from '../components/BingoSlot'; import BingoSlot from '../components/BingoSlot';
import { LoadDataOrFallback, SaveDataToAppStorage, ClearSavedData } from '../data/Loader'; import { LoadDataOrFallback, SaveDataToAppStorage, ClearSavedData } from '../data/Loader';
import Freespace from '../data/weapons/freespaec.png';
const CheckForBingpot = (data /* {src: img, hit: bool}[] */) => {
let bingpot = true;
let rowIdx = 0;
let colIdx = 0;
for (rowIdx = 0; rowIdx < GridSize; rowIdx++) {
bingpot = true;
for (colIdx = 0; colIdx < GridSize; colIdx++) {
if (!data[rowIdx][colIdx].hit) {
bingpot = false;
break;
}
}
if (bingpot) return true;
}
for (colIdx = 0; colIdx < GridSize; colIdx++) {
bingpot = true;
for (rowIdx = 0; rowIdx < GridSize; rowIdx++) {
if (!data[rowIdx][colIdx].hit) {
bingpot = false;
break;
}
}
if (bingpot) return true;
}
bingpot = true;
rowIdx = 0;
colIdx = 0;
while (rowIdx < GridSize && colIdx < GridSize) {
if (!data[rowIdx][colIdx].hit) {
bingpot = false;
break;
}
rowIdx++;
colIdx++;
}
if (bingpot) return true;
bingpot = true;
rowIdx = 0;
colIdx = (GridSize - 1);
while (rowIdx < GridSize) {
if (!data[rowIdx][colIdx].hit) {
bingpot = false;
break;
}
rowIdx++;
colIdx--;
}
return bingpot;
};
export default () => { export default () => {
const [showConfetti, setShowConfetti] = useState(false);
const data = LoadDataOrFallback(); const data = LoadDataOrFallback();
SaveDataToAppStorage(data); SaveDataToAppStorage(data);
const boardDims = {
width: 7, height: 7,
};
const bingoSlots = data.map((x, idx) => (
<BingoSlot
src={x.src}
hitIn={x.hit}
// eslint-disable-next-line react/no-array-index-key
key={idx}
propagateChanges={(newState) => {
data[idx].hit = newState;
SaveDataToAppStorage(data);
}}
/>
));
let idx = 0;
const rows = []; const rows = [];
data.forEach((row, rowIdx) => {
for (let row = 1; row <= boardDims.height; row++) { rows.push(
const cols = []; <div style={{ display: 'flex', flexDirection: 'row', justifyContent: 'space-evenly' }}>
for (let col = 1; col <= boardDims.width; col++) { {row.map((x, colIdx) => (
if (!(row === Math.round(boardDims.height / 2)
&& col === Math.round(boardDims.width / 2))) {
cols.push(bingoSlots[idx]);
idx++;
} else {
// Special case for the freespace, which lives at the end of the data array
cols.push(
<BingoSlot <BingoSlot
src={Freespace} src={x.src}
hitIn={data[data.length - 1].hit} hitIn={x.hit}
key="freespaec" // eslint-disable-next-line react/no-array-index-key
key={colIdx}
propagateChanges={(newState) => { propagateChanges={(newState) => {
data[data.length - 1].hit = newState; data[rowIdx][colIdx].hit = newState;
SaveDataToAppStorage(data); SaveDataToAppStorage(data);
if (CheckForBingpot(data)) {
setShowConfetti(true);
// setTimeout(() => setShowConfetti(false), 3000);
}
}} }}
/>, />
); ))}
// cols.push(<Blank />); </div>,
}
}
const tblRow = (
<div
key={row}
style={{
display: 'flex',
flexDirection: 'row',
justifyContent: 'space-evenly',
}}
>
{cols}
</div>
); );
rows.push(tblRow); });
}
return ( return (
<div> <div>
<Confetti
run={showConfetti}
recycle={false}
colors={['#ffd300', '#88e4e1', '#ff2c5f', '#fe8102', '#47e4af', '#008ec0']}
/>
<div style={{ display: '2rem auto', textAlign: 'center' }}> <div style={{ display: '2rem auto', textAlign: 'center' }}>
<img <img
src="/img/title.png" src="/img/title.png"
alt="title" alt="title"
style={{ style={{
userSelect: 'none', userSelect: 'none',
// display: 'block',
textAlign: 'center', textAlign: 'center',
maxWidth: '768px', maxWidth: '768px',
width: '50vw', width: '50vw',
@ -104,6 +133,7 @@ export default () => {
<Popup <Popup
flowing flowing
hoverable hoverable
position="top left"
trigger={( trigger={(
<Button <Button
color="red" color="red"
@ -126,6 +156,7 @@ export default () => {
<Popup <Popup
flowing flowing
hoverable hoverable
position="top right"
trigger={( trigger={(
<Button <Button
style={{ style={{

View file

@ -7260,6 +7260,13 @@ react-app-polyfill@^3.0.0:
regenerator-runtime "^0.13.9" regenerator-runtime "^0.13.9"
whatwg-fetch "^3.6.2" whatwg-fetch "^3.6.2"
react-confetti@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/react-confetti/-/react-confetti-6.1.0.tgz#03dc4340d955acd10b174dbf301f374a06e29ce6"
integrity sha512-7Ypx4vz0+g8ECVxr88W9zhcQpbeujJAVqL14ZnXJ3I23mOI9/oBVTQ3dkJhUmB0D6XOtCZEM6N0Gm9PMngkORw==
dependencies:
tween-functions "^1.2.0"
react-dev-utils@^12.0.1: react-dev-utils@^12.0.1:
version "12.0.1" version "12.0.1"
resolved "https://registry.yarnpkg.com/react-dev-utils/-/react-dev-utils-12.0.1.tgz#ba92edb4a1f379bd46ccd6bcd4e7bc398df33e73" resolved "https://registry.yarnpkg.com/react-dev-utils/-/react-dev-utils-12.0.1.tgz#ba92edb4a1f379bd46ccd6bcd4e7bc398df33e73"
@ -8424,6 +8431,11 @@ tsutils@^3.21.0:
dependencies: dependencies:
tslib "^1.8.1" tslib "^1.8.1"
tween-functions@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/tween-functions/-/tween-functions-1.2.0.tgz#1ae3a50e7c60bb3def774eac707acbca73bbc3ff"
integrity sha512-PZBtLYcCLtEcjL14Fzb1gSxPBeL7nWvGhO5ZFPGqziCcr8uvHp0NDmdjBchp6KHL+tExcg0m3NISmKxhU394dA==
type-check@^0.4.0, type-check@~0.4.0: type-check@^0.4.0, type-check@~0.4.0:
version "0.4.0" version "0.4.0"
resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.4.0.tgz#07b8203bfa7056c0657050e3ccd2c37730bab8f1" resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.4.0.tgz#07b8203bfa7056c0657050e3ccd2c37730bab8f1"