Michal_Pilarski

client.js

Apr 16th, 2021
611
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let socket = io();
  2.  
  3. let alfabet = ['a', 'ą', 'b', 'c', 'ć', 'd', 'e', 'ę', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'ł', 'm', 'n', 'ń', 'o', 'ó', 'p', 'q', 'r', 's', 'ś', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'ź', 'ż'];
  4.  
  5. let form = document.getElementById('form');
  6. let input = document.getElementById('input');
  7. let chat = document.getElementById('chat');
  8. let players = document.getElementById('players');
  9. let chooseNick = document.getElementById('Nickname');
  10. let nickInput = document.getElementById('Nickinput');
  11. let bool = true;
  12. let GameStartFlag = true;
  13. let canWrite = true;
  14. let Nick = '';
  15.  
  16. let points = 0;
  17.  
  18. // Pozyskiwanie wszystkich klientów w pokoju (raz po wczytaniu)
  19. socket.on('clients', function(clients){
  20.     console.log(clients);
  21.     if(bool)
  22.     {
  23.         for(i in clients.name)
  24.         {
  25.             let item = document.createElement('p');
  26.             item.textContent = clients.name[i] + ' punkty: ' + clients.score[i];
  27.             players.appendChild(item);
  28.         }
  29.         bool = false;
  30.     }
  31. });
  32. // Ustawianie nicku i usunięcie pola do podania nazwy
  33. chooseNick.addEventListener('submit', function(e){
  34.     e.preventDefault();
  35.     if(nickInput.value)
  36.     {
  37.         Nick = nickInput.value;
  38.         socket.emit('nickname', nickInput.value)
  39.         nickInput.remove();
  40.         chooseNick.remove();
  41.     }
  42. });
  43. // dodawanie nicków do tablicy z graczami
  44. socket.on('nickname', function(nick){
  45.     let item = document.createElement('p');
  46.     item.textContent = nick + ' punkty: 0';
  47.     item.setAttribute("id", nick);
  48.     players.appendChild(item);
  49. });
  50.  
  51. // usuwanie rozłączonych graczy
  52. socket.on('clientOut',function(nick){
  53.     document.getElementById(nick).remove();
  54. });
  55.  
  56. // wysyłanie wiadomości
  57. form.addEventListener('submit', function(e) {
  58.     e.preventDefault();
  59.     if (input.value && canWrite) {
  60.         let message = {
  61.             text: input.value,
  62.             name: 'nick'
  63.         };
  64.         socket.emit('chat message', message);
  65.         input.value = '';
  66.     }
  67. });
  68.  
  69. // odbieranie wiadomości i wyświetlanie na czacie
  70. socket.on('chat message', function(msg) {
  71.     let item = document.createElement('p');
  72.     item.textContent = msg.name + ': ' + msg.text;
  73.     chat.appendChild(item);
  74. });
  75. // rozpoczęcie gry, otrzymanie informacji kto rysuje i co
  76. socket.on('gameStart', function(gameStatus){
  77.     if(GameStartFlag)
  78.     {
  79.         GameStartFlag = false;
  80.         let whoDraw = document.querySelector('.painter');
  81.         let choosenWord = document.querySelector('.word');
  82.         let gameCount = document.getElementById('games');
  83.         gameCount.textContent = 'Pozostało ' + gameStatus.games + ' gier';
  84.         whoDraw.textContent += gameStatus.painter;
  85.         if(Nick == gameStatus.painter)
  86.         {
  87.             choosenWord.textContent += gameStatus.word;
  88.             canWrite = false;
  89.         }
  90.         else
  91.         {
  92.             canWrite = true;
  93.             let codedWord = '';
  94.             for(i in gameStatus.word)
  95.             {
  96.                 for(j in alfabet)
  97.                 {
  98.                     if(gameStatus.word[i] == alfabet[j])
  99.                     {
  100.                         codedWord += '_ '
  101.                     }
  102.                     else if(gameStatus.word[i] == ' ')
  103.                     {
  104.                         codedWord += ' ';
  105.                     }
  106.                 }
  107.             }
  108.             choosenWord.textContent += codedWord;
  109.         }
  110.     }
  111. });
  112. socket.on('gameStop', function(winnerStatus){
  113.     let whoDraw = document.querySelector('.painter');
  114.     let choosenWord = document.querySelector('.word');
  115.     whoDraw.textContent = 'Rysuje: ';
  116.     choosenWord.textContent = 'Słowo: ';
  117.  
  118.     let winner = document.getElementById(winnerStatus.name);
  119.     winner.textContent = winnerStatus.name + ' punkty: '+ winnerStatus.score;
  120.     GameStartFlag = true;
  121. });
  122.  
  123. // po odgadnięciu hasła przez gracza
  124. socket.on('RightGuess', function(msg){
  125.     let item = document.createElement('p');
  126.     item.style.color = 'green';
  127.     item.textContent = msg.name + ' odgadł hasło!';
  128.     chat.appendChild(item);
  129.  
  130.     canWrite = false;
  131. });
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×