Files
automatic-dispose/src/autoloaderV2.js
2018-11-28 20:33:16 +01:00

505 lines
18 KiB
JavaScript

//=require ../node_modules/dexie/dist/dexie.js
class AutomaticDispose {
// Variablen
get ADisSettings() {
return {
url: "https://jf.git.rest/Jomaar/automatic-dispose/raw/branch/",
branch: "master",
all_missions: /***ADis_AllMissions***/ 0,
name: this.name
}
}
constructor() {
this.ADis_MyHead = document.getElementsByTagName('head')[0];
this.ADis_MyBody = document.getElementsByTagName('body')[0];
// -
// - Sprechwunsch Variablen
// -
this.Jails = [];
this.JailRegEx = /\(Freie Zellen\:\s+(\d)\,\s+Entfernung\:\s+(\d+\,\d+)\s+km(?:\,\s+Abgabe an Besitzer\:\s+(\d+)%)?\)/g;
this.Hospitals = [];
this.RescueVehicleFrameWatchDog = null;
this.PoliceVehicleFrameWatchDog = null;
let that = this;
this.ADisDB = new Dexie('LSS_AutomaticDispo');
this.ADisDB.version(1).stores({
/**
* AvailabelMissions
* - missionID
* - type
* - name
* - jsondata
*/
ADisAvailabelMissions: "++missionID,type,name",
/**
* Sprechwunsch
* - vehicleID
* - done
*/
ADisSprechwunsch: "++vehicleID,done",
/**
* Sprechwunsch Police
* - missionID
* - done
*/
ADisSprechwunschPolice: "++missionID,last_check,next_check,done",
/**
* Krankentransport
* - missionID
* - done
*/
ADisKrankentransport: "++missionID,last_check,next_check,done",
/**
* Settings
* - id
* - name
* - done
*/
ADisSettings: "&name,value",
/**
* MissionsOwn
* MissionsVerband
* MissionsVerbandEvent
* - missionID
* - type
* - name
* - available
* - done
* - street
* - village
* - last_check
* - next_check
*/
ADisMissionsOwn: "++missionID,type,name,last_check,next_check,available,done",
ADisMissionsVerband: "++missionID,type,name,last_check,next_check,available,done",
ADisMissionsVerbandEvent: "++missionID,type,name,last_check,next_check,available,done",
});
this.ADisDB.on("populate", function () {
that.ADisDB.ADisSettings.add({
name: "Krankentransport-Automatic",
value: 'true'
});
that.ADisDB.ADisSettings.add({
name: "Sprechwunsch-Automatic",
value: 'true'
});
that.ADisDB.ADisSettings.add({
name: "MissionsOwn-Automatic",
value: 'true'
});
that.ADisDB.ADisSettings.add({
name: "Alliance-Automatic",
value: 'false'
});
that.ADisDB.ADisSettings.add({
name: "AllianceEvent-Automatic",
value: 'false'
});
});
this.ADisDB.open().catch(function (err) {
console.error(err.stack || err);
});
this.ADisDB.on("ready", () => {
if (window.location.pathname === "/" || window.location.pathname === "/#") {
that.IndexPage()
setInterval(that.CollectRescueSprechwuensche, 2500);
setInterval(that.OpenNextRescueSprechwunsch, 2500);
that.CollectRescueSprechwuensche();
setInterval(that.CollectPoliceSprechwuensche, 2500);
setInterval(that.OpenNextPoliceSprechwunsch, 2500);
that.CollectPoliceSprechwuensche();
}
/**
* / /=require ../script/mission.js
* / /=require ../script/sprechwunsch.js
*/
});
console.log('nonstatic', this.ADisSettings)
}
get Settings() {
console.log('static', this.ADisSettings);
console.log('static WindowName', this);
return this.ADisSettings;
}
includesString(string1, string2) {
return string1.toLowerCase().includes(string2.toLowerCase());
}
ADSettings() {
let that = this;
return new Promise((resolve, reject) =>{
res = {};
that.ADisDB.ADisSettings.each((setting) => {
res[setting.name] = setting.value;
});
resolve(res);
})
}
get WindowName() {
return window.name
}
// -
// - Sprechwunsch Queue
// -
RemovePoliceSprechwunschFromQueue(WunschID) {
return this.ADisDB.ADisSprechwunschPolice.where({
missionID: WunschID
}).delete();
}
RemoveRescueSprechwunschFromQueue(WunschID) {
return this.ADisDB.ADisSprechwunsch.where({
vehicleID: WunschID
}).delete();
}
// - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
// -
// - Assign & Unassign Missions to Dispatchers
// -
// - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
CollectRescueSprechwuensche() {
let that = this;
new Promise(async function (resolve, reject) {
await that.ADisDB.ADisSprechwunsch.where({
done: 'false'
}).each((myWunsch) => {
console.log('that.CollectRescueSprechwuensche: remove', myWunsch);
if ($("#vehicle_list_" + myWunsch.vehicleID).find(".building_list_fms").text() != "5") {
return that.RemoveRescueSprechwunschFromQueue(myWunsch.vehicleID);
}
})
return resolve();
}).then(() => {
$("#building_list").find(".building_list_vehicle_element").each(function () {
var VehicleStatus = $(this).find(".building_list_fms").text();
var VehicleType = Number(parseInt($(this).find("a").attr("vehicle_type_id")));
var VehicleID = $(this).attr("vehicle_id");
if (VehicleStatus == "5" && [32, 52].indexOf(VehicleType) === -1) {
that.AddRescueSprechwunschToQueue(VehicleID);
}
});
});
}
OpenNextRescueSprechwunsch() {
let that = this;
console.log(this.ADSettings);
console.log(this.ADSettings());
this.ADSettings().then((setting) => {
if (setting['Sprechwunsch-Automatic'] == 'true') {
return that.ADisDB.ADisSprechwunsch.where({
done: 'false'
}).limit(1)
}
}).then((Wunsch) => {
if (Wunsch !== undefined) {
Wunsch.each((myWunsch) => {
let VehicleID = myWunsch.vehicleID;
that.UpdateRescueSprechwunschToDone(VehicleID).then(() => {
if ($("#adis-sprechwunsch-rescue-frame").attr("vehicle_id") == "empty") {
$("#adis-sprechwunsch-rescue-frame").attr("src", "https://www.leitstellenspiel.de/vehicles/" + VehicleID);
$("#adis-sprechwunsch-rescue-frame").attr("vehicle_id", VehicleID);
that.RescueVehicleFrameWatchDog = setTimeout(async function () {
that.RemoveRescueSprechwunschFromQueue(VehicleID).then((deleteCount) => {
$("#adis-sprechwunsch-rescue-frame").attr("src", "");
$("#adis-sprechwunsch-rescue-frame").attr("vehicle_id", "empty");
});
}, 1500);
}
});
});
}
});
}
AddRescueSprechwunschToQueue(WunschID) {
return this.ADisDB.ADisSprechwunsch.put({
vehicleID: WunschID,
done: 'false'
}).catch((err) => {})
}
UpdateRescueSprechwunschToDone(WunschID) {
return this.ADisDB.ADisSprechwunsch.put({
vehicleID: WunschID,
done: 'true'
}).catch((err) => {})
}
// - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
// -
// - Assign & Unassign Missions to Dispatchers
// -
// - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
CollectPoliceSprechwuensche() {
let that = this;
new Promise(async function (resolve, reject) {
await that.ADisDB.ADisSprechwunschPolice.where({
done: 'false'
}).each((myWunsch) => {
if (!$("#mission_" + myWunsch.missionID).text().match(/Gefangene sollen abtransportiert werden/g)) {
$("#mission_" + myWunsch.missionID).remove();
return that.RemovePoliceSprechwunschFromQueue(myWunsch.missionID);
}
})
return resolve();
}).then(() => {
var MOIODOS = $('div#mission_list div.missionSideBarEntry:visible');
return MOIODOS.each(function (index) {
if ($(this).text().match(/Gefangene sollen abtransportiert werden/g)) {
var MissionID = $(this).attr("mission_id");
that.AddPoliceSprechwunschToQueue(MissionID);
}
});
}).then(() => {
$("#building_list").find(".building_list_vehicle_element").each(function () {
var VehicleStatus = $(this).find(".building_list_fms").text();
var VehicleType = Number(parseInt($(this).find("a").attr("vehicle_type_id")));
var VehicleID = $(this).attr("vehicle_id");
if (VehicleStatus == "5" && [32, 52].indexOf(VehicleType) !== -1) {
var map2 = $('#radio_messages_important > li.radio_message_vehicle_' + VehicleID + ' a:contains("Zum Einsatz")');
if (map2.length > 0) {
var _meap = String(map2.attr('href'));
var MissionID = _meap.substr(_meap.lastIndexOf("/") + 1);
if (MissionID) {
that.AddPoliceSprechwunschToQueue(MissionID);
}
}
}
});
});
}
OpenNextPoliceSprechwunsch() {
let that = this;
this.ADSettings().then((setting) => {
if (setting['Sprechwunsch-Automatic'] == 'true') {
return that.ADisDB.ADisSprechwunschPolice.where({
done: 'false'
}).limit(1)
}
}).then((Wunsch) => {
if (Wunsch !== undefined) {
Wunsch.each((myWunsch) => {
let MissionID = myWunsch.missionID;
that.UpdatePoliceSprechwunschToDone(MissionID).then(() => {
if ($("#adis-sprechwunsch-police-frame").attr("mission_id") == "empty") {
$("#adis-sprechwunsch-police-frame").attr("src", "https://www.leitstellenspiel.de/missions/" + MissionID);
$("#adis-sprechwunsch-police-frame").attr("mission_id", MissionID);
that.PoliceVehicleFrameWatchDog = setTimeout(async function (MissionID) {
that.RemovePoliceSprechwunschFromQueue(MissionID).then((deleteCount) => {
$("#adis-sprechwunsch-police-frame").attr("src", "");
$("#adis-sprechwunsch-police-frame").attr("mission_id", "empty");
});
}, 2500, MissionID);
}
});
});
}
});
}
AddPoliceSprechwunschToQueue(WunschID) {
return this.ADisDB.ADisSprechwunschPolice.put({
missionID: WunschID,
done: 'false'
}).catch((err) => {})
}
UpdatePoliceSprechwunschToDone(WunschID) {
console.log(WunschID, typeof WunschID);
return this.ADisDB.ADisSprechwunschPolice.add({
missionID: WunschID,
done: 'true'
}).catch((err) => {})
}
// -
// - Index Page Loader
// -
IndexPage() {
let that = this;
// -
// - Google Font
// -
var styleElement = document.createElement("link");
styleElement.rel = "stylesheet";
styleElement.href = "https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700";
this.ADis_MyHead.appendChild(styleElement);
// -
// - FontAwesome 5
// -
var scriptElement = document.createElement("script");
scriptElement.type = "text/javascript";
scriptElement.src = "https://use.fontawesome.com/releases/v5.5.0/js/all.js";
this.ADis_MyHead.appendChild(scriptElement);
// -
// - Navbar Dashboard
// -
var styleElement = document.createElement("link");
styleElement.rel = "stylesheet";
styleElement.type = "text/css";
styleElement.media = "screen";
styleElement.href = this.ADisSettings.url + this.ADisSettings.branch + "/style/interface.css";
this.ADis_MyHead.appendChild(styleElement);
// Create Dashboard
$('#news_li').before('<li id="adis-nav-item"></li>');
$('#adis-nav-item').html(`/***ADis_Navbar***/0`)
$("#adis-dashboard").css({
display: "none",
opacity: "0"
});
//
// Click Events
//
$("#adis-setting-krankentransport-automatic").click(function () {
var tf = $(this).hasClass("active");
console.log(tf);
that.ADisDB.ADisSettings.put({
name: "Krankentransport-Automatic",
value: !tf ? 'true' : 'false'
}).then((res) => {
console.log(res);
that.UIUpdateSettings();
});
});
$("#adis-setting-full-automatic").click(function () {
var tf = $(this).hasClass("active");
console.log(tf);
that.ADisDB.ADisSettings.put({
name: "MissionsOwn-Automatic",
value: !tf ? 'true' : 'false'
}).then(() => {
that.UIUpdateSettings();
});
});
$("#adis-setting-sprechwunsch-automatic").click(function () {
var tf = $(this).hasClass("active");
console.log(tf);
that.ADisDB.ADisSettings.put({
name: "Sprechwunsch-Automatic",
value: !tf ? 'true' : 'false'
}).then(() => {
that.UIUpdateSettings();
});
});
$("#adis-setting-alliance-automatic").click(function () {
var tf = $(this).hasClass("active");
that.ADisDB.ADisSettings.put({
name: "Alliance-Automatic",
value: !tf ? 'true' : 'false'
}).then(() => {
that.UIUpdateSettings();
});
});
$("#adis-setting-alliance-automatic").click(function () {
var tf = $(this).hasClass("active");
that.ADisDB.ADisSettings.put({
name: "AllianceEvent-Automatic",
value: !tf ? 'true' : 'false'
}).then(() => {
that.UIUpdateSettings();
});
});
//
// Dashboard Functions
//
$("#adis-open-button").click(function () {
$("#adis-dashboard").css("display", "flex");
$("#adis-dashboard").animate({
opacity: 1
}, 200);
});
$("#adis-dashboard-close-button").click(function () {
$("#adis-dashboard").animate({
opacity: 0
}, 200, function () {
$(this).css("display", "none");
});
});
// initial call to load the current setting state
that.UIUpdateSettings();
// Create Iframes
$("body").append('<div id="adis-frame-container"></div>');
$("#adis-frame-container").html(`/***ADis_Iframes***/0`);
$("#adis-frame-container").css({
display: "none",
opacity: "0"
});
}
//
// Display Settings
//
UIUpdateSettings() {
this.ADisDB.ADisSettings.each((setting) => {
if (setting.name == "Krankentransport-Automatic") {
if (setting.value == "true") {
$("#adis-setting-krankentransport-automatic").addClass("active");
} else {
$("#adis-setting-krankentransport-automatic").removeClass("active");
}
} else if (setting.name == "Sprechwunsch-Automatic") {
if (setting.value == "true") {
$("#adis-setting-sprechwunsch-automatic").addClass("active");
} else {
$("#adis-setting-sprechwunsch-automatic").removeClass("active");
}
} else if (setting.name == "MissionsOwn-Automatic") {
if (setting.value == "true") {
$("#adis-setting-full-automatic").addClass("active");
} else {
$("#adis-setting-full-automatic").removeClass("active");
}
} else if (setting.name == "Alliance-Automatic") {
if (setting.value == "true") {
$("#adis-setting-alliance-automatic").addClass("active");
} else {
$("#adis-setting-alliance-automatic").removeClass("active");
}
} else if (setting.name == "AllianceEvents-Automatic") {
if (setting.value == "true") {
$("#adis-setting-allianceevents-automatic").addClass("active");
} else {
$("#adis-setting-allianceevents-automatic").removeClass("active");
}
}
});
}
}
window.AutomaticDisposeCall = new AutomaticDispose();