{"version":3,"file":"bundle.js","sources":["../src/main.js"],"sourcesContent":["/**\n * Main container for the RPSLS App\n */\nfunction App() {\n var _this = this;\n\n /** \n * Initialization function. \n * - sets up the websocket connection\n * - loads the userID\n * - builds the initial UI.\n */\n _this.init = () => {\n\n _this.handleEvents();\n const wsURL = 'wss://foamngcalc.execute-api.us-west-2.amazonaws.com/Prod'\n _this.ws = new WebSocket(wsURL)\n\n _this.played = false;\n\n // Persist a user id to use across connections\n // The backend gets a connection ID with every request, but that changes if\n // the browser has to reconnect. This gives a stable player identifier without\n // them having to log in.\n _this.userId = localStorage.getItem(\"rockpaper-userid\");\n if(_this.userId == null) {\n _this.userId = Math.random().toString(36).substr(2, 17);\n localStorage.setItem(\"rockpaper-userid\", _this.userId);\n }\n\n // Handler which runs every time a message comes from the websocket.\n _this.ws.onmessage = e => {\n d = JSON.parse(e.data)\n // Only when needed:\n // Add the game ID to the URL so the link can be shared with others\n if (_this.gameId != d.gameId && d.gameId != \"\") {\n _this.gameId = d.gameId\n window.location = window.location + \"#\" + d.gameId\n }\n // Update the round that we're playing on now\n _this.roundId = d.round\n // Update the UI with the data from the websocket\n _this.updateUI(d)\n }\n\n // Handler which does UI updates\n _this.updateUI = d => {\n // If the server sends a roundSummary, that means a round is over.\n if (\"roundSummary\" in d) {\n _this.statusElem.innerHTML = d.roundSummary\n // Update the images for the moves\n setPlayImg(_this.youPlayElem, `${d.yourPlay}.png`, true)\n setPlayImg(_this.themPlayElem, `${d.theirPlay}.png`, false)\n // Set the colors based on who won\n if(d.winner) {\n _this.youPlayElem.style.backgroundColor = \"lightgreen\"\n _this.themPlayElem.style.backgroundColor = \"palevioletred\"\n } else if(d.yourPlay != d.theirPlay) {\n _this.themPlayElem.style.backgroundColor = \"lightgreen\"\n _this.youPlayElem.style.backgroundColor = \"palevioletred\"\n }\n // Set a flag meaning we're ready to play again\n _this.played = false;\n }\n // Update the scores no matter if the round is over or not\n _this.youScoreElem.innerHTML = `You: ${d.yourScore}`\n _this.themScoreElem.innerHTML = `Them: ${d.theirScore}`\n }\n\n // Handler which runs when a new websocket connection is opened\n _this.ws.onopen = () => {\n // Use the URL bar to see if the player is in a game already.\n // No hash in the URL means start a new game\n var url = new URL(window.location)\n if (url.hash == \"\") {\n console.log(\"hash was empty, creating a game\")\n // no game is created yet\n _this.ws.send(JSON.stringify({\n 'action': 'new',\n 'userId': _this.userId,\n }))\n _this.statusElem.innerHTML = \"Created a game. Share the link with a friend to play!\"\n } else {\n // cut the hash off\n _this.gameId = url.hash.substring(1)\n console.log(\"attempting to connect to \" + _this.gameId)\n _this.ws.send(JSON.stringify({\n 'action': 'join',\n 'userId': _this.userId,\n 'gameId': _this.gameId,\n }))\n _this.statusElem.innerHTML = \"Joined Game! Make a play now.\"\n }\n }\n\n }\n\n // Handler when a play is made\n _this.makePlay = e => {\n // Find the button element where the click event came from\n var elem = e.target.closest(\"button\")\n // Don't let a player submit a play if the browser knows they already did\n if (_this.played) {\n console.log(\"ignoring second play attempt\")\n return\n }\n console.log(\"got a play event for \" + elem + \" named \" + elem.id)\n // Send a play event to the server\n _this.ws.send(JSON.stringify({\n 'action': 'play',\n 'gameId': _this.gameId,\n 'userId': _this.userId,\n 'round': _this.roundId,\n 'play': elem.id,\n }))\n _this.statusElem.innerHTML = `You played ${elem.id}, waiting on other player ....`\n _this.played = true\n _this.themPlayElem.style.backgroundColor = \"\"\n _this.youPlayElem.style.backgroundColor = \"\"\n setPlayImg(_this.themPlayElem, \"loading.gif\", true)\n setPlayImg(_this.youPlayElem, `${elem.id}.png`, true)\n }\n\n // Set up all the click handlers for the UI buttons\n _this.handleEvents = () => {\n console.log(\"setting up onclick events\")\n document.querySelector('#rock').onclick = _this.makePlay\n document.querySelector('#paper').onclick = _this.makePlay\n document.querySelector('#scissors').onclick = _this.makePlay\n document.querySelector('#lizard').onclick = _this.makePlay\n document.querySelector('#spock').onclick = _this.makePlay\n document.querySelector('#copytoclipboard').onclick = copyURLToClipboard\n _this.statusElem = document.querySelector('#status')\n _this.youScoreElem = document.querySelector('#youscore')\n _this.themScoreElem = document.querySelector('#themscore')\n _this.youPlayElem = document.querySelector(\"#youplay\")\n _this.themPlayElem = document.querySelector(\"#themplay\")\n }\n}\n\n/** Set image in the UI for the hand gesture of the player.\n * If the reverse flag is set, display the image flipped\n * This makes it so the same set of images can be used\n * to show them coming from the left or right player\n */ \nconst setPlayImg = (element, url, reverse) => {\n var img = document.createElement(\"img\")\n img.src = url\n if (reverse) {\n img.style.transform = \"scaleX(-1)\";\n }\n if(element.firstElementChild == null) {\n element.appendChild(img)\n } else {\n element.replaceChild(img, element.firstElementChild)\n }\n}\n\n// Function discovered somewhere online\n// Creates a temporary element to hold the URL text\n// so it can be copied to the clipboard, then delete the element\nconst copyURLToClipboard = () => {\n const el = document.createElement('textarea');\n el.value = document.location;\n el.setAttribute('readonly', '');\n el.style.position = 'absolute';\n el.style.left = '-9999px';\n document.body.appendChild(el);\n el.select();\n document.execCommand('copy');\n document.body.removeChild(el);\n};\n\nvar app = new App();\n\n// Run the init message once the onload event fires\nwindow.addEventListener(\"load\", function () { app.init() }, false);\n"],"names":[],"mappings":"AAAA;AACA;AACA;AACA,SAAS,GAAG,GAAG;AACf,EAAE,IAAI,KAAK,GAAG,IAAI,CAAC;AACnB;AACA;AACA;AACA;AACA;AACA;AACA;AACA,EAAE,KAAK,CAAC,IAAI,GAAG,MAAM;AACrB;AACA,IAAI,KAAK,CAAC,YAAY,EAAE,CAAC;AACzB,IAAI,MAAM,KAAK,GAAG,4DAA2D;AAC7E,IAAI,KAAK,CAAC,EAAE,GAAG,IAAI,SAAS,CAAC,KAAK,EAAC;AACnC;AACA,IAAI,KAAK,CAAC,MAAM,GAAG,KAAK,CAAC;AACzB;AACA;AACA;AACA;AACA;AACA,IAAI,KAAK,CAAC,MAAM,GAAG,YAAY,CAAC,OAAO,CAAC,kBAAkB,CAAC,CAAC;AAC5D,IAAI,GAAG,KAAK,CAAC,MAAM,IAAI,IAAI,EAAE;AAC7B,MAAM,K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