add keyboard shortcuts

This commit is contained in:
mehbark 2023-03-29 19:42:39 -04:00
parent b8d13d31a0
commit 98f418e095
3 changed files with 32 additions and 5 deletions

View file

@ -133,7 +133,7 @@
></g-chess-board> ></g-chess-board>
<button style="display: none" id="finish">show me the answer please i toootally guessed already</button> <button style="display: none" id="finish">show me the answer please i toootally guessed already</button>
<h1 id="game-over"></h1> <h1 id="game-over"></h1>
<button title="or press `p`" id="play-again">&gt; play again &lt;</button> <button title="or press `r`" id="play-again">&gt; play again &lt;</button>
<pre id="stats"></pre> <pre id="stats"></pre>
<button title="or press `c`" id="copy-stats">^ copy ^</button> <button title="or press `c`" id="copy-stats">^ copy ^</button>
<br> <br>

16
main.js
View file

@ -642,8 +642,12 @@ function show_game_over() {
game_over.innerText = `GAME OVER; player was ${todays_player.name}`; game_over.innerText = `GAME OVER; player was ${todays_player.name}`;
console.log("GAME OVER"); console.log("GAME OVER");
} }
function copy_stats_to_clipboard() {
navigator.clipboard.writeText(document.getElementById("stats").innerText);
}
function show_win() { function show_win() {
wins++; wins++;
in_game_over = true;
let win_count = document.getElementById("win-count"); let win_count = document.getElementById("win-count");
win_count.style.display = "block"; win_count.style.display = "block";
win_count.innerText = `You have won ${wins} time${wins == 1 ? "" : "s"} without refreshing${wins_exclamation_marks()}`; win_count.innerText = `You have won ${wins} time${wins == 1 ? "" : "s"} without refreshing${wins_exclamation_marks()}`;
@ -662,7 +666,7 @@ ${history}`;
stats.innerText = stats_text; stats.innerText = stats_text;
let copy_stats = document.getElementById("copy-stats"); let copy_stats = document.getElementById("copy-stats");
copy_stats.style.display = "block"; copy_stats.style.display = "block";
copy_stats.addEventListener("click", _ => navigator.clipboard.writeText(stats_text)); copy_stats.addEventListener("click", _ => copy_stats_to_clipboard());
let play_again = document.getElementById("play-again"); let play_again = document.getElementById("play-again");
play_again.style.display = "block"; play_again.style.display = "block";
play_again.addEventListener("click", _ => reset()); play_again.addEventListener("click", _ => reset());
@ -708,6 +712,7 @@ function wins_exclamation_marks() {
let wins_divided_by_ten = Math.floor(wins / 10); let wins_divided_by_ten = Math.floor(wins / 10);
return wins_divided_by_ten > 0 ? "!".repeat(wins_divided_by_ten) : "."; return wins_divided_by_ten > 0 ? "!".repeat(wins_divided_by_ten) : ".";
} }
var in_game_over = false;
// ERROR: ! // ERROR: !
// INELEGANT // INELEGANT
// "HACK"! // "HACK"!
@ -715,6 +720,7 @@ function reset() {
let play_again = document.getElementById("play-again"); let play_again = document.getElementById("play-again");
play_again.style.display = "none"; play_again.style.display = "none";
play_again.onclick = _ => console.log("you can't play again again sorry :]"); play_again.onclick = _ => console.log("you can't play again again sorry :]");
in_game_over = false;
todays_player = get_todays_player(); todays_player = get_todays_player();
game = new Chess(); game = new Chess();
board.fen = game.fen(); board.fen = game.fen();
@ -777,5 +783,13 @@ window.addEventListener("DOMContentLoaded", () => {
document document
.getElementById("finish") .getElementById("finish")
.addEventListener("click", _ => show_game_over()); .addEventListener("click", _ => show_game_over());
document.addEventListener("keypress", e => {
if (e.key == "r" && in_game_over) {
reset();
}
else if (e.key == "c" && in_game_over) {
copy_stats_to_clipboard();
}
});
append_players_to(document.getElementById("players")); append_players_to(document.getElementById("players"));
}); });

19
main.ts
View file

@ -777,8 +777,12 @@ function show_game_over() {
console.log("GAME OVER"); console.log("GAME OVER");
} }
function copy_stats_to_clipboard() {
navigator.clipboard.writeText(document.getElementById("stats").innerText);
}
function show_win() { function show_win() {
wins++; wins++;
in_game_over = true;
let win_count = document.getElementById("win-count"); let win_count = document.getElementById("win-count");
win_count.style.display = "block"; win_count.style.display = "block";
win_count.innerText = `You have won ${wins} time${ win_count.innerText = `You have won ${wins} time${
@ -802,9 +806,7 @@ ${history}`;
let copy_stats = document.getElementById("copy-stats"); let copy_stats = document.getElementById("copy-stats");
copy_stats.style.display = "block"; copy_stats.style.display = "block";
copy_stats.addEventListener("click", _ => copy_stats.addEventListener("click", _ => copy_stats_to_clipboard());
navigator.clipboard.writeText(stats_text)
);
let play_again = document.getElementById("play-again"); let play_again = document.getElementById("play-again");
play_again.style.display = "block"; play_again.style.display = "block";
@ -862,6 +864,7 @@ function wins_exclamation_marks() {
return wins_divided_by_ten > 0 ? "!".repeat(wins_divided_by_ten) : "."; return wins_divided_by_ten > 0 ? "!".repeat(wins_divided_by_ten) : ".";
} }
var in_game_over = false;
// ERROR: ! // ERROR: !
// INELEGANT // INELEGANT
// "HACK"! // "HACK"!
@ -871,6 +874,8 @@ function reset() {
play_again.onclick = _ => play_again.onclick = _ =>
console.log("you can't play again again sorry :]"); console.log("you can't play again again sorry :]");
in_game_over = false;
todays_player = get_todays_player(); todays_player = get_todays_player();
game = new Chess(); game = new Chess();
board.fen = game.fen(); board.fen = game.fen();
@ -953,5 +958,13 @@ window.addEventListener("DOMContentLoaded", () => {
.getElementById("finish") .getElementById("finish")
.addEventListener("click", _ => show_game_over()); .addEventListener("click", _ => show_game_over());
document.addEventListener("keypress", e => {
if (e.key == "r" && in_game_over) {
reset();
} else if (e.key == "c" && in_game_over) {
copy_stats_to_clipboard();
}
});
append_players_to(document.getElementById("players")); append_players_to(document.getElementById("players"));
}); });