Skip to content

Commit 2041ea5

Browse files
committed
- fix alphabet game for mobile
1 parent e667193 commit 2041ea5

File tree

1 file changed

+23
-60
lines changed

1 file changed

+23
-60
lines changed

games/alphabet.html

Lines changed: 23 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,14 @@
493493
}
494494
}
495495

496-
#mobile-input{position:absolute;opacity:0;height:0;width:0;top:0;left:0}
496+
#mobile-input {
497+
position: absolute;
498+
opacity: 0;
499+
height: 0;
500+
width: 0;
501+
top: 0;
502+
left: 0
503+
}
497504

498505
.mobile-keyboard-note {
499506
display: none
@@ -682,6 +689,7 @@ <h3 class="text-lg font-medium mb-2">Letter Performance</h3>
682689
let letterTimestamps = [];
683690
let attempts = parseInt(localStorage.getItem("attempts") || "0");
684691
const MAX_HISTORY = 10;
692+
const lastKeyPressed = {};
685693
const alphabetEl = document.getElementById('alphabet');
686694
const resultEl = document.getElementById('result');
687695
const highscoreEl = document.getElementById('highscore');
@@ -705,8 +713,7 @@ <h3 class="text-lg font-medium mb-2">Letter Performance</h3>
705713
const tabButtons = document.querySelectorAll('.tab-button');
706714
const tabContents = document.querySelectorAll('.tab-content');
707715
const mobileInputEl = document.getElementById('mobile-input');
708-
const lastKeyPressed = {};
709-
716+
710717
function isMobileDevice() {
711718
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) || window.innerWidth < 768;
712719
}
@@ -1138,20 +1145,9 @@ <h3 class="text-lg font-medium mb-2">Letter Performance</h3>
11381145
}
11391146
}
11401147

1141-
function handleKeyPress(event) {
1142-
processKeyPress(event.key);
1143-
}
1144-
1145-
function handleMobileInput(event) {
1146-
const inputValue = event.target.value.toLowerCase();
1147-
for(let i = 0; i < inputValue.length; i++) {
1148-
processKeyPress(inputValue[i]);
1149-
}
1150-
mobileInputEl.value = '';
1151-
}
1152-
1153-
function handleKeyboardInput(key) {
1154-
if (key === 'a' && currentState !== GameState.PLAYING) {
1148+
function processKeyPress(key) {
1149+
const lowerKey = key.toLowerCase();
1150+
if (lowerKey === 'a' && currentState !== GameState.PLAYING) {
11551151
document.querySelectorAll('.letter')[0].classList.remove('current');
11561152
currentIndex = 1;
11571153
attempts++;
@@ -1169,7 +1165,7 @@ <h3 class="text-lg font-medium mb-2">Letter Performance</h3>
11691165
return;
11701166
}
11711167
if (currentState !== GameState.PLAYING) return;
1172-
if (key === alphabet[currentIndex]) {
1168+
if (lowerKey === alphabet[currentIndex]) {
11731169
const letterEls = document.querySelectorAll('.letter');
11741170
letterEls[currentIndex].classList.add('letter-typed');
11751171
currentIndex++;
@@ -1194,49 +1190,16 @@ <h3 class="text-lg font-medium mb-2">Letter Performance</h3>
11941190
}
11951191
}
11961192

1197-
function processKeyPress(key) {
1198-
const lowerKey = key.toLowerCase();
1199-
if(lowerKey === 'a' && currentState !== GameState.PLAYING) {
1200-
document.querySelectorAll('.letter')[0].classList.remove('current');
1201-
currentIndex = 1;
1202-
attempts++;
1203-
localStorage.setItem("attempts", attempts);
1204-
attemptsEl.textContent = attempts;
1205-
updateGameState(GameState.PLAYING);
1206-
startTime = new Date().getTime();
1207-
letterTimestamps[0] = startTime;
1208-
letterTimestamps[1] = startTime;
1209-
const letterEls = document.querySelectorAll('.letter');
1210-
letterEls[0].classList.add('text-green-600');
1211-
const progress = (currentIndex / alphabet.length) * 100;
1212-
updateProgress(progress);
1213-
highlightCurrentLetter();
1214-
return;
1193+
function handleKeyPress(event) {
1194+
processKeyPress(event.key);
12151195
}
1216-
if(currentState !== GameState.PLAYING) return;
1217-
if(lowerKey === alphabet[currentIndex]) {
1218-
const letterEls = document.querySelectorAll('.letter');
1219-
letterEls[currentIndex].classList.add('letter-typed');
1220-
currentIndex++;
1221-
letterTimestamps[currentIndex] = new Date().getTime();
1222-
const progress = (currentIndex / alphabet.length) * 100;
1223-
updateProgress(progress);
1224-
if(currentIndex === alphabet.length) {
1225-
const finalTime = new Date().getTime() - startTime;
1226-
resultEl.textContent = `${finalTime.toLocaleString()}ms`;
1227-
const isNewHighScore = updateHighScore(finalTime);
1228-
if(isNewHighScore) {
1229-
gameMessageEl.textContent = "New high score! Press 'A' to try again";
1230-
}
1231-
updateGameState(GameState.COMPLETED);
1232-
} else {
1233-
highlightCurrentLetter();
1196+
1197+
function handleMobileInput(event) {
1198+
const inputValue = event.target.value.toLowerCase();
1199+
for (let i = 0; i < inputValue.length; i++) {
1200+
processKeyPress(inputValue[i]);
12341201
}
1235-
} else {
1236-
const letterEls = document.querySelectorAll('.letter');
1237-
letterEls[currentIndex].classList.add('text-red-600');
1238-
updateGameState(GameState.ERROR);
1239-
}
1202+
mobileInputEl.value = '';
12401203
}
12411204

12421205
window.addEventListener("resize", () => {
@@ -1284,4 +1247,4 @@ <h3 class="text-lg font-medium mb-2">Letter Performance</h3>
12841247
</script>
12851248
<script src="../sidebar.js"></script>
12861249
</body>
1287-
</html>
1250+
</html>

0 commit comments

Comments
 (0)