forked from floflo3299/LSS-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvehicleChanges.js
324 lines (303 loc) · 17.2 KB
/
vehicleChanges.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// ==UserScript==
// @name vehicleChanges
// @version 2.0.3
// @description ändert die Einstellungen von AB, SEG ELW, GRTW, Außenlastbehälter und DGL
// @author original from DrTraxx (fixed from other)
// @include /^https?:\/\/(w{3}\.)?(polizei\.)?leitstellenspiel\.de\/$/
// @grant GM_addStyle
// ==/UserScript==
/* global $, user_id, I18n */
(async function () {
'use strict';
/**
* Vehicles-API nach Fahrzeugtypen durchsuchen
* @param {Array} ids Array mit den Fahrzeugtypen-IDs
* @returns {Array} Array mit den gefilterten Fahrzeugen
*/
const returnApiVehicles = async ids => {
return new Promise(async resolve => {
const aVehicles = await $.getJSON('/api/vehicles');
resolve(aVehicles.filter(v => ids.includes(v.vehicle_type)));
});
};
/**
* @param {String} target Art der Fahrzeuge und Einstellungen
* @param {boolean} bolHtml HTML gesucht?
* @returns {} Object mit den gesuchten Einstellungen oder HTML als String
*/
const switchTargetValue = (target, bolHtml) => {
switch (target) {
case "container":
if (bolHtml) {
return `<div class="form-check">
<input class="form-check-input" type="checkbox" id="ve_ch_cbx_1">
<label class="form-check-label" for="ve_ch_cbx_1">
Zufälliges WLF
</label>
</div>
<div class="form-check hidden" id="veChHiddenContent">
<input class="form-check-input" type="checkbox" id="ve_ch_cbx_2">
<label class="form-check-label" for="ve_ch_cbx_2">
WLF von fremden Wachen zulassen
</label>
</div>
<select class="custom-select" id="ve_ch_select_1">
<option selected value="2">WLF an Einsatzstelle behalten</option>
<option value="3">WLF zur Wache schicken</option>
</select>`;
} else {
return { "tractive_random": $("#ve_ch_cbx_1")[0].checked ? 1 : 0, "tractive_building_random": $("#ve_ch_cbx_2")[0].checked ? 1 : 0, "vehicle_mode": $("#ve_ch_select_1").val() };
}
case "seg_elw":
if (bolHtml) {
return `<div class="form-check">
<input class="form-check-input" type="checkbox" id="ve_ch_cbx_1">
<label class="form-check-label" for="ve_ch_cbx_1">
Rettungsdienst automatisch ein Krankenhaus zuweisen
</label>
</div>
<div class="hidden" id="veChHiddenContent">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="ve_ch_cbx_2">
<label class="form-check-label" for="ve_ch_cbx_2">
Nur eigene Krankenhäuser anfahren
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="ve_ch_cbx_3">
<label class="form-check-label" for="ve_ch_cbx_3">
Nur an Krankenhäuser mit passenden Ausbau einliefern
</label>
</div>
<label for="ve_ch_select_1">Maximale Abgabe vom Creditsverdienst</label>
<br>
<select class="custom-select" id="ve_ch_select_1" style="margin-left:2em;width:15em">
<option selected value="0">0 Prozent</option>
<option value="10">10 Prozent</option>
<option value="20">20 Prozent</option>
<option value="30">30 Prozent</option>
<option value="40">40 Prozent</option>
<option value="50">50 Prozent</option>
</select>
<br>
<label for="ve_ch_select_2">Maximale Entfernung zum Krankenhaus</label>
<br>
<select class="custom-select" id="ve_ch_select_2" style="margin-left:2em;width:15em">
<option selected value="1">1 Kilometer</option>
<option value="5">5 Kilometer</option>
<option value="20">20 Kilometer</option>
<option value="50">50 Kilometer</option>
<option value="100">100 Kilometer</option>
<option value="200">200 Kilometer</option>
</select>
<br>
<label for="ve_ch_select_3">Freizulassende Plätze im Krankenhaus</label>
<br>
<select class="custom-select" id="ve_ch_select_3" style="margin-left:2em;width:15em">
<option selected value="0">0 Plätze</option>
<option value="1">1 Plätze</option>
<option value="2">2 Plätze</option>
<option value="3">3 Plätze</option>
<option value="4">4 Plätze</option>
<option value="5">5 Plätze</option>
</select>
</div>`;
} else {
return { "hospital_automatic": $("#ve_ch_cbx_1")[0].checked ? 1 : 0, "hospital_own": $("#ve_ch_cbx_2")[0].checked ? 1 : 0, "hospital_right_building_extension": $("#ve_ch_cbx_3")[0].checked ? 1 : 0, "hospital_max_price": $("#ve_ch_select_1").val(), "hospital_max_distance": $("#ve_ch_select_2").val(), "hospital_free_space": $("#ve_ch_select_3").val() };
}
case "grtw":
if (bolHtml) {
return `<select class="custom-select" id="ve_ch_select_1">
<option selected value="0">Maximal 7 leichtverletzte Patienten</option>
<option value="1">Maximal 3 (auch schwerverletzte) Patienten, Notarzt als Besatzung nötig</option>
</select>`;
} else {
return { "vehicle_mode": $("#ve_ch_select_1").val() };
}
case "water_bin":
if (bolHtml) {
return `<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="ve_ch_cbx_1">
<label class="form-check-label" for="ve_ch_cbx_1">
Zufälliger Hubschrauber
</label>
</div>
<div class="form-check hidden" id="veChHiddenContent">
<input class="form-check-input" type="checkbox" value="" id="ve_ch_cbx_2">
<label class="form-check-label" for="ve_ch_cbx_2">
Hubschrauber von fremden Wachen zulassen
</label>
</div>`;
} else {
return { "tractive_random": $("#ve_ch_cbx_1")[0].checked ? 1 : 0, "tractive_building_random": $("#ve_ch_cbx_2")[0].checked ? 1 : 0 };
}
case "dgl":
if (bolHtml) {
return `<div class="form-check">
<input class="form-check-input" type="checkbox" id="ve_ch_cbx_1">
<label class="form-check-label" for="ve_ch_cbx_1">
Gefangenentransporten automatisch Zellen zuweisen
</label>
</div>
<div class="hidden" id="veChHiddenContent">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="ve_ch_cbx_2">
<label class="form-check-label" for="ve_ch_cbx_2">
Nur eigene Zellen anfahren
</label>
</div>
<br>
<label for="ve_ch_select_1">Maximale Entfernung zur Zelle</label>
<br>
<select class="custom-select" id="ve_ch_select_1" style="margin-left:2em;width:15em">
<option selected value="1">1 Kilometer</option>
<option value="5">5 Kilometer</option>
<option value="20">20 Kilometer</option>
<option value="50">50 Kilometer</option>
<option value="100">100 Kilometer</option>
<option value="200">200 Kilometer</option>
</select>
<br>
<label for="ve_ch_select_2">Freizulassende Zellen in den Wachen</label>
<br>
<select class="custom-select" id="ve_ch_select_2" style="margin-left:2em;width:15em">
<option selected value="0">0 Plätze</option>
<option value="1">1 Plätze</option>
<option value="2">2 Plätze</option>
<option value="3">3 Plätze</option>
<option value="4">4 Plätze</option>
<option value="5">5 Plätze</option>
</select>
<br>
<label for="ve_ch_select_3">Maximale Abgabe vom Creditsverdienst</label>
<br>
<select class="custom-select" id="ve_ch_select_3" style="margin-left:2em;width:15em">
<option selected value="0">0 Prozent</option>
<option value="10">10 Prozent</option>
<option value="20">20 Prozent</option>
<option value="30">30 Prozent</option>
<option value="40">40 Prozent</option>
<option value="50">50 Prozent</option>
</select>
<label for="ve_ch_ipt_num_1">Individuelle Verzögerung für den automatisierten Gefangenentransport (Minuten)</label>
<br>
<input class="numeric integer optional form-control" id="ve_ch_ipt_num_1" max="240" min="0" placeholder="Trage einen Wert ein, um den Standardwert zu überschreiben" type="number">
</div>`;
} else {
return {
"vehicle_extra_information_attributes": {
"police_cell_automatic": $("#ve_ch_cbx_1")[0].checked ? 1 : 0,
"police_cell_own": $("#ve_ch_cbx_2")[0].checked ? 1 : 0,
"police_cell_max_distance": $("#ve_ch_select_1").val(),
"police_cell_free_space": $("#ve_ch_select_2").val(),
"police_cell_max_price": $("#ve_ch_select_3").val()
},
"vehicle[prisoner_transportation_delay]": +$("#ve_ch_ipt_num_1").val()
};
}
}
};
/**
* HTML fuer die Fahrzeugeinstellungen
* @param {String} target Art der Fahrzeuge und Einstellungen
* @param {BigInteger} count Anzahl der zu aendernden Fahrzeuge
* @returns {String} HTML fuer die Einstellungen
*/
const getTargetHtml = (target, count) => {
let returnValue = `<h4>Einstellungen für ${ count.toLocaleString() } Fahrzeuge ändern!</h4>`;
returnValue += switchTargetValue(target, true);
returnValue += `<br><a class="btn btn-success" id="veChSaveAll" bullet_point="${ target }" style="margin-top:2em">Einstellungen übernehmen</a>`;
return returnValue;
}
/**
* HTML für das Modal zusammenbauen
* @param {String} target Art der Fahrzeuge und Einstellungen
* @param {Array} ids Array der Fahrzeugtypen-IDs
* @returns {String} HTML fuer das Modal
*/
const buildModalHtml = async (target, ids) => {
selectedVehicles = await returnApiVehicles(ids);
$("#veChModalBody").html(getTargetHtml(target, selectedVehicles.length));
};
const setVehiclePreferences = async (target) => {
let count = 0;
const preferences = switchTargetValue(target, false);
$("#veChModalBody")
.append(`<div class="progress" style="margin-top:2em">
<div class="progress-bar bg-success" role="progressbar" style="width: 0%;color: black" aria-valuenow="0" aria-valuemin="0" aria-valuemax="${ selectedVehicles.length }" id="veChPrgs">0 / ${ selectedVehicles.length.toLocaleString() }</div>
</div>`);
for (const vehicle of selectedVehicles) {
count++;
const percent = Math.round(count / selectedVehicles.length * 100);
$("#veChPrgs")
.attr("aria-valuenow", count)
.css({ "width": percent + "%" })
.text(count.toLocaleString() + " / " + selectedVehicles.length.toLocaleString());
await $.post("/vehicles/" + vehicle.id, { "vehicle": preferences, "authenticity_token": $("meta[name=csrf-token]").attr("content"), "_method": "put" });
}
}
let selectedVehicles;
GM_addStyle(`.modal {
display: none;
position: fixed; /* Stay in place front is invalid - may break your css so removed */
padding-top: 100px;
left: 0;
right:0;
top: 0;
bottom: 0;
overflow: auto;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.4);
z-index: 9999;
}
.modal-body{
height: 650px;
overflow-y: auto;
}`);
$("body")
.prepend(
`<div class="modal fade bd-example-modal-lg" id="veChModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">❌</span>
</button>
<h3 class="modal-title"><center>Fahrzeugeinstellungen</center></h3>
<div class="btn-group">
<a class="btn btn-primary btn-xs ve_ch_btn_trigger" types="[47,48,49,54,62,71,77,78]" target="container">Abrollbehälter</a>
<a class="btn btn-primary btn-xs ve_ch_btn_trigger" types="[59]" target="seg_elw">ELW (SEG)</a>
<a class="btn btn-primary btn-xs ve_ch_btn_trigger" types="[73]" target="grtw">GRTW</a>
<a class="btn btn-primary btn-xs ve_ch_btn_trigger" types="[93]" target="water_bin">Außenlastbehälter</a>
<a class="btn btn-primary btn-xs ve_ch_btn_trigger" types="[103]" target="dgl">FuStW (DGL)</a>
</div>
</div>
<div class="modal-body" id="veChModalBody">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Schließen</button>
<div class="pull-left">v ${ GM_info.script.version }</div>
</div>
</div>
</div>`);
$("ul .dropdown-menu[aria-labelledby='menu_profile'] >> a[href='/settings/index?mission_speed=true']")
.parent()
.after(`<li role="presentation"><a data-toggle="modal" data-target="#veChModal" style="cursor:pointer" id="veChOpenModal">
<span class="glyphicon glyphicon-cog"></span> Fahrzeugeinstellungen</a>
</li>`);
$("body").on("click", ".ve_ch_btn_trigger", async function () {
const target = $(this).attr("target"),
types = JSON.parse($(this).attr("types"));
buildModalHtml(target, types);
});
$("body").on("change", "#ve_ch_cbx_1", function () {
if (this.checked) {
$(`#veChHiddenContent`).removeClass("hidden");
} else {
$(`#veChHiddenContent`).addClass("hidden");
}
});
$("body").on("click", "#veChSaveAll", function () {
setVehiclePreferences($(this).attr("bullet_point"));
});
})();