Created
December 21, 2016 16:54
-
-
Save emiflake/4fb88020a23cdd956718203ad3b5d13f to your computer and use it in GitHub Desktop.
Some talkomatic utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.prototype.shuffle = function () { | |
var a = this.split(""), | |
n = a.length; | |
for(var i = n - 1; i > 0; i--) { | |
var j = Math.floor(Math.random() * (i + 1)); | |
var tmp = a[i]; | |
a[i] = a[j]; | |
a[j] = tmp; | |
} | |
return a.join(""); | |
} | |
var compareStrings = function( s, ss ) { | |
return s.indexOf(ss) !== -1 | |
} | |
var oldC = ""; | |
function msg_recd() { | |
var A = $("#text1").val(); | |
var B = $("#text2").val(); | |
var C = $("#text3").val(); | |
var active = B; | |
console.log(active.split('')); | |
if ( active != oldC && active.split('')[0] == "=") { | |
oldC = active; | |
setText(eval(active.substring(1))); | |
} | |
} | |
function incoming (message) { | |
msg_recd(); | |
yak ("Received: " + message); | |
serverWaitTimer(); // Received something from server, so reset timer | |
/* Split message into pieces. Note that we can't just split the entire string using the vertical bar | |
separator because the text portion of U or A commands could include vertical bars. */ | |
var msgpart = split_rem(message, "|", 2); | |
var command = msgpart[0]; // Get the one-letter command code | |
var theslot = msgpart[1]; // Get slot number sent with this message | |
var therest = msgpart[2]; // Remainder of the message after command and slot | |
var slotId = '#slot' + theslot; // ID of the slot's main DIV | |
var textId = '#text' + theslot; // ID of the slot's textarea | |
var textAr = $('textarea' + textId)[0]; // Handle on the textarea | |
var thetext; | |
var scrollAmount; | |
switch (command) { | |
case 'U': /* Update (update to a talker's text): U|slot|deleteChars|changeIndex|newStuff */ | |
/* Nonsense values for deleteChars or changeIndex could cause odd effects here but won't crash the program. */ | |
msgpart = split_rem(therest, "|", 2); | |
thetext = $(textAr).val().substring( msgpart[0] ); // Delete specified number of characters from beginning | |
thetext = thetext.substring(0, msgpart[1] ) + msgpart[2]; // Replace/Add new text at indicated start position | |
$(textAr).val( thetext ); | |
scrollAmount = textAr.scrollHeight - textAr.clientHeight; | |
if (scrollAmount > 0) { // Scroll down if necessary to keep bottom-most text visible | |
$(textAr).scrollTop(scrollAmount); | |
} | |
break; | |
case 'A': /* All (all of a talker's information and text): A|slot|name|location|3rd party authenticator|profile URL|text */ | |
msgpart = split_rem(therest, "|", 4); | |
slotInit (theslot, msgpart[0], msgpart[1], msgpart[2], msgpart[3]); // Display user's name and location | |
$(textAr).val(msgpart[4]); // Display user's text | |
// $(textAr).scrollTop( textAr.scrollHeight - textAr.clientHeight ); | |
break; | |
case 'X': /* Exit (talker exited): X|slot */ | |
slotClear (theslot); // Clear the slot header and text | |
break; | |
case 'E': /* Enter (new talker entered): E|slot|name|location|3rd party authenticator|profile URL */ | |
msgpart = therest.split("|"); | |
slotInit (theslot, msgpart[0], msgpart[1], msgpart[2], msgpart[3]); // Display new user's name and location | |
/* Respond by sending A command with full text. Server adds slot, my name, and my location and forwards it to new talker */ | |
sendomatic ( 'A|' + $(TG.myBlather).val() ); | |
if (TG.sound) { | |
/* Firefox plays the sound every time without redoing the "new Audio" line below, | |
but Chrome plays it only once unless we redo this every time. */ | |
EntrySound = new Audio('/sounds/doorbell.mp3'); | |
EntrySound.play(); | |
} | |
break; | |
case 'W': /* Where (server tells client what slot talker is in): W|slot */ | |
if (isNaN(theslot)) { | |
/* Slot number empty or nonnumeric value means the room is full or another problem prevents entry. */ | |
if ( (theslot === '') || (typeof(theslot) === 'undefined') ) { | |
theslot = "full"; | |
} | |
window.location = "index.html#" + theslot; | |
} | |
else { | |
getMySlot(theslot); | |
} | |
break; | |
case 'R': /* Room info: R|host's slot|privacy code */ | |
TG.roomHost = theslot; | |
TG.roomPrivacy = therest; | |
showPrivacy(); | |
break; | |
case 'P': /* Poke (server tells client connection is still alive) */ | |
break; | |
} // end of switch | |
} | |
function setText(text) { | |
sendomatic( 'U|0|0|' + text ); | |
$("#text0").val(text) | |
}; | |
function generateMatrix(length) { | |
var text = ""; | |
for ( var i = 0; i < length; i++ ) { | |
text += String(Math.ceil(Math.random() - 0.5)); | |
} | |
return text; | |
} | |
var interval = setInterval(() => { | |
setText(generateMatrix(1000)); | |
}, 500 ); | |
setTimeout(() => {clearInterval(interval)}, 3000); | |
slowType = function(text, speed) { | |
var l = text.length; | |
var cache = ""; | |
var interval = setInterval(() => { | |
cache+=text[cache.length]; | |
setText(cache); | |
if ( cache.length >= l ) { | |
clearInterval(interval); | |
} | |
}, speed); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment