From af2d3a8f6c9fa0453fdf25c7ea62ff021ffa739a Mon Sep 17 00:00:00 2001
From: lowercasedonkey <lowercasedonkey@gmail.com>
Date: Mon, 3 Aug 2020 23:50:49 -0400
Subject: [PATCH] final fixes

---
 src/pregmod/FCTV/FCTV.js | 73 ++++++++++++++++++++++++++++------------
 1 file changed, 52 insertions(+), 21 deletions(-)

diff --git a/src/pregmod/FCTV/FCTV.js b/src/pregmod/FCTV/FCTV.js
index 5ccefb14552..9538cd91ac4 100644
--- a/src/pregmod/FCTV/FCTV.js
+++ b/src/pregmod/FCTV/FCTV.js
@@ -101,15 +101,20 @@ globalThis.FctvDisplay = function({usedRemote = 0, seeAll = 0} = {}) {
 		if (!usedRemote) {
 			p.append(`set FCTV to find a random show. Your larger-than-life screen flashes on, and is soon playing content from the popular streaming service. `);
 			if (V.cheatMode > 0 || V.debugMode > 0 || V.FCTV.remote) {
-				p.append(
+				// Create "Use remote" link.  Hide once clicked.
+				span = document.createElement("span");
+				span.id = "useRemote";
+				span.append(
 					App.UI.DOM.link(
 						"Use your remote",
 						() => {
 							usedRemote = 1;
 							jQuery("#remote").empty().append(createRemote());
+							jQuery("#useRemote").empty();
 						}
 					)
 				);
+				p.append(span);
 			}
 			p.append(randomShow());
 		} else if (usedRemote && (V.cheatMode > 0 || V.debugMode > 0 || V.FCTV.remote)) {
@@ -155,6 +160,9 @@ globalThis.FctvDisplay = function({usedRemote = 0, seeAll = 0} = {}) {
 		frag.append(p);
 		return jQuery("#FctvWatch").empty().append(frag);
 
+		/** Creates a remote control for FCTV
+		 * @returns {node}
+		 */
 		function createRemote() {
 			const p = document.createElement("p");
 			p.classList.add("FctvRemote");
@@ -164,7 +172,7 @@ globalThis.FctvDisplay = function({usedRemote = 0, seeAll = 0} = {}) {
 
 			for (const i of _possibleChannels) {
 				if (showChannel(i, {usedRemote: usedRemote, seeAll: seeAll}).canSelect > 0) {
-					if (V.FCTV.channel.selected !== i) {
+					if (V.FCTV.channel.selected !== i) { // Selected button
 						buttons.push(
 							App.UI.DOM.link(
 								i,
@@ -174,7 +182,7 @@ globalThis.FctvDisplay = function({usedRemote = 0, seeAll = 0} = {}) {
 								}
 							)
 						);
-					} else {
+					} else { // Channel you can choose
 						span = document.createElement("span");
 						span.classList.add("white");
 						span.style.fontWeight = "bold";
@@ -192,7 +200,7 @@ globalThis.FctvDisplay = function({usedRemote = 0, seeAll = 0} = {}) {
 						);
 						buttons.push(span);
 					}
-				} else {
+				} else { // Channel you cannot choose
 					buttons.push(
 						App.UI.DOM.disabledLink(i, [showChannel(i).text])
 					);
@@ -239,15 +247,19 @@ globalThis.FctvDisplay = function({usedRemote = 0, seeAll = 0} = {}) {
 
 		function randomShow() {
 			const frag = new DocumentFragment();
+
+			// Fallback if channel is bad
 			if (Number.isNaN(V.FCTV.channel.selected)) {
 				V.FCTV.channel.selected = 1;
 			}
 
 			if (!usedRemote) {
 				const channels = FCTV.channels();
-				channels.push(3); // Double chance for slave sale
+				channels.push(3); // Double chance for slave sale. Replaces old channel 4.
 				for (let i = 0; ; i++){
+					// Roll for a channel
 					const channel = _.sample(channels);
+					// See if we can show it
 					const x = showChannel(channel);
 					if (x.canSelect === -1) {
 						App.UI.DOM.appendNewElement("p", frag, channelFailed(x.text));
@@ -267,9 +279,10 @@ globalThis.FctvDisplay = function({usedRemote = 0, seeAll = 0} = {}) {
 		}
 
 		function showChannel(i = 1, {usedRemote = 0, seeAll = 0} = {}) {
-			let x = {
-				canSelect: 1,
-			};
+			let x = {canSelect: 1};
+			if (seeAll) {
+				return x;
+			}
 			if (App.Data.FCTV.channels.hasOwnProperty(i)) {
 				if (App.Data.FCTV.channels[i].hasOwnProperty("tags")) {
 					x = checkTags(App.Data.FCTV.channels[i].tags);
@@ -280,19 +293,35 @@ globalThis.FctvDisplay = function({usedRemote = 0, seeAll = 0} = {}) {
 					}
 					x.text = `This channel appears at random times`;
 				}
+			} else {
+				throw `Channel "${i}" does not exist`;
 			}
 			if (i === 11) {
 				if (V.purchasedSagBGone && V.FCTV.channel[num(i, true)] > 2) {
 					x.canSelect = -1; x.text = `Product purchase detected, skipping commercials`;
 				}
 			}
-
-			if (seeAll) { x.canSelect = 1; }
 			return x;
 		}
-	}
 
+		/**
+		 * Text to frame if content won't work due to tags.
+		 * @param {string} text Reason this channel/episode can't be shown
+		 * @returns {node}
+		 */
+		function channelFailed(text) {
+			const frag = new DocumentFragment;
+			frag.append(`A notification is shown: `);
+			App.UI.DOM.appendNewElement("span", frag, text, "note");
+			frag.append(`, changing program.`);
+			return frag;
+		}
+	}
 
+	/**
+	 * Displays just the channel itself, including art
+	 * @returns {node}
+	 */
 	function displayShow() {
 		const frag = new DocumentFragment();
 
@@ -300,6 +329,8 @@ globalThis.FctvDisplay = function({usedRemote = 0, seeAll = 0} = {}) {
 		/** @type {FctvChannel} */
 		const channel = App.Data.FCTV.channels[sel];
 		const epToShow = getEpisode(sel);
+
+		// Fail code, so we fail
 		if (epToShow === -1) {
 			frag.append(`no valid episodes`);
 			return frag;
@@ -308,6 +339,7 @@ globalThis.FctvDisplay = function({usedRemote = 0, seeAll = 0} = {}) {
 		// Increment the viewing record for this channel
 		V.FCTV.channel[num(sel, true)]++;
 
+		// Slave, if needed.  Hosts and market slaves.
 		let slave;
 		if (channel.episode[epToShow].slaves) {
 			slave = channel.episode[epToShow].slaves[0];
@@ -335,11 +367,13 @@ globalThis.FctvDisplay = function({usedRemote = 0, seeAll = 0} = {}) {
 		return frag;
 
 		function getEpisode(sel) {
-			let epToShow = -1;
+			let epToShow = -1; // -1 is the fail code.
 			const epsArray = [];
 			/** @type {FctvChannel} */
 			const channel = App.Data.FCTV.channels[sel];
 			const viewedCount = V.FCTV.channel[num(sel, true)] || 0;
+
+			// Produce an array of episodes we can watch.
 			for (let i = 0; i < App.Data.FCTV.channels[sel].episode.length; i++) {
 				const ep = App.Data.FCTV.channels[sel].episode[i];
 				if (ep.tags) {
@@ -353,7 +387,7 @@ globalThis.FctvDisplay = function({usedRemote = 0, seeAll = 0} = {}) {
 			}
 			const availableEp = epsArray.length;
 			if (epsArray.length === 0) {
-				return -1;
+				return -1; // Nothing to watch, fail.
 			}
 
 			if (availableEp > viewedCount) { // If we watched ep 0 last time, our view count will be 1.  Now we can use 1 as our new ep, etc.
@@ -372,6 +406,11 @@ globalThis.FctvDisplay = function({usedRemote = 0, seeAll = 0} = {}) {
 		}
 	}
 
+	/**
+	 * Checks the tags on a channel or an episode to determine if it can be shown.
+	 * @param {FctvTags} tags
+	 * @returns {Object} x
+	 */
 	function checkTags(tags){
 		let x = {
 			canSelect: 1,
@@ -416,12 +455,4 @@ globalThis.FctvDisplay = function({usedRemote = 0, seeAll = 0} = {}) {
 		}
 		return x;
 	}
-
-	function channelFailed(text) {
-		const frag = new DocumentFragment;
-		frag.append(`A notification is shown: `);
-		App.UI.DOM.appendNewElement("span", frag, text, "note");
-		frag.append(`, changing program.`);
-		return frag;
-	}
 };
-- 
GitLab