Advertisement
deadroot

highload: goods: fail

Nov 9th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. (async () => {
  2. let userName = '';
  3.  
  4. let interval = null;
  5. let gameTotalTime = null;
  6. let finished = false;
  7.  
  8. const shopwatchTimeout = 50; // ms
  9. const keyTab = 9;
  10. const keyEnter = 13;
  11. const nbsp = " ";
  12. const lt = "<";
  13. const gt = ">";
  14.  
  15. const startStopwatch = () => {
  16. if (interval !== null) {
  17. return;
  18. }
  19.  
  20. let startTime = null;
  21. interval = setInterval(() => {
  22. if (startTime === null) {
  23. startTime = new Date().getTime();
  24. }
  25.  
  26. const updatedTime = new Date().getTime();
  27. gameTotalTime = ((updatedTime - startTime) / 1000).toFixed(3);
  28.  
  29. document.getElementById("time").innerHTML = gameTotalTime;
  30. }, shopwatchTimeout);
  31. };
  32.  
  33. const finishGame = async () => {
  34. clearInterval(interval)
  35.  
  36. try {
  37. const rawResponse = await fetch('/users/add', {
  38. method: 'POST',
  39. headers: {
  40. 'Accept': 'application/json',
  41. 'Content-Type': 'application/json'
  42. },
  43. body: JSON.stringify({userName: userName, gameTime: gameTotalTime, secret: '33402127-a713-4835-88e7-f9039ada9b02'})
  44. });
  45.  
  46. const content = await rawResponse.json();
  47. console.log(content)
  48. alert("Спасибо за ваше участие в игре!\nВаш результат: " + gameTotalTime)
  49. } catch (e) {
  50. console.error(e)
  51. alert("Ошибка отправки запроса!")
  52. }
  53. };
  54.  
  55. const startGame = () => {
  56. document.getElementById("nickname").innerHTML = userName;
  57.  
  58. const
  59. notDoneArea = document.getElementById("notDoneArea"),
  60. doneArea = document.getElementById("doneArea"),
  61.  
  62. keydown = e => {
  63. if (e.which !== keyTab) {
  64. return;
  65. }
  66.  
  67. // prevent html page navigation using tab button
  68. e.preventDefault();
  69.  
  70. if (notDoneArea.innerHTML.substr(0, nbsp.length * 4) === nbsp.repeat(4)) {
  71. doneArea.innerHTML += nbsp.repeat(4);
  72. notDoneArea.innerHTML = notDoneArea.innerHTML.substr(nbsp.repeat(4).length);
  73. }
  74. },
  75.  
  76. keypress = e => {
  77. startStopwatch();
  78.  
  79. if (e.which === keyTab) {
  80. return;
  81. }
  82.  
  83. const ch = String.fromCharCode(e.which);
  84.  
  85. if (notDoneArea.innerHTML[0] === "\n" && e.which === keyEnter) {
  86. doneArea.innerHTML += "\n";
  87. notDoneArea.innerHTML = notDoneArea.innerHTML.substr(1);
  88. } else if (notDoneArea.innerHTML.substr(0, nbsp.length) === nbsp && ch === " ") {
  89. doneArea.innerHTML += nbsp;
  90. notDoneArea.innerHTML = notDoneArea.innerHTML.substr(nbsp.length);
  91. } else if (notDoneArea.innerHTML.substr(0, lt.length) === lt && ch === "<") {
  92. doneArea.innerHTML += lt;
  93. notDoneArea.innerHTML = notDoneArea.innerHTML.substr(lt.length);
  94. } else if (notDoneArea.innerHTML.substr(0, gt.length) === gt && ch === ">") {
  95. doneArea.innerHTML += gt;
  96. notDoneArea.innerHTML = notDoneArea.innerHTML.substr(gt.length);
  97. } else if (notDoneArea.innerHTML[0] === ch) {
  98. doneArea.innerHTML += notDoneArea.innerHTML[0];
  99. notDoneArea.innerHTML = notDoneArea.innerHTML.substr(1);
  100. }
  101.  
  102. if (notDoneArea.innerHTML.length === 0 && !finished) {
  103. finished = true;
  104.  
  105. setTimeout(async () => {
  106. await finishGame();
  107. }, 10);
  108. }
  109. };
  110.  
  111.  
  112. document.addEventListener("keydown", keydown)
  113.  
  114. document.addEventListener("keypress", keypress);
  115. }
  116.  
  117. const fetchUserName = async () => {
  118. while (true) {
  119. userName = prompt("Введите ваше имя или никнейм", "")
  120.  
  121. if (userName === '' || userName === null) {
  122. continue;
  123. }
  124.  
  125. if (/[^a-zA-Z0-9]/.test(userName) || userName.length < 4) {
  126. alert("Введите корректное имя или никнейм, содержащее только буквы и цифры с длиной не менее 4 символов");
  127. continue;
  128. }
  129.  
  130. try {
  131. const rawResponse = await fetch('/users/checkExists', {
  132. method: 'POST',
  133. headers: {
  134. 'Accept': 'application/json',
  135. 'Content-Type': 'application/json'
  136. },
  137. body: JSON.stringify({userName: userName})
  138. });
  139.  
  140. const content = await rawResponse.json();
  141. console.log(content)
  142.  
  143. if (content.success) {
  144. break;
  145. } else {
  146. alert("Это имя занято!")
  147. }
  148. } catch (e) {
  149. console.error(e)
  150. alert("Ошибка отправки запроса!")
  151. }
  152. }
  153. }
  154.  
  155. await fetchUserName();
  156.  
  157. startGame();
  158. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement