statki/public/assets/js/landing.js
MaciejkaG a212161733 Major changes
- Improved PWA support on mobile.
- Added Vs. AI game menu.
- Added the first AI algorithm.
- Vs. AI is now available with every option leading to simple bot
- Fixed a bug where placing ships in certain rotation around the boundaries of the board would wrongly colour them when sunk.
- Improved code clarity and comments
- Added an option in the settings to change your nickname
- Added version display in the settings
- Slightly improved box scaling and layout
- Made multiple improvements to the PWA support
- Added multiple minor features
- Brought back full SPA URL functionality, now if you copy a URL to specific view/page in the menu and go into it, you will actually arrive at that page if it's not restricted.
2024-04-14 19:17:20 +02:00

205 lines
5.9 KiB
JavaScript

String.prototype.replaceAt = function (index, replacement) {
return this.substring(0, index) + replacement + this.substring(index + replacement.length);
}
const socket = io();
// Temporarily commented out, as it causes huge graphical glitches
// const charset = ["0", "1", "!", "@", "#", "$", "%", "&"];
// const initialContent = $("#scrolldowntext").html();
// setInterval(() => {
// var content = $("#scrolldowntext").html();
// const len = content.length;
// for (let i = 0; i < len; i++) {
// const duration = Math.random() * 20 + 40;
// setTimeout(() => {
// let previousChar = content.charAt(i);
// let randomChar = charset[Math.floor(Math.random() * charset.length)];
// content = content.replaceAt(i, randomChar);
// $("#scrolldowntext").html(content);
// setTimeout(() => {
// content = content.replaceAt(i, previousChar);
// $("#scrolldowntext").html(content);
// if (i == len - 1) {
// content = initialContent;
// $("#scrolldowntext").html(initialContent);
// }
// }, duration * len + duration * i);
// }, duration * i);
// }
// }, 5000);
document.addEventListener("wheel", (event) => {
if (event.deltaY > 0) {
$(".loginContainer").addClass("active");
animateShips();
} else if (event.deltaY < 0) {
$(".loginContainer").removeClass("active");
}
});
let touchStart = 0;
window.addEventListener("touchstart", function (event) {
touchStart = event.touches[0].clientY;
});
window.addEventListener("touchend", function (event) {
touchEnd = event.changedTouches[0].clientY;
if (touchStart - touchEnd > 50) {
$(".loginContainer").addClass("active");
animateShips();
} else if (touchStart - touchEnd < -50) {
$(".loginContainer").removeClass("active");
}
});
function animateShips() {
setTimeout(() => {
for (let i = 1; i <= 10; i++) {
setTimeout(() => {
$(`#f${i}`).css("scale", "1");
}, 100 * (i - 1));
}
}, 400);
const fields = document.querySelectorAll(".field");
fields.forEach(field => {
if (!field.id) {
$(field).css("scale", "1");
}
});
}
switchView("loginView");
const loginForm = document.getElementById('loginForm');
const emailInput = document.getElementById('email');
loginForm.addEventListener('submit', (e) => {
e.preventDefault();
if (emailInput.value) {
lockUI(true);
console.log("Logging in with e-mail:", emailInput.value);
socket.emit("email login", emailInput.value, (response) => {
console.log(response);
switch (response.status) {
case "ok":
console.log("Logged in.");
console.log(response);
if (response.next === "done") {
console.log("No authorisation required.");
$("body").addClass("closed");
setTimeout(() => {
window.location.reload();
}, 700);
return;
}
lockUI(false);
switchView("authView");
progressParalax();
break;
default:
Toastify({
text: response.error,
duration: 5000,
newWindow: true,
gravity: "bottom",
position: "right",
stopOnFocus: true,
className: "bshipstoast",
}).showToast();
lockUI(false);
break;
}
});
emailInput.value = '';
} else {
Toastify({
text: window.locale["E-mail address is required"],
duration: 5000,
newWindow: true,
gravity: "bottom",
position: "right",
stopOnFocus: true,
className: "bshipstoast",
}).showToast();
}
});
const authForm = document.getElementById('authForm');
const codeInput = document.getElementById('authcode');
authForm.addEventListener('submit', (e) => {
e.preventDefault();
if (codeInput.value) {
lockUI(true);
console.log("Logging in with e-mail:", codeInput.value);
socket.emit("email auth", codeInput.value, (response) => {
switch (response.status) {
case "ok":
console.log("Authorised.");
lockUI(false);
$("body").addClass("closed");
setTimeout(() => {
window.location.reload();
}, 700);
break;
default:
Toastify({
text: response.error,
duration: 5000,
newWindow: true,
gravity: "bottom",
position: "right",
stopOnFocus: true,
className: "bshipstoast",
}).showToast();
lockUI(false);
break;
}
});
emailInput.value = '';
} else {
Toastify({
text: window.locale["Auth code is required"],
duration: 5000,
newWindow: true,
gravity: "bottom",
position: "right",
stopOnFocus: true,
className: "bshipstoast",
}).showToast();
}
});
var parallaxTranslateX = 0;
function progressParalax() {
parallaxTranslateX -= 30;
$(".bgShip.big").css("translate", `${parallaxTranslateX}vw`);
$(".bgShip.small").css("translate", `${parallaxTranslateX * 0.625}vw`);
}