* Baumeister

Fügt einen Präfix zum Namen neuer Gebäude hinzu, wählt die nächste Integrierte Leitstelle aus und selektiert das Startfahrzeug für Feuerwachen aus. Zudem erstellt es über einen separaten Button eine Rettungswache (Klein) und SEG am aktuellen Standort.

  1. // ==UserScript==
  2. // @name * Baumeister
  3. // @namespace bos-ernie.leitstellenspiel.de
  4. // @version 1.4.0
  5. // @license BSD-3-Clause
  6. // @author BOS-Ernie
  7. // @description Fügt einen Präfix zum Namen neuer Gebäude hinzu, wählt die nächste Integrierte Leitstelle aus und selektiert das Startfahrzeug für Feuerwachen aus. Zudem erstellt es über einen separaten Button eine Rettungswache (Klein) und SEG am aktuellen Standort.
  8. // @match https://www.leitstellenspiel.de/
  9. // @match https://polizei.leitstellenspiel.de/
  10. // @icon https://www.google.com/s2/favicons?sz=64&domain=leitstellenspiel.de
  11. // @run-at document-idle
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. /* global building_new_marker, building_new_dragend */
  16.  
  17. (function () {
  18. "use strict";
  19.  
  20. const buildingImages = [
  21. {
  22. buildingTypeId: 7,
  23. caption: "Leitstelle",
  24. image: "/images/building_leitstelle.png",
  25. },
  26. {
  27. buildingTypeId: 0,
  28. caption: "Feuerwache",
  29. image: "/images/building_fire.png",
  30. },
  31. {
  32. buildingTypeId: 18,
  33. caption: "Feuerwache (Kleinwache)",
  34. image: "/images/building_fire.png",
  35. },
  36. {
  37. buildingTypeId: 1,
  38. caption: "Feuerwehrschule",
  39. image: "/images/building_fireschool.png",
  40. },
  41. {
  42. buildingTypeId: 2,
  43. caption: "Rettungswache",
  44. image: "/images/building_rescue_station.png",
  45. },
  46. {
  47. buildingTypeId: 20,
  48. caption: "Rettungswache (Kleinwache)",
  49. image: "/images/building_rescue_station.png",
  50. },
  51. {
  52. buildingTypeId: 3,
  53. caption: "Rettungsschule",
  54. image: "/images/building_rettungsschule.png",
  55. },
  56. {
  57. buildingTypeId: 4,
  58. caption: "Krankenhaus",
  59. image: "/images/building_hospital.png",
  60. },
  61. {
  62. buildingTypeId: 5,
  63. caption: "Rettungshubschrauber-Station",
  64. image: null,
  65. },
  66. {
  67. buildingTypeId: 12,
  68. caption: "Schnelleinsatzgruppe (SEG)",
  69. image: "/images/building_seg.png",
  70. },
  71. {
  72. buildingTypeId: 6,
  73. caption: "Polizeiwache",
  74. image: "/images/building_polizeiwache.png",
  75. },
  76. {
  77. buildingTypeId: 19,
  78. caption: "Polizeiwache (Kleinwache)",
  79. image: "/images/building_polizeiwache.png",
  80. },
  81. {
  82. buildingTypeId: 11,
  83. caption: "Bereitschaftspolizei",
  84. image: "/images/building_bereitschaftspolizei.png",
  85. },
  86. {
  87. buildingTypeId: 17,
  88. caption: "Polizei-Sondereinheiten",
  89. image: null,
  90. },
  91. {
  92. buildingTypeId: 13,
  93. caption: "Polizeihubschrauberstation",
  94. image: "/images/building_helipad_polizei.png",
  95. },
  96. {
  97. buildingTypeId: 8,
  98. caption: "Polizeischule",
  99. image: "/images/building_polizeischule.png",
  100. },
  101. {
  102. buildingTypeId: 9,
  103. caption: "THW",
  104. image: "/images/building_thw.png",
  105. },
  106. {
  107. buildingTypeId: 10,
  108. caption: "THW Bundesschule",
  109. image: "/images/building_thw_school.png",
  110. },
  111. {
  112. buildingTypeId: 14,
  113. caption: "Bereitstellungsraum",
  114. image: null,
  115. },
  116. {
  117. buildingTypeId: 15,
  118. caption: "Wasserrettung",
  119. image: "/images/building_wasserwacht.png",
  120. },
  121. {
  122. buildingTypeId: 21,
  123. caption: "Rettungshundestaffel",
  124. image: "/images/building_rescue_dog_unit.png",
  125. },
  126. ];
  127.  
  128. class Coordinate {
  129. constructor(latitude, longitude) {
  130. this.latitude = latitude;
  131. this.longitude = longitude;
  132. }
  133. }
  134.  
  135. let controlCenters = [];
  136.  
  137. const date = new Date().toISOString().slice(2, 10).replace(/-/g, "");
  138.  
  139. const prefixes = {
  140. 7: "ILS",
  141. 0: "Florian " + date + " NW",
  142. 18: "Florian " + date + " KW",
  143. 1: "FS",
  144. 2: "Rettung " + date + " NW",
  145. 20: "Rettung " + date + " KW",
  146. 3: "RS",
  147. 4: "KH",
  148. 5: "Christoph",
  149. 12: "SEG " + date,
  150. 6: "Dora " + date + " NW",
  151. 19: "Dora " + date + " KW",
  152. 11: "Bruno " + date,
  153. 17: "Polizei-Sondereinheiten",
  154. 13: "Bussard",
  155. 8: "PS",
  156. 9: "Heros " + date,
  157. 10: "TS",
  158. 14: "BSR",
  159. 15: "Neptun " + date,
  160. 21: "Antonius " + date,
  161. 16: "Polizeizellen",
  162. };
  163.  
  164. const createdBuildingsListId = "created-buildings-list";
  165.  
  166. async function getBuildings() {
  167. if (
  168. !sessionStorage.aBuildings ||
  169. JSON.parse(sessionStorage.aBuildings).lastUpdate < new Date().getTime() - 5 * 1000 * 60
  170. ) {
  171. const buildings = await fetch("/api/buildings.json").then(response => response.json());
  172.  
  173. try {
  174. sessionStorage.setItem("aBuildings", JSON.stringify({ lastUpdate: new Date().getTime(), value: buildings }));
  175. } catch (e) {
  176. return buildings;
  177. }
  178. }
  179.  
  180. return JSON.parse(sessionStorage.aBuildings).value;
  181. }
  182.  
  183. async function initControlCenters() {
  184. const buildings = await getBuildings();
  185.  
  186. controlCenters = buildings.filter(building => building.building_type === 7);
  187. }
  188.  
  189. function calculateDistanceInKm(coordinateA, coordinateB) {
  190. const R = 6371;
  191. const dLat = deg2rad(coordinateB.latitude - coordinateA.latitude);
  192. const dLon = deg2rad(coordinateB.longitude - coordinateA.longitude);
  193. const a =
  194. Math.sin(dLat / 2) * Math.sin(dLat / 2) +
  195. Math.cos(deg2rad(coordinateA.latitude)) *
  196. Math.cos(deg2rad(coordinateB.latitude)) *
  197. Math.sin(dLon / 2) *
  198. Math.sin(dLon / 2);
  199. const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  200. return R * c;
  201. }
  202.  
  203. function deg2rad(deg) {
  204. return deg * (Math.PI / 180);
  205. }
  206.  
  207. function getIdOfClosestControlCenter(coordinate) {
  208. let localControlCenters = controlCenters.map(controlCenter => {
  209. const distance = calculateDistanceInKm(
  210. coordinate,
  211. new Coordinate(controlCenter.latitude, controlCenter.longitude),
  212. );
  213. return {
  214. id: controlCenter.id,
  215. caption: controlCenter.caption,
  216. distance: distance,
  217. };
  218. });
  219.  
  220. localControlCenters.sort((a, b) => a.distance - b.distance);
  221.  
  222. return localControlCenters[0].id;
  223. }
  224.  
  225. function getCurrentCity() {
  226. const address = document.getElementById("building_address").value;
  227.  
  228. return address
  229. .substring(address.lastIndexOf(",") + 1)
  230. .trim()
  231. .replace(/\d+/g, "")
  232. .trim();
  233. }
  234.  
  235. function updateBuildingName(buildingType) {
  236. let buildingName = "";
  237.  
  238. const prefix = prefixes[buildingType];
  239. if (prefix) {
  240. buildingName = prefix + " ";
  241. }
  242.  
  243. document.getElementById("building_name").value = buildingName + getCurrentCity();
  244. }
  245.  
  246. function buildingTypeChangeEvent(event) {
  247. const buildingType = event.target.value;
  248. updateBuildingName(buildingType);
  249.  
  250. if (buildingType === "0") {
  251. document.getElementById("building_start_vehicle_feuerwache").value = 30;
  252. }
  253. if (buildingType === "18") {
  254. document.getElementById("building_start_vehicle_feuerwache_kleinwache").value = 30;
  255. }
  256. }
  257.  
  258. function selectClosestControlCenter(coordinate) {
  259. document.getElementById("building_leitstelle_building_id").value = getIdOfClosestControlCenter(coordinate);
  260. }
  261.  
  262. function overwriteDrangEndListener() {
  263. let latitude = null;
  264. let longitude = null;
  265. const buildingNewDragendOriginal = building_new_dragend;
  266. building_new_dragend = function () {
  267. let coordinates = building_new_marker.getLatLng();
  268.  
  269. let coordinatesChanged = false;
  270. if (latitude !== coordinates.lat) {
  271. latitude = coordinates.lat;
  272. coordinatesChanged = true;
  273. }
  274.  
  275. if (longitude !== coordinates.lng) {
  276. longitude = coordinates.lng;
  277. coordinatesChanged = true;
  278. }
  279.  
  280. if (coordinatesChanged) {
  281. selectClosestControlCenter(new Coordinate(latitude, longitude));
  282. }
  283.  
  284. buildingNewDragendOriginal();
  285.  
  286. setTimeout(() => {
  287. updateBuildingName(document.getElementById("building_building_type").value);
  288. }, 250);
  289. };
  290. }
  291.  
  292. function selectBuildingType(id) {
  293. const buildingType = document.getElementById("building_building_type");
  294.  
  295. for (let i = 0; i < buildingType.options.length; i++) {
  296. if (buildingType.options[i].value === id) {
  297. buildingType.selectedIndex = i;
  298. buildingType.dispatchEvent(new Event("change"));
  299. break;
  300. }
  301. }
  302.  
  303. console.warn("[Baumeister] Failed to select building type");
  304. }
  305.  
  306. function getImageUrlByBuildingTypeId(buildingTypeId) {
  307. const buildingImage = buildingImages.find(
  308. buildingImage => buildingImage.buildingTypeId === parseInt(buildingTypeId),
  309. );
  310. if (buildingImage) {
  311. return buildingImage.image;
  312. }
  313.  
  314. return null;
  315. }
  316.  
  317. async function createBuilding() {
  318. const coordinate = building_new_marker.getLatLng();
  319.  
  320. const form = document.getElementById("new_building");
  321. const formData = new FormData(form);
  322.  
  323. const response = await fetch("https://www.leitstellenspiel.de/buildings", {
  324. headers: {
  325. "x-csrf-token": document.querySelector('meta[name="csrf-token"]'),
  326. "x-requested-with": "XMLHttpRequest",
  327. },
  328. method: "POST",
  329. body: formData,
  330. });
  331.  
  332. const responseText = await response.text();
  333.  
  334. const parser = new DOMParser();
  335. const responseParser = parser.parseFromString(responseText, "text/html");
  336. const alerts = responseParser.querySelectorAll("span.label-danger");
  337.  
  338. if (alerts.length > 0) {
  339. alerts.forEach(alert => {
  340. const message = alert.innerText;
  341.  
  342. const alertElement = document.createElement("div");
  343. alertElement.className = "alert alert-danger";
  344. alertElement.innerText = message;
  345.  
  346. document.getElementById("detail_16").parentElement.insertAdjacentElement("beforeend", alertElement);
  347. });
  348.  
  349. return;
  350. }
  351.  
  352. const buildingId = responseText.match(/\/buildings\/(\d+)/)[1];
  353.  
  354. const buildingName = document.getElementById("building_name").value;
  355.  
  356. const createdBuildingsListItem = document.createElement("li");
  357. createdBuildingsListItem.innerHTML = `<a href="/buildings/${buildingId}" target="_blank" class="text-success">${buildingName}</a>`;
  358. document.getElementById(createdBuildingsListId).appendChild(createdBuildingsListItem);
  359.  
  360. const iconUrl = getImageUrlByBuildingTypeId(formData.get("building[building_type]"));
  361.  
  362. if (iconUrl) {
  363. const markerOptions = {
  364. icon: L.icon({
  365. iconUrl: iconUrl,
  366. iconSize: [32, 37],
  367. iconAnchor: [16, 37],
  368. popupAnchor: [0, -37],
  369. }),
  370. };
  371. L.marker([coordinate.lat, coordinate.lng], markerOptions).addTo(map);
  372. } else {
  373. L.marker([coordinate.lat, coordinate.lng]).addTo(map);
  374. }
  375. }
  376.  
  377. async function rescueBuildingsButtonClickEvent(event) {
  378. event.preventDefault();
  379.  
  380. const buildButton = event.target.closest("button");
  381. buildButton.disabled = true;
  382.  
  383. // Create rescue station building
  384. selectBuildingType("20");
  385. await createBuilding();
  386.  
  387. // Create rapid response team building
  388. selectBuildingType("12");
  389. await createBuilding();
  390.  
  391. buildButton.disabled = false;
  392. }
  393.  
  394. function addRescueBuildingsButton() {
  395. const buttonId = "rescue-buildings-button";
  396.  
  397. if (document.getElementById(buttonId)) {
  398. return;
  399. }
  400.  
  401. const rescueBuildingsButton = document.createElement("button");
  402. rescueBuildingsButton.id = buttonId;
  403. rescueBuildingsButton.type = "button";
  404. rescueBuildingsButton.className = "btn btn-default";
  405. rescueBuildingsButton.innerHTML = "🚑";
  406. rescueBuildingsButton.addEventListener("click", rescueBuildingsButtonClickEvent);
  407.  
  408. document.getElementById("detail_16").parentElement.insertAdjacentElement("beforeend", rescueBuildingsButton);
  409. }
  410.  
  411. function addOrderedList() {
  412. const orderedList = document.createElement("ol");
  413. orderedList.id = createdBuildingsListId;
  414.  
  415. document.getElementById("detail_16").parentElement.insertAdjacentElement("beforeend", orderedList);
  416. }
  417.  
  418. async function buildButtonClickEvent(event) {
  419. event.preventDefault();
  420.  
  421. const buildButton = event.target;
  422.  
  423. buildButton.disabled = true;
  424.  
  425. await createBuilding();
  426.  
  427. buildButton.disabled = false;
  428. }
  429.  
  430. function addEventListeners() {
  431. const buildButtons = document.querySelectorAll("input[type=submit].build_with_credits_step");
  432.  
  433. for (let i = 0; i < buildButtons.length; i++) {
  434. buildButtons[i].addEventListener("click", buildButtonClickEvent);
  435. }
  436. }
  437.  
  438. async function main() {
  439. await initControlCenters();
  440.  
  441. const observer = new MutationObserver(mutationRecords => {
  442. mutationRecords.forEach(mutation => {
  443. if (!mutation.target.querySelector("#new_building")) {
  444. return;
  445. }
  446.  
  447. addEventListeners();
  448. addRescueBuildingsButton();
  449. addOrderedList();
  450.  
  451. document.getElementById("building_building_type").addEventListener("change", buildingTypeChangeEvent);
  452.  
  453. overwriteDrangEndListener();
  454.  
  455. updateBuildingName();
  456.  
  457. const element = document.getElementById("building_new_info_message");
  458. if (element) {
  459. element.remove();
  460. }
  461. });
  462. });
  463.  
  464. observer.observe(document.getElementById("buildings"), {
  465. childList: true,
  466. });
  467. }
  468.  
  469. main();
  470. })();