From dfa9548b5997d311aa597615c02628b7433a785b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 16 Sep 2021 04:46:42 +0300 Subject: [PATCH] Add Stations class --- src/client/classes/Stations.js | 105 +++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/client/classes/Stations.js diff --git a/src/client/classes/Stations.js b/src/client/classes/Stations.js new file mode 100644 index 0000000..4f9a631 --- /dev/null +++ b/src/client/classes/Stations.js @@ -0,0 +1,105 @@ +const _importDynamic = new Function('modulePath', 'return import(modulePath)'); +const fetch = (...args) => _importDynamic('node-fetch').then(({default: fetch}) => fetch(...args)); + +module.exports = class { + constructor() { + this.list = new Array(); + this.logger = require("../funcs/logger.js"); + } + + async fetch(options){ + try { + this.logger('Stations', 'Started fetching list – ' + options.url); + let list = await fetch(options.url) + .then(this.checkFetchStatus) + .then(response => response.json()); + + if(!this.list){ + this.logger('Stations'); + this.list.forEach(station => { + console.log("- " + station.name); + }); + console.log("\n"); + } + + this.list = list; + this.logger('Stations', 'Successfully fetched list'); + } catch (error) { + this.logger('Stations', 'Fetching list failed'); + console.error(error + "\n"); + + this.fetch(options); + } + } + + checkFetchStatus(response) { + if (response.ok) { // res.status >= 200 && res.status < 300 + return response; + } else { + throw new Error(response.status + " " + response.statusText); + } + } + + search(key) { + if (this.list === null) return false; + let foundStations = []; + if (!key) return false; + if (key == "radio") return false; + + this.list + .filter( + x => x.name.toUpperCase().includes(key.toUpperCase()) || x === key + ) + .forEach(x => + foundStations.push({ station: x, name: x.name, probability: 100 }) + ); + + if (key.startsWith("radio ")) key = key.slice(6); + const probabilityIncrement = 100 / key.split(" ").length / 2; + for (let i = 0; i < key.split(" ").length; i++) { + this.list + .filter( + x => x.name.toUpperCase().includes(key.split(" ")[i].toUpperCase()) || x === key + ) + .forEach(x => + foundStations.push({ station: x, name: x.name, probability: probabilityIncrement }) + ); + } + if (foundStations.length === 0) return false; + for (let i = 0; i < foundStations.length; i++) { + for (let j = 0; j < foundStations.length; j++) { + if (foundStations[i] === foundStations[j] && i !== j) foundStations.splice(i, 1); + } + } + for (let i = 0; i < foundStations.length; i++) { + if (foundStations[i].name.length > key.length) { + foundStations[i].probability -= + (foundStations[i].name.split(" ").length - key.split(" ").length) * + (probabilityIncrement * 0.5); + } else if (foundStations[i].name.length === key.length) { + foundStations[i].probability += probabilityIncrement * 0.9; + } + + for (let j = 0; j < key.split(" ").length; j++) { + if (!foundStations[i].name.toUpperCase().includes(key.toUpperCase().split(" ")[j])) { + foundStations[i].probability -= probabilityIncrement * 0.5; + } + } + } + let highestProbabilityStation; + for (let i = 0; i < foundStations.length; i++) { + if ( + !highestProbabilityStation || + highestProbabilityStation.probability < foundStations[i].probability + ) + highestProbabilityStation = foundStations[i]; + if ( + highestProbabilityStation && + highestProbabilityStation.probability === foundStations[i].probability + ) { + highestProbabilityStation = foundStations[i].station; + } + } + return highestProbabilityStation; + } +};