forked from Suchty112/Leitstellenspiel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirstresponder.user.js
218 lines (193 loc) · 11.2 KB
/
firstresponder.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// ==UserScript==
// @name FirstResponder (Original by JuMaHo)
// @version 1.5.0
// @description wählt das nächstgelegene FirstResponder-Fahrzeug aus
// @author DrTraxx
// @match *://www.leitstellenspiel.de/missions/*
// @match *://www.leitstellenspiel.de/aaos/*/edit
// @match *://www.leitstellenspiel.de/buildings/*/edit
// @match *://www.missionchief.co.uk/missions/*
// @match *://www.missionchief.co.uk/aaos/*/edit
// @match *://www.missionchief.co.uk/buildings/*/edit
// @match *://www.missionchief.com/missions/*
// @match *://www.missionchief.com/aaos/*/edit
// @match *://www.missionchief.com/buildings/*/edit
// @grant none
// ==/UserScript==
/* global $,I18n */
(async function() {
'use strict';
async function getVehicleTypes() {
if (!localStorage.aVehicleTypesNew || JSON.parse(localStorage.aVehicleTypesNew).lastUpdate < (new Date().getTime() - 5 * 1000 * 60)) {
await $.getJSON("https://api.lss-cockpit.de/de_DE/vehicletypes.json").done(data => localStorage.setItem('aVehicleTypesNew', JSON.stringify({ lastUpdate: new Date().getTime(), value: data })));
}
return JSON.parse(localStorage.aVehicleTypesNew).value;
}
if (!localStorage.firstResponder) localStorage.firstResponder = JSON.stringify({ "vehicleTypes": {}, "aaoId": {} });
if (!localStorage.fr_dispatchSetup) localStorage.fr_dispatchSetup = JSON.stringify({ "dispatchId": [], "useIt": false, "additionalBuildings": [] });
var aVehicleTypes = [];
var frSettings = JSON.parse(localStorage.firstResponder);
var dispatchSetup = JSON.parse(localStorage.fr_dispatchSetup);
var lang = I18n.locale;
var aBuildings = await $.getJSON('/api/buildings');
if (lang == "de_DE") {
aVehicleTypes = await getVehicleTypes();
} else {
if (!localStorage.aVehicleTypes || JSON.parse(localStorage.aVehicleTypes).lastUpdate < (new Date().getTime() - 5 * 1000 * 60)) {
await $.getJSON("https://suchty112.github.io/cars." + lang + ".json").done(data => localStorage.setItem('aVehicleTypes', JSON.stringify({ lastUpdate: new Date().getTime(), value: data })));
}
aVehicleTypes = JSON.parse(localStorage.aVehicleTypes).value;
}
if (!frSettings.vehicleTypes[lang]) frSettings.vehicleTypes[lang] = [];
if (!dispatchSetup.additionalBuildings) dispatchSetup.additionalBuildings = [];
function mapVehicles(arrClasses, trigger) {
var returnValue = [];
if (trigger == "type") {
returnValue = $.map(arrClasses, function(item) {
return aVehicleTypes.filter((obj) => obj.name == item)[0].id;
});
} else if (trigger == "name") {
returnValue = $.map(arrClasses, function(item) {
return aVehicleTypes.filter((obj) => obj.id == item)[0].name;
});
}
return returnValue;
}
function mapDispatchCenter(arrDispatchCenter, trigger) {
var returnValue = [];
if (trigger == "name") {
returnValue = $.map(arrDispatchCenter, function(item) {
return aBuildings.filter((obj) => obj.id == item)[0].caption;
});
} else if (trigger == "id") {
returnValue = $.map(arrDispatchCenter, function(item) {
return aBuildings.filter((obj) => obj.caption == item)[0].id;
});
}
return returnValue;
}
if (window.location.pathname.includes("aaos") && window.location.pathname.includes("edit")) {
$(".boolean.optional.checkbox")
.before(`<label class="form-check-label" for="frSaveAaoId">
<input class="form-check-input" type="checkbox" id="frSaveAaoId" ${ window.location.pathname.includes(frSettings.aaoId[lang]) ? "checked" : "" }>
${ lang == "de_DE" ? "Diese ID für den First Responder nutzen." : "Use this id for FirstResponder." }
</label>`);
}
if (window.location.pathname.includes("missions")) {
var arrVehicles = [];
var dispatchCenter = mapDispatchCenter(dispatchSetup.additionalBuildings, "name");
var i;
for (i in aVehicleTypes) {
arrVehicles.push(aVehicleTypes[i].name);
}
arrVehicles.sort((a, b) => a.toUpperCase() > b.toUpperCase() ? 1 : -1);
for (i in aBuildings) {
var e = aBuildings[i];
if (e.leitstelle_building_id && !dispatchCenter.includes(aBuildings.filter((obj) => obj.id == e.leitstelle_building_id)[0].caption)) {
dispatchCenter.push(aBuildings.filter((obj) => obj.id == e.leitstelle_building_id)[0].caption);
}
}
dispatchCenter.sort((a, b) => a.toUpperCase() > b.toUpperCase() ? 1 : -1);
if (frSettings.aaoId[lang]) {
$("#available_aao_" + frSettings.aaoId[lang])
.parent()
.after(`<button type="button" class="btn btn-success btn-xs" data-toggle="modal" data-target="#frModal" style="height:24px">
<div class="glyphicon glyphicon-cog" style="color:LightSteelBlue"></div>
</button>
<div class="modal fade" id="frModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title" id="frModalLabel">${ lang == "de_DE" ? "Einstellungen" : "Settings" }</h3>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="frModalBody">
<label for="frSelectVehicles">${ lang == "de_DE" ? "Fahrzeugtypen (Mehrfachauswahl mit Strg + Klick)" : "vehicle-types (multiple-choice with Strg + click)" }</label>
<select multiple class="form-control" id="frSelectVehicles" style="height:20em;width:40em"></select>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="frCbxUseLst" ${ dispatchSetup.useIt ? "checked" : "" }>
<label class="form-check-label" for="frCbxUseLst" style="margin-top:2em">${ lang == "de_DE" ? "nur Fahrzeuge bestimmter Leitstellen wählen" : "only use specific dispatchcenter" }</label>
</div>
<label for="frSelectDispatch">${ lang == "de_DE" ? "Leitstellen (Mehrfachauswahl mit Strg + Klick)" : "dispatchcenter (multiple-choice with Strg + click)" }</label>
<select multiple class="form-control" id="frSelectDispatch" style="height:10em;width:40em"></select>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">${ lang == "de_DE" ? "Schließen" : "close" }</button>
<button type="button" class="btn btn-success" id="frSavePreferences">${ lang == "de_DE" ? "Speichern" : "save" }</button>
</div>
</div>
</div>
</div>`);
}
for (i in arrVehicles) {
$("#frSelectVehicles").append(`<option>${ arrVehicles[i] }</option>`);
}
for (i in dispatchCenter) {
$("#frSelectDispatch").append(`<option>${ dispatchCenter[i] }</option>`);
}
$("#frSelectVehicles").val(mapVehicles(frSettings.vehicleTypes[lang], "name"));
$("#frSelectDispatch").val(mapDispatchCenter(dispatchSetup.dispatchId, "name"));
}
if (window.location.pathname.includes("buildings") && window.location.pathname.includes("edit")) {
$(".building_leitstelle_building_id")
.after(`<div class="form-check">
<input type="checkbox" class="form-check-input" id="frCbxBuildingId" ${ $.inArray(+window.location.pathname.replace(/\D+/g, ""), dispatchSetup.additionalBuildings) > -1 ? "checked" : "" }>
<label class="form-check-label" for="frCbxBuildingId">${ lang == "de_DE" ? "Wachen-ID im First Responder berücksichtigen" : "use this building id for First Responder" }</label>
</div>`);
}
$("body").on("click", "#frSaveAaoId", function() {
if ($("#frSaveAaoId")[0].checked) {
frSettings.aaoId[lang] = window.location.pathname.replace(/\D+/g, "");
} else {
delete frSettings.aaoId[lang];
}
localStorage.firstResponder = JSON.stringify(frSettings);
});
$("body").on("click", "#frCbxBuildingId", function() {
var buildingId = +window.location.pathname.replace(/\D+/g, "")
if ($("#frCbxBuildingId")[0].checked) {
dispatchSetup.additionalBuildings.push(buildingId);
} else {
dispatchSetup.additionalBuildings.splice($.inArray(buildingId, dispatchSetup.additionalBuildings), 1);
if (dispatchSetup.dispatchId.includes(buildingId)) {
dispatchSetup.dispatchId.splice($.inArray(buildingId, dispatchSetup.dispatchId), 1);
}
}
localStorage.fr_dispatchSetup = JSON.stringify(dispatchSetup);
});
$("body").on("click", "#frCbxUseLst", function() {
if ($("#frCbxUseLst")[0].checked) {
$("#frSelectDispatch").val(mapDispatchCenter(dispatchSetup.dispatchId, "name"));
} else {
$("#frSelectDispatch").val([]);
}
});
$("body").on("click", "#frSavePreferences", function() {
frSettings.vehicleTypes[lang] = mapVehicles($("#frSelectVehicles").val(), "type");
dispatchSetup.dispatchId = $("#frSelectDispatch").val() ? mapDispatchCenter($("#frSelectDispatch").val(), "id") : [];
dispatchSetup.useIt = $("#frCbxUseLst")[0].checked;
localStorage.fr_dispatchSetup = JSON.stringify(dispatchSetup);
localStorage.firstResponder = JSON.stringify(frSettings);
$("#frSavePreferences").addClass("hidden");
if (lang == "de_DE") {
$("#frModalBody").html("<h3><center>Die Einstellungen wurden gespeichert.</center></h5>");
} else {
$("#frModalBody").html("<h3><center>Settings successfully saved.</center></h5>");
}
});
$("#aao_" + frSettings.aaoId[lang]).click(function() {
$(".vehicle_checkbox").each(function() {
var vType = +$(this).attr("vehicle_type_id");
var vId = $(this).attr("value");
var lstId = +$(this).attr("building_id").split("_")[1];
var buId = +$(this).attr("building_id").split("_")[0];
if (frSettings.vehicleTypes[lang].includes(vType) && !$("#vehicle_checkbox_" + vId)[0].checked && !$("#vehicle_checkbox_" + vId)[0].disabled &&
(dispatchSetup.useIt === false || dispatchSetup.dispatchId.includes(lstId) || dispatchSetup.additionalBuildings.includes(buId))) {
$("#vehicle_checkbox_" + vId).click();
return false;
}
});
});
})();