forked from ecin/joynes
/
index.ejs
306 lines (272 loc) · 8.69 KB
/
index.ejs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<script>
var emulator;
var socket;
var canRun;
var shareId;
var partnerIdPresent = /\/(\w+)/.exec(document.location.pathname);
var requestedTitle;
var promptChrome = function() {
var chromeRegex = /chrome/i
if (navigator.userAgent.match(chromeRegex)) {
canRun = true;
} else {
canRun = false;
var answer = confirm("Does not run in chrome, press ok to be redirected to aquire it.");
if(answer) {
window.location = "http://google.com/chrome";
}
}
}
promptChrome();
$(document).ready( function(){
if(!canRun) { return ; }
var roms = [["Contra", "/roms/Contra.nes"],
["Dr. Mario", "/roms/Dr_Mario.nes"],
["Bubble Bobble", "/roms/Bubble_Bobble.nes"],
["Bubble Bobble 2", "/roms/Bubble_Bobble2.nes"]]
emulator = new JSNES({
'ui': $("#emulator").JSNESUI({
"Nintendo": roms,
"Homebrew": [["Concentration Room", "/roms/croom.nes"]]
})
});
$('#message-box .trigger').click( function() {
console.log(requestedTitle);
loadGame(requestedTitle);
}).confirm({
dialogShow:'fadeIn',
dialogSpeed:'fast',
buttons: {
wrapper:'<span class="message-box-confirm"></span>',
separator:' '
}
});
socket = io.connect('http://' + document.location.hostname + ':' + (document.location.port || '80') + '/');
// Display shareId
socket.on("connect", function(evt) {
var shareId = encodeShareId(socket.socket.sessionid);
var hostname = location.protocol + "//" + location.host + "/";
console.log(hostname + shareId);
document.querySelector("#shareId span").innerHTML += hostname + shareId;
});
socket.on("change-request", function(title) {
requestedTitle = title;
$("#message-box .trigger").click();
});
socket.on("role", function(message) {
switch (message.initialize) {
case 'm': joynesClient = new joynes.Master(emulator, socket); break;
case 's': joynesClient = new joynes.Slave(emulator, socket); break;
default: alert("something's wrong");
}
joynesClient.onRomLoaded = function(gameUrl) {
popScreen();
for(var i in roms) {
if(roms[i][1] == gameUrl) {
displayPoster(roms[i][0]);
break;
}
}
}
});
// partnerId is a URL parameter
if(partnerIdPresent) {
var partnerId = partnerIdPresent[1];
socket.emit("register:s", decodeShareId(partnerId));
document.querySelector("#shareId").style.display = "none";
} else {
socket.emit("register:m");
}
});
var encodeShareId = function(payload) {
var prefix = payload.slice(0, payload.length/2);
var suffix = payload.slice(payload.length/2, payload.length);
return parseInt(prefix).toString(36) + parseInt(suffix).toString(36);
}
var decodeShareId = function(payload) {
var prefix = payload.slice(0, payload.length/2);
var suffix = payload.slice(payload.length/2, payload.length);
return parseInt(prefix, 36).toString() + parseInt(suffix, 36).toString();
}
var currentPoster = undefined;
var displayPoster = function(title, timeout) {
console.log(title);
timeout = timeout || 500;
var poster = document.querySelector("#posters li[data-game='" + title + "']");
// Hide previous poster
if(currentPoster && (!poster || currentPoster.getAttribute('data-game') != poster.getAttribute('data-game'))) {
currentPoster.style.opacity = 0;
}
currentPoster = poster;
// Display title's poster
if(currentPoster) {
currentPoster.style.opacity = 1;
}
}
var popScreen = function(timeout) {
var nesScreen = $(".nes-screen");
if(!nesScreen.data("popped")) {
timeout = timeout || 1000;
nesScreen.data("popped", true);
setTimeout(function() {
// We're scaling element up; if this causes a slowdown
// in rendering, we should be removing a negative scale here.
nesScreen.
css("-webkit-transform", "skewY(0) scale(5)").
css("left", "-79px");
$("#container-main").css("opacity", "0.4");
}, timeout);
}
}
var loadGame = function(title) {
var roms = document.querySelector(".nes-roms select");
// Load game's ROM
for(var j = 0; j < roms.options.length; j++) {
var rom = roms.options.item(j);
if(rom.innerHTML == title) {
roms.selectedIndex = j;
$(".nes-roms select").change();
}
}
}
document.onready = function() {
if(!canRun) { return ; }
// Game setup (cartridges, posters)
var games = document.querySelectorAll("#games li");
for(var i = 0; i < games.length; i++) {
var game = games.item(i);
game.onmouseover = function(event) {
var title = event.target.getAttribute("data-game");
displayPoster(title);
}
// Restore poster to loaded game
game.onmouseout = function(event) {
var gameIndex = document.querySelector("select").selectedIndex;
var games = document.querySelectorAll("select option");
var title = games.item(gameIndex).innerHTML;
displayPoster(title);
}
game.onclick = function(event) {
var title = event.target.getAttribute("data-game");
if (partnerIdPresent) {
// We're sending a request to master to select a game
socket.emit("proxy", {"command": "change-request", "data": title})
}
else {
displayPoster(title);
loadGame(title);
}
}
}
}
</script>
<div id="wrapper-main">
<div id="container-main" >
<div id="container-title">
<div id="title">
<p>This is the title.</p>
</div>
<div id="ui-container">
<div id="ui-bar-container">
<div id="cta-container">
<div id="cta">
<p>Nintendo is always better with a buddy.</p>
</div>
<div id="cta-button-container">
<div id="cta-button">
<a href="#" id="cta-button-red">loading...</a>
</div>
<!--
<div id="cta-button">
<a href="#" id="cta-button-green">Ready!</a>
</div>
-->
</div>
</div>
<div id="red-glow-container">
<div id="red-glow">
<p>This is the red light.</p>
</div>
</div>
<!--
<div id="green-glow-container">
<div id="green-glow">
<p>This is the green light.</p>
</div>
</div>
-->
<div id="ui-bar">
<p>This will be the UI Bar.</p>
</div>
</div>
<div id="ui-popup-container">
<div id="ui-popup">
<p>This will be the UI Popup.</p>
</div>
<div id="link-box">
<input type="text" name="sometext" size="100" value="http://localhost:3333/cefsqev8ov19" onClick='selectText(this);'>
</div>
<div id="copy-link-button-container">
<a href="#" id="copy-link-button">Click to copy link</a>
</div>
<div id="bff-container">
<div id="bff">
<p>Share this link with you BFF. Cause Nintendo is always better with a buddy.</p>
</div>
</div>
</div>
</div>
</div>
<div id="container-message-box">
<div id="message-box"><span class="trigger"></span></div>
</div>
<div id="container-tv">
<div id="tv">
<!-- <p>This is a tv.</p> -->
</div>
</div>
<div id="container-nes">
<div id="nes">
<!-- <p>This is a nes system.</p> -->
</div>
</div>
<div id="container-posters">
<ul id="posters">
<li data-game="Dr. Mario"/>
<li data-game="Contra"/>
<li data-game="Bubble Bobble"/>
<li data-game="Bubble Bobble 2"/>
</ul>
</div>
<div id="container-controllers">
<ul>
<li class="controller" id="controller1"></li>
<li class="controller" id="controller2"></li>
</ul>
</div>
<div id="container-games">
<ul id="games">
<li data-game="Dr. Mario" />
<li data-game="Contra" />
<li data-game="Bubble Bobble" />
<li data-game="Bubble Bobble 2" />
</ul>
</div>
<div id="container-couch">
<div id="couch">
<!-- <p>This is the couch.</p> -->
</div>
</div>
<div id="container-bg">
<div id="bg">
<!-- <p>This is the background</p> -->
</div>
</div>
<!-- <div id="container-floor">
<div id="floor">
<p>This is the floor</p>
</div>
</div> -->
</div>
<div id="emulator"></div>
</div>