Fixed 2 bugs with map loading and sending

When a map was sent the receiver didn't save the resolutions tihs meant that if the original map owner left or wasn't the party leader the map would be blank when new users joined. Also if a map sender left when sending a map and rejoined their version of the map would be overriden by a corrupted version.
This commit is contained in:
Mitchell McCaffrey 2020-08-28 17:06:13 +10:00
parent 7f7471b1e9
commit b4e52ebb23
3 changed files with 51 additions and 29 deletions

View File

@ -31,13 +31,15 @@ function MapInteraction({
disabledControls, disabledControls,
}) { }) {
let mapSourceMap = map; let mapSourceMap = map;
if (map && map.type === "file") { if (map && map.type === "file" && map.resolutions) {
if ( // Set to the quality if available
map.resolutions && if (map.quality !== "original" && map.resolutions[map.quality]) {
map.quality !== "original" &&
map.resolutions[map.quality]
) {
mapSourceMap = map.resolutions[map.quality]; mapSourceMap = map.resolutions[map.quality];
} else if (!map.file) {
// If no file fallback to the highest resolution
for (let resolution in map.resolutions) {
mapSourceMap = map.resolutions[resolution];
}
} }
} }

View File

@ -147,6 +147,10 @@ export function MapDataProvider({ children }) {
return maps.find((map) => map.id === mapId); return maps.find((map) => map.id === mapId);
} }
async function getMapFromDB(mapId) {
return await database.table("maps").get(mapId);
}
const ownedMaps = maps.filter((map) => map.owner === userId); const ownedMaps = maps.filter((map) => map.owner === userId);
const value = { const value = {
@ -160,6 +164,7 @@ export function MapDataProvider({ children }) {
updateMapState, updateMapState,
putMap, putMap,
getMap, getMap,
getMapFromDB,
}; };
return ( return (
<MapDataContext.Provider value={value}>{children}</MapDataContext.Provider> <MapDataContext.Provider value={value}>{children}</MapDataContext.Provider>

View File

@ -33,7 +33,7 @@ function NetworkedMapAndTokens({ session }) {
} = useContext(MapLoadingContext); } = useContext(MapLoadingContext);
const { putToken, getToken } = useContext(TokenDataContext); const { putToken, getToken } = useContext(TokenDataContext);
const { putMap, getMap } = useContext(MapDataContext); const { putMap, updateMap, getMapFromDB } = useContext(MapDataContext);
const [currentMap, setCurrentMap] = useState(null); const [currentMap, setCurrentMap] = useState(null);
const [currentMapState, setCurrentMapState] = useState(null); const [currentMapState, setCurrentMapState] = useState(null);
@ -243,11 +243,13 @@ function NetworkedMapAndTokens({ session }) {
if (id === "map") { if (id === "map") {
const newMap = data; const newMap = data;
if (newMap && newMap.type === "file") { if (newMap && newMap.type === "file") {
const cachedMap = getMap(newMap.id); const cachedMap = await getMapFromDB(newMap.id);
if (cachedMap && cachedMap.lastModified === newMap.lastModified) { if (cachedMap && cachedMap.lastModified >= newMap.lastModified) {
setCurrentMap(cachedMap); setCurrentMap(cachedMap);
} else { } else {
await putMap(newMap); // Save map data but remove last modified so if there is an error
// during the map request the cache is invalid
await putMap({ ...newMap, lastModified: 0 });
reply("mapRequest", newMap.id, "map"); reply("mapRequest", newMap.id, "map");
} }
} else { } else {
@ -255,17 +257,29 @@ function NetworkedMapAndTokens({ session }) {
} }
} }
if (id === "mapRequest") { if (id === "mapRequest") {
const map = getMap(data); const map = await getMapFromDB(data);
function replyWithFile(file, preview) { function replyWithPreview(preview) {
if (map.resolutions[preview]) {
reply(
"mapResponse",
{
id: map.id,
resolutions: { [preview]: map.resolutions[preview] },
},
"map"
);
}
}
function replyWithFile(file) {
reply( reply(
"mapResponse", "mapResponse",
{ {
...map, id: map.id,
file, file,
resolutions: {}, // Add last modified back to file to set cache as valid
// If preview don't send the last modified so that it will not be cached lastModified: map.lastModified,
lastModified: preview ? 0 : map.lastModified,
}, },
"map" "map"
); );
@ -273,35 +287,36 @@ function NetworkedMapAndTokens({ session }) {
switch (map.quality) { switch (map.quality) {
case "low": case "low":
replyWithFile(map.resolutions.low.file, false); replyWithFile(map.resolutions.low.file);
break; break;
case "medium": case "medium":
replyWithFile(map.resolutions.low.file, true); replyWithPreview("low");
replyWithFile(map.resolutions.medium.file, false); replyWithFile(map.resolutions.medium.file);
break; break;
case "high": case "high":
replyWithFile(map.resolutions.medium.file, true); replyWithPreview("medium");
replyWithFile(map.resolutions.high.file, false); replyWithFile(map.resolutions.high.file);
break; break;
case "ultra": case "ultra":
replyWithFile(map.resolutions.medium.file, true); replyWithPreview("medium");
replyWithFile(map.resolutions.ultra.file, false); replyWithFile(map.resolutions.ultra.file);
break; break;
case "original": case "original":
if (map.resolutions.medium) { if (map.resolutions.medium) {
replyWithFile(map.resolutions.medium.file, true); replyWithPreview("medium");
} else if (map.resolutions.low) { } else if (map.resolutions.low) {
replyWithFile(map.resolutions.low.file, true); replyWithPreview("low");
} }
replyWithFile(map.file, false); replyWithFile(map.file);
break; break;
default: default:
replyWithFile(map.file, false); replyWithFile(map.file);
} }
} }
if (id === "mapResponse") { if (id === "mapResponse") {
await putMap(data); await updateMap(data.id, data);
setCurrentMap(data); const newMap = await getMapFromDB(data.id);
setCurrentMap(newMap);
} }
if (id === "mapState") { if (id === "mapState") {
setCurrentMapState(data); setCurrentMapState(data);