|
33 | 33 | { version: "0.12.0", message: "Keyboard shortcut for Flip A/B (default Alt+R, can be changed in the WME keyboard shortcuts)." }, |
34 | 34 | { version: "0.12.1", message: "Fixed \"illegal turn\" on save after switching a segment to one-way before reversing." }, |
35 | 35 | { 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." }, |
37 | 37 | ]; |
38 | 38 |
|
39 | 39 | // --------------------------------------------------------------------------- |
|
201 | 201 | // eslint-disable-next-line no-bitwise |
202 | 202 | const NODE_EDIT_FLAGS = 1 | 4 | 8; // EDIT_GEOMETRY | EDIT_OPEN_TURNS | EDIT_CLOSE_TURNS |
203 | 203 |
|
| 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 | + |
204 | 234 | // Returns why the segment can't be reversed, or null if it can |
205 | 235 | function getReverseBlocker(segmentId, sdk) { |
206 | 236 | const segment = sdk.DataModel.Segments.getById({ segmentId }); |
207 | 237 | if (!segment) return "segment not found"; |
208 | 238 | if (sdk.DataModel.Segments.connectsToBigJunction({ segmentId })) return "segment is part of a big junction"; |
209 | 239 | if (!sdk.DataModel.Segments.hasPermissions({ segmentId })) return "insufficient permissions"; |
210 | 240 | 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 | + } |
211 | 250 | for (const [end, nodeId] of [["A", segment.fromNodeId], ["B", segment.toNodeId]]) { |
212 | 251 | if (nodeId == null) continue; |
213 | 252 | const nodeName = `node ${end} (id ${nodeId})`; |
|
0 commit comments