2020-10-16 08:06:03 +00:00
|
|
|
"use strict";
|
2020-10-01 19:13:23 +00:00
|
|
|
|
|
|
|
// Modules from file
|
2021-02-15 21:26:18 +00:00
|
|
|
import shared from "./scripts/shared.js";
|
|
|
|
import { authenticate, urlExists, isF95URL } from "./scripts/network-helper.js";
|
|
|
|
import { getGameInfo } from "./scripts/scraper.js";
|
|
|
|
import { searchGame, searchMod } from "./scripts/searcher.js";
|
|
|
|
import { getUserData as retrieveUserData } from "./scripts/user-scraper.js";
|
|
|
|
import { fetchLatest } from "./scripts/latest-fetch.js";
|
2020-12-17 21:44:40 +00:00
|
|
|
const fetchPlatformData = require("./scripts/platform-data.js").fetchPlatformData;
|
2020-10-12 08:27:48 +00:00
|
|
|
|
|
|
|
// Classes from file
|
2021-02-15 21:26:18 +00:00
|
|
|
import Credentials from "./scripts/classes/credentials.js";
|
|
|
|
import GameInfo from "./scripts/classes/game-info.js";
|
|
|
|
import LoginResult from "./scripts/classes/login-result.js";
|
|
|
|
import UserData from "./scripts/classes/user-data.js";
|
|
|
|
import PrefixParser from "./scripts/classes/prefix-parser.js";
|
2020-09-29 15:11:43 +00:00
|
|
|
|
2020-12-26 14:23:41 +00:00
|
|
|
//#region Global variables
|
|
|
|
const USER_NOT_LOGGED = "User not authenticated, unable to continue";
|
|
|
|
//#endregion
|
|
|
|
|
2020-10-03 16:16:45 +00:00
|
|
|
//#region Export classes
|
2020-10-02 12:01:51 +00:00
|
|
|
module.exports.GameInfo = GameInfo;
|
|
|
|
module.exports.LoginResult = LoginResult;
|
|
|
|
module.exports.UserData = UserData;
|
2020-12-03 10:55:06 +00:00
|
|
|
module.exports.PrefixParser = PrefixParser;
|
2020-10-03 16:16:45 +00:00
|
|
|
//#endregion Export classes
|
2020-09-29 15:11:43 +00:00
|
|
|
|
2020-10-03 16:16:45 +00:00
|
|
|
//#region Export properties
|
2020-09-29 15:11:43 +00:00
|
|
|
/**
|
2020-11-02 14:26:06 +00:00
|
|
|
* @public
|
|
|
|
* Set the logger level for module debugging.
|
2020-09-29 15:11:43 +00:00
|
|
|
*/
|
2020-11-02 14:06:09 +00:00
|
|
|
/* istambul ignore next */
|
2020-11-02 14:26:06 +00:00
|
|
|
module.exports.loggerLevel = shared.logger.level;
|
|
|
|
exports.loggerLevel = "warn"; // By default log only the warn messages
|
2021-02-15 21:26:18 +00:00
|
|
|
|
2020-10-02 15:43:14 +00:00
|
|
|
/**
|
|
|
|
* Indicates whether a user is logged in to the F95Zone platform or not.
|
|
|
|
*/
|
2021-02-15 21:26:18 +00:00
|
|
|
export function isLogged(): boolean {
|
2020-10-29 21:14:40 +00:00
|
|
|
return shared.isLogged;
|
2020-09-29 15:11:43 +00:00
|
|
|
};
|
2020-10-03 16:16:45 +00:00
|
|
|
//#endregion Export properties
|
2020-09-29 15:11:43 +00:00
|
|
|
|
|
|
|
//#region Export methods
|
|
|
|
/**
|
|
|
|
* Log in to the F95Zone platform.
|
|
|
|
* This **must** be the first operation performed before accessing any other script functions.
|
|
|
|
* @returns {Promise<LoginResult>} Result of the operation
|
|
|
|
*/
|
2021-02-15 21:26:18 +00:00
|
|
|
export async function login(username: string, password: string): Promise<LoginResult> {
|
2020-11-22 09:42:43 +00:00
|
|
|
/* istanbul ignore next */
|
2020-10-29 21:14:40 +00:00
|
|
|
if (shared.isLogged) {
|
2020-11-01 21:23:03 +00:00
|
|
|
shared.logger.info(`${username} already authenticated`);
|
|
|
|
return new LoginResult(true, `${username} already authenticated`);
|
2020-10-29 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 21:23:03 +00:00
|
|
|
shared.logger.trace("Fetching token...");
|
|
|
|
const creds = new Credentials(username, password);
|
|
|
|
await creds.fetchToken();
|
2020-10-02 12:01:51 +00:00
|
|
|
|
2020-11-01 21:23:03 +00:00
|
|
|
shared.logger.trace(`Authentication for ${username}`);
|
2021-02-15 21:26:18 +00:00
|
|
|
const result = await authenticate(creds);
|
|
|
|
shared.setIsLogged(result.success);
|
2020-09-29 15:11:43 +00:00
|
|
|
|
2020-12-17 21:44:40 +00:00
|
|
|
// Load platform data
|
|
|
|
if (result.success) await fetchPlatformData();
|
|
|
|
|
2020-12-15 14:22:10 +00:00
|
|
|
/* istambul ignore next */
|
2020-11-01 21:23:03 +00:00
|
|
|
if (result.success) shared.logger.info("User logged in through the platform");
|
|
|
|
else shared.logger.warn(`Error during authentication: ${result.message}`);
|
|
|
|
|
2020-10-29 21:14:40 +00:00
|
|
|
return result;
|
2020-10-02 15:52:59 +00:00
|
|
|
};
|
|
|
|
|
2020-09-29 15:11:43 +00:00
|
|
|
/**
|
2020-10-10 09:33:54 +00:00
|
|
|
* Chek if exists a new version of the game.
|
2020-09-29 15:11:43 +00:00
|
|
|
* You **must** be logged in to the portal before calling this method.
|
|
|
|
* @param {GameInfo} info Information about the game to get the version for
|
2020-10-10 09:33:54 +00:00
|
|
|
* @returns {Promise<Boolean>} true if an update is available, false otherwise
|
2020-09-29 15:11:43 +00:00
|
|
|
*/
|
2021-02-15 21:26:18 +00:00
|
|
|
export async function checkIfGameHasUpdate(info: GameInfo): Promise<boolean> {
|
2020-11-22 09:42:43 +00:00
|
|
|
/* istanbul ignore next */
|
2020-11-01 21:23:03 +00:00
|
|
|
if (!shared.isLogged) {
|
2020-10-29 21:14:40 +00:00
|
|
|
shared.logger.warn(USER_NOT_LOGGED);
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-29 15:11:43 +00:00
|
|
|
|
2020-10-29 21:14:40 +00:00
|
|
|
// F95 change URL at every game update,
|
|
|
|
// so if the URL is different an update is available
|
2021-02-15 21:26:18 +00:00
|
|
|
const exists = await urlExists(info.url, true);
|
2020-10-29 21:14:40 +00:00
|
|
|
if (!exists) return true;
|
2020-10-12 12:11:36 +00:00
|
|
|
|
2020-10-29 21:14:40 +00:00
|
|
|
// Parse version from title
|
2021-02-15 21:26:18 +00:00
|
|
|
const onlineInfo = await getGameInfo(info.url);
|
2020-11-02 17:32:36 +00:00
|
|
|
const onlineVersion = onlineInfo.version;
|
2020-11-01 21:23:03 +00:00
|
|
|
|
|
|
|
// Compare the versions
|
2020-10-29 21:14:40 +00:00
|
|
|
return onlineVersion.toUpperCase() !== info.version.toUpperCase();
|
2020-10-02 15:52:59 +00:00
|
|
|
};
|
2020-11-01 21:23:03 +00:00
|
|
|
|
2020-09-29 15:11:43 +00:00
|
|
|
/**
|
|
|
|
* Starting from the name, it gets all the information about the game you are looking for.
|
|
|
|
* You **must** be logged in to the portal before calling this method.
|
|
|
|
* @param {String} name Name of the game searched
|
2020-11-01 21:23:03 +00:00
|
|
|
* @param {Boolean} mod Indicate if you are looking for mods or games
|
2020-10-02 15:52:59 +00:00
|
|
|
* @returns {Promise<GameInfo[]>} List of information obtained where each item corresponds to
|
2020-10-21 13:31:11 +00:00
|
|
|
* an identified game (in the case of homonymy of titles)
|
2020-09-29 15:11:43 +00:00
|
|
|
*/
|
2021-02-15 21:26:18 +00:00
|
|
|
export async function getGameData (name: string, mod: boolean): Promise<GameInfo[]> {
|
2020-11-22 09:42:43 +00:00
|
|
|
/* istanbul ignore next */
|
2020-11-01 21:23:03 +00:00
|
|
|
if (!shared.isLogged) {
|
2020-10-29 21:14:40 +00:00
|
|
|
shared.logger.warn(USER_NOT_LOGGED);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-11-01 21:23:03 +00:00
|
|
|
// Gets the search results of the game/mod being searched for
|
2020-12-15 13:09:16 +00:00
|
|
|
const urls = mod ?
|
2021-02-15 21:26:18 +00:00
|
|
|
await searchMod(name) :
|
|
|
|
await searchGame(name);
|
2020-10-29 21:14:40 +00:00
|
|
|
|
|
|
|
// Process previous partial results
|
2020-11-01 21:23:03 +00:00
|
|
|
const results = [];
|
|
|
|
for (const url of urls) {
|
|
|
|
// Start looking for information
|
2021-02-15 21:26:18 +00:00
|
|
|
const info = await getGameInfo(url);
|
2020-12-15 13:09:16 +00:00
|
|
|
if (info) results.push(info);
|
2020-10-29 21:14:40 +00:00
|
|
|
}
|
2020-11-01 21:23:03 +00:00
|
|
|
return results;
|
2020-10-02 15:52:59 +00:00
|
|
|
};
|
2020-11-01 21:23:03 +00:00
|
|
|
|
2020-10-10 09:45:43 +00:00
|
|
|
/**
|
|
|
|
* Starting from the url, it gets all the information about the game you are looking for.
|
|
|
|
* You **must** be logged in to the portal before calling this method.
|
|
|
|
* @param {String} url URL of the game to obtain information of
|
|
|
|
* @returns {Promise<GameInfo>} Information about the game. If no game was found, null is returned
|
|
|
|
*/
|
2021-02-15 21:26:18 +00:00
|
|
|
export async function getGameDataFromURL(url: string): Promise<GameInfo> {
|
2020-11-22 09:42:43 +00:00
|
|
|
/* istanbul ignore next */
|
2020-11-01 21:23:03 +00:00
|
|
|
if (!shared.isLogged) {
|
2020-10-29 21:14:40 +00:00
|
|
|
shared.logger.warn(USER_NOT_LOGGED);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-11-01 21:23:03 +00:00
|
|
|
// Check URL validity
|
2021-02-15 21:26:18 +00:00
|
|
|
const exists = await urlExists(url);
|
2020-11-01 21:23:03 +00:00
|
|
|
if (!exists) throw new URIError(`${url} is not a valid URL`);
|
2021-02-15 21:26:18 +00:00
|
|
|
if (!isF95URL(url)) throw new Error(`${url} is not a valid F95Zone URL`);
|
2020-11-01 21:23:03 +00:00
|
|
|
|
2020-10-29 21:14:40 +00:00
|
|
|
// Get game data
|
2021-02-15 21:26:18 +00:00
|
|
|
return await getGameInfo(url);
|
2020-10-10 09:45:43 +00:00
|
|
|
};
|
2020-11-01 21:23:03 +00:00
|
|
|
|
2020-09-29 15:11:43 +00:00
|
|
|
/**
|
2020-09-29 21:38:08 +00:00
|
|
|
* Gets the data of the currently logged in user.
|
2020-10-02 15:43:14 +00:00
|
|
|
* You **must** be logged in to the portal before calling this method.
|
2020-10-21 13:31:11 +00:00
|
|
|
* @returns {Promise<UserData>} Data of the user currently logged in
|
2020-09-29 15:11:43 +00:00
|
|
|
*/
|
2021-02-15 21:26:18 +00:00
|
|
|
export async function getUserData(): Promise<UserData> {
|
2020-11-22 09:42:43 +00:00
|
|
|
/* istanbul ignore next */
|
2020-11-01 21:23:03 +00:00
|
|
|
if (!shared.isLogged) {
|
2020-10-29 21:14:40 +00:00
|
|
|
shared.logger.warn(USER_NOT_LOGGED);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-02-15 21:26:18 +00:00
|
|
|
return await retrieveUserData();
|
2020-10-02 15:52:59 +00:00
|
|
|
};
|
2020-11-30 09:40:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the latest updated games that match the specified parameters.
|
|
|
|
* You **must** be logged in to the portal before calling this method.
|
|
|
|
* @param {Object} args
|
|
|
|
* Parameters used for the search.
|
|
|
|
* @param {String[]} [args.tags]
|
|
|
|
* List of tags to be included in the search (max 5).
|
|
|
|
* @param {Number} [args.datelimit]
|
|
|
|
* Number of days since the game was last updated.
|
|
|
|
* The entered value will be approximated to the nearest valid one.
|
|
|
|
* Use `0` to select no time limit.
|
|
|
|
* @param {String[]} [args.prefixes]
|
|
|
|
* Prefixes to be included in the search.
|
|
|
|
* @param {String} [args.sorting]
|
|
|
|
* Method of sorting the results between (default: `date`):
|
2020-11-30 12:23:24 +00:00
|
|
|
* `date`, `likes`, `views`, `name`, `rating`
|
2020-11-30 09:40:23 +00:00
|
|
|
* @param {Number} limit Maximum number of results
|
|
|
|
* @returns {Promise<GameInfo[]>} List of games
|
|
|
|
*/
|
2021-02-15 21:26:18 +00:00
|
|
|
export async function getLatestUpdates(args, limit: number): Promise<GameInfo[]> {
|
2020-11-30 09:40:23 +00:00
|
|
|
// Check limit value
|
|
|
|
if(limit <= 0) throw new Error("limit must be greater than 0");
|
|
|
|
|
|
|
|
// Prepare the parser
|
2020-12-03 10:55:06 +00:00
|
|
|
const parser = new PrefixParser();
|
2020-11-30 09:40:23 +00:00
|
|
|
|
|
|
|
// Get the closest date limit
|
|
|
|
let filterDate = 0;
|
|
|
|
if(args.datelimit) {
|
|
|
|
const validDate = [365, 180, 90, 30, 14, 7, 3, 1, 0];
|
2020-12-26 14:23:41 +00:00
|
|
|
filterDate = getNearestValueFromArray(validDate, args.datelimit);
|
2020-11-30 09:40:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the games
|
|
|
|
const query = {
|
2020-12-03 10:55:06 +00:00
|
|
|
tags: args.tags ? parser.prefixesToIDs(args.tags) : [],
|
|
|
|
prefixes: args.prefixes ? parser.prefixesToIDs(args.prefixes) : [],
|
2020-11-30 09:40:23 +00:00
|
|
|
sort: args.sorting ? args.sorting : "date",
|
|
|
|
date: filterDate,
|
|
|
|
};
|
2021-02-15 21:26:18 +00:00
|
|
|
const urls = await fetchLatest(query, limit);
|
2020-11-30 09:40:23 +00:00
|
|
|
// Get the gamedata from urls
|
2021-02-15 21:26:18 +00:00
|
|
|
const promiseList = urls.map((u: string) => exports.getGameDataFromURL(u));
|
2020-11-30 13:01:16 +00:00
|
|
|
return await Promise.all(promiseList);
|
2020-11-30 09:40:23 +00:00
|
|
|
};
|
2020-09-29 15:11:43 +00:00
|
|
|
//#endregion
|
2020-12-26 14:23:41 +00:00
|
|
|
|
|
|
|
//#region Private Methods
|
|
|
|
/**
|
|
|
|
* Given an array of numbers, get the nearest value for a given `value`.
|
|
|
|
* @param {Number[]} array List of default values
|
|
|
|
* @param {Number} value Value to search
|
|
|
|
*/
|
2021-02-15 21:26:18 +00:00
|
|
|
function getNearestValueFromArray(array: number[], value: number) {
|
2020-12-26 14:23:41 +00:00
|
|
|
// Script taken from:
|
|
|
|
// https://www.gavsblog.com/blog/find-closest-number-in-array-javascript
|
|
|
|
array.sort((a, b) => {
|
|
|
|
return Math.abs(value - a) - Math.abs(value - b);
|
|
|
|
});
|
|
|
|
return array[0];
|
|
|
|
}
|
|
|
|
//#endregion
|