Converted classes scripts
parent
df8b77fa4e
commit
c6bf753f29
|
@ -1,22 +1,35 @@
|
|||
"use strict";
|
||||
|
||||
// Modules from file
|
||||
const { getF95Token } = require("../network-helper.js");
|
||||
import { getF95Token } from "../network-helper.js";
|
||||
|
||||
class Credentials {
|
||||
constructor(username, password) {
|
||||
/**
|
||||
* Represents the credentials used to access the platform.
|
||||
*/
|
||||
export class Credentials {
|
||||
/**
|
||||
* Username
|
||||
*/
|
||||
username: string;
|
||||
/**
|
||||
* Password of the user.
|
||||
*/
|
||||
password: string;
|
||||
/**
|
||||
* One time token used during login.
|
||||
*/
|
||||
token: string = null;
|
||||
|
||||
constructor(username: string, password: string) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.token = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
* Fetch and save the token used to log in to F95Zone.
|
||||
*/
|
||||
async fetchToken() {
|
||||
async fetchToken(): Promise<void> {
|
||||
this.token = await getF95Token();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Credentials;
|
|
@ -1,97 +1,82 @@
|
|||
"use strict";
|
||||
|
||||
class GameInfo {
|
||||
constructor() {
|
||||
/**
|
||||
* Information of a game/mod on the platform.
|
||||
*/
|
||||
export class GameInfo {
|
||||
//#region Properties
|
||||
/**
|
||||
* Unique ID of the game on the platform.
|
||||
* @type Number
|
||||
*/
|
||||
this.id = -1;
|
||||
id = -1;
|
||||
/**
|
||||
* Game name
|
||||
* @type String
|
||||
*/
|
||||
this.name = null;
|
||||
name: string = null;
|
||||
/**
|
||||
* Game author
|
||||
* @type String
|
||||
*/
|
||||
this.author = null;
|
||||
author: string = null;
|
||||
/**
|
||||
* URL to the game's official conversation on the F95Zone portal
|
||||
* @type String
|
||||
*/
|
||||
this.url = null;
|
||||
url: string = null;
|
||||
/**
|
||||
* Game description
|
||||
* @type String
|
||||
*/
|
||||
this.overview = null;
|
||||
overview: string = null;
|
||||
/**
|
||||
* Game language.
|
||||
* @type String
|
||||
* Game language
|
||||
*/
|
||||
this.language = null;
|
||||
language: string = null;
|
||||
/**
|
||||
* List of supported OS.
|
||||
* @type
|
||||
* List of supported OS
|
||||
*/
|
||||
this.supportedOS = [];
|
||||
supportedOS: string[] = [];
|
||||
/**
|
||||
* Specify whether the game has censorship
|
||||
* measures regarding NSFW scenes.
|
||||
* @type Boolean
|
||||
* measures regarding NSFW scenes
|
||||
*/
|
||||
this.censored = null;
|
||||
censored: boolean = null;
|
||||
/**
|
||||
* List of tags associated with the game
|
||||
* @type String[]
|
||||
*/
|
||||
this.tags = [];
|
||||
tags: string[] = [];
|
||||
/**
|
||||
* Graphics engine used for game development
|
||||
* @type String
|
||||
*/
|
||||
this.engine = null;
|
||||
engine: string = null;
|
||||
/**
|
||||
* Development of the game
|
||||
* @type String
|
||||
*/
|
||||
this.status = null;
|
||||
status: string = null;
|
||||
/**
|
||||
* Game description image URL
|
||||
* @type String
|
||||
*/
|
||||
this.previewSrc = null;
|
||||
previewSrc: string = null;
|
||||
/**
|
||||
* Game version
|
||||
* @type String
|
||||
*/
|
||||
this.version = null;
|
||||
version: string = null;
|
||||
/**
|
||||
* Last time the game underwent updates
|
||||
* @type Date
|
||||
*/
|
||||
this.lastUpdate = null;
|
||||
lastUpdate: Date = null;
|
||||
/**
|
||||
* Specifies if the game is original or a mod
|
||||
* @type Boolean
|
||||
*/
|
||||
this.isMod = false;
|
||||
isMod = false;
|
||||
/**
|
||||
* Changelog for the last version.
|
||||
* @type String
|
||||
* Changelog for the last version
|
||||
*/
|
||||
this.changelog = null;
|
||||
changelog: string = null;
|
||||
//#endregion Properties
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the object to a dictionary used for JSON serialization.
|
||||
*/
|
||||
/* istanbul ignore next */
|
||||
toJSON() {
|
||||
toJSON(): Record<string, unknown> {
|
||||
return {
|
||||
id: this.id,
|
||||
name: this.name,
|
||||
|
@ -117,7 +102,7 @@ class GameInfo {
|
|||
* @param {String} json JSON string used to create the new object
|
||||
* @returns {GameInfo}
|
||||
*/
|
||||
static fromJSON(json) {
|
||||
static fromJSON(json: string): GameInfo {
|
||||
// Convert string
|
||||
const temp = Object.assign(new GameInfo(), JSON.parse(json));
|
||||
|
||||
|
@ -126,4 +111,3 @@ class GameInfo {
|
|||
return temp;
|
||||
}
|
||||
}
|
||||
module.exports = GameInfo;
|
||||
|
|
|
@ -3,18 +3,18 @@
|
|||
/**
|
||||
* Object obtained in response to an attempt to login to the portal.
|
||||
*/
|
||||
class LoginResult {
|
||||
constructor(success, message) {
|
||||
export class LoginResult {
|
||||
/**
|
||||
* Result of the login operation
|
||||
* @type Boolean
|
||||
*/
|
||||
this.success = success;
|
||||
success: boolean;
|
||||
/**
|
||||
* Login response message
|
||||
* @type String
|
||||
*/
|
||||
message: string;
|
||||
|
||||
constructor(success: boolean, message: string) {
|
||||
this.success = success;
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
module.exports = LoginResult;
|
||||
|
|
|
@ -1,15 +1,12 @@
|
|||
"use strict";
|
||||
|
||||
// Modules from file
|
||||
const shared = require("../shared.js");
|
||||
import shared = require("../shared.js");
|
||||
|
||||
/**
|
||||
* Convert prefixes and platform tags from string to ID and vice versa.
|
||||
*/
|
||||
class PrefixParser {
|
||||
constructor() {
|
||||
}
|
||||
|
||||
export class PrefixParser {
|
||||
//#region Private methods
|
||||
/**
|
||||
* @private
|
||||
|
@ -18,23 +15,19 @@ class PrefixParser {
|
|||
* @param {Any} value Value associated with the key
|
||||
* @returns {String|undefined} Key found or undefined
|
||||
*/
|
||||
_getKeyByValue(object, value) {
|
||||
_getKeyByValue(object: { [x: string]: unknown; }, value: unknown): string | undefined {
|
||||
return Object.keys(object).find(key => object[key] === value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* Makes an array of strings uppercase.
|
||||
* @param {String[]} a
|
||||
* @returns {String[]}
|
||||
*/
|
||||
_toUpperCaseArray(a) {
|
||||
_toUpperCaseArray(a: string[]): string[] {
|
||||
/**
|
||||
* Makes a string uppercase.
|
||||
* @param {String} s
|
||||
* @returns {String}
|
||||
*/
|
||||
function toUpper(s) {
|
||||
function toUpper(s: string): string {
|
||||
return s.toUpperCase();
|
||||
}
|
||||
return a.map(toUpper);
|
||||
|
@ -43,10 +36,8 @@ class PrefixParser {
|
|||
/**
|
||||
* @private
|
||||
* Check if `dict` contains `value` as a value.
|
||||
* @param {Object.<number, string>} dict
|
||||
* @param {String} value
|
||||
*/
|
||||
_valueInDict(dict, value) {
|
||||
_valueInDict(dict: { [s: number]: string; }, value: string): boolean {
|
||||
const array = Object.values(dict);
|
||||
const upperArr = this._toUpperCaseArray(array);
|
||||
const element = value.toUpperCase();
|
||||
|
@ -57,10 +48,9 @@ class PrefixParser {
|
|||
/**
|
||||
* @public
|
||||
* Convert a list of prefixes to their respective IDs.
|
||||
* @param {String[]} prefixes
|
||||
*/
|
||||
prefixesToIDs(prefixes) {
|
||||
const ids = [];
|
||||
prefixesToIDs(prefixes: string[]) : number[] {
|
||||
const ids: number[] = [];
|
||||
for(const p of prefixes) {
|
||||
// Check what dict contains the value
|
||||
let dict = null;
|
||||
|
@ -80,10 +70,9 @@ class PrefixParser {
|
|||
/**
|
||||
* @public
|
||||
* It converts a list of IDs into their respective prefixes.
|
||||
* @param {number[]} ids
|
||||
*/
|
||||
idsToPrefixes(ids) {
|
||||
const prefixes = [];
|
||||
idsToPrefixes(ids: number[]): string[] {
|
||||
const prefixes:string[] = [];
|
||||
for(const id of ids) {
|
||||
// Check what dict contains the key
|
||||
let dict = null;
|
||||
|
@ -99,5 +88,3 @@ class PrefixParser {
|
|||
return prefixes;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PrefixParser;
|
|
@ -1,53 +1,53 @@
|
|||
"use strict";
|
||||
|
||||
// Core modules
|
||||
const fs = require("fs");
|
||||
const promisify = require("util").promisify;
|
||||
import * as fs from "fs";
|
||||
import { promisify } from "util";
|
||||
|
||||
// Public modules from npm
|
||||
const md5 = require("md5");
|
||||
import * as md5 from "md5";
|
||||
|
||||
// Promisifed functions
|
||||
const areadfile = promisify(fs.readFile);
|
||||
const awritefile = promisify(fs.writeFile);
|
||||
const aunlinkfile = promisify(fs.unlink);
|
||||
|
||||
class Session {
|
||||
constructor(path) {
|
||||
export class Session {
|
||||
//#region Properties
|
||||
/**
|
||||
* Max number of days the session is valid.
|
||||
*/
|
||||
this.SESSION_TIME = 1;
|
||||
|
||||
private readonly SESSION_TIME: number = 1;
|
||||
/**
|
||||
* Path of the session map file on disk.
|
||||
*/
|
||||
this._path = path;
|
||||
|
||||
private path: string = null;
|
||||
/**
|
||||
* Indicates if the session is mapped on disk.
|
||||
*/
|
||||
this._isMapped = fs.existsSync(this._path);
|
||||
|
||||
private isMapped = null;
|
||||
/**
|
||||
* Date of creation of the session.
|
||||
*/
|
||||
this._created = new Date(Date.now());
|
||||
|
||||
private created = null;
|
||||
/**
|
||||
* MD5 hash of the username and the password.
|
||||
*/
|
||||
this._hash = null;
|
||||
private hash = null;
|
||||
//#endregion Properties
|
||||
|
||||
constructor(path: string) {
|
||||
this.path = path;
|
||||
this.isMapped = fs.existsSync(this.path);
|
||||
this.created = new Date(Date.now());
|
||||
this.hash = null;
|
||||
}
|
||||
|
||||
//#region Private Methods
|
||||
/**
|
||||
* @private
|
||||
* Get the difference in days between two dates.
|
||||
* @param {Date} a
|
||||
* @param {Date} b
|
||||
*/
|
||||
_dateDiffInDays(a, b) {
|
||||
private dateDiffInDays(a: Date, b: Date) {
|
||||
const MS_PER_DAY = 1000 * 60 * 60 * 24;
|
||||
|
||||
// Discard the time and time-zone information.
|
||||
|
@ -58,72 +58,70 @@ class Session {
|
|||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* Convert the object to a dictionary serializable in JSON.
|
||||
*/
|
||||
_toJSON() {
|
||||
private toJSON(): Record<string, unknown> {
|
||||
return {
|
||||
created: this._created,
|
||||
hash: this._hash,
|
||||
created: this.created,
|
||||
hash: this.hash,
|
||||
};
|
||||
}
|
||||
//#endregion Private Methods
|
||||
|
||||
//#region Public Methods
|
||||
create(username, password) {
|
||||
/**
|
||||
* Create a new session
|
||||
*/
|
||||
create(username: string, password: string):void {
|
||||
// First, create the hash of the credentials
|
||||
const value = `${username}%%%${password}`;
|
||||
this._hash = md5(value);
|
||||
this.hash = md5(value);
|
||||
|
||||
// Update the creation date
|
||||
this._created = new Date(Date.now());
|
||||
this.created = new Date(Date.now());
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
* Save the session to disk.
|
||||
*/
|
||||
async save() {
|
||||
async save() : Promise<void> {
|
||||
// Update the creation date
|
||||
this._created = new Date(Date.now());
|
||||
this.created = new Date(Date.now());
|
||||
|
||||
// Convert data
|
||||
const json = this._toJSON();
|
||||
const json = this.toJSON();
|
||||
const data = JSON.stringify(json);
|
||||
|
||||
// Write data
|
||||
await awritefile(this._path, data);
|
||||
await awritefile(this.path, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
* Load the session from disk.
|
||||
*/
|
||||
async load() {
|
||||
async load(): Promise<void> {
|
||||
// Read data
|
||||
const data = await areadfile(this._path);
|
||||
const data = await areadfile(this.path, { encoding: 'utf-8', flag: 'r' });
|
||||
const json = JSON.parse(data);
|
||||
|
||||
// Assign values
|
||||
this._created = json.created;
|
||||
this._hash = json.hash;
|
||||
this.created = json.created;
|
||||
this.hash = json.hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
* Delete the session from disk.
|
||||
*/
|
||||
async delete() {
|
||||
await aunlinkfile(this._path);
|
||||
async delete(): Promise<void> {
|
||||
await aunlinkfile(this.path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
* Check if the session is valid.
|
||||
*/
|
||||
isValid(username, password) {
|
||||
isValid(username:string, password:string) : boolean {
|
||||
// Get the number of days from the file creation
|
||||
const diff = this._dateDiffInDays(new Date(Date.now()), this._created);
|
||||
const diff = this.dateDiffInDays(new Date(Date.now()), this.created);
|
||||
|
||||
// The session is valid if the number of days is minor than SESSION_TIME
|
||||
let valid = diff < this.SESSION_TIME;
|
||||
|
@ -131,11 +129,9 @@ class Session {
|
|||
if(valid) {
|
||||
// Check the hash
|
||||
const value = `${username}%%%${password}`;
|
||||
valid = md5(value) === this._hash;
|
||||
valid = md5(value) === this.hash;
|
||||
}
|
||||
return valid;
|
||||
}
|
||||
//#endregion Public Methods
|
||||
}
|
||||
|
||||
module.exports = Session;
|
|
@ -3,24 +3,17 @@
|
|||
/**
|
||||
* Class containing the data of the user currently connected to the F95Zone platform.
|
||||
*/
|
||||
class UserData {
|
||||
constructor() {
|
||||
export class UserData {
|
||||
/**
|
||||
* User name.
|
||||
* @type String
|
||||
*/
|
||||
this.username = "";
|
||||
username: string = null;
|
||||
/**
|
||||
* Path to the user's profile picture.
|
||||
* @type String
|
||||
*/
|
||||
this.avatarSrc = null;
|
||||
avatarSrc: string = null;
|
||||
/**
|
||||
* List of followed game thread URLs.
|
||||
* @type String[]
|
||||
*/
|
||||
this.watchedGameThreads = [];
|
||||
}
|
||||
watchedGameThreads: string[] = [];
|
||||
}
|
||||
|
||||
module.exports = UserData;
|
||||
|
|
Loading…
Reference in New Issue