Skip to content

Commit

Permalink
refactor: Cleaning up a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
xdy committed Nov 7, 2020
1 parent 71661cc commit 6c54995
Show file tree
Hide file tree
Showing 9 changed files with 386 additions and 388 deletions.
721 changes: 365 additions & 356 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,5 @@
},
"browserslist": [
"last 3 versions"
],
"dependencies": {
"compare-versions": "^3.6.0"
}
]
}
3 changes: 1 addition & 2 deletions src/module/entities/TwodsixItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
* @extends {Item}
*/
export default class TwodsixItem extends Item {
data:TwodsixItemData;

/**
* Augment the basic Item data model with additional dynamic data.
*/
prepareData():void {
super.prepareData();

const itemData:TwodsixItemData = this.data;
const itemData:TwodsixItemData = <TwodsixItemData>this.data;

switch (itemData.type) {
case 'skills':
Expand Down
3 changes: 1 addition & 2 deletions src/module/handlebars.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as util from "util";
import {advantageDisadvantageTerm} from "./i18n";
import {calcModFor, getKeyByValue} from "./utils/sheetUtils";
import {TWODSIX} from "./config";
Expand All @@ -12,7 +11,7 @@ export default function registerHandlebarsHelpers():void {
});

Handlebars.registerHelper('capitalize', (str) => {
if (!util.isString(str)) {
if (typeof str === 'string') {
return '';
}
return str.charAt(0).toUpperCase() + str.slice(1);
Expand Down
9 changes: 2 additions & 7 deletions src/module/hooks/ready.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import {Migration} from "../migration";
import compareVersions from "compare-versions";
import {TwodsixRolls} from "../utils/TwodsixRolls";


export function before(worldVersion:string, MIGRATIONS_IMPLEMENTED:string):boolean {
return compareVersions(worldVersion, MIGRATIONS_IMPLEMENTED) === -1;
}

Hooks.once("ready", async function () {

//Things that need to be done once settings have been set (and should probably be moved elsewhere...)
Expand All @@ -27,11 +22,11 @@ Hooks.once("ready", async function () {
}
}

const needMigration = worldVersion === null || compareVersions(worldVersion, systemVersion) === -1;
const needMigration = worldVersion === null || !isNewerVersion(worldVersion, systemVersion);

// Perform the migration
if (needMigration && game.user.isGM) {
if (!worldVersion || before(worldVersion, MIGRATIONS_IMPLEMENTED)) {
if (!worldVersion || !isNewerVersion(worldVersion, MIGRATIONS_IMPLEMENTED)) {
ui.notifications.error(`Your world data is from a Twodsix system version before migrations were implemented (in 0.6.1). This is most likely not a problem if you have used the system recently, but errors may occur.`, {permanent: true});
}
await Migration.migrateWorld();
Expand Down
23 changes: 11 additions & 12 deletions src/module/migration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {TwodsixItemData} from "./entities/TwodsixItem";
import {before} from "./hooks/ready";
import type {TwodsixItemData} from "./entities/TwodsixItem";

//TODO Move all types to a better place
export type UpdateData = {
Expand All @@ -15,14 +14,14 @@ export class Migration {
const actorData = actor.data;
await this.migrateActorItems(actorData, systemMigrationVersion, actor);

if (before(systemMigrationVersion, "0.6.20")) {
if (!isNewerVersion(systemMigrationVersion, "0.6.20")) {
updateData['data.hits.value'] = 0;
updateData['data.hits.min'] = 0;
updateData['data.hits.max'] = 0;
updateData['data.radiationDose.max'] = 0;
}

if (before(systemMigrationVersion, "0.6.35")) {
if (!isNewerVersion(systemMigrationVersion, "0.6.35")) {
updateData['data.primaryArmor.value'] = 0;
updateData['data.secondaryArmor.value'] = 0;
updateData['data.radiationProtection.value'] = 0;
Expand All @@ -44,15 +43,15 @@ export class Migration {
private static migrateItemData(item:TwodsixItemData, systemMigrationVersion:string):UpdateData {
const updateData:UpdateData = <UpdateData>{};

if (before(systemMigrationVersion, "0.6.9")) {
if (!isNewerVersion(systemMigrationVersion, "0.6.9")) {
updateData['data.name'] = item.name;
}

if (before(systemMigrationVersion, "0.6.15")) {
if (!isNewerVersion(systemMigrationVersion, "0.6.15")) {
updateData['data.skillModifier'] = 0;
}

if (before(systemMigrationVersion, "0.6.22")) {
if (!isNewerVersion(systemMigrationVersion, "0.6.22")) {
if (item.type != 'skills') {
if (!item.data.skill) {
updateData['data.skill'] = "";
Expand All @@ -63,22 +62,22 @@ export class Migration {
}
}

if (before(systemMigrationVersion, "0.6.23")) {
if (!isNewerVersion(systemMigrationVersion, "0.6.23")) {
if (item.type === 'skills') {
updateData['data.description'] = "";
updateData['data.shortDesc'] = "";
}
}

if (before(systemMigrationVersion, "0.6.24")) {
if (!isNewerVersion(systemMigrationVersion, "0.6.24")) {
if (item.type === 'skills') {
updateData['data.subtype'] = "";
updateData['data.reference'] = "";
updateData['data.key'] = "";
}
}

if (before(systemMigrationVersion, "0.6.25")) {
if (!isNewerVersion(systemMigrationVersion, "0.6.25")) {
// This migration failed horribly, so removed in 0.6.26
// let cost;
// try {
Expand All @@ -105,13 +104,13 @@ export class Migration {
}
}

if (before(systemMigrationVersion, "0.6.35")) {
if (!isNewerVersion(systemMigrationVersion, "0.6.35")) {
if (item.type === 'armor') {
updateData['data.secondaryArmor.value'] = 0;
updateData['data.radiationProtection.value'] = 0;
}
}
if (before(systemMigrationVersion, "0.6.43")) {
if (!isNewerVersion(systemMigrationVersion, "0.6.43")) {
if (item.type === 'skills') {
updateData['data.difficulty'] = 'Average';
}
Expand Down
4 changes: 2 additions & 2 deletions src/module/sheets/TwodsixActorSheet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {TwodsixRolls} from "../utils/TwodsixRolls";
import {AbstractTwodsixActorSheet} from "./AbstractTwodsixActorSheet";
import TwodsixItem from "../entities/TwodsixItem";
import {UpdateData} from "../migration";
import type TwodsixItem from "../entities/TwodsixItem";
import type {UpdateData} from "../migration";
import {calcModFor} from "../utils/sheetUtils";
import {TWODSIX} from "../config";

Expand Down
4 changes: 2 additions & 2 deletions src/module/utils/TwodsixRolls.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {TWODSIX} from "../config";
import TwodsixItem from "../entities/TwodsixItem";
import TwodsixActor from "../entities/TwodsixActor";
import type TwodsixItem from "../entities/TwodsixItem";
import type TwodsixActor from "../entities/TwodsixActor";
import {advantageDisadvantageTerm} from "../i18n";
import {calcModFor, getKeyByValue} from "./sheetUtils";

Expand Down
2 changes: 1 addition & 1 deletion static/templates/items/item-sheet.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<div class="item-qty">
<label for="data.quantity" class="resource-label">{{localize "TWODSIX.Items.Equipment.Quantity"}}
<input id="data.quantity" type="text" name="data.quantity" value="{{data.quantity}}" data-dtype="Number"/><
<input id="data.quantity" type="text" name="data.quantity" value="{{data.quantity}}" data-dtype="Number"/>
</label>
</div>

Expand Down

0 comments on commit 6c54995

Please sign in to comment.