euchre-live

Euchre web-app for the socially distant family
git clone git://git.alexkarle.com/euchre-live.git
Log | Files | Refs | README | LICENSE

debug.js (3076B) [raw]


      1 const ws = new WebSocket(`ws://${window.location.host}/play`);
      2 let GAME_PHASE = '';
      3 const send = (payload) => ws.send(JSON.stringify(payload));
      4 const getel = (id) => document.getElementById(id);
      5 ws.onmessage = (event) => { 
      6   msg = JSON.parse(event.data);
      7   if (msg.msg_type !== 'pong') { console.log(msg) }
      8 
      9   if (msg.msg_type === 'game_state') {
     10     GAME_PHASE = msg.game.phase;
     11     gameState = '<br>Game:          ' + msg.table_id + '<br>' +
     12       'Players:       ' + msg.game.players + '<br>' +
     13       'Spectators:    ' + msg.game.spectators + '<br>' +
     14       'Game Phase:  '   + msg.game.phase + '<br>' +
     15       'Player Turn:  '   + msg.game.turn + '<br>' +
     16       'Dealer:  '   + msg.game.dealer + '<br>' +
     17       'Trump:  '   + msg.game.trump + '<br>' +
     18       'Tricks:  '   + msg.game.tricks + '<br>' +
     19       'Hand Lengths:  '   + msg.game.hand_lengths + '<br>' +
     20       'Score:  '   + msg.game.score + '<br>'
     21 
     22     if (typeof msg.game.trump_nominee !== 'undefined') {
     23       gameState += 'Trump Nominee: ' + '<img class="card" src="cards/' + msg.game.trump_nominee + '.svg"><br>'
     24     }
     25 
     26     getel('hand').innerHTML = 'HAND: <br>'
     27     if (msg.hand) {
     28       for (let i = 0; i < msg.hand.length; i++) {
     29         const c = msg.hand[i]
     30         getel('hand').innerHTML += '<img onclick="play(' + "'" +  c + "'" + ')" class="card" src="cards/' + c + '.svg">'
     31       }
     32     }
     33 
     34     if (msg.game.table) {
     35       getel('table').innerHTML = ''
     36       for (let i = 0; i < msg.game.table.length; i++) {
     37         let c = msg.game.table[i]
     38         if (c === null) {
     39           c = '2B' // show back
     40         }
     41         getel('table').innerHTML += '<img class="card" src="cards/' + c + '.svg">'
     42       }
     43     } else {
     44       getel('table').innerHTML = 'No cards on table'
     45     }
     46     getel('game').innerHTML = gameState
     47   } else if (msg.msg_type === 'error') {
     48     getel('error').innerHTML += msg.msg + '<br>'
     49   } else if (msg.msg_type === 'chat') {
     50     getel('chat_box').innerHTML += msg.msg + '<br>'
     51   }
     52 };
     53 
     54 function joinGame() {
     55   uname = getel('username').value;
     56   gname = getel('gamename').value;
     57   pass  = getel('password').value;
     58   console.log('U: ' + uname + ' G: ' + gname);
     59   send({action:'join_table', player_name: uname, table: gname, password: pass})
     60 }
     61 function sit() {
     62   seat = getel('seat_no').value;
     63   send({action:'take_seat', seat: seat})
     64 }
     65 function stand() {
     66   send({action:'stand_up'})
     67 }
     68 function start_game() {
     69   seat = getel('start_seat').value;
     70   send({action:'start_game', start_seat: seat})
     71 }
     72 function vote(a) {
     73   order_suit = getel('order_suit').value;
     74   console.log(order_suit);
     75   send({action:'order', vote: order_suit, loner: a})
     76 }
     77 function pass_kitty() {
     78   send({action:'order', vote: 'pass'})
     79 }
     80 function play(card) {
     81   if (GAME_PHASE === 'dealer_swap') {
     82     send({action:'dealer_swap', card: card})
     83   } else {
     84     send({action:'play_card', card: card})
     85   }
     86 }
     87 function chat() {
     88   send({action:'chat', msg: getel('chat').value })
     89   getel('chat').value = '';
     90 }
     91 function leaveGame() {
     92   send({action:'leave_table'})
     93 }
     94 
     95 window.setInterval(() => { send({action:'ping'}) }, 5000);
     96