kernel: function(value) { var match, number; if ((match = value.match(/^\s*(please)?\s*factor\s*(\d+)\s*\,?\s*(please)?\s*$/i)) != null) { number = parseInt(match[2]); } else { return false; } if (number < 1) { return "Can't factor " + $$.pretty(number); } else { var factors = factor(number); if (factors.length == 1) { return $$.pretty(number) + " is prime."; } else { return $$.pretty(number) + " = " + $$.select(factor(number), function(factor) { return $$.pretty(factor); }).join(" * "); } } }
_this.getAllEpisodes = function() { return $$.flatten($$.select(_seasons, function(season) { return season.episodes(); })); }
Season.getEpisodes = function(seasons) { return $$.flatten($$.select(seasons, function(season) { return season.episodes(); })); };
/** * Get a JSON array of all the MST3K episodes with associated meta information. * @return {!Array.<!{episode: string}>} */ function getMst3kEpisodesMeta() { var show = Show.get("MST3K"); var eps = $$.select(show.getAllEpisodes(), function(ep) { var record = { season: ep.season().name(), episode: ep.toString(), number: ep.number(), title: ep.title() }; // Determine host. var season = +record.season; if (record.season == "K" || !isNaN(season) && ( season <= 4 || season == 5 && ep.number() <= 12)) { record.host = "Joel"; } else { record.host = "Mike"; } // Determine who was Tom. if (record.season == "K" || !isNaN(season) && season <= 1) { record.Tom = "Josh Weinstein"; } else { record.Tom = "Kevin Murphy"; } // Determine who was Crow. if (record.season == "K" || record.season == "M" || !isNaN(season) && season <= 7) { record.Crow = "Trace Beaulieu"; } else { record.Crow = "Bill Corbett"; } // Determine the Mads. record.Mads = []; // - First the primary Mad. if (isNaN(season) || season <= 7) { record.Mads.push("Dr. Forrester"); } else { record.Mads.push("Pearl Forrester"); } // - And then the sidekicks. if (record.season == "K" || !isNaN(season) && season <= 6) { record.Mads.push("TV's Frank"); } else if (!isNaN(season)) { if (season == 7) { record.Mads.push("Pearl Forrester"); } else if (season == 8 && record.number < 13) { // ? record.Mads.push("Professor Bobo"); } else { record.Mads.push("Observer/Brain Guy"); record.Mads.push("Professor Bobo"); } } return record; }); // console.log(eps); return eps; }
// Check for a request for a random MST3K episode function checkMstConsole(value) { if (!value.match(/^mst(\s|$)/)) { return; } var args = value.split(/\s+/), show = Show.get("MST3K"), seasons = show.seasons(), mode = "random", lookups = [], searchTerm, match; for (var i = 1; i < args.length; ++i) { var arg = args[i]; if (arg.indexOf("-s=") === 0) { // Include only these seasons seasons = assertSeasons(seasons, arg.substr(3)); mode = "random"; } else if (arg.indexOf("-ns=") === 0) { // They want to disallow these seasons seasons = deassertSeasons(seasons, arg.substr(4)); mode = "random"; } else if (arg.indexOf("-f=") === 0) { mode = "find"; searchTerm = arg.substr(3); } else if (arg.match(/^[k\d]\d{2,3}(,[k\d]\d{2,3})*$/i)) { // They want to look up specific episodes mode = "lookup"; lookups = lookups.concat(arg.split(/,/)); } else if (arg.match(/^((k|\d{1,2})\d{2})\-(k?\d+)$/i)) { // They're interested in a range of episodes mode = "lookup"; $$.each(show.getEpRange(arg), function(ep) { lookups.push(ep.toString()); }); } } // Perform the user action if (mode == "random") { var list = Season.getEpisodes(seasons), // getEpList(seasons), ix = Math.floor(list.length * Math.random()); return "Your random " + show.name() + " episode (from season" + (_keys(seasons).length > 1 ? "s" : "") + " " + _englishJoin(_keys(seasons)) + ") is:<br/>\n" + list[ix].toString(true); } else if (mode == "find") { // Display episodes that match 'searchTerm' var regex = new RegExp(searchTerm, "i"), list = $$.where(Season.getEpisodes(seasons), function(item) { return item.toString(true).match(regex) != null; }); return "Your search over '" + searchTerm + "' yielded:<br/>\n" + $$.select(list, function(ep) { var str = ep.toString(true), match = str.match(regex); return str.substr(0, match.index) + "<b>" + match[0] + "</b>" + str.substr(match.index + match[0].length); }).join("<br/>\n"); } else if (mode == "lookup") { var str = ""; $$.each(lookups, function(ep) { if (!(ep = show.getEpisode(ep))) { return; } var randID = "search" + ("" + Math.random()).substr(2), q = show.name().toLowerCase() + " " + ep.title().match(/^([^a-z]*)/)[0].replace(/(^\s+|\s+$)/g,""); str += (str.length > 0 ? "<br />\n" : "") + ep.toString(true);// + " - " + ep.title(); if (node) { // In test mode, don't do anything more. return; } console.log(q); str += '<div id="' + randID + '">Fetching YouTube links ...</div>'; youtubeQueue.attach(function(signal) { // Excise the name for the search var request = gapi.client.youtube.search.list({ q: q, part: 'snippet', maxResults: 5, order: 'relevance', type: 'video' //videoDuration: 'long' }); request.execute(function(response) { signal(); // Score each thing by looking up its info var vids = [], // The number of in-flight requests, nWaiting = 0, // A continuation for when we're done with everything cont = function() { // Filter out any that are too short, and keep only the top 5 vids = $$.where(vids, function(vid) { return vid.data.data.duration >= 3600; }).slice(0, 5); var vidLinks = []; $$.each(vids, function(vid) { vidLinks.push('<div class="yt-video-link">' + '<a href="https://www.youtube.com/watch?v=' + vid.item.id.videoId + '" title="' + vid.item.snippet.title + '">' + '<img src="' + vid.item.snippet.thumbnails["default"].url + '"></a>' + '</img>' + '<br />' + vid.item.snippet.title + ' (' + Math.round(vid.data.data.duration / 60) + 'm)' + '</div>'); }); $('#' + randID).html(vidLinks.join("") + '<div style="clear: both;"></div>'); }; $$.each(response.items, function(item, ix) { if (item.id.videoId) { ++nWaiting; youtubeQueue.attach(function(signal) { $.getJSON('http://gdata.youtube.com/feeds/api/videos/' + item.id.videoId + '?v=2&alt=jsonc', function(data) { signal(); vids.push({ item: item, data: data }) if (--nWaiting == 0) { // Everyone is back, so run the continuation cont(); } }); }); } }); }); }); }); return "The " + show.name() + " episode" + (lookups.length > 1 ? "s" : "") + " you requested " + (lookups.length > 1 ? "are" : "is") + ":<br />\n" + str; } }