refactor: resource loading

This commit is contained in:
cha0s 2021-01-18 21:41:32 -06:00
parent 431cf7792c
commit fec684b210

View File

@ -37,36 +37,25 @@ export default (latus) => class Universe extends JsonResource {
this.#roomsFlat.push(room);
}
static async extendJson(json) {
const uri = json.extends;
const extended = await super.extendJson(json);
async load(json = {}) {
super.load(json);
const {tps = 60, uri} = json;
const {fromResourceType: {Room}} = resource(latus);
if (uri) {
const universePath = dirname(uri);
extended.rooms = Object.fromEntries(
await Promise.all(
(
await pglob(
'**/*.room.json',
{
cwd: join(process.cwd(), this.root, universePath),
},
)
await Promise.all(
(
await pglob(
'**/*.room.json',
{
cwd: join(process.cwd(), this.constructor.root, universePath),
},
)
.map(async (roomUri) => [
roomUri,
await Room.load({extends: join(universePath, roomUri)}),
]),
),
).map(async (roomUri) => {
this.addRoom(roomUri, await Room.load({extends: join(universePath, roomUri)}));
}),
);
}
return extended;
}
load({rooms = {}, tps = 60} = {}) {
Object.entries(rooms).forEach(([uri, room]) => {
this.addRoom(uri, room);
});
this.#tps = tps;
}