-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShareAlliancePost.js
227 lines (181 loc) · 10 KB
/
ShareAlliancePost.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
// ==UserScript==
// @name ShareAlliancePost
// @namespace Leitstellenspiel
// @version 3.5.3
// @author jalibu, JuMaHo, BayHH, christianlouis
// @include https://www.leitstellenspiel.de/missions/*
// @updateURL https://raw.githubusercontent.com/christianlouis/LSS-misc/master/ShareAlliancePost.js
// @downloadURL https://raw.githubusercontent.com/christianlouis/LSS-misc/master/ShareAlliancePost.js
// ==/UserScript==
(() => {
'use strict';
const jumpNext = true; // Set to 'true', to jump to next mission after submitting an alert.
const enableKeyboard = true; // Set to 'false', to disable keyboard shortcuts.
const shortcutKeys = [17, 89]; // 17 = ctrl 89 = y
const defaultPostToChat = false ; // Set to 'false', to disable default post in alliance chat.
const messages = ['Schlussfahrzeug ab %MY_CUSTOM_TIME%. // RD frei.', // First entry is default
'Schlussfahrzeug ab %MY_CUSTOM_TIME_SHORT%. // RD frei.',
'GSL: Schlussfahrzeug ab %MY_CUSTOM_TIME_GSL%.',
'Schlussfahrzeug ab %MY_CUSTOM_TIME_VERY_SHORT%. Achtung: Eventeinsatz, bitte auf die Fahrzeuge achten.'];
// Create Button and add event listener
const initButtons = () => {
let btnMarkup = '<div class="btn-group" style="margin-left: 5px; margin-right: 5px;">';
btnMarkup += '<a href="#" class="btn btn-success btn-sm alert_notify_alliance" title="Alarmieren, im Verband freigeben und Nachricht in Verbands-Chat">';
btnMarkup += '<img class="icon icons8-Phone-Filled" src="/images/icons8-phone_filled.svg" width="18" height="18">';
btnMarkup += '<img class="icon icons8-Share" src="/images/icons8-share.svg" width="20" height="20">';
btnMarkup += '<span class="glyphicon glyphicon-bullhorn" style="font-size: 13px;"></span>';
btnMarkup += '</a></div>';
let optionsBtnMarkup = '<a href="#" id="openAllianceShareOptions" class="btn btn-sm btn-default" title="Einstellungen" style="margin: 0">';
optionsBtnMarkup += '<span class="glyphicon glyphicon-option-horizontal"></span></a>';
optionsBtnMarkup += '<div class="btn btn-sm btn-default" style="margin:0; padding: 1px; display: none;" id="allianceShareOptions"><input type="text" id="allianceShareText" value="' + messages[0] + '">';
optionsBtnMarkup += '<label style="margin-left: 2px; margin-right: 2px;"><input type="checkbox" ' + (defaultPostToChat ? 'checked' : '') + ' id="postToChat" name="postToChat" value="true">An VB Chat?</label>';
optionsBtnMarkup += '<div style="text-align: left;"><ul>';
$.each(messages, (index, msg) => {
optionsBtnMarkup += '<li class="customAllianceShareText">' + msg + '</li>';
});
optionsBtnMarkup += '</ul></div>';
optionsBtnMarkup += '</div>';
$('.alert_next_alliance').parent().append(btnMarkup);
$('.alert_notify_alliance').first().parent().prepend(optionsBtnMarkup);
$('#openAllianceShareOptions').click(() => {
$('#allianceShareOptions').show();
$('#openAllianceShareOptions').hide();
});
$('.customAllianceShareText').click(function() {
$('#allianceShareText').val($(this).text());
});
if(jumpNext){
$('.alert_notify_alliance').append('<span style="margin-left: 5px;" class="glyphicon glyphicon-arrow-right"></span>');
}
$('.alert_notify_alliance').click(processAllianceShare);
};
// Add Keylisteners
const initKeys = () => {
if(enableKeyboard){
let keys = [];
$(document).keydown((e) => {
keys.push(e.which);
if(keys.length >= shortcutKeys.length){
let pressedAll = true;
$.each(shortcutKeys, (index, value) =>{
if(keys.indexOf(value) < 0){
pressedAll = false;
return;
}
});
if(pressedAll){
// Is there an extra key pressed?
if(keys.length > shortcutKeys.length){
// Remove regular (expected pressed) keys from list
let extraKey = keys.filter( ( el ) => !shortcutKeys.includes( el ) );
// As number 9 key has value 48, substract that to get an expected key (value) range from 1-9
extraKey = extraKey[extraKey.length - 1] - 48;
// If the extra button has the (value) number 1-9,
// and the message array as a corresponding number of messages, select it
if(extraKey > 0 && extraKey <=10 && extraKey <= messages.length){
$('#allianceShareText').val(messages[extraKey - 1]);
}
}
processAllianceShare();
}
}
});
$(document).keyup((e) => {
keys.splice(keys.indexOf(e.which));
});
}
};
const processAllianceShare = () => {
$('#allianceShareOptions').hide();
$('#openAllianceShareOptions').show();
const sendToAlliance = $('#postToChat').is(':checked') ? 1 : 0;
const missionShareLink = $('#mission_alliance_share_btn').attr('href');
const missionId = missionShareLink.replace('/missions/','').replace('/alliance', '');
const csrfToken = $('meta[name="csrf-token"]').attr('content');
const message = $('#allianceShareText').val();
$('.alert_notify_alliance').html('Teilen..');
$.get('/missions/' + missionId + '/alliance' , () => {
$('.alert_notify_alliance').html('Chatten..');
$.post( "/mission_replies", {authenticity_token: csrfToken, mission_reply: {alliance_chat: sendToAlliance, content: message, mission_id: missionId}}, (data, status, xhr) => {
$('.alert_notify_alliance').html('Alarmieren..');
if(jumpNext){
$('.alert_next').first().click();
} else {
$('#mission_alarm_btn').click();
}
} );
});
};
const transformMessages = () => {
try {
// Prepare values for %ADDRESS% and %PATIENTS_LEFT%
// Possible inputs 'xy street, 1234 city', '1234 city', '123 city | 2' (where 2 is number of patients)
let addressAndPatrientRow = $('.mission_header_info >> small').first().text().trim().split(',');
addressAndPatrientRow = addressAndPatrientRow[addressAndPatrientRow.length-1].split('|');
const address = addressAndPatrientRow[0];
const patientsLeft = addressAndPatrientRow.length === 2 ? addressAndPatrientRow[1] : 0;
const offsetInHours = 3;
const offsetInHoursS = 1;
const offsetInMinutesEvent = 30;
const offsetInHoursGSL = 6;
var customTimeLong = new Date();
customTimeLong.addHours(offsetInHours);
customTimeLong.roundNext15Min();
var customTimeShort = new Date();
customTimeShort.addHours(offsetInHoursS);
customTimeShort.roundNext15Min();
var customTimeGSL = new Date();
customTimeGSL.addHours(offsetInHoursGSL);
customTimeGSL.roundNext15Min();
var customTimeVeryShort = new Date();
customTimeVeryShort.addMinutes(offsetInMinutesEvent);
customTimeVeryShort.roundNext15Min();
var options = { weekday: 'short', year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric', timeZoneName: 'short' };
var customTimeDateLong = customTimeLong.toLocaleDateString('de-DE', options);
var customTimeDateShort = customTimeShort.toLocaleDateString('de-DE', options);
var customTimeDateGSL = customTimeGSL.toLocaleDateString('de-DE', options);
var customTimeDateVeryShort = customTimeVeryShort.toLocaleDateString('de-DE', options);
// Prepare required Vehicles
const alertText = $('.alert-danger');
const requiredVehiclesIdentifier = 'Zusätzlich benötigte Fahrzeuge:';
let requiredVehicles = 'Keine weiteren Fahrzeuge benötigt.';
if(alertText && alertText.text().indexOf(requiredVehiclesIdentifier) >= 0) {
requiredVehicles = alertText.text().trim().substr(requiredVehiclesIdentifier.length, alertText.text().trim().length - 1);
}
for(let i = 0; i<messages.length; i++){
messages[i] = messages[i].replace('%ADDRESS%', address);
messages[i] = messages[i].replace('%MY_CUSTOM_TIME%', customTimeDateLong);
messages[i] = messages[i].replace('%MY_CUSTOM_TIME_SHORT%', customTimeDateShort);
messages[i] = messages[i].replace('%MY_CUSTOM_TIME_VERY_SHORT%', customTimeDateVeryShort);
messages[i] = messages[i].replace('%MY_CUSTOM_TIME_GSL%', customTimeDateGSL);
messages[i] = messages[i].replace('%PATIENTS_LEFT%', patientsLeft);
messages[i] = messages[i].replace('%REQUIRED_VEHICLES%', requiredVehicles);
}
} catch (e){
console.log('Error transforming messages: ' + e);
}
};
Date.prototype.roundNext15Min = function () {
var intervals = Math.floor(this.getMinutes() / 15);
if(this.getMinutes() % 15 !== 0)
intervals++;
if(intervals === 4) {
this.setHours(this.getHours()+1);
intervals = 0;
}
this.setMinutes(intervals * 15);
this.setSeconds(0);
return this;
}
Date.prototype.addHours = function(h) {
this.setTime(this.getTime() + (h*60*60*1000));
return this;
}
Date.prototype.addMinutes = function(m) {
this.setTime(this.getTime() + (m*60*1000));
return this;
}
transformMessages();
initButtons();
initKeys();
})();