grungnet/src/database.js

248 lines
6.8 KiB
JavaScript
Raw Normal View History

import Dexie from "dexie";
import blobToBuffer from "./helpers/blobToBuffer";
function loadVersions(db) {
2020-05-03 08:12:39 -04:00
// v1.2.0
db.version(1).stores({
maps: "id, owner",
states: "mapId",
tokens: "id, owner",
user: "key",
});
2020-05-03 08:12:39 -04:00
// v1.2.1 - Move from blob files to array buffers
db.version(2)
.stores({})
2020-05-03 08:12:39 -04:00
.upgrade(async (tx) => {
const maps = await Dexie.waitFor(tx.table("maps").toArray());
let mapBuffers = {};
for (let map of maps) {
mapBuffers[map.id] = await Dexie.waitFor(blobToBuffer(map.file));
}
return tx
.table("maps")
.toCollection()
.modify((map) => {
2020-05-03 08:12:39 -04:00
map.file = mapBuffers[map.id];
});
});
2020-05-18 02:31:32 -04:00
// v1.3.0 - Added new default tokens
db.version(3)
.stores({})
.upgrade((tx) => {
return tx
.table("states")
.toCollection()
.modify((state) => {
function mapTokenId(id) {
switch (id) {
case "__default-Axes":
return "__default-Barbarian";
2020-05-18 02:31:32 -04:00
case "__default-Bird":
return "__default-Druid";
2020-05-18 02:31:32 -04:00
case "__default-Book":
return "__default-Wizard";
2020-05-18 02:31:32 -04:00
case "__default-Crown":
return "__default-Humanoid";
2020-05-18 02:31:32 -04:00
case "__default-Dragon":
return "__default-Dragon";
2020-05-18 02:31:32 -04:00
case "__default-Eye":
return "__default-Warlock";
2020-05-18 02:31:32 -04:00
case "__default-Fist":
return "__default-Monk";
2020-05-18 02:31:32 -04:00
case "__default-Horse":
return "__default-Fey";
2020-05-18 02:31:32 -04:00
case "__default-Leaf":
return "__default-Druid";
2020-05-18 02:31:32 -04:00
case "__default-Lion":
return "__default-Monstrosity";
2020-05-18 02:31:32 -04:00
case "__default-Money":
return "__default-Humanoid";
2020-05-18 02:31:32 -04:00
case "__default-Moon":
return "__default-Cleric";
2020-05-18 02:31:32 -04:00
case "__default-Potion":
return "__default-Sorcerer";
2020-05-18 02:31:32 -04:00
case "__default-Shield":
return "__default-Paladin";
2020-05-18 02:31:32 -04:00
case "__default-Skull":
return "__default-Undead";
2020-05-18 02:31:32 -04:00
case "__default-Snake":
return "__default-Beast";
2020-05-18 02:31:32 -04:00
case "__default-Sun":
return "__default-Cleric";
2020-05-18 02:31:32 -04:00
case "__default-Swords":
return "__default-Fighter";
2020-05-18 02:31:32 -04:00
case "__default-Tree":
return "__default-Plant";
2020-05-18 02:31:32 -04:00
case "__default-Triangle":
return "__default-Sorcerer";
2020-05-18 02:31:32 -04:00
default:
return "__default-Fighter";
2020-05-18 02:31:32 -04:00
}
}
for (let stateId in state.tokens) {
state.tokens[stateId].tokenId = mapTokenId(
state.tokens[stateId].tokenId
);
state.tokens[stateId].lastEditedBy = "";
state.tokens[stateId].rotation = 0;
2020-05-18 02:31:32 -04:00
}
});
});
2020-05-31 02:25:05 -04:00
// v1.3.1 - Added show grid option
db.version(4)
.stores({})
.upgrade((tx) => {
return tx
.table("maps")
.toCollection()
.modify((map) => {
map.showGrid = false;
});
});
// v1.4.0 - Added fog subtraction
db.version(5)
.stores({})
.upgrade((tx) => {
return tx
.table("states")
.toCollection()
.modify((state) => {
for (let fogAction of state.fogDrawActions) {
if (fogAction.type === "add" || fogAction.type === "edit") {
for (let shape of fogAction.shapes) {
shape.data.holes = [];
}
}
}
});
});
// v1.4.2 - Added map resolutions
db.version(6)
.stores({})
.upgrade((tx) => {
return tx
.table("maps")
.toCollection()
.modify((map) => {
map.resolutions = {};
map.quality = "original";
});
});
2020-08-05 20:07:10 -04:00
// v1.5.0 - Fixed default token rogue spelling
db.version(7)
.stores({})
.upgrade((tx) => {
return tx
.table("states")
.toCollection()
.modify((state) => {
for (let id in state.tokens) {
if (state.tokens[id].tokenId === "__default-Rouge") {
state.tokens[id].tokenId = "__default-Rogue";
}
}
});
});
// v1.5.0 - Added map snap to grid option
db.version(8)
.stores({})
.upgrade((tx) => {
return tx
.table("maps")
.toCollection()
.modify((map) => {
map.snapToGrid = true;
});
});
// v1.5.1 - Added lock, visibility and modified to tokens
db.version(9)
.stores({})
.upgrade((tx) => {
return tx
.table("states")
.toCollection()
.modify((state) => {
for (let id in state.tokens) {
state.tokens[id].lastModifiedBy = state.tokens[id].lastEditedBy;
delete state.tokens[id].lastEditedBy;
state.tokens[id].lastModified = Date.now();
state.tokens[id].locked = false;
state.tokens[id].visible = true;
}
});
});
2020-08-27 05:09:16 -04:00
// v1.5.1 - Added token prop category and remove isVehicle bool
db.version(10)
.stores({})
.upgrade((tx) => {
return tx
.table("tokens")
.toCollection()
.modify((token) => {
token.category = token.isVehicle ? "vehicle" : "character";
delete token.isVehicle;
});
});
// v1.5.2 - Added automatic cache invalidation to maps
db.version(11)
.stores({})
2020-10-01 01:05:30 -04:00
.upgrade((tx) => {
return tx
.table("maps")
.toCollection()
.modify((map) => {
map.lastUsed = map.lastModified;
});
});
// v1.5.2 - Added automatic cache invalidation to tokens
db.version(12)
.stores({})
2020-10-01 01:05:30 -04:00
.upgrade((tx) => {
return tx
.table("tokens")
.toCollection()
.modify((token) => {
token.lastUsed = token.lastModified;
});
});
// v1.6.0 - Added map grouping and grid scale and offset
2020-10-01 01:05:30 -04:00
db.version(13)
.stores({})
.upgrade((tx) => {
return tx
.table("maps")
.toCollection()
.modify((map) => {
map.group = "";
map.grid = {
size: { x: map.gridX, y: map.gridY },
scale: { x: 1, y: 1 },
offset: { x: 0, y: 0 },
type: "square",
};
delete map.gridX;
delete map.gridY;
delete map.gridType;
2020-10-01 01:05:30 -04:00
});
});
// v1.6.0 - Added token grouping
db.version(14)
.stores({})
.upgrade((tx) => {
return tx
.table("tokens")
.toCollection()
.modify((token) => {
token.group = "";
});
});
}
// Get the dexie database used in DatabaseContext
export function getDatabase(options) {
let db = new Dexie("OwlbearRodeoDB", options);
loadVersions(db);
return db;
}