|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + <meta name="viewport" content="width=device-witdth, initial-scale=1.0" /> |
| 6 | + |
| 7 | + <title>Netrunner Mobile Random Card</title> |
| 8 | + |
| 9 | + <style> |
| 10 | + * { |
| 11 | + box-sizing: border-box; |
| 12 | + } |
| 13 | + body { |
| 14 | + background-color: black; |
| 15 | + color: grey; |
| 16 | + font-family: "Courier New"; |
| 17 | + font-size: .75em; |
| 18 | + } |
| 19 | + img { |
| 20 | + max-width: 100%; |
| 21 | + height: auto; |
| 22 | + padding-left: 10px; |
| 23 | + padding-right: 10px; |
| 24 | + display: block; |
| 25 | + margin-left: auto; |
| 26 | + margin-right: auto; |
| 27 | + } |
| 28 | + a:link { color: grey; } |
| 29 | + a:visited { color: grey; } |
| 30 | + a:hover { color: white; } |
| 31 | + a:active { color: white; } |
| 32 | + </style> |
| 33 | + |
| 34 | + </head> |
| 35 | + |
| 36 | + <body onload="displayCard()"> |
| 37 | + <div id="card"></div> |
| 38 | + |
| 39 | + <div id="buttons" style="padding-top: 1rem;"> |
| 40 | + |
| 41 | + </div> |
| 42 | + |
| 43 | + <div id="footer"> |
| 44 | + <p><center>Inspired by <a href="https://chrome.google.com/webstore/detail/random-netrunner-card/ebclfmmncipkjpnpmbefgcdlnobecgeh">Random Netrunner Card</a> Chrome extension</center></p> |
| 45 | + <p><center>Uses the public NetrunnerDB <a href="https://netrunnerdb.com/api/2.0/doc">API</a>.</center></p> |
| 46 | + <p><center>This is still a work-in-progress. Learn more about the project at <a href="http://pawnstorm.net/projects/netrunner-random-card.html">pawnstorm.net/projects/netrunner-random-card.html</a></center></p> |
| 47 | + </div> |
| 48 | + </body> |
| 49 | + |
| 50 | +<script> |
| 51 | + |
| 52 | +const cardList = []; |
| 53 | +var altURL = ""; |
| 54 | + |
| 55 | +// |
| 56 | +// get all cards |
| 57 | +// Create a request variable and assign a new HMLHttpRequest object to it. |
| 58 | +var request = new XMLHttpRequest() |
| 59 | + |
| 60 | +// Open a new connection, using the GET request on the URL endpoint |
| 61 | +request.open('GET', 'https://netrunnerdb.com/api/2.0/public/cards', false) |
| 62 | +request.onload = function() { |
| 63 | + // Begin accessing JSON data here |
| 64 | + var cards = JSON.parse(this.response) |
| 65 | + // Some error handling |
| 66 | + if (request.status >= 200 && request.status < 400) { |
| 67 | + cards.data.forEach(card => { |
| 68 | + let aCard = String(card.code); |
| 69 | + cardList.push(aCard); |
| 70 | + }) |
| 71 | + |
| 72 | + } else { |
| 73 | + const errorMessage = document.createElement('marquee') |
| 74 | + errorMessage.textContent = 'Gah, is not working!' |
| 75 | + app.appendChild(errorMessage) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +request.send() |
| 80 | + |
| 81 | + // cycle through all cards and append ID's to an array |
| 82 | + // choose an array element at random |
| 83 | +// get all packs |
| 84 | +// get all cycles |
| 85 | +// filter out all packs from rotated cycles |
| 86 | +// filter cards to remaining packs |
| 87 | +// get mwl |
| 88 | +// filter out banned cards |
| 89 | +// ?store all legal card codes in an array? |
| 90 | +// randomly choose legal card and display image |
| 91 | +// bonus: indicate if card is unicorn |
| 92 | +// |
| 93 | + |
| 94 | +// display the card |
| 95 | + |
| 96 | +// check to see where the card image is |
| 97 | +function checkImage(cardNumber) { |
| 98 | + var cardRequest = new XMLHttpRequest(); |
| 99 | + var cardRequestURL = 'https://netrunnerdb.com/api/2.0/public/card/' + cardNumber; |
| 100 | + cardRequest.open('GET', cardRequestURL , false); |
| 101 | + cardRequest.onload = function() { |
| 102 | + var cardData = JSON.parse(this.response); |
| 103 | + if (cardData.data[0].hasOwnProperty('image_url')) { |
| 104 | + var imageURL = cardData.data[0]['image_url']; |
| 105 | + altURL = imageURL; |
| 106 | + return imageURL; |
| 107 | + } else { |
| 108 | + var imageURL = 'https://netrunnerdb.com/card_image/' + cardNumber + '.png'; |
| 109 | + altURL = imageURL; |
| 110 | + return imageURL; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + cardRequest.send(); |
| 115 | +} |
| 116 | + |
| 117 | +function getRandomCard(cardList) { |
| 118 | + return cardList[Math.floor(Math.random()*cardList.length)]; |
| 119 | +} |
| 120 | + |
| 121 | +const randomCard = getRandomCard(cardList); |
| 122 | + |
| 123 | +const app = document.getElementById('card'); |
| 124 | +const cardImg = document.createElement('img'); |
| 125 | +var imgURL = checkImage(randomCard); |
| 126 | +console.log('The image URL is: ' + imgURL); |
| 127 | +console.log('The image URL might also be: ' + altURL); |
| 128 | +cardImg.src = altURL; |
| 129 | + |
| 130 | +// Create a link to the card: https://netrunnerdb.com/en/card/${randomCard} |
| 131 | + |
| 132 | +//app.appendChild(cardImg); |
| 133 | + |
| 134 | +var refreshButton = document.getElementById('new-card'); |
| 135 | +refreshButton.onclick = displayNewCard(); |
| 136 | + |
| 137 | +function displayCard() { |
| 138 | + app.innerHTML = '<a href="https://netrunnerdb.com/en/card/' + randomCard + '"><img src="' + altURL + '"></a>' |
| 139 | +} |
| 140 | + |
| 141 | +</script> |
0 commit comments