2020-12-11 13:24:39 +11:00
|
|
|
import React, { useState, useContext, useEffect } from "react";
|
2020-07-16 17:27:39 +10:00
|
|
|
|
|
|
|
|
import TokenDataContext from "../contexts/TokenDataContext";
|
|
|
|
|
import MapDataContext from "../contexts/MapDataContext";
|
|
|
|
|
import MapLoadingContext from "../contexts/MapLoadingContext";
|
|
|
|
|
import AuthContext from "../contexts/AuthContext";
|
|
|
|
|
import DatabaseContext from "../contexts/DatabaseContext";
|
2020-12-31 17:56:51 +11:00
|
|
|
import PartyContext from "../contexts/PartyContext";
|
2020-07-16 17:27:39 +10:00
|
|
|
|
|
|
|
|
import { omit } from "../helpers/shared";
|
|
|
|
|
import useDebounce from "../helpers/useDebounce";
|
2020-12-05 17:16:06 +11:00
|
|
|
import useNetworkedState from "../helpers/useNetworkedState";
|
2020-07-16 17:27:39 +10:00
|
|
|
// Load session for auto complete
|
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
2020-10-23 10:27:22 +11:00
|
|
|
import Session from "./Session";
|
2020-07-16 17:27:39 +10:00
|
|
|
|
|
|
|
|
import Map from "../components/map/Map";
|
|
|
|
|
import Tokens from "../components/token/Tokens";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @typedef {object} NetworkedMapProps
|
|
|
|
|
* @property {Session} session
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {NetworkedMapProps} props
|
|
|
|
|
*/
|
|
|
|
|
function NetworkedMapAndTokens({ session }) {
|
|
|
|
|
const { userId } = useContext(AuthContext);
|
2020-12-31 17:56:51 +11:00
|
|
|
const partyState = useContext(PartyContext);
|
2020-07-17 15:57:52 +10:00
|
|
|
const {
|
|
|
|
|
assetLoadStart,
|
|
|
|
|
assetLoadFinish,
|
|
|
|
|
assetProgressUpdate,
|
|
|
|
|
isLoading,
|
|
|
|
|
} = useContext(MapLoadingContext);
|
2020-07-16 17:27:39 +10:00
|
|
|
|
2020-09-11 16:56:40 +10:00
|
|
|
const { putToken, getToken, updateToken } = useContext(TokenDataContext);
|
2020-10-22 20:57:29 +11:00
|
|
|
const { putMap, updateMap, getMapFromDB, updateMapState } = useContext(
|
|
|
|
|
MapDataContext
|
|
|
|
|
);
|
2020-07-16 17:27:39 +10:00
|
|
|
|
|
|
|
|
const [currentMap, setCurrentMap] = useState(null);
|
2020-12-05 17:16:06 +11:00
|
|
|
const [currentMapState, setCurrentMapState] = useNetworkedState(
|
|
|
|
|
null,
|
|
|
|
|
session,
|
2021-01-04 09:34:25 +11:00
|
|
|
"map_state",
|
|
|
|
|
100,
|
|
|
|
|
true,
|
|
|
|
|
"mapId"
|
2020-12-05 17:16:06 +11:00
|
|
|
);
|
2020-12-11 13:24:39 +11:00
|
|
|
const [assetManifest, setAssetManifest] = useNetworkedState(
|
|
|
|
|
[],
|
|
|
|
|
session,
|
2021-01-03 12:12:13 +11:00
|
|
|
"manifest",
|
|
|
|
|
100,
|
|
|
|
|
false
|
2020-12-11 13:24:39 +11:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
function loadAssetManifestFromMap(map, mapState) {
|
|
|
|
|
const assets = [];
|
|
|
|
|
if (map.type === "file") {
|
|
|
|
|
const { id, lastModified, owner } = map;
|
|
|
|
|
assets.push({ type: "map", id, lastModified, owner });
|
|
|
|
|
}
|
|
|
|
|
let processedTokens = new Set();
|
|
|
|
|
for (let tokenState of Object.values(mapState.tokens)) {
|
|
|
|
|
const token = getToken(tokenState.tokenId);
|
|
|
|
|
if (
|
|
|
|
|
token &&
|
|
|
|
|
token.type === "file" &&
|
|
|
|
|
!processedTokens.has(tokenState.tokenId)
|
|
|
|
|
) {
|
|
|
|
|
processedTokens.add(tokenState.tokenId);
|
|
|
|
|
// Omit file from token peer will request file if needed
|
|
|
|
|
const { id, lastModified, owner } = token;
|
|
|
|
|
assets.push({ type: "token", id, lastModified, owner });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
setAssetManifest(assets);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function compareAssets(a, b) {
|
|
|
|
|
return a.type === b.type && a.id === b.id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return true if an asset is out of date
|
|
|
|
|
function assetNeedsUpdate(oldAsset, newAsset) {
|
|
|
|
|
return (
|
|
|
|
|
compareAssets(oldAsset, newAsset) &&
|
|
|
|
|
oldAsset.lastModified > newAsset.lastModified
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function addAssetIfNeeded(asset) {
|
|
|
|
|
// Asset needs updating
|
|
|
|
|
const exists = assetManifest.some((oldAsset) =>
|
|
|
|
|
compareAssets(oldAsset, asset)
|
|
|
|
|
);
|
|
|
|
|
const needsUpdate = assetManifest.some((oldAsset) =>
|
|
|
|
|
assetNeedsUpdate(oldAsset, asset)
|
|
|
|
|
);
|
|
|
|
|
if (!exists || needsUpdate) {
|
|
|
|
|
setAssetManifest((prevAssets) => [
|
|
|
|
|
...prevAssets.filter((prevAsset) => compareAssets(prevAsset, asset)),
|
|
|
|
|
asset,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!assetManifest) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function requestAssetsIfNeeded() {
|
|
|
|
|
for (let asset of assetManifest) {
|
|
|
|
|
if (asset.owner === userId) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const owner = Object.values(partyState).find(
|
|
|
|
|
(player) => player.userId === asset.owner
|
|
|
|
|
);
|
|
|
|
|
if (!owner) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (asset.type === "map") {
|
|
|
|
|
const cachedMap = await getMapFromDB(asset.id);
|
|
|
|
|
if (cachedMap && cachedMap.lastModified >= asset.lastModified) {
|
|
|
|
|
// Update last used for cache invalidation
|
|
|
|
|
const lastUsed = Date.now();
|
|
|
|
|
await updateMap(cachedMap.id, { lastUsed });
|
|
|
|
|
setCurrentMap({ ...cachedMap, lastUsed });
|
|
|
|
|
} else {
|
|
|
|
|
session.sendTo(owner.sessionId, "mapRequest", asset.id, "map");
|
|
|
|
|
}
|
2020-12-12 15:03:24 +11:00
|
|
|
} else if (asset.type === "token") {
|
|
|
|
|
const cachedToken = getToken(asset.id);
|
|
|
|
|
if (cachedToken && cachedToken.lastModified >= asset.lastModified) {
|
|
|
|
|
// Update last used for cache invalidation
|
|
|
|
|
const lastUsed = Date.now();
|
|
|
|
|
await updateToken(cachedToken.id, { lastUsed });
|
|
|
|
|
} else {
|
|
|
|
|
session.sendTo(owner.sessionId, "tokenRequest", asset.id, "token");
|
|
|
|
|
}
|
2020-12-11 13:24:39 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
requestAssetsIfNeeded();
|
2020-12-12 15:03:24 +11:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2020-12-11 13:24:39 +11:00
|
|
|
}, [assetManifest, partyState, session]);
|
2020-07-16 17:27:39 +10:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Map state
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const { database } = useContext(DatabaseContext);
|
|
|
|
|
// Sync the map state to the database after 500ms of inactivity
|
|
|
|
|
const debouncedMapState = useDebounce(currentMapState, 500);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (
|
|
|
|
|
debouncedMapState &&
|
|
|
|
|
debouncedMapState.mapId &&
|
|
|
|
|
currentMap &&
|
|
|
|
|
currentMap.owner === userId &&
|
|
|
|
|
database
|
|
|
|
|
) {
|
2020-10-22 20:57:29 +11:00
|
|
|
updateMapState(debouncedMapState.mapId, debouncedMapState);
|
2020-07-16 17:27:39 +10:00
|
|
|
}
|
2020-10-22 20:57:29 +11:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2020-07-16 17:27:39 +10:00
|
|
|
}, [currentMap, debouncedMapState, userId, database]);
|
|
|
|
|
|
|
|
|
|
function handleMapChange(newMap, newMapState) {
|
2021-01-04 09:48:08 +11:00
|
|
|
// Clear map before sending new one
|
|
|
|
|
setCurrentMap(null);
|
|
|
|
|
session.socket?.emit("map", null);
|
|
|
|
|
|
2021-01-03 16:45:55 +11:00
|
|
|
setCurrentMapState(newMapState, true, true);
|
2020-07-16 17:27:39 +10:00
|
|
|
setCurrentMap(newMap);
|
2020-12-11 13:24:39 +11:00
|
|
|
|
|
|
|
|
if (newMap && newMap.type === "file") {
|
|
|
|
|
const { file, resolutions, ...rest } = newMap;
|
2021-01-01 12:44:56 +11:00
|
|
|
session.socket?.emit("map", rest);
|
2020-12-11 13:24:39 +11:00
|
|
|
} else {
|
2021-01-01 12:44:56 +11:00
|
|
|
session.socket?.emit("map", newMap);
|
2020-12-11 13:24:39 +11:00
|
|
|
}
|
2020-07-18 16:58:29 +10:00
|
|
|
|
|
|
|
|
if (!newMap || !newMapState) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-11 13:24:39 +11:00
|
|
|
loadAssetManifestFromMap(newMap, newMapState);
|
2020-07-16 17:27:39 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleMapStateChange(newMapState) {
|
2021-01-03 16:45:55 +11:00
|
|
|
setCurrentMapState(newMapState, true, true);
|
2020-07-16 17:27:39 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function addMapDrawActions(actions, indexKey, actionsKey) {
|
|
|
|
|
setCurrentMapState((prevMapState) => {
|
|
|
|
|
const newActions = [
|
|
|
|
|
...prevMapState[actionsKey].slice(0, prevMapState[indexKey] + 1),
|
|
|
|
|
...actions,
|
|
|
|
|
];
|
|
|
|
|
const newIndex = newActions.length - 1;
|
|
|
|
|
return {
|
|
|
|
|
...prevMapState,
|
|
|
|
|
[actionsKey]: newActions,
|
|
|
|
|
[indexKey]: newIndex,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateDrawActionIndex(change, indexKey, actionsKey) {
|
|
|
|
|
const newIndex = Math.min(
|
|
|
|
|
Math.max(currentMapState[indexKey] + change, -1),
|
|
|
|
|
currentMapState[actionsKey].length - 1
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
setCurrentMapState((prevMapState) => ({
|
|
|
|
|
...prevMapState,
|
|
|
|
|
[indexKey]: newIndex,
|
|
|
|
|
}));
|
|
|
|
|
return newIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleMapDraw(action) {
|
|
|
|
|
addMapDrawActions([action], "mapDrawActionIndex", "mapDrawActions");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleMapDrawUndo() {
|
2020-12-05 17:16:06 +11:00
|
|
|
updateDrawActionIndex(-1, "mapDrawActionIndex", "mapDrawActions");
|
2020-07-16 17:27:39 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleMapDrawRedo() {
|
2020-12-05 17:16:06 +11:00
|
|
|
updateDrawActionIndex(1, "mapDrawActionIndex", "mapDrawActions");
|
2020-07-16 17:27:39 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleFogDraw(action) {
|
|
|
|
|
addMapDrawActions([action], "fogDrawActionIndex", "fogDrawActions");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleFogDrawUndo() {
|
2020-12-05 17:16:06 +11:00
|
|
|
updateDrawActionIndex(-1, "fogDrawActionIndex", "fogDrawActions");
|
2020-07-16 17:27:39 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleFogDrawRedo() {
|
2020-12-05 17:16:06 +11:00
|
|
|
updateDrawActionIndex(1, "fogDrawActionIndex", "fogDrawActions");
|
2020-07-16 17:27:39 +10:00
|
|
|
}
|
|
|
|
|
|
2020-11-04 15:03:34 +11:00
|
|
|
function handleNoteChange(note) {
|
2020-11-03 17:15:39 +11:00
|
|
|
setCurrentMapState((prevMapState) => ({
|
|
|
|
|
...prevMapState,
|
|
|
|
|
notes: {
|
|
|
|
|
...prevMapState.notes,
|
|
|
|
|
[note.id]: note,
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 14:41:33 +11:00
|
|
|
function handleNoteRemove(noteId) {
|
|
|
|
|
setCurrentMapState((prevMapState) => ({
|
|
|
|
|
...prevMapState,
|
|
|
|
|
notes: omit(prevMapState.notes, [noteId]),
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 17:27:39 +10:00
|
|
|
/**
|
|
|
|
|
* Token state
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
async function handleMapTokenStateCreate(tokenState) {
|
|
|
|
|
// If file type token send the token to the other peers
|
|
|
|
|
const token = getToken(tokenState.tokenId);
|
|
|
|
|
if (token && token.type === "file") {
|
2020-12-11 13:24:39 +11:00
|
|
|
const { id, lastModified, owner } = token;
|
|
|
|
|
addAssetIfNeeded({ type: "token", id, lastModified, owner });
|
2020-07-16 17:27:39 +10:00
|
|
|
}
|
|
|
|
|
handleMapTokenStateChange({ [tokenState.id]: tokenState });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleMapTokenStateChange(change) {
|
|
|
|
|
if (currentMapState === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setCurrentMapState((prevMapState) => ({
|
|
|
|
|
...prevMapState,
|
|
|
|
|
tokens: {
|
|
|
|
|
...prevMapState.tokens,
|
|
|
|
|
...change,
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleMapTokenStateRemove(tokenState) {
|
|
|
|
|
setCurrentMapState((prevMapState) => {
|
|
|
|
|
const { [tokenState.id]: old, ...rest } = prevMapState.tokens;
|
|
|
|
|
return { ...prevMapState, tokens: rest };
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
async function handlePeerData({ id, data, reply }) {
|
|
|
|
|
if (id === "mapRequest") {
|
2020-08-28 17:06:13 +10:00
|
|
|
const map = await getMapFromDB(data);
|
2020-07-18 17:22:26 +10:00
|
|
|
|
2020-12-11 13:24:39 +11:00
|
|
|
function replyWithMap(preview, resolution) {
|
|
|
|
|
let response = {
|
|
|
|
|
...map,
|
|
|
|
|
resolutions: undefined,
|
|
|
|
|
file: undefined,
|
|
|
|
|
// Remove last modified so if there is an error
|
|
|
|
|
// during the map request the cache is invalid
|
|
|
|
|
lastModified: 0,
|
|
|
|
|
// Add last used for cache invalidation
|
|
|
|
|
lastUsed: Date.now(),
|
|
|
|
|
};
|
|
|
|
|
// Send preview if available
|
2020-08-28 17:06:13 +10:00
|
|
|
if (map.resolutions[preview]) {
|
2020-12-11 13:24:39 +11:00
|
|
|
response.resolutions = { [preview]: map.resolutions[preview] };
|
|
|
|
|
reply("mapResponse", response, "map");
|
2020-08-28 17:06:13 +10:00
|
|
|
}
|
2020-12-11 13:24:39 +11:00
|
|
|
// Send full map at the desired resolution if available
|
2020-10-27 19:30:39 +11:00
|
|
|
if (map.resolutions[resolution]) {
|
2020-12-11 13:24:39 +11:00
|
|
|
response.file = map.resolutions[resolution].file;
|
2020-10-27 19:30:39 +11:00
|
|
|
} else if (map.file) {
|
|
|
|
|
// The resolution might not exist for other users so send the file instead
|
2020-12-11 13:24:39 +11:00
|
|
|
response.file = map.file;
|
2020-10-27 19:30:39 +11:00
|
|
|
} else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-12-11 13:24:39 +11:00
|
|
|
// Add last modified back to file to set cache as valid
|
|
|
|
|
response.lastModified = map.lastModified;
|
|
|
|
|
reply("mapResponse", response, "map");
|
2020-07-16 17:27:39 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (map.quality) {
|
|
|
|
|
case "low":
|
2020-12-11 13:24:39 +11:00
|
|
|
replyWithMap(undefined, "low");
|
2020-07-16 17:27:39 +10:00
|
|
|
break;
|
|
|
|
|
case "medium":
|
2020-12-11 13:24:39 +11:00
|
|
|
replyWithMap("low", "medium");
|
2020-07-16 17:27:39 +10:00
|
|
|
break;
|
|
|
|
|
case "high":
|
2020-12-11 13:24:39 +11:00
|
|
|
replyWithMap("medium", "high");
|
2020-07-16 17:27:39 +10:00
|
|
|
break;
|
|
|
|
|
case "ultra":
|
2020-12-11 13:24:39 +11:00
|
|
|
replyWithMap("medium", "ultra");
|
2020-07-16 17:27:39 +10:00
|
|
|
break;
|
|
|
|
|
case "original":
|
2020-10-30 11:52:23 +11:00
|
|
|
if (map.resolutions) {
|
|
|
|
|
if (map.resolutions.medium) {
|
2020-12-11 13:24:39 +11:00
|
|
|
replyWithMap("medium");
|
2020-10-30 11:52:23 +11:00
|
|
|
} else if (map.resolutions.low) {
|
2020-12-11 13:24:39 +11:00
|
|
|
replyWithMap("low");
|
|
|
|
|
} else {
|
|
|
|
|
replyWithMap();
|
2020-10-30 11:52:23 +11:00
|
|
|
}
|
2020-12-11 13:24:39 +11:00
|
|
|
} else {
|
|
|
|
|
replyWithMap();
|
2020-07-17 15:02:21 +10:00
|
|
|
}
|
2020-07-16 17:27:39 +10:00
|
|
|
break;
|
|
|
|
|
default:
|
2020-12-11 13:24:39 +11:00
|
|
|
replyWithMap();
|
2020-07-16 17:27:39 +10:00
|
|
|
}
|
|
|
|
|
}
|
2020-12-11 13:24:39 +11:00
|
|
|
|
2020-07-16 17:27:39 +10:00
|
|
|
if (id === "mapResponse") {
|
2020-12-11 13:24:39 +11:00
|
|
|
const newMap = data;
|
|
|
|
|
await putMap(newMap);
|
|
|
|
|
setCurrentMap(newMap);
|
2020-07-16 17:27:39 +10:00
|
|
|
}
|
2020-12-11 13:24:39 +11:00
|
|
|
|
2020-07-16 17:27:39 +10:00
|
|
|
if (id === "tokenRequest") {
|
|
|
|
|
const token = getToken(data);
|
2020-09-11 16:56:40 +10:00
|
|
|
// Add a last used property for cache invalidation
|
|
|
|
|
reply("tokenResponse", { ...token, lastUsed: Date.now() }, "token");
|
2020-07-16 17:27:39 +10:00
|
|
|
}
|
|
|
|
|
if (id === "tokenResponse") {
|
|
|
|
|
const newToken = data;
|
2020-12-12 15:03:24 +11:00
|
|
|
await putToken(newToken);
|
2020-07-16 17:27:39 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handlePeerDataProgress({ id, total, count }) {
|
|
|
|
|
if (count === 1) {
|
|
|
|
|
assetLoadStart();
|
|
|
|
|
}
|
|
|
|
|
if (total === count) {
|
|
|
|
|
assetLoadFinish();
|
|
|
|
|
}
|
|
|
|
|
assetProgressUpdate({ id, total, count });
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 13:56:12 +11:00
|
|
|
async function handleSocketMap(map) {
|
2020-12-11 13:24:39 +11:00
|
|
|
if (map) {
|
2021-01-01 13:56:12 +11:00
|
|
|
// If we're the owner get the full map from the database
|
|
|
|
|
if (map.type === "file" && map.owner === userId) {
|
|
|
|
|
const fullMap = await getMapFromDB(map.id);
|
|
|
|
|
setCurrentMap(fullMap);
|
|
|
|
|
} else {
|
|
|
|
|
setCurrentMap(map);
|
|
|
|
|
}
|
2020-12-11 13:24:39 +11:00
|
|
|
} else {
|
|
|
|
|
setCurrentMap(null);
|
|
|
|
|
}
|
2020-12-05 17:16:06 +11:00
|
|
|
}
|
2021-01-01 13:56:12 +11:00
|
|
|
|
2020-12-12 16:08:10 +11:00
|
|
|
session.on("peerData", handlePeerData);
|
|
|
|
|
session.on("peerDataProgress", handlePeerDataProgress);
|
2021-01-01 12:44:56 +11:00
|
|
|
session.socket?.on("map", handleSocketMap);
|
2020-07-16 17:27:39 +10:00
|
|
|
|
|
|
|
|
return () => {
|
2020-12-12 16:08:10 +11:00
|
|
|
session.off("peerData", handlePeerData);
|
|
|
|
|
session.off("peerDataProgress", handlePeerDataProgress);
|
2021-01-01 12:44:56 +11:00
|
|
|
session.socket?.off("map", handleSocketMap);
|
2020-07-16 17:27:39 +10:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2020-07-17 15:57:52 +10:00
|
|
|
const canChangeMap = !isLoading;
|
|
|
|
|
|
2020-07-16 17:27:39 +10:00
|
|
|
const canEditMapDrawing =
|
|
|
|
|
currentMap !== null &&
|
|
|
|
|
currentMapState !== null &&
|
|
|
|
|
(currentMapState.editFlags.includes("drawing") ||
|
|
|
|
|
currentMap.owner === userId);
|
|
|
|
|
|
|
|
|
|
const canEditFogDrawing =
|
|
|
|
|
currentMap !== null &&
|
|
|
|
|
currentMapState !== null &&
|
|
|
|
|
(currentMapState.editFlags.includes("fog") || currentMap.owner === userId);
|
|
|
|
|
|
2020-11-05 16:21:52 +11:00
|
|
|
const canEditNotes =
|
|
|
|
|
currentMap !== null &&
|
|
|
|
|
currentMapState !== null &&
|
|
|
|
|
(currentMapState.editFlags.includes("notes") ||
|
|
|
|
|
currentMap.owner === userId);
|
|
|
|
|
|
2020-07-16 17:27:39 +10:00
|
|
|
const disabledMapTokens = {};
|
|
|
|
|
// If we have a map and state and have the token permission disabled
|
|
|
|
|
// and are not the map owner
|
|
|
|
|
if (
|
|
|
|
|
currentMapState !== null &&
|
|
|
|
|
currentMap !== null &&
|
|
|
|
|
!currentMapState.editFlags.includes("tokens") &&
|
|
|
|
|
currentMap.owner !== userId
|
|
|
|
|
) {
|
|
|
|
|
for (let token of Object.values(currentMapState.tokens)) {
|
|
|
|
|
if (token.owner !== userId) {
|
|
|
|
|
disabledMapTokens[token.id] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Map
|
|
|
|
|
map={currentMap}
|
|
|
|
|
mapState={currentMapState}
|
|
|
|
|
onMapTokenStateChange={handleMapTokenStateChange}
|
|
|
|
|
onMapTokenStateRemove={handleMapTokenStateRemove}
|
|
|
|
|
onMapChange={handleMapChange}
|
|
|
|
|
onMapStateChange={handleMapStateChange}
|
|
|
|
|
onMapDraw={handleMapDraw}
|
|
|
|
|
onMapDrawUndo={handleMapDrawUndo}
|
|
|
|
|
onMapDrawRedo={handleMapDrawRedo}
|
|
|
|
|
onFogDraw={handleFogDraw}
|
|
|
|
|
onFogDrawUndo={handleFogDrawUndo}
|
|
|
|
|
onFogDrawRedo={handleFogDrawRedo}
|
2020-11-04 15:03:34 +11:00
|
|
|
onMapNoteChange={handleNoteChange}
|
2020-11-05 14:41:33 +11:00
|
|
|
onMapNoteRemove={handleNoteRemove}
|
2020-07-16 17:27:39 +10:00
|
|
|
allowMapDrawing={canEditMapDrawing}
|
|
|
|
|
allowFogDrawing={canEditFogDrawing}
|
2020-07-17 15:57:52 +10:00
|
|
|
allowMapChange={canChangeMap}
|
2020-11-05 16:21:52 +11:00
|
|
|
allowNoteEditing={canEditNotes}
|
2020-07-16 17:27:39 +10:00
|
|
|
disabledTokens={disabledMapTokens}
|
2020-07-28 17:59:26 +10:00
|
|
|
session={session}
|
2020-07-16 17:27:39 +10:00
|
|
|
/>
|
|
|
|
|
<Tokens onMapTokenStateCreate={handleMapTokenStateCreate} />
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default NetworkedMapAndTokens;
|