Skip to content

Commit

Permalink
Better theme resolution, more MUI and SEASONAL THEMES
Browse files Browse the repository at this point in the history
  • Loading branch information
romw314 committed Dec 5, 2023
1 parent 52803cf commit 6c749af
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 13 deletions.
20 changes: 15 additions & 5 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { useState, useEffect, useContext } from 'react';
import { Helmet, HelmetProvider } from 'react-helmet-async';
import { Button, Dialog, DialogTitle, DialogContent, DialogContentText, DialogActions } from '@mui/material';
import { Theme, getImage } from './Theme';
import { Button, Dialog, DialogTitle, DialogContent, DialogContentText, DialogActions, Box } from '@mui/material';
import { Theme, getImage, checkThemeAvailability } from './Theme';
import * as gameLogic from './gameLogic.js';
import styles from './App.module.css';
import { DebugRunContext, getLg } from './DebugRunContext';
import clone from 'just-clone';
import Columns from 'react-columns';
import NavBar from './NavBar';
import { useNavigate, useSearchParams } from 'react-router-dom';
import themes from './themes.json';

function Square({ currentMove, value, onSquareClick, selectedPiece, index, theme, squares, testid }) {
const isDark = ((Math.floor(index / 8) % 2) !== (index % 2));
Expand Down Expand Up @@ -145,7 +146,7 @@ function Board({ theme, data, onPlay, onRevert }) {
window.chess.advanced = data;

return (
<div className={styles.board} style={theme.board.style}>
<Box sx={{ flexGrow: 1, boxShadow: 2 }} className={styles.board} style={theme.board.style}>
<BoardRow index='0' onSquareClick={handleClick} squares={squares} selectedPiece={selectedPiece} theme={theme} move={data.currentMove} />
<BoardRow index='1' onSquareClick={handleClick} squares={squares} selectedPiece={selectedPiece} theme={theme} move={data.currentMove} />
<BoardRow index='2' onSquareClick={handleClick} squares={squares} selectedPiece={selectedPiece} theme={theme} move={data.currentMove} />
Expand All @@ -154,7 +155,7 @@ function Board({ theme, data, onPlay, onRevert }) {
<BoardRow index='5' onSquareClick={handleClick} squares={squares} selectedPiece={selectedPiece} theme={theme} move={data.currentMove} />
<BoardRow index='6' onSquareClick={handleClick} squares={squares} selectedPiece={selectedPiece} theme={theme} move={data.currentMove} />
<BoardRow index='7' onSquareClick={handleClick} squares={squares} selectedPiece={selectedPiece} theme={theme} move={data.currentMove} />
</div>
</Box>
);
}

Expand Down Expand Up @@ -372,7 +373,16 @@ class setupData {

function ThemedApp() {
const [params] = useSearchParams();
return <App setupData={new setupData(Theme(params.get('theme') ?? 'dark', getLg))} navigate={useNavigate()} />;
const theme = params.get('theme') ?? 'dark';
const navigate = useNavigate();
if (checkThemeAvailability(themes.themes[theme]) === false)
return (
<>
<NavBar onNavigate={navigate} />
<span>Sorry, this theme is not available</span>
</>
);
return <App setupData={new setupData(Theme(theme, getLg))} navigate={navigate} />;
}

function createHistoryObject(squares) {
Expand Down
18 changes: 14 additions & 4 deletions src/Home.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useNavigate, NavLink, Outlet } from 'react-router-dom';
import { useNavigate, Link, Outlet } from 'react-router-dom';
import styles from './Home.module.css';
import NavBar from './NavBar';
import themes from './themes.json';
import { checkThemeAvailability } from './Theme';

function Home() {
const navigate = useNavigate();
Expand All @@ -21,16 +22,25 @@ function HomePage() {
return (
<>
<p>Chess No. 25 is a simple and open source chess game for two players.</p>
<p>Chess No. 25 has multiple themes available. <NavLink to="/themes">Try them now!</NavLink></p>
<p>Chess No. 25 has multiple themes available. <Link to="/themes">Try them now!</Link></p>
<p>If you found a bug or have an idea, please <a href="https://github.com/romw314/chess-no-25/issues/new/choose">report it on GitHub</a>.</p>
</>
);
}

function ThemesPage() {
let themelist = [];
for (const theme in themes.themes)
themelist.push(<li><NavLink to={`/play/?theme=${theme}`}>{themes.themes[theme].fullName || theme}</NavLink></li>);
for (const theme in themes.themes) {
const available = checkThemeAvailability(themes.themes[theme]);
if (available === false) {
themelist.push(<li key={theme} style={{ color: 'grey' }}>{themes.themes[theme].fullName ?? theme} (not available)</li>);
continue;
}
let style = {};
if (available) // empty string '' if falsy
style.color = 'green';
themelist.push(<li className={styles.themeLink} key={theme}><Link to={`/play/?theme=${theme}`} style={style}>{themes.themes[theme].fullName ?? theme}</Link><strong>{available}</strong></li>);
}
return (
<>
<p>Please choose a theme:</p>
Expand Down
9 changes: 9 additions & 0 deletions src/Home.module.css
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
.home {
}

.themeLink a {
text-decoration: none;
color: blue;
}

.themeLink:hover a {
text-decoration: underline;
}
2 changes: 1 addition & 1 deletion src/NavBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function NavButton({ to, href, children }) {

function NavBar({ onNavigate, children }) {
return (
<Box sx={{ flexGrow: 1 }}>
<Box sx={{ flexGrow: 1, boxShadow: 112 }}>
<AppBar position="sticky">
<Toolbar>
<Typography variant="h6" component="div" sx={{ flexGrow: 1 }}>
Expand Down
27 changes: 26 additions & 1 deletion src/Theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,30 @@ function Theme(themeId, logrun) {
return result;
}

export { Theme, getImage, pieceIs };
function checkThemeAvailability(theme) {
if (!theme)
return false;
const av = theme.availability ?? { always: true };
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
const now = new Date();
if (av.monthSeason)
for (const month of av.monthSeason)
if (months.includes(month) && new Date(`0 ${month}`).getMonth() === now.getMonth()) {
const joinWithCommas = av.monthSeason.slice();
const last = joinWithCommas.pop();
return ` - Special theme: available only in ${joinWithCommas.join(', ')}${joinWithCommas.length ? ' and ' : ''}${last}`;
}
if (av.daySeason)
for (const dateString of av.dateSeason) {
const dateStringParts = dateString.split(' ');
if (dateStringParts.length !== 2 || isNaN(Number(dateStringParts[0])) || !months.includes(dateStringParts[1]))
continue;
const date = new Date(dateString);
if (date.getDay() === now.getDay() && date.getMonth === now.getMonth())
return ` - Special theme: available only on ${dateString}`;
}
return av.always ? '' : false;
}

export { Theme, getImage, pieceIs, checkThemeAvailability };
export default ThemeData;
4 changes: 2 additions & 2 deletions src/themes.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@
},
"christmas": {
"fullName": "Christmas",
"avalability": {
"availability": {
"monthSeason": [
12
"December"
]
},
"bodyStyle": {
Expand Down

1 comment on commit 6c749af

@vercel
Copy link

@vercel vercel bot commented on 6c749af Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

chess-no-25-docs – ./

chess-no-25-docs-romw314.vercel.app
chess-no-25-docs-git-master-romw314.vercel.app
chess-no-25-docs.vercel.app

Please sign in to comment.