From 85951615a9f12d718792e3b21b70238e88fbc6f5 Mon Sep 17 00:00:00 2001 From: tobspr Date: Thu, 14 May 2020 08:56:18 +0200 Subject: [PATCH] Fix tslint errors --- gulp/webpack.config.js | 2 + gulp/webpack.production.config.js | 2 + package.json | 1 + src/js/core/atlas_definitions.js | 9 +-- src/js/core/buffer_maintainer.js | 10 +-- src/js/core/singleton_factory.js | 21 ++++++- src/js/core/utils.js | 81 +------------------------ src/js/game/shape_definition_manager.js | 6 +- src/js/globals.d.ts | 4 +- jsconfig.json => src/js/jsconfig.json | 0 src/js/profile/setting_types.js | 2 +- src/js/savegame/savegame_serializer.js | 18 ------ src/js/savegame/serialization.js | 9 ++- src/js/savegame/serializer_internal.js | 72 +--------------------- src/js/states/ingame.js | 4 +- tsconfig.json => src/js/tsconfig.json | 0 tslint.json => src/js/tslint.json | 0 17 files changed, 41 insertions(+), 200 deletions(-) rename jsconfig.json => src/js/jsconfig.json (100%) rename tsconfig.json => src/js/tsconfig.json (100%) rename tslint.json => src/js/tslint.json (100%) diff --git a/gulp/webpack.config.js b/gulp/webpack.config.js index 31d2d7f5..f3bdb601 100644 --- a/gulp/webpack.config.js +++ b/gulp/webpack.config.js @@ -1,3 +1,5 @@ +// @ts-nocheck + const path = require("path"); const webpack = require("webpack"); const utils = require("./buildutils"); diff --git a/gulp/webpack.production.config.js b/gulp/webpack.production.config.js index 420d4aba..e06e15c1 100644 --- a/gulp/webpack.production.config.js +++ b/gulp/webpack.production.config.js @@ -1,3 +1,5 @@ +// @ts-nocheck + const path = require("path"); const webpack = require("webpack"); const utils = require("./buildutils"); diff --git a/package.json b/package.json index b978337a..49c72bb7 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "private": true, "scripts": { "dev": "./gulp/gulp main.serveDev", + "tslint": "cd src/js && tsc", "prettier-all": "prettier --write src/**/*.* && prettier --write gulp/**/*.*" }, "dependencies": { diff --git a/src/js/core/atlas_definitions.js b/src/js/core/atlas_definitions.js index 043b3996..42cd2bce 100644 --- a/src/js/core/atlas_definitions.js +++ b/src/js/core/atlas_definitions.js @@ -22,17 +22,10 @@ export class AtlasDefinition { } } -// @ts-ignore export const atlasFiles = require + // @ts-ignore .context("../../../res_built/atlas/", false, /.*\.json/i) .keys() .map(f => f.replace(/^\.\//gi, "")) .map(f => require("../../../res_built/atlas/" + f)) .map(data => new AtlasDefinition(data)); - -// export const atlasDefinitions = { -// qualityPreload: atlasFiles.filter((atlas) => atlas.meta.image.indexOf("_preload") >= 0), -// qualityLow: atlasFiles.filter((atlas) => atlas.meta.image.indexOf("_low") >= 0), -// qualityMedium: atlasFiles.filter((atlas) => atlas.meta.image.indexOf("_medium") >= 0), -// qualityHigh: atlasFiles.filter((atlas) => atlas.meta.image.indexOf("_high") >= 0), -// }; diff --git a/src/js/core/buffer_maintainer.js b/src/js/core/buffer_maintainer.js index 3be1c08f..8421cbc5 100644 --- a/src/js/core/buffer_maintainer.js +++ b/src/js/core/buffer_maintainer.js @@ -1,13 +1,7 @@ import { GameRoot } from "../game/root"; -import { - makeOffscreenBuffer, - freeCanvas, - getBufferVramUsageBytes, - getBufferStats, - clearBufferBacklog, -} from "./buffer_utils"; +import { clearBufferBacklog, freeCanvas, getBufferStats, makeOffscreenBuffer } from "./buffer_utils"; import { createLogger } from "./logging"; -import { round2Digits, round1Digit } from "./utils"; +import { round1Digit } from "./utils"; /** * @typedef {{ diff --git a/src/js/core/singleton_factory.js b/src/js/core/singleton_factory.js index 27d192c5..7fa38bd3 100644 --- a/src/js/core/singleton_factory.js +++ b/src/js/core/singleton_factory.js @@ -1,11 +1,21 @@ +import { createLogger } from "./logging"; + +const logger = createLogger("singleton_factory"); + // simple factory pattern export class SingletonFactory { - constructor() { + constructor(id) { + this.id = id; + // Store array as well as dictionary, to speed up lookups this.entries = []; this.idToEntry = {}; } + getId() { + return this.id; + } + register(classHandle) { // First, construct instance const instance = new classHandle(); @@ -39,6 +49,7 @@ export class SingletonFactory { findById(id) { const entry = this.idToEntry[id]; if (!entry) { + logger.error("Object with id", id, "is not registered!"); assert(false, "Factory: Object with id '" + id + "' is not registered!"); return null; } @@ -68,6 +79,14 @@ export class SingletonFactory { return this.entries; } + /** + * Returns all registered ids + * @returns {Array} + */ + getAllIds() { + return Object.keys(this.idToEntry); + } + /** * Returns amount of stored entries * @returns {number} diff --git a/src/js/core/utils.js b/src/js/core/utils.js index 541662a2..0995fe7f 100644 --- a/src/js/core/utils.js +++ b/src/js/core/utils.js @@ -436,26 +436,6 @@ export function formatBigNumber(num, divider = ".") { return sign + "" + num; } - // if (num > 1e51) return sign + T.common.number_format.sedecillion.replace("%amount%", "" + roundSmart(num / 1e51)); - // if (num > 1e48) - // return sign + T.common.number_format.quinquadecillion.replace("%amount%", "" + roundSmart(num / 1e48)); - // if (num > 1e45) - // return sign + T.common.number_format.quattuordecillion.replace("%amount%", "" + roundSmart(num / 1e45)); - // if (num > 1e42) return sign + T.common.number_format.tredecillion.replace("%amount%", "" + roundSmart(num / 1e42)); - // if (num > 1e39) return sign + T.common.number_format.duodecillions.replace("%amount%", "" + roundSmart(num / 1e39)); - // if (num > 1e36) return sign + T.common.number_format.undecillions.replace("%amount%", "" + roundSmart(num / 1e36)); - // if (num > 1e33) return sign + T.common.number_format.decillions.replace("%amount%", "" + roundSmart(num / 1e33)); - // if (num > 1e30) return sign + T.common.number_format.nonillions.replace("%amount%", "" + roundSmart(num / 1e30)); - // if (num > 1e27) return sign + T.common.number_format.octillions.replace("%amount%", "" + roundSmart(num / 1e27)); - // if (num >= 1e24) return sign + T.common.number_format.septillions.replace("%amount%", "" + roundSmart(num / 1e24)); - // if (num >= 1e21) return sign + T.common.number_format.sextillions.replace("%amount%", "" + roundSmart(num / 1e21)); - // if (num >= 1e18) return sign + T.common.number_format.quintillions.replace("%amount%", "" + roundSmart(num / 1e18)); - // if (num >= 1e15) return sign + T.common.number_format.quantillions.replace("%amount%", "" + roundSmart(num / 1e15)); - // if (num >= 1e12) return sign + T.common.number_format.trillions.replace("%amount%", "" + roundSmart(num / 1e12)); - // if (num >= 1e9) return sign + T.common.number_format.billions.replace("%amount%", "" + roundSmart(num / 1e9)); - // if (num >= 1e6) return sign + T.common.number_format.millions.replace("%amount%", "" + roundSmart(num / 1e6)); - // if (num > 99999) return sign + T.common.number_format.thousands.replace("%amount%", "" + roundSmart(num / 1e3)); - let rest = num; let out = ""; @@ -474,7 +454,7 @@ export function formatBigNumber(num, divider = ".") { * @param {string=} divider THe divider for numbers like 50,000 (divider=',') * @returns {string} */ -export function formatBigNumberFull(num, divider = T.common.number_format.divider_thousands || " ") { +export function formatBigNumberFull(num, divider = ",") { if (num < 1000) { return num + ""; } @@ -492,65 +472,6 @@ export function formatBigNumberFull(num, divider = T.common.number_format.divide return out.substring(0, out.length - 1); } -/** - * Formats an amount of seconds into something like "5s ago" - * @param {number} secs Seconds - * @returns {string} - */ -export function formatSecondsToTimeAgo(secs) { - const seconds = Math_floor(secs); - const minutes = Math_floor(seconds / 60); - const hours = Math_floor(minutes / 60); - const days = Math_floor(hours / 24); - - const trans = T.common.time; - - if (seconds <= 60) { - if (seconds <= 1) { - return trans.one_second_before; - } - return trans.seconds_before.replace("%amount%", "" + seconds); - } else if (minutes <= 60) { - if (minutes <= 1) { - return trans.one_minute_before; - } - return trans.minutes_before.replace("%amount%", "" + minutes); - } else if (hours <= 60) { - if (hours <= 1) { - return trans.one_hour_before; - } - return trans.hours_before.replace("%amount%", "" + hours); - } else { - if (days <= 1) { - return trans.one_day_before; - } - return trans.days_before.replace("%amount%", "" + days); - } -} - -/** - * Formats seconds into a readable string like "5h 23m" - * @param {number} secs Seconds - * @returns {string} - */ -export function formatSeconds(secs) { - const trans = T.common.time; - secs = Math_ceil(secs); - if (secs < 60) { - return trans.seconds_short.replace("%seconds%", "" + secs); - } else if (secs < 60 * 60) { - const minutes = Math_floor(secs / 60); - const seconds = secs % 60; - return trans.minutes_seconds_short - .replace("%seconds%", "" + seconds) - .replace("%minutes%", "" + minutes); - } else { - const hours = Math_floor(secs / 3600); - const minutes = Math_floor(secs / 60) % 60; - return trans.hours_minutes_short.replace("%minutes%", "" + minutes).replace("%hours%", "" + hours); - } -} - /** * Delayes a promise so that it will resolve after a *minimum* amount of time only * @param {Promise} promise The promise to delay diff --git a/src/js/game/shape_definition_manager.js b/src/js/game/shape_definition_manager.js index e3ef6e6e..d2647c3f 100644 --- a/src/js/game/shape_definition_manager.js +++ b/src/js/game/shape_definition_manager.js @@ -143,11 +143,7 @@ export class ShapeDefinitionManager extends BasicSerializableObject { */ getDefinitionFromSimpleShapes(subShapes, color = enumColors.uncolored) { const shapeLayer = /** @type {import("./shape_definition").ShapeLayer} */ (subShapes.map( - subShape => ({ - subShape, - rotation: 0, - color, - }) + subShape => ({ subShape, color }) )); return this.registerOrReturnHandle(new ShapeDefinition({ layers: [shapeLayer] })); diff --git a/src/js/globals.d.ts b/src/js/globals.d.ts index 89c21dfd..71e2db5c 100644 --- a/src/js/globals.d.ts +++ b/src/js/globals.d.ts @@ -148,7 +148,7 @@ declare interface Math { } declare interface String { - padStart(size: number, fill: string): string; + padStart(size: number, fill?: string): string; padEnd(size: number, fill: string): string; } @@ -170,6 +170,8 @@ declare interface SingletonFactoryTemplate { entries: Array; idToEntry: any; + getId(): string; + getAllIds(): Array; register(classHandle: new (...args: any[]) => T): void; hasId(id: string): boolean; findById(id: string): T; diff --git a/jsconfig.json b/src/js/jsconfig.json similarity index 100% rename from jsconfig.json rename to src/js/jsconfig.json diff --git a/src/js/profile/setting_types.js b/src/js/profile/setting_types.js index f35d17a6..04ed4e57 100644 --- a/src/js/profile/setting_types.js +++ b/src/js/profile/setting_types.js @@ -30,7 +30,7 @@ export class BaseSetting { /** * @param {Application} app * @param {Element} element - * @param {HUDModalDialogs} dialogs + * @param {any} dialogs */ bind(app, element, dialogs) { this.app = app; diff --git a/src/js/savegame/savegame_serializer.js b/src/js/savegame/savegame_serializer.js index 0c02e550..c1f0f500 100644 --- a/src/js/savegame/savegame_serializer.js +++ b/src/js/savegame/savegame_serializer.js @@ -141,24 +141,6 @@ export class SavegameSerializer { // entities errorReason = errorReason || root.entityMgr.deserialize(savegame.entityMgr); - // resources - errorReason = - errorReason || - this.internal.deserializeEntityArrayFixedType( - root, - savegame.entities.resources, - this.internal.deserializeResource - ); - - // buildings - errorReason = - errorReason || - this.internal.deserializeEntityArray( - root, - savegame.entities.buildings, - this.internal.deserializeBuilding - ); - // other stuff errorReason = errorReason || root.time.deserialize(savegame.time); errorReason = errorReason || root.camera.deserialize(savegame.camera); diff --git a/src/js/savegame/serialization.js b/src/js/savegame/serialization.js index a6c8f4c0..e121ca71 100644 --- a/src/js/savegame/serialization.js +++ b/src/js/savegame/serialization.js @@ -270,8 +270,7 @@ export function deserializeSchema(obj, schema, data, baseclassErrorResult = null const errorStatus = schema[key].deserializeWithVerify(data[key], obj, key, obj.root); if (errorStatus) { - error( - "serialization", + logger.error( "Deserialization failed with error '" + errorStatus + "' on object", obj, "and key", @@ -294,17 +293,17 @@ export function deserializeSchema(obj, schema, data, baseclassErrorResult = null export function verifySchema(schema, data) { for (const key in schema) { if (!data.hasOwnProperty(key)) { - error("verify", "Data", data, "does not contain", key, "(schema:", schema, ")"); + logger.error("Data", data, "does not contain", key, "(schema:", schema, ")"); return "verify: missing key required by schema in stored data: " + key; } if (!schema[key].allowNull() && (data[key] === null || data[key] === undefined)) { - error("verify", "Data", data, "has null value for", key, "(schema:", schema, ")"); + logger.error("Data", data, "has null value for", key, "(schema:", schema, ")"); return "verify: non-nullable entry is null: " + key; } const errorStatus = schema[key].verifySerializedValue(data[key]); if (errorStatus) { - error("verify", errorStatus); + logger.error(errorStatus); return "verify: " + errorStatus; } } diff --git a/src/js/savegame/serializer_internal.js b/src/js/savegame/serializer_internal.js index 298871e5..449f4fc0 100644 --- a/src/js/savegame/serializer_internal.js +++ b/src/js/savegame/serializer_internal.js @@ -6,6 +6,7 @@ import { Vector } from "../core/vector"; import { createLogger } from "../core/logging"; import { gMetaBuildingRegistry } from "../core/global_registries"; import { Entity } from "../game/entity"; +import { MapResourcesSystem } from "../game/systems/map_resources"; const logger = createLogger("serializer_internal"); @@ -80,61 +81,6 @@ export class SerializerInternal { return null; } - /** - * Deserializes a building - * @param {GameRoot} root - * @param {{ $: string, data: any }} payload - */ - deserializeBuilding(root, payload) { - const data = payload.data; - const id = payload.$; - if (!gMetaBuildingRegistry.hasId(id)) { - return "Metaclass not found for building: '" + id + "'"; - } - const meta = gMetaBuildingRegistry.findById(id); - if (!meta) { - return "Metaclass not found for building: '" + id + "'"; - } - - const tile = new Vector(data.x, data.y).toTileSpace(); - const instance = root.logic.internalPlaceBuildingLocalClientOnly({ - tile: tile, - metaBuilding: meta, - uid: data.uid, - }); - - // Apply component specific properties - const errorStatus = this.deserializeComponents(instance, data.components); - if (errorStatus) { - return errorStatus; - } - - // Apply enhancements - instance.updateEnhancements(); - } - - /** - * Deserializes a blueprint - * @param {GameRoot} root - * @param {any} data - * @returns {string|void} - */ - deserializeBlueprint(root, data) { - const id = data.meta; - const metaClass = gMetaBuildingRegistry.findById(id); - if (!metaClass) { - return "Metaclass not found for blueprint: '" + id + "'"; - } - - const tile = new Vector(data.x, data.y).toTileSpace(); - const instance = root.logic.internalPlaceBlueprintLocalClientOnly({ - tile: tile, - metaBuilding: metaClass, - uid: data.uid, - }); - return this.deserializeComponents(instance, data.components); - } - /////// COMPONENTS //// /** @@ -161,20 +107,4 @@ export class SerializerInternal { } } } - - /** - * Deserializes a resource - * @param {GameRoot} root - * @param {object} data - * @returns {string|void} - */ - deserializeResource(root, data) { - const id = data.key; - const instance = new MapResource(root, this.neutralFaction, id); - root.logic.internalPlaceMapEntityLocalClientOnly( - new Vector(data.x, data.y).toTileSpace(), - instance, - data.uid - ); - } } diff --git a/src/js/states/ingame.js b/src/js/states/ingame.js index 87b5a930..d63af2ec 100644 --- a/src/js/states/ingame.js +++ b/src/js/states/ingame.js @@ -198,10 +198,10 @@ export class InGameState extends GameState { this.core.initializeRoot(this, this.savegame); if (this.savegame.hasGameDump()) { - this.app.gameAnalytics.handleGameStarted(this.savegame); + this.app.gameAnalytics.handleGameStarted(); this.stage4bResumeGame(); } else { - this.app.gameAnalytics.handleGameStarted(this.savegame); + this.app.gameAnalytics.handleGameStarted(); this.stage4aInitEmptyGame(); } } diff --git a/tsconfig.json b/src/js/tsconfig.json similarity index 100% rename from tsconfig.json rename to src/js/tsconfig.json diff --git a/tslint.json b/src/js/tslint.json similarity index 100% rename from tslint.json rename to src/js/tslint.json