annotate www/martin/js/nodescore-client.js @ 101:52e44ee1c791 tip master

enabled all scores in autostart script
author Rob Canning <rc@kiben.net>
date Tue, 21 Apr 2015 16:20:57 +0100
parents cb1862bbcd5a
children
rev   line source
rc@74 1 /////////////////////////////////////////////////
rc@74 2 // connect to websocket
rob@95 3
rc@74 4 var socket = io.connect();
rob@83 5 function initPage() { socket.emit("initPage");
rob@83 6 console.log("init.client.log")
rob@83 7 }
rob@83 8
rc@74 9 //socket.on("metroPulse", metronomeTick);
rc@74 10 /////////////////////////////////////////////////
rob@95 11
rc@74 12 socket.on("metroPulse", pulseInClient);
rc@74 13 function pulseInClient(pulse,groupID,metrobeat){
rc@74 14 var groupPage=document.getElementById('group').value
rc@74 15 if (groupID == groupPage) {
rc@74 16 metronomeTick(1000, groupID, metrobeat);
rc@74 17 }
rc@74 18 }
rob@83 19
rob@95 20 //////////////////////////////////////////////////
rob@95 21 // AUDIO SCORE CUES
rob@95 22
rob@95 23 var audioUnit1 = new Audio('audio/testfile.ogg');
rob@95 24 var audioUnit49 = new Audio('audio/testfile.ogg');
rob@95 25 var audioUnit3 = new Audio('audio/testfile.ogg');
rob@95 26 var audioUnit4 = new Audio('audio/testfile.ogg');
rob@95 27
rob@95 28 function playAudio(a) {
rob@95 29 a.play();
rob@95 30 }
rob@95 31
rob@95 32 //playAudio(audio_unit_1);
rob@95 33
rob@95 34
rc@74 35 /////////////////////////////////////////////////
rc@74 36 function metroCss(beat, beatcolor,text){
rc@74 37 var color = beatcolor;
rc@74 38 $(".metrocase > div").each(function(){$(this).stop()});
rc@74 39 $(".metrocase > div").each(function(){$(this).css('background-color', beatcolor)});
rc@74 40 $(".metrocase > div").each(function(){$(this).text(" ")});
rob@84 41 }
rc@74 42
rc@74 43 function metronomeTick(pulse, voice,metrobeat) {
rc@74 44 var color = "gray"
rob@83 45 metroCss(0, "red", "4")
rob@83 46 setTimeout(function(){metroCss(0, "black", "4")},150);
rc@74 47 };
rc@74 48
rc@74 49 /////////////////////////////////////////////////
rc@74 50 // update the stopwatch value on the client page in line with server
rob@83 51 socket.on("chronFromServer", function(chron) { $("div#client_chronometer").text(chron); });
rc@74 52
rc@74 53 // server time
rc@74 54 socket.on("dateTime", function(datetime) {
rob@83 55 $("div#datetime").text("" +datetime);
rc@74 56 });
rc@74 57
rc@74 58 /////////////////////////////////////////////////
rob@93 59
rob@93 60 // countin (solo part)
rob@84 61
rc@74 62 socket.on("countinFromServer", countinClient);
rob@83 63 function countinClient(groupID, currentseconds,mm,text,colour,background,unit){
rc@74 64 var groupPage=document.getElementById('group').value
rc@74 65 if (groupID == groupPage) {
rob@83 66 $("#counttitle").css('color','black');
rc@74 67 $("#counttitle").text(text);
rc@74 68 $("#count").text(currentseconds);
rc@74 69 document.getElementById("count").style.color=colour;
rob@83 70 }}
rc@74 71
rc@74 72 /////////////////////////////////////////////////
rob@93 73
rob@93 74 // countdown to change (solo)
rob@93 75
rc@74 76 socket.on("counterText", cText);
rc@74 77 function cText(groupID, currentseconds,text){
rob@83 78 $("#totalcountdown"+groupID).text(text);
rc@74 79 var groupPage=document.getElementById('group').value
rc@74 80 if (groupID == groupPage) {
rob@83 81 //console.log(text)
rc@74 82 $("#totalcountdown").text(text);
rc@74 83 }}
rc@74 84
rob@93 85 // when voice is changed in UI grab and update from server
rc@74 86
rob@93 87 function changeSoloVoice(v) { socket.emit("changeSoloVoice", v); }
rob@84 88
rob@93 89 // makes sure page is loaded if not it grabs svg etc
rc@74 90
rob@93 91 socket.on("pageIni", pageIni);
rob@93 92 function pageIni (group,unit,time,mm,seqcounter,nextunit) {
rob@93 93 //console.log(unit + " ---> " + nextunit)
rob@93 94 var soloist = $("#group").attr('value'); //4//$("#group").value;
rob@93 95 if (group==1){
rob@93 96 var n1 = $("#previewbox-1").html().indexOf("svg");
rob@93 97 //console.log(n1)
rob@93 98 if (n1 == -1){
rob@93 99 //console.log("n1 == -1 so load svg:")
rob@93 100 $("#previewbox-1").html("<img src='svg/"+(unit) + ".svg" + "' width='100%'>")
rob@93 101 }
rob@93 102 }
rob@93 103
rob@93 104 if (group==2){
rob@93 105 var n2 = $("#previewbox-2").html().indexOf("svg");
rob@93 106 if (n2 == -1){
rob@93 107 $("#previewbox-2").html("<img src='svg/"+(unit) + ".svg" + "' width='100%'>")
rob@93 108 }
rob@93 109 }
rob@84 110
rob@93 111 if (group==3){
rob@93 112 var n3 = $("#previewbox-3").html().indexOf("svg");
rob@93 113 if (n3 == -1){
rob@93 114 $("#previewbox-3").html("<img src='svg/"+(unit) + ".svg" + "' width='100%'>")
rob@93 115 }
rob@93 116 }
rob@84 117
rob@93 118 if (group==4){
rob@93 119 var n4 = $("#previewbox-4").html().indexOf("svg");
rob@93 120 if (n4 == -1){
rob@93 121 $("#previewbox-4").html("<img src='svg/"+(unit) + ".svg" + "' width='100%'>")
rob@93 122 }
rob@83 123 }
rob@93 124
rob@93 125
rob@93 126 if (group==soloist) {
rob@93 127 //console.log("INI sooolooooist=: ---------------------- : " + soloist)
rob@93 128 var n = $("#previewbox-solo").html().indexOf("svg");
rob@93 129 if (n == -1){
rob@93 130 $("#previewbox-solo").html("<img src='svg/"+(unit) + ".svg" + "' width='100%'>")
rob@93 131 $("#previewbox-solo-next").html("<img src='svg/"+(nextunit) + ".svg" + "' width='100%'>")
rob@93 132 }
rob@93 133 }
rob@95 134
rob@95 135
rob@93 136 }
rob@93 137
rob@95 138
rob@93 139 socket.on("pageFlip", pageFlip);
rob@93 140
rob@93 141 // some if logic to check if file is already displayed
rob@93 142 // if not load it - client doesnt need to be present at start
rob@93 143
rob@93 144 function pageFlip (group,unit,time,mm,seqcounter,nextunit) {
rob@93 145 var soloist = $("#group").attr('value');
rob@93 146
rob@93 147 if (group==1){
rob@93 148 $("#previewbox-1").html("<img src='svg/"+(unit) + ".svg" + "' width='100%'>")
rob@93 149 }
rob@93 150
rob@93 151 if (group==2){
rob@93 152 $("#previewbox-2").html("<img src='svg/"+(unit) + ".svg" + "' width='100%'>")
rob@93 153 }
rob@93 154
rob@93 155 if (group==3){
rob@93 156
rob@93 157 $("#previewbox-3").html("<img src='svg/"+(unit) + ".svg" + "' width='100%'>")
rob@93 158 }
rob@93 159
rob@93 160 if (group==4){
rob@93 161 $("#previewbox-4").html("<img src='svg/"+(unit) + ".svg" + "' width='100%'>")
rob@93 162 }
rob@93 163
rob@95 164
rob@95 165 // page flip for solo part section
rob@93 166
rob@93 167 if (group==soloist) {
rob@95 168
rob@95 169 // changes the svg and preview images
rob@93 170 $("#previewbox-solo").html("<img src='svg/"+(unit) + ".svg" + "' width='100%'>")
rob@93 171 $("#previewbox-solo-next").html("<img src='svg/"+(nextunit) + ".svg" + "' width='100%'>")
rob@95 172
rob@95 173 // check for audio assets to play
rob@95 174 // audio assets should have a value attribute which should be compared with the unit
rob@95 175 // if the unit has an associated audio attribute it should play
rob@95 176
rob@95 177 if ( window["audioUnit"+unit] ){
rob@95 178 console.log("audio asset detected for this unit... playing now...")
rob@95 179 playAudio(window["audioUnit"+unit]);
rob@95 180 } else { console.log("no audio for this unit.."); }
rc@74 181 }
rob@95 182
rob@95 183 // check for effect associated with unit
rob@95 184 // maybe in future we could use the webAudio API?
rob@95 185
rc@74 186 }
rc@74 187
rc@74 188
rob@95 189
rc@74 190 ////////////////////////////////////////////////
rc@74 191
rc@74 192 /* commented out for now as interferes with chat
rc@74 193 need to introduce metakey...manyana... not so usefull anyway really...
rc@74 194
rc@74 195 keyboard controls
rc@74 196 +++++++++++++++++++++++++
rc@74 197
rc@74 198 SPACE to toggle visibilty of preview
rc@74 199 m to toggle visibilty of metronome
rc@74 200 s to toggle visibilty of stopwatch
rc@74 201 h to hide all above
rc@74 202
rc@74 203
rob@83 204 */
rob@93 205
rc@74 206 function toggle_visibility(id) {
rc@74 207 var e = document.getElementById(id);
rc@74 208 if(e.style.display == 'block')
rc@74 209 e.style.display = 'none';
rc@74 210 else
rc@74 211 e.style.display = 'block';
rc@74 212 }
rc@74 213
rc@74 214 $(document).keypress(function(e){
rc@74 215 // console.log(e.which);
rob@90 216 // "0" button for hide chat
rob@90 217 var hcheckWebkitandIE=(e.which==48 ? 1 : 0);
rob@90 218 var hcheckMoz=(e.which==48 ? 1 : 0);
rc@74 219 if (hcheckWebkitandIE || hcheckMoz) {
rob@90 220 toggle_visibility('comms')
rc@74 221 }
rc@74 222 });
rc@74 223
rob@83 224 //*/
rc@74 225
rc@74 226 ////////////////////////////////////////////////
rc@74 227 // this needs to have a variable to define the websocket
rc@74 228 // otherwise we will pings from all sockets connected
rc@74 229 // no! the server broadcasts the ping and the clients emit the pong!
rc@74 230 // ah but then the time reported back from the server needs to be targeted
rc@74 231 // to specific client..
rob@93 232
rc@74 233 //////////////////////////////////////////////
rc@74 234 // Latency "Pong"
rob@90 235
rob@83 236 socket.on("timeFromServer", function(n) {
rob@83 237 socket.emit("clientTimeResponse",n); });
rc@74 238
rc@74 239 socket.on("latencyFromServer", function(latency) {
rc@74 240 $("#client_latency").text("Latency: "+latency+"ms.")
rc@74 241 });
rc@74 242
rc@74 243 function getLatencies(x) { socket.emit("getLatencies", x); }
rc@74 244
rc@74 245 //////////////////////////////////////////////
rc@74 246
rc@74 247
rc@74 248 socket.on('connect', function () {
rc@74 249 $('#chat').addClass('connected');
rc@74 250 });
rc@74 251
rc@74 252 socket.on('announcement', function (msg) {
rob@90 253 $('#Lines').append($('<p>').append($('<em>').text(msg)));
rc@74 254 });
rc@74 255
rob@91 256 socket.on('nicknames', function (nicknames) {
rc@74 257 $('#nicknames').empty().append($('<span>Online: </span>'));
rc@74 258 for (var i in nicknames) {
rc@74 259 $('#nicknames').append($('<b>').text(nicknames[i]));
rc@74 260 }
rc@74 261 });
rc@74 262
rc@74 263 socket.on('user message', message);
rc@74 264 socket.on('reconnect', function () {
rc@74 265 $('#lines').remove();
rc@74 266 message('System', 'Reconnected to the server');
rc@74 267 });
rc@74 268
rc@74 269 socket.on('reconnecting', function () {
rc@74 270 message('System', 'Attempting to re-connect to the server');
rc@74 271 });
rc@74 272
rc@74 273 socket.on('error', function (e) {
rc@74 274 message('System', e ? e : 'A unknown error occurred');
rc@74 275 });
rc@74 276
rc@74 277 function message (from, msg) {
rob@83 278 $('#lines').prepend($('<p>').prepend($('<b>').text(from), msg));
rc@74 279 }
rc@74 280
rc@74 281 // dom manipulation
rc@74 282 $(function () {
rc@74 283 $('#set-nickname').submit(function (ev) {
rc@74 284 socket.emit('nickname', $('#nick').val(), function (set) {
rc@74 285 if (!set) {
rc@74 286 clear();
rc@74 287 return $('#chat').addClass('nickname-set');
rc@74 288 }
rc@74 289 $('#nickname-err').css('visibility', 'visible');
rc@74 290 });
rc@74 291 return false;
rc@74 292 });
rc@74 293
rc@74 294 $('#send-message').submit(function () {
rc@74 295 message('me', $('#message').val());
rc@74 296 socket.emit('user message', $('#message').val());
rc@74 297 clear();
rc@74 298 $('#lines').get(0).scrollTop = 10000000;
rc@74 299 return false;
rc@74 300 });
rc@74 301
rc@74 302 function clear () {
rc@74 303 $('#message').val('').focus();
rc@74 304 };
rob@79 305 });