Skip to content

Commit e667193

Browse files
authored
Update alphabet.html
Bugfix for mobile
1 parent ad2c819 commit e667193

File tree

1 file changed

+55
-57
lines changed

1 file changed

+55
-57
lines changed

games/alphabet.html

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -493,13 +493,7 @@
493493
}
494494
}
495495

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

504498
.mobile-keyboard-note {
505499
display: none
@@ -711,7 +705,8 @@ <h3 class="text-lg font-medium mb-2">Letter Performance</h3>
711705
const tabButtons = document.querySelectorAll('.tab-button');
712706
const tabContents = document.querySelectorAll('.tab-content');
713707
const mobileInputEl = document.getElementById('mobile-input');
714-
708+
const lastKeyPressed = {};
709+
715710
function isMobileDevice() {
716711
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) || window.innerWidth < 768;
717712
}
@@ -1144,7 +1139,18 @@ <h3 class="text-lg font-medium mb-2">Letter Performance</h3>
11441139
}
11451140

11461141
function handleKeyPress(event) {
1147-
const key = event.key.toLowerCase();
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) {
11481154
if (key === 'a' && currentState !== GameState.PLAYING) {
11491155
document.querySelectorAll('.letter')[0].classList.remove('current');
11501156
currentIndex = 1;
@@ -1188,57 +1194,49 @@ <h3 class="text-lg font-medium mb-2">Letter Performance</h3>
11881194
}
11891195
}
11901196

1191-
function handleMobileInput(event) {
1192-
const input = event.target;
1193-
const lastChar = input.value.slice(-1).toLowerCase();
1194-
if (lastChar) {
1195-
handleKeyboardInput(lastChar);
1196-
input.value = '';
1197-
}
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;
11981215
}
1199-
1200-
function handleKeyboardInput(key) {
1201-
if (key === 'a' && currentState !== GameState.PLAYING) {
1202-
document.querySelectorAll('.letter')[0].classList.remove('current');
1203-
currentIndex = 1;
1204-
attempts++;
1205-
localStorage.setItem("attempts", attempts);
1206-
attemptsEl.textContent = attempts;
1207-
updateGameState(GameState.PLAYING);
1208-
startTime = new Date().getTime();
1209-
letterTimestamps[0] = startTime;
1210-
letterTimestamps[1] = startTime;
1211-
const letterEls = document.querySelectorAll('.letter');
1212-
letterEls[0].classList.add('text-green-600');
1213-
const progress = (currentIndex / alphabet.length) * 100;
1214-
updateProgress(progress);
1215-
highlightCurrentLetter();
1216-
return;
1217-
}
1218-
if (currentState !== GameState.PLAYING) return;
1219-
if (key === alphabet[currentIndex]) {
1220-
const letterEls = document.querySelectorAll('.letter');
1221-
letterEls[currentIndex].classList.add('letter-typed');
1222-
currentIndex++;
1223-
letterTimestamps[currentIndex] = new Date().getTime();
1224-
const progress = (currentIndex / alphabet.length) * 100;
1225-
updateProgress(progress);
1226-
if (currentIndex === alphabet.length) {
1227-
const finalTime = new Date().getTime() - startTime;
1228-
resultEl.textContent = `${finalTime.toLocaleString()}ms`;
1229-
const isNewHighScore = updateHighScore(finalTime);
1230-
if (isNewHighScore) {
1231-
gameMessageEl.textContent = "New high score! Press 'A' to try again";
1232-
}
1233-
updateGameState(GameState.COMPLETED);
1234-
} else {
1235-
highlightCurrentLetter();
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";
12361230
}
1231+
updateGameState(GameState.COMPLETED);
12371232
} else {
1238-
const letterEls = document.querySelectorAll('.letter');
1239-
letterEls[currentIndex].classList.add('text-red-600');
1240-
updateGameState(GameState.ERROR);
1233+
highlightCurrentLetter();
12411234
}
1235+
} else {
1236+
const letterEls = document.querySelectorAll('.letter');
1237+
letterEls[currentIndex].classList.add('text-red-600');
1238+
updateGameState(GameState.ERROR);
1239+
}
12421240
}
12431241

12441242
window.addEventListener("resize", () => {
@@ -1286,4 +1284,4 @@ <h3 class="text-lg font-medium mb-2">Letter Performance</h3>
12861284
</script>
12871285
<script src="../sidebar.js"></script>
12881286
</body>
1289-
</html>
1287+
</html>

0 commit comments

Comments
 (0)