|
| 1 | +//* This file is explicitly licensed under the MIT license. *// |
| 2 | +//* Copyright (c) 2025 Citadel Station Developers *// |
| 3 | + |
| 4 | +GLOBAL_LIST_EMPTY_TYPED(gm_pings, /datum/gm_ping) |
| 5 | +GLOBAL_VAR_INIT(gm_ping_ghost_allowed, FALSE) |
| 6 | + |
| 7 | +/** |
| 8 | + * Contextual GM ping system. |
| 9 | + */ |
| 10 | +/datum/gm_ping |
| 11 | + /// unique id, lazy |
| 12 | + /// * not for persistent purposes, that comes later |
| 13 | + /// * not guaranteed to be unique, just used to filter/resolve with ref() |
| 14 | + var/lazy_unsafe_uid |
| 15 | + var/static/lazy_unsafe_uid_next = 0 |
| 16 | + |
| 17 | + /// originating ckey |
| 18 | + var/originating_ckey |
| 19 | + /// originating key because it's prettier than ckey |
| 20 | + var/originating_key |
| 21 | + |
| 22 | + /// where was the ping made? |
| 23 | + var/turf/created_at |
| 24 | + |
| 25 | + /// originating mob weakref, if any |
| 26 | + var/datum/weakref/originating_mob_weakref |
| 27 | + /// cached tgui data for originating mob if it's deleted, if any |
| 28 | + var/list/originating_mob_ui_data_snapshot |
| 29 | + |
| 30 | + /// original message. NOT SANITIZED. treat as saycode pre-parse. |
| 31 | + var/unsanitized_message |
| 32 | + |
| 33 | + /// linked component. if it exists, we have in-world-context using it. |
| 34 | + var/datum/component/gm_ping/context_component |
| 35 | + /// linked component ui data snapshot if it's deleted, if any |
| 36 | + var/list/context_component_ui_data_snapshot |
| 37 | + |
| 38 | +/datum/gm_ping/New() |
| 39 | + ++lazy_unsafe_uid_next |
| 40 | + if(lazy_unsafe_uid_next >= SHORT_REAL_LIMIT) |
| 41 | + lazy_unsafe_uid_next = -(SHORT_REAL_LIMIT - 1) |
| 42 | + lazy_unsafe_uid = num2text(lazy_unsafe_uid_next, 999) |
| 43 | + |
| 44 | +/datum/gm_ping/Destroy() |
| 45 | + originating_mob_weakref = null |
| 46 | + GLOB.gm_pings -= src |
| 47 | + QDEL_NULL(context_component) |
| 48 | + return ..() |
| 49 | + |
| 50 | +/datum/gm_ping/proc/chat_output() |
| 51 | + var/rendered = say_emphasis(html_encode(unsanitized_message)) |
| 52 | + return "[lazy_unsafe_uid] @ [context_component?.parent] - From '[originating_ckey]', a '[SPAN_TOOLTIP(rendered, "[length_char(unsanitized_message)] character message")]'" |
| 53 | + |
| 54 | +/datum/gm_ping/proc/link_context(atom/target) |
| 55 | + if(context_component) |
| 56 | + QDEL_NULL(context_component) |
| 57 | + if(target) |
| 58 | + context_component = target.AddComponent(/datum/component/gm_ping, src) |
| 59 | + if(QDELETED(context_component)) |
| 60 | + context_component = null |
| 61 | + |
| 62 | +// for stuff that may change; refreshing will pull an update |
| 63 | +/datum/gm_ping/proc/ui_panel_data() |
| 64 | + return list( |
| 65 | + "pingOrigination" = pull_ui_panel_mob_data(), |
| 66 | + "pingContext" = pull_ui_panel_context_data(), |
| 67 | + ) |
| 68 | + |
| 69 | +// for stuff that never changes; even refreshing won't change it |
| 70 | +/datum/gm_ping/proc/ui_panel_static_data() |
| 71 | + return list( |
| 72 | + "ref" = ref(src), |
| 73 | + "lazyUid" = lazy_unsafe_uid, |
| 74 | + "playerCkey" = originating_ckey, |
| 75 | + "playerKey" = originating_key, |
| 76 | + // DO NOT REMOVE THE FUCKING HTML ENCODE OR YOU WILL XSS THE ADMINS!!! |
| 77 | + "messageAsHtml" = say_emphasis(html_encode(unsanitized_message)), |
| 78 | + "pingLocation" = encode_ui_panel_location_data(), |
| 79 | + ) |
| 80 | + |
| 81 | +/datum/gm_ping/proc/encode_ui_location(atom/target) |
| 82 | + . = list() |
| 83 | + var/turf/l_turf = get_turf(target) |
| 84 | + if(l_turf) |
| 85 | + .["coords"] = list( |
| 86 | + "name" = l_turf.name, |
| 87 | + "x" = l_turf.x, |
| 88 | + "y" = l_turf.y, |
| 89 | + "z" = l_turf.z, |
| 90 | + ) |
| 91 | + else |
| 92 | + .["coords"] = null |
| 93 | + if(l_turf) |
| 94 | + var/datum/map/l_map = SSmapping.ordered_levels[l_turf.z]?.parent_map |
| 95 | + if(l_map) |
| 96 | + .["sector"] = list( |
| 97 | + "id" = l_map.id, |
| 98 | + "name" = l_map.name, |
| 99 | + ) |
| 100 | + else |
| 101 | + .["sector"] = null |
| 102 | + var/obj/overmap/entity/l_overmap = get_overmap_sector(l_turf) |
| 103 | + if(l_overmap) |
| 104 | + .["overmap"] = list( |
| 105 | + "entity" = l_overmap.name, |
| 106 | + "x" = l_overmap.get_tile_x_f(), |
| 107 | + "y" = l_overmap.get_tile_y_f(), |
| 108 | + "map" = l_overmap.overmap?.name, |
| 109 | + ) |
| 110 | + else |
| 111 | + .["overmap"] = null |
| 112 | + else |
| 113 | + .["sector"] = null |
| 114 | + .["overmap"] = null |
| 115 | + |
| 116 | +/datum/gm_ping/proc/encode_ui_panel_location_data() |
| 117 | + return list( |
| 118 | + "name" = created_at.name, |
| 119 | + "location" = encode_ui_location(created_at), |
| 120 | + ) |
| 121 | + |
| 122 | +/datum/gm_ping/proc/encode_ui_panel_mob_data(mob/target) |
| 123 | + return list( |
| 124 | + "name" = target.real_name, |
| 125 | + "visibleName" = target.name, |
| 126 | + "location" = encode_ui_location(target) |
| 127 | + ) |
| 128 | + |
| 129 | +/datum/gm_ping/proc/pull_ui_panel_mob_data() |
| 130 | + var/mob/resolved = originating_mob_weakref?.resolve() |
| 131 | + if(resolved) |
| 132 | + var/list/encoded = encode_ui_panel_mob_data(resolved) |
| 133 | + originating_mob_ui_data_snapshot = encoded |
| 134 | + return list( |
| 135 | + "deleted" = FALSE, |
| 136 | + "data" = encoded, |
| 137 | + ) |
| 138 | + else if(originating_mob_ui_data_snapshot) |
| 139 | + return list( |
| 140 | + "deleted" = TRUE, |
| 141 | + "data" = originating_mob_ui_data_snapshot, |
| 142 | + ) |
| 143 | + else |
| 144 | + // if we never got a snapshot for some reason treat it as gone |
| 145 | + return null |
| 146 | + |
| 147 | +/datum/gm_ping/proc/encode_ui_panel_context_data(datum/component/gm_ping/target) |
| 148 | + return list( |
| 149 | + // bite me it's an /atom component |
| 150 | + "name" = target.parent:name, |
| 151 | + "location" = encode_ui_location(target.parent), |
| 152 | + "ref" = REF(target.parent), |
| 153 | + ) |
| 154 | + |
| 155 | +/datum/gm_ping/proc/pull_ui_panel_context_data() |
| 156 | + if(QDELETED(context_component)) |
| 157 | + if(context_component_ui_data_snapshot) |
| 158 | + return list( |
| 159 | + "deleted" = TRUE, |
| 160 | + "data" = context_component_ui_data_snapshot, |
| 161 | + ) |
| 162 | + // if we never got a snapshot for some reason treat it as gone |
| 163 | + else |
| 164 | + return null |
| 165 | + else |
| 166 | + var/list/encoded = encode_ui_panel_context_data(context_component) |
| 167 | + context_component_ui_data_snapshot = encoded |
| 168 | + return list( |
| 169 | + "deleted" = FALSE, |
| 170 | + "data" = encoded, |
| 171 | + ) |
0 commit comments