|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +let game_keyup = null; |
| 4 | +let game_keydown = null; |
| 5 | + |
| 6 | + |
| 7 | +window.RufflePlayer = window.RufflePlayer || {}; |
| 8 | + |
| 9 | +var callIntervalId = null; |
| 10 | + |
| 11 | +var guest_video_id = null; |
| 12 | +var guest_data_id = null; |
| 13 | + |
| 14 | +function on_host_load() { |
| 15 | + const ruffle = window.RufflePlayer.newest(); |
| 16 | + const player = ruffle.createPlayer(); |
| 17 | + const container = document.getElementById("container"); |
| 18 | + player.style.width = "100%"; |
| 19 | + player.style.height = "800px"; |
| 20 | + container.appendChild(player); |
| 21 | + player.load("tank-trouble.swf"); |
| 22 | + const peer = new Peer(); |
| 23 | + console.log("peer=", peer); |
| 24 | + peer.on('open', function(id) { |
| 25 | + console.log('My peer ID is: ' + id); |
| 26 | + let conn = peer.connect(guest_data_id); |
| 27 | + conn.on('open', function() { |
| 28 | + console.log("Keyboard connection established"); |
| 29 | + // Receive messages |
| 30 | + conn.on('data', function(data) { |
| 31 | + console.log("received data", data); |
| 32 | + window.dispatchEvent(new KeyboardEvent(data["type"], { |
| 33 | + code: data['code'], |
| 34 | + })); |
| 35 | + }); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + |
| 40 | + const videopeer = new Peer(); |
| 41 | + callIntervalId = setInterval(function(p) { |
| 42 | + const canvasElt = document.querySelector("ruffle-player")?.shadowRoot.querySelector("canvas"); |
| 43 | + if (canvasElt != null) { |
| 44 | + console.log("Canvas exists, setting up call now"); |
| 45 | + const stream = canvasElt.captureStream(25); // 25 FPS |
| 46 | + var call = p.call(guest_video_id, stream); |
| 47 | + console.log("stream=", stream); |
| 48 | + // Disabled, we'll re-enable this for lag-compensation |
| 49 | + // document.getElementById("receiving-video").srcObject = stream; |
| 50 | + // document.getElementById("receiving-video").play(); |
| 51 | + clearInterval(callIntervalId); |
| 52 | + } else { |
| 53 | + console.log("canvas still null"); |
| 54 | + } |
| 55 | + }, 1000, videopeer); |
| 56 | +} |
| 57 | + |
| 58 | +function transmitKeystroke(conn, type, event) { |
| 59 | + console.log("transmitting ", type, event); |
| 60 | + conn.send({type: type, code: event.code}); |
| 61 | +} |
| 62 | + |
| 63 | +var displayPeerIdIntervalId = null; |
| 64 | + |
| 65 | +function on_guest_load() { |
| 66 | + const peer = new Peer(); |
| 67 | + |
| 68 | + console.log("peer=", peer); |
| 69 | + peer.on('open', function(id) { |
| 70 | + console.log('Opened, data peer ID is: ' + id); |
| 71 | + guest_data_id = id; |
| 72 | + }); |
| 73 | + peer.on('connection', function(conn) { |
| 74 | + document.getElementById("connectiondetails").innerHTML = ""; |
| 75 | + conn.on('open', function() { |
| 76 | + console.log("Keyboard connection established"); |
| 77 | + document.addEventListener("keyup", function(ev) {transmitKeystroke(conn, "keyup", ev)}); |
| 78 | + document.addEventListener("keydown", function(ev) {transmitKeystroke(conn, "keydown", ev)}); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + |
| 83 | + const videopeer = new Peer(); |
| 84 | + videopeer.on('open', function(id) { |
| 85 | + console.log('Opened, video peer ID is: ' + id); |
| 86 | + guest_video_id = id; |
| 87 | + }) |
| 88 | + videopeer.on('call', function(call) { |
| 89 | + console.log("received call"); |
| 90 | + call.on('stream', function(stream) { |
| 91 | + console.log("On stream, setting video element to ", stream); |
| 92 | + document.getElementById("receiving-video").srcObject = stream; |
| 93 | + document.getElementById("receiving-video").play(); |
| 94 | + }); |
| 95 | + call.answer(); |
| 96 | + }); |
| 97 | + |
| 98 | + displayPeerIdIntervalId = setInterval(function() { |
| 99 | + if (guest_data_id != null && guest_video_id != null) { |
| 100 | + let combinedID = `${guest_data_id}/${guest_video_id}` |
| 101 | + document.getElementById("connectiondetails").innerHTML = |
| 102 | + `Please pass your connection ID <input readonly size="${combinedID.length}" value="${combinedID}"></input> to the host. |
| 103 | + The game will automatically start when the host clicks the 'Start game' button` |
| 104 | + clearInterval(displayPeerIdIntervalId); |
| 105 | + } else { |
| 106 | + console.log("still null"); |
| 107 | + } |
| 108 | + }, 200); |
| 109 | + |
| 110 | + |
| 111 | +} |
| 112 | + |
| 113 | +function submit_host_id() { |
| 114 | + let guest_combined_id = document.getElementById("guest_combined_id").value.trim(); |
| 115 | + if (guest_combined_id.length == 73) { |
| 116 | + guest_data_id = guest_combined_id.split('/')[0]; |
| 117 | + guest_video_id = guest_combined_id.split('/')[1]; |
| 118 | + on_host_load(); |
| 119 | + document.getElementById("connectiondetails").innerHTML = ''; |
| 120 | + } else { |
| 121 | + document.getElementById("error-connectiondetails").innerText = "That does not look right"; |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +function click_host() { |
| 126 | + document.getElementById("hostguestchoice").remove(); |
| 127 | + document.getElementById("connectiondetails").innerHTML = ` |
| 128 | + <p>Please paste the ID you received from the guest</p> |
| 129 | + <input id="guest_combined_id" size="73"> |
| 130 | + <button onclick="submit_host_id()">Start game</button> |
| 131 | + <div id="error-connectiondetails"></div> |
| 132 | + ` |
| 133 | +} |
| 134 | + |
| 135 | +function click_guest() { |
| 136 | + document.getElementById("hostguestchoice").remove(); |
| 137 | + on_guest_load(); |
| 138 | +} |
0 commit comments