Skip to content

Commit 4e06a2e

Browse files
committed
Disable For Segments with TurnGuidance You Are Not Allowed to Edit
1 parent 2b12881 commit 4e06a2e

1 file changed

Lines changed: 40 additions & 1 deletion

File tree

‎wme-reverse-segment.js‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
{ version: "0.12.0", message: "Keyboard shortcut for Flip A/B (default Alt+R, can be changed in the WME keyboard shortcuts)." },
3434
{ version: "0.12.1", message: "Fixed \"illegal turn\" on save after switching a segment to one-way before reversing." },
3535
{ version: "1.0.0", message: "First public release on Greasy Fork." },
36-
{ version: "1.0.1", message: "Messages name the affected node as node A or node B." },
36+
{ version: "1.0.1", message: "Messages name the affected node as node A or node B. Segments with turn guidance you are not allowed to edit (rank) are no longer reversed." },
3737
];
3838

3939
// ---------------------------------------------------------------------------
@@ -201,13 +201,52 @@
201201
// eslint-disable-next-line no-bitwise
202202
const NODE_EDIT_FLAGS = 1 | 4 | 8; // EDIT_GEOMETRY | EDIT_OPEN_TURNS | EDIT_CLOSE_TURNS
203203

204+
// Mirrors WME's check for country/rank based permissions such as
205+
// "allowEditingTurnGuidanceRank": a boolean user permission wins, otherwise the
206+
// country's minimum rank applies; null means nobody may edit it.
207+
function hasRankPermission(key, countryId) {
208+
const id = countryId ?? W.model.getTopCountry?.()?.getID();
209+
if (id == null) return false;
210+
let value = W.model.getUserPermissions?.()?.[id]?.[key];
211+
if (typeof value !== "boolean") value = W.model.countries?.getObjectById(id)?.getAttribute(key) ?? null;
212+
if (typeof value === "boolean") return value;
213+
if (typeof value === "number") {
214+
const { user } = W.loginManager;
215+
return (user?.getAttribute?.("rank") ?? user?.attributes?.rank ?? user?.rank ?? 0) >= value;
216+
}
217+
return false;
218+
}
219+
220+
function getSegmentCountryId(seg) {
221+
const street = W.model.streets.getObjectById(seg.getAttribute("primaryStreetID"));
222+
const city = street && W.model.cities.getObjectById(street.getAttribute("cityID"));
223+
return city?.getAttribute("countryID") ?? null;
224+
}
225+
226+
// Turn guidance of a turn can be edited if its from segment allows it and the
227+
// editor's rank is high enough for the country (same as WME's turn panel).
228+
function canEditTurnGuidance(fromSegmentId) {
229+
const seg = W.model.segments.getObjectById(fromSegmentId);
230+
if (!seg?.canEditTurnGuidance?.()) return false;
231+
return hasRankPermission("allowEditingTurnGuidanceRank", getSegmentCountryId(seg));
232+
}
233+
204234
// Returns why the segment can't be reversed, or null if it can
205235
function getReverseBlocker(segmentId, sdk) {
206236
const segment = sdk.DataModel.Segments.getById({ segmentId });
207237
if (!segment) return "segment not found";
208238
if (sdk.DataModel.Segments.connectsToBigJunction({ segmentId })) return "segment is part of a big junction";
209239
if (!sdk.DataModel.Segments.hasPermissions({ segmentId })) return "insufficient permissions";
210240
if (segment.hasClosures) return "segment has active closures";
241+
// Restoring the turns rewrites their turn guidance, which needs the rights for it
242+
const turns = [...sdk.DataModel.Turns.getTurnsFromSegment({ segmentId }), ...sdk.DataModel.Turns.getTurnsToSegment({ segmentId })];
243+
const guided = turns.find(t => t.isAllowed && Object.keys(GUIDANCE_FLAGS).some(f => t[f]) && !canEditTurnGuidance(t.fromSegmentId));
244+
if (guided) {
245+
// Turns leaving A->B and entering B->A pass node B, all others node A
246+
const atB = guided.fromSegmentId === +segmentId ? guided.fromSegmentFwd : !guided.toSegmentFwd;
247+
const [end, nodeId] = atB ? ["B", segment.toNodeId] : ["A", segment.fromNodeId];
248+
return `a turn at node ${end} (id ${nodeId}) has turn guidance you are not allowed to edit`;
249+
}
211250
for (const [end, nodeId] of [["A", segment.fromNodeId], ["B", segment.toNodeId]]) {
212251
if (nodeId == null) continue;
213252
const nodeName = `node ${end} (id ${nodeId})`;

0 commit comments

Comments
 (0)