2020-10-16 08:06:03 +00:00
|
|
|
"use strict";
|
2020-10-01 19:13:23 +00:00
|
|
|
|
|
|
|
// Modules from file
|
2020-11-01 21:23:03 +00:00
|
|
|
const shared = require("./scripts/shared.js");
|
|
|
|
const networkHelper = require("./scripts/network-helper.js");
|
|
|
|
const scraper = require("./scripts/scraper.js");
|
|
|
|
const searcher = require("./scripts/searcher.js");
|
2020-11-01 23:14:28 +00:00
|
|
|
const uScraper = require("./scripts/user-scraper.js");
|
2020-10-12 08:27:48 +00:00
|
|
|
|
|
|
|
// Classes from file
|
2020-11-01 21:23:03 +00:00
|
|
|
const Credentials = require("./scripts/classes/credentials.js");
|
|
|
|
const GameInfo = require("./scripts/classes/game-info.js");
|
|
|
|
const LoginResult = require("./scripts/classes/login-result.js");
|
|
|
|
const UserData = require("./scripts/classes/user-data.js");
|
2020-09-29 15:11:43 +00:00
|
|
|
|
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-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
|
2020-10-02 15:43:14 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* Indicates whether a user is logged in to the F95Zone platform or not.
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2020-11-02 14:06:09 +00:00
|
|
|
/* istambul ignore next */
|
2020-11-02 14:26:06 +00:00
|
|
|
module.exports.isLogged = function isLogged() {
|
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
|
|
|
|
2020-10-02 12:01:51 +00:00
|
|
|
//#region Global variables
|
2020-10-21 13:31:11 +00:00
|
|
|
const USER_NOT_LOGGED = "User not authenticated, unable to continue";
|
2020-10-02 12:01:51 +00:00
|
|
|
//#endregion
|
|
|
|
|
2020-09-29 15:11:43 +00:00
|
|
|
//#region Export methods
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* Log in to the F95Zone platform.
|
|
|
|
* This **must** be the first operation performed before accessing any other script functions.
|
|
|
|
* @param {String} username Username used for login
|
|
|
|
* @param {String} password Password used for login
|
|
|
|
* @returns {Promise<LoginResult>} Result of the operation
|
|
|
|
*/
|
|
|
|
module.exports.login = async function (username, password) {
|
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}`);
|
2020-11-02 14:06:09 +00:00
|
|
|
const result = await networkHelper.authenticate(creds);
|
2020-10-29 21:14:40 +00:00
|
|
|
shared.isLogged = result.success;
|
2020-09-29 15:11:43 +00:00
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @public
|
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
|
|
|
*/
|
2020-11-01 21:23:03 +00:00
|
|
|
module.exports.checkIfGameHasUpdate = async function (info) {
|
|
|
|
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
|
2020-11-01 21:23:03 +00:00
|
|
|
const exists = await networkHelper.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
|
2020-11-01 21:23:03 +00:00
|
|
|
const onlineVersion = await scraper.getGameInfo(info.url).version;
|
|
|
|
|
|
|
|
// 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
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* 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
|
|
|
*/
|
2020-11-01 21:23:03 +00:00
|
|
|
module.exports.getGameData = async function (name, mod) {
|
|
|
|
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
|
|
|
|
let urls = [];
|
|
|
|
if(mod) urls = await searcher.searchMod(name);
|
|
|
|
else urls = await searcher.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
|
2020-11-02 09:21:36 +00:00
|
|
|
const info = await scraper.getGameInfo(url);
|
2020-11-01 21:23:03 +00:00
|
|
|
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
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
module.exports.getGameDataFromURL = async function (url) {
|
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
|
|
|
|
const exists = await networkHelper.urlExists(url);
|
|
|
|
if (!exists) throw new URIError(`${url} is not a valid URL`);
|
|
|
|
if (!networkHelper.isF95URL(url)) throw new Error(`${url} is not a valid F95Zone URL`);
|
|
|
|
|
2020-10-29 21:14:40 +00:00
|
|
|
// Get game data
|
2020-11-01 21:23:03 +00:00
|
|
|
return await scraper.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
|
|
|
/**
|
|
|
|
* @public
|
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
|
|
|
*/
|
2020-09-29 21:38:08 +00:00
|
|
|
module.exports.getUserData = async function () {
|
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 23:14:28 +00:00
|
|
|
return await uScraper.getUserData();
|
2020-10-02 15:52:59 +00:00
|
|
|
};
|
2020-09-29 15:11:43 +00:00
|
|
|
//#endregion
|