Skip to content

Commit 9b607bc

Browse files
committed
Add difficulty levels
1 parent 31cac16 commit 9b607bc

File tree

6 files changed

+93
-22
lines changed

6 files changed

+93
-22
lines changed

src/App.css

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,33 @@ body {
105105
position: absolute;
106106
top: 0;
107107
left: 0;
108-
padding: 30px;
108+
right: 0;
109+
display: grid;
110+
grid-auto-flow: column;
111+
padding-left: 20px;
112+
padding-right: 20px;
113+
color: black;
114+
h1 {
115+
margin: 0;
116+
margin-top: 5px;
117+
font-size: 24px;
118+
}
119+
& > div:first-child {
120+
text-align: left;
121+
align-self: center;
122+
}
123+
& > div:last-child {
124+
text-align: right;
125+
align-self: center;
126+
}
127+
}
128+
129+
.modal--intro {
130+
label {
131+
display: block;
132+
margin-bottom: 5px;
133+
}
134+
button {
135+
margin-top: 15px;
136+
}
109137
}

src/App.tsx

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,24 @@ const countriesArray = land.features;
1616

1717
const sortedCountries = weightedShuffle(Object.entries(weights));
1818

19-
const countries = sortedCountries.map(([countryCode]) => {
19+
const rawCountries = sortedCountries.map(([countryCode]) => {
2020
return countriesArray.find((item) => item.properties.isocode === countryCode);
2121
});
2222

23-
const initialRotation = geoCentroid(countries[0]);
23+
const initialRotation = geoCentroid(rawCountries[0]);
2424

25-
const numCountries = sortedCountries.length;
25+
const countriesByDifficulty = {
26+
easy: rawCountries.filter(
27+
(countryItem) => weights[countryItem.properties.isocode] >= 0.8,
28+
),
29+
medium: rawCountries.filter(
30+
(countryItem) => weights[countryItem.properties.isocode] >= 0.5,
31+
),
32+
hard: rawCountries.filter(
33+
(countryItem) => weights[countryItem.properties.isocode] >= 0.3,
34+
),
35+
veryHard: [...rawCountries],
36+
};
2637

2738
export default function App() {
2839
const [correctGuesses, setCorrectGuesses] = useState(0);
@@ -31,6 +42,7 @@ export default function App() {
3142
const [countryIndex, setCountryIndex] = useState(0);
3243
const [showAnswer, setShowAnswer] = useState<string | null>(null);
3344
const [showGameOver, setShowGameOver] = useState<boolean | null>(null);
45+
const [countries, setCountries] = useState(countriesByDifficulty.hard);
3446
const [rotation, setRotation] = useState([
3547
-initialRotation[0],
3648
-initialRotation[1],
@@ -39,7 +51,7 @@ export default function App() {
3951
const country = countries[countryIndex];
4052

4153
const updateSelectedCountry = useCallback(() => {
42-
if (countryIndex + 1 < numCountries) {
54+
if (countryIndex + 1 < countries.length) {
4355
const newIndex = countryIndex + 1;
4456
setCountryIndex(newIndex);
4557
const newRotation = geoCentroid(countries[newIndex]);
@@ -66,8 +78,11 @@ export default function App() {
6678
}
6779
}
6880

69-
function onStart() {
81+
function onStart(difficulty) {
7082
setShowGameOver(false);
83+
setCountries(countriesByDifficulty[difficulty]);
84+
const newRotation = geoCentroid(countriesByDifficulty[difficulty][0]);
85+
setRotation([-newRotation[0], -newRotation[1]]);
7186
}
7287

7388
useLayoutEffect(() => {
@@ -84,14 +99,17 @@ export default function App() {
8499
/>
85100
<div className="ui">
86101
<div>
87-
Round: {countryIndex + 1}/{numCountries}
102+
Round: {countryIndex + 1}/{countries.length}
88103
</div>
89104
<div>
105+
<h1>Country Guesser</h1>
90106
<Timer gameRunning={showGameOver === false} />
91107
</div>
92-
<div>Streak: {streak}</div>
93-
<div>Correct: {correctGuesses}</div>
94-
<div>Incorrect: {incorrectGuesses}</div>
108+
<div>
109+
<div>Streak: {streak}</div>
110+
<div>Correct: {correctGuesses}</div>
111+
<div>Incorrect: {incorrectGuesses}</div>
112+
</div>
95113
</div>
96114
<div className="overlay">
97115
{showGameOver ? (

src/IntroModal.tsx

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,49 @@
11
import NiceModal, { useModal } from '@ebay/nice-modal-react';
22
import { Dialog } from '@reach/dialog';
3+
import { weights } from './weights';
4+
import { useState } from 'react';
5+
6+
const weightValues = Object.values(weights);
7+
const easyCountries = weightValues.filter((weight) => weight >= 0.8).length;
8+
const mediumCountries = weightValues.filter((weight) => weight >= 0.5).length;
9+
const hardCountries = weightValues.filter((weight) => weight >= 0.3).length;
10+
const veryHardCountries = weightValues.length;
311

412
export const IntroModal = NiceModal.create(({ onStart }) => {
513
const modal = useModal();
614

7-
function startGame() {
8-
onStart();
15+
function startGame(event) {
16+
event.preventDefault();
17+
const data = new FormData(event.target);
18+
onStart(data.get('difficulty'));
919
modal.hide();
1020
}
1121

1222
return (
1323
<Dialog isOpen={modal.visible} onDismiss={modal.hide}>
14-
<div className="modal modal--intro">
24+
<form className="modal modal--intro" onSubmit={startGame}>
1525
<h1>Country Guesser</h1>
1626
<h2>How to play</h2>
1727
<p>Type the name of the country highlighted.</p>
18-
<button type="button" onClick={startGame}>
19-
Start
20-
</button>
21-
</div>
28+
<h2>Select difficulty</h2>
29+
<label>
30+
<input type="radio" name="difficulty" value="easy" /> American (
31+
{easyCountries} countries)
32+
</label>
33+
<label>
34+
<input type="radio" name="difficulty" value="medium" /> Tourist (
35+
{mediumCountries} countries)
36+
</label>
37+
<label>
38+
<input type="radio" name="difficulty" value="hard" defaultChecked />{' '}
39+
GeoGuessr enjoyer ({hardCountries} countries)
40+
</label>
41+
<label>
42+
<input type="radio" name="difficulty" value="veryHard" /> Ultra
43+
violence ({veryHardCountries} countries)
44+
</label>
45+
<button type="submit">Start</button>
46+
</form>
2247
</Dialog>
2348
);
2449
});

src/weights.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ export const weights = {
5353

5454
AE: 0.8,
5555
EE: 0.8,
56-
BH: 0.8,
5756
SA: 0.8,
5857
HR: 0.8,
5958
CO: 0.8,
@@ -120,7 +119,6 @@ export const weights = {
120119
UZ: 0.5,
121120
DM: 0.5,
122121
IQ: 0.5,
123-
BZ: 0.5,
124122
CV: 0.5,
125123
GH: 0.5,
126124
MM: 0.5,
@@ -129,7 +127,9 @@ export const weights = {
129127
PK: 0.5,
130128
NG: 0.5,
131129
SD: 0.5,
130+
BH: 0.5,
132131

132+
BZ: 0.4,
133133
BB: 0.4,
134134
BA: 0.4,
135135
PW: 0.4,

world-topo.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

world.json

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)