Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (async () => {
- let userName = '';
- let interval = null;
- let gameTotalTime = null;
- let finished = false;
- const shopwatchTimeout = 50; // ms
- const keyTab = 9;
- const keyEnter = 13;
- const nbsp = " ";
- const lt = "<";
- const gt = ">";
- const startStopwatch = () => {
- if (interval !== null) {
- return;
- }
- let startTime = null;
- interval = setInterval(() => {
- if (startTime === null) {
- startTime = new Date().getTime();
- }
- const updatedTime = new Date().getTime();
- gameTotalTime = ((updatedTime - startTime) / 1000).toFixed(3);
- document.getElementById("time").innerHTML = gameTotalTime;
- }, shopwatchTimeout);
- };
- const finishGame = async () => {
- clearInterval(interval)
- try {
- const rawResponse = await fetch('/users/add', {
- method: 'POST',
- headers: {
- 'Accept': 'application/json',
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify({userName: userName, gameTime: gameTotalTime, secret: '33402127-a713-4835-88e7-f9039ada9b02'})
- });
- const content = await rawResponse.json();
- console.log(content)
- alert("Спасибо за ваше участие в игре!\nВаш результат: " + gameTotalTime)
- } catch (e) {
- console.error(e)
- alert("Ошибка отправки запроса!")
- }
- };
- const startGame = () => {
- document.getElementById("nickname").innerHTML = userName;
- const
- notDoneArea = document.getElementById("notDoneArea"),
- doneArea = document.getElementById("doneArea"),
- keydown = e => {
- if (e.which !== keyTab) {
- return;
- }
- // prevent html page navigation using tab button
- e.preventDefault();
- if (notDoneArea.innerHTML.substr(0, nbsp.length * 4) === nbsp.repeat(4)) {
- doneArea.innerHTML += nbsp.repeat(4);
- notDoneArea.innerHTML = notDoneArea.innerHTML.substr(nbsp.repeat(4).length);
- }
- },
- keypress = e => {
- startStopwatch();
- if (e.which === keyTab) {
- return;
- }
- const ch = String.fromCharCode(e.which);
- if (notDoneArea.innerHTML[0] === "\n" && e.which === keyEnter) {
- doneArea.innerHTML += "\n";
- notDoneArea.innerHTML = notDoneArea.innerHTML.substr(1);
- } else if (notDoneArea.innerHTML.substr(0, nbsp.length) === nbsp && ch === " ") {
- doneArea.innerHTML += nbsp;
- notDoneArea.innerHTML = notDoneArea.innerHTML.substr(nbsp.length);
- } else if (notDoneArea.innerHTML.substr(0, lt.length) === lt && ch === "<") {
- doneArea.innerHTML += lt;
- notDoneArea.innerHTML = notDoneArea.innerHTML.substr(lt.length);
- } else if (notDoneArea.innerHTML.substr(0, gt.length) === gt && ch === ">") {
- doneArea.innerHTML += gt;
- notDoneArea.innerHTML = notDoneArea.innerHTML.substr(gt.length);
- } else if (notDoneArea.innerHTML[0] === ch) {
- doneArea.innerHTML += notDoneArea.innerHTML[0];
- notDoneArea.innerHTML = notDoneArea.innerHTML.substr(1);
- }
- if (notDoneArea.innerHTML.length === 0 && !finished) {
- finished = true;
- setTimeout(async () => {
- await finishGame();
- }, 10);
- }
- };
- document.addEventListener("keydown", keydown)
- document.addEventListener("keypress", keypress);
- }
- const fetchUserName = async () => {
- while (true) {
- userName = prompt("Введите ваше имя или никнейм", "")
- if (userName === '' || userName === null) {
- continue;
- }
- if (/[^a-zA-Z0-9]/.test(userName) || userName.length < 4) {
- alert("Введите корректное имя или никнейм, содержащее только буквы и цифры с длиной не менее 4 символов");
- continue;
- }
- try {
- const rawResponse = await fetch('/users/checkExists', {
- method: 'POST',
- headers: {
- 'Accept': 'application/json',
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify({userName: userName})
- });
- const content = await rawResponse.json();
- console.log(content)
- if (content.success) {
- break;
- } else {
- alert("Это имя занято!")
- }
- } catch (e) {
- console.error(e)
- alert("Ошибка отправки запроса!")
- }
- }
- }
- await fetchUserName();
- startGame();
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement