From 2db581d9e755b6532bd37868b4a0a657f291e5ff Mon Sep 17 00:00:00 2001 From: oitsjustjose Date: Tue, 17 Jan 2023 17:22:03 -0500 Subject: [PATCH] =?UTF-8?q?Cleanup=20&=20Confetti=20=E2=9D=A4=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/App.jsx | 2 + src/data/Loader.js | 45 ++++-- src/data/splats/{Splats.jsx => Splats.js} | 0 src/data/weapons/{Weapons.jsx => Weapons.js} | 0 src/views/Home.jsx | 141 +++++++++++-------- yarn.lock | 12 ++ 7 files changed, 138 insertions(+), 63 deletions(-) rename src/data/splats/{Splats.jsx => Splats.js} (100%) rename src/data/weapons/{Weapons.jsx => Weapons.js} (100%) diff --git a/package.json b/package.json index a4d69ff..0e0282a 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "private": true, "dependencies": { "react": "^18.2.0", + "react-confetti": "^6.1.0", "react-dom": "^18.2.0", "react-router-dom": "^6.3.0", "react-scripts": "5.0.1", diff --git a/src/App.jsx b/src/App.jsx index fb330b1..0876980 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,4 +1,6 @@ import React from 'react'; import Routes from './Routes'; +export const GridSize = 7; + export default () => (); diff --git a/src/data/Loader.js b/src/data/Loader.js index 2681307..4bb22c2 100644 --- a/src/data/Loader.js +++ b/src/data/Loader.js @@ -1,4 +1,7 @@ +/* eslint-disable no-plusplus */ import GetWeapons from './weapons/Weapons'; +import { GridSize } from '../App'; +import Freespace from './weapons/freespaec.png'; export const Shuffle = (arr) => { const arrayIndices = new Set(); @@ -11,18 +14,44 @@ export const Shuffle = (arr) => { }; export const LoadDataOrFallback = () => { - const stored = window.localStorage.getItem('splatoon_card'); - return stored ? JSON.parse(stored) : Shuffle(GetWeapons()).map((x) => ({ - src: x, - hit: false, - })); + const stored = window.localStorage.getItem('splingo_state'); + if (stored) { + // Need to convert obj to 2d arr + 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) => { - window.localStorage.setItem('splatoon_card', JSON.stringify(bingoCard)); +export const SaveDataToAppStorage = (data) => { + const asObj = {}; + data.forEach((row, idx) => { + asObj[idx] = row; + }); + window.localStorage.setItem('splingo_state', JSON.stringify(asObj)); }; export const ClearSavedData = () => { - window.localStorage.removeItem('splatoon_card'); + window.localStorage.removeItem('splingo_state'); window.location.reload(); }; diff --git a/src/data/splats/Splats.jsx b/src/data/splats/Splats.js similarity index 100% rename from src/data/splats/Splats.jsx rename to src/data/splats/Splats.js diff --git a/src/data/weapons/Weapons.jsx b/src/data/weapons/Weapons.js similarity index 100% rename from src/data/weapons/Weapons.jsx rename to src/data/weapons/Weapons.js diff --git a/src/views/Home.jsx b/src/views/Home.jsx index 24f5f61..f8c3e95 100644 --- a/src/views/Home.jsx +++ b/src/views/Home.jsx @@ -1,82 +1,111 @@ /* 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 { GridSize } from '../App'; import BingoSlot from '../components/BingoSlot'; 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 () => { + const [showConfetti, setShowConfetti] = useState(false); + const data = LoadDataOrFallback(); SaveDataToAppStorage(data); - const boardDims = { - width: 7, height: 7, - }; - - const bingoSlots = data.map((x, idx) => ( - { - data[idx].hit = newState; - SaveDataToAppStorage(data); - }} - /> - )); - - let idx = 0; const rows = []; - - for (let row = 1; row <= boardDims.height; row++) { - const cols = []; - for (let col = 1; col <= boardDims.width; col++) { - 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( + data.forEach((row, rowIdx) => { + rows.push( +
+ {row.map((x, colIdx) => ( { - data[data.length - 1].hit = newState; + data[rowIdx][colIdx].hit = newState; SaveDataToAppStorage(data); + if (CheckForBingpot(data)) { + setShowConfetti(true); + // setTimeout(() => setShowConfetti(false), 3000); + } }} - />, - ); - // cols.push(); - } - } - - const tblRow = ( -
- {cols} -
+ /> + ))} +
, ); - rows.push(tblRow); - } + }); return (
+ +
title { {