foundryvtt-reve-de-dragon/module/migrations.js

104 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-09-17 16:07:38 +02:00
import { LOG_HEAD, SYSTEM_RDD } from "./constants.js";
class Migration {
get code() { return "sample"; }
get version() { return "0.0.0"; }
async migrate() { }
}
class _10_0_16_MigrationSortsReserve extends Migration {
get code() { return "creation-item-sort-reserve"; }
get version() { return "10.0.16"; }
async migrate() {
await game.actors
.filter((actor) => actor.type == "personnage")
.filter((actor) => actor.system.reve?.reserve?.list?.length ?? 0 > 0)
.forEach(async (actor) => {
const sortsReserve = actor.system.reve.reserve.list.map(this.conversionSortReserve);
console.log(LOG_HEAD + "Migration des sorts ", sortsReserve);
await actor.createEmbeddedDocuments("Item", sortsReserve, {
renderSheet: false,
});
await actor.update({'system.reve.reserve.list':[]})
});
}
conversionSortReserve(it) {
return {
type: 'sortreserve',
name: it.sort.name,
img: it.sort.img,
system: {
// ATTENTION, utilisation de data / _id possibles, encore présents pour les anciens sorts en réserve
sortid: it.sort._id,
draconic: it.sort.draconic,
ptreve: (it.sort.system ?? it.sort.data).ptreve_reel,
coord: it.coord,
heurecible: 'Vaisseau',
},
};
}
}
export class Migrations {
static getMigrations() {
return [
new _10_0_16_MigrationSortsReserve()
];
}
constructor() {
game.settings.register(SYSTEM_RDD, "systemMigrationVersion", {
name: "System Migration Version",
scope: "world",
config: false,
type: String,
default: "0.0.0",
});
}
migrate() {
const currentVersion = game.settings.get(
SYSTEM_RDD,
"systemMigrationVersion"
);
if (isNewerVersion(game.system.version, currentVersion)) {
const migrations = Migrations.getMigrations().filter(m => isNewerVersion(m.version, currentVersion));
// if (true) {
// const migrations = Migrations.getMigrations();
if (migrations.length > 0) {
migrations.sort((a, b) =>
isNewerVersion(a.version, b.version)
? 1
: isNewerVersion(b.version, a.version)
? -1
: 0
);
migrations.forEach(async (m) => {
ui.notifications.info(
`Executing migration ${m.code}: version ${currentVersion} is lower than ${m.version}`
);
await m.migrate();
});
ui.notifications.info(
`Migrations done, version will change to ${game.system.version}`
);
} else {
console.log(
LOG_HEAD +
`No migration needeed, version will change to ${game.system.version}`
);
}
game.settings.set(
SYSTEM_RDD,
"systemMigrationVersion",
game.system.version
);
} else {
console.log(LOG_HEAD + `No system version changed`);
}
}
}