Free javascript Hosting


datenbankinfo.js

Uploaded on May 01 2022 02:37 by info

//==UserScript==
// @name DB-Zugriff
// @namespace http://tampermonkey.net/
// @version 1.0
// @description DB-Zugriff
// @author Geheimratsecke
// @match https://*.die-staemme.de/*screen=info_village*
// @match https://*.die-staemme.de/*screen=overview
// @match https://*.die-staemme.de/*screen=overview_villages*mode=incomings*subtype=attacks*
// ==/UserScript==
let win = typeof unsafeWindow != 'undefined' ? unsafeWindow : window;
win.win = win;

//Konfigurationen
//-----------------------------------
//dbmode: SF oder USER (Für SF wird ein spezieller Key benötigt)
win.dbmode = "SF";
win.dbkey = "IaofF0A}0[:T2:WM3!MvDkTVkM5n";

//Wenn auf true gesetzt, dann wird beim öffenen einer Dorfübersicht automatisch die Datenbank abgefragt
win.showVillageDetails = true;
win.autoLoadVillageDetails = true;

win.showVillageOverviewWideget = true;
win.showButtonOnAttackScreen = true;



//-----------------------------------
// Code

win.timeConverter = function timeConverter(UNIX_timestamp) {
if (UNIX_timestamp == 0) {
return "Keine Daten";
}
var a = new Date(UNIX_timestamp * 1000);
var months = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
var year = a.getFullYear();
var month = months[a.getMonth()];
var date = a.getDate() < 10 ? '0' + a.getDate() : a.getDate();
var hour = a.getHours() < 10 ? '0' + a.getHours() : a.getHours();
var min = a.getMinutes() < 10 ? '0' + a.getMinutes() : a.getMinutes();
var sec = a.getSeconds() < 10 ? '0' + a.getSeconds() : a.getSeconds();
var time = date + '.' + month + '.' + year + ' ' + hour + ':' + min + ':' + sec;
return time;
}

win.getFormatedTimeLeftToUnixTimestamp = function getFormatedTimeLeftToUnixTimestamp(UNIX_timestamp) {
var date1 = new Date(UNIX_timestamp * 1000);
date1.setMilliseconds(date1.getMilliseconds() + 300);
var date2 = new Date(Timing.getCurrentServerTime());
var diff = date1 - date2;
var hours = Math.floor(diff / (1000 * 60 * 60));
diff -= hours * (1000 * 60 * 60);

var mins = Math.floor(diff / (1000 * 60));
diff -= mins * (1000 * 60);
if (mins < 10) {
mins = "0" + mins;
}

var seconds = Math.floor(diff / (1000));
if (seconds < 10) {
seconds = "0" + seconds;
}

return hours + ":" + mins + ":" + seconds;
}

win.unitTableBoilerPlate = "<div id='unit_infos' width='100%'> <table class='vis overview_table' width='100%' style='vertical-align:middle;'> <thead> <th>Typ</th> <th>Datum</th> <th><img src='https://dsde.innogamescdn.com/graphic/unit/unit_spear.png'></th> <th><img src='https://dsde.innogamescdn.com/graphic/unit/unit_sword.png'></th> <th><img src='https://dsde.innogamescdn.com/graphic/unit/unit_axe.png'></th> <th><img src='https://dsde.innogamescdn.com/graphic/unit/unit_archer.png'></th> <th><img src='https://dsde.innogamescdn.com/graphic/unit/unit_spy.png'></th> <th><img src='https://dsde.innogamescdn.com/graphic/unit/unit_light.png'></th> <th><img src='https://dsde.innogamescdn.com/graphic/unit/unit_marcher.png'></th> <th><img src='https://dsde.innogamescdn.com/graphic/unit/unit_heavy.png'></th> <th><img src='https://dsde.innogamescdn.com/graphic/unit/unit_ram.png'></th> <th><img src='https://dsde.innogamescdn.com/graphic/unit/unit_catapult.png'></th> <th><img src='https://dsde.innogamescdn.com/graphic/unit/unit_snob.png'></th> </thead> <tbody> $rows </tbody> </table> </div>";

win.unitTableRow = "<tr class='nowrap row_$rowType'> <th>$type</th> <td>$date</td> <td>$spear</td> <td>$sword</td> <td>$axe</td> <td>$archer</td> <td>$spy</td> <td>$light</td> <td>$marcher</td> <td>$heavy</td> <td>$ram</td> <td>$catapult</td> <td>$snob</td> </tr>";

win.incTableBoilerPlate = "<div id='inc_infos' width='100%'> <table class='vis overview_table' width='100%' style='vertical-align:middle;'> <thead> <th>Nr.</th> <th>Herkunft</th> <th>Ankunft</th> <th>Ankunft in</th> <th>Typ</th> <th>Grund</th> </thead> <tbody> $rows </tbody> </table> </div>";

win.incTableRow = "<tr class='nowrap row_$rowType'> <th>$id</th> <td>$from</td> <td>$arrival</td> <td><span class='timer'>$arrivalTimer</span></td> <td>$type</td> <td>$reason</td> </tr>";


win.showDatabaseDetails = function showDatabaseDetails(x, y, callback, additionals) {
var formData = new FormData();
formData.append("Key", win.dbkey);
formData.append("X", x);
formData.append("Y", y);
var request = new XMLHttpRequest();
var url = win.dbmode === "SF" ? "https://diestaemmedb.de/API/SF/VillageInfo.php" : "https://diestaemmedb.de/API/User/VillageInfo.php";
request.open("POST", url);
request.onreadystatechange = function() {
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
if (this.responseText) {
callback(JSON.parse(this.responseText), x, y, additionals);
// win.open(JSON.parse(this.responseText), x, y);
} else {
UI.ErrorMessage("Datenbankverbindung fehlgeschlagen. Bitte richtigen Key oder Modus einstellen.", 5000);
}
} else if (this.readyState === XMLHttpRequest.DONE && this.status !== 200) {
UI.ErrorMessage("Datenbankverbindung ist nicht verfügbar.", 5000);
}
}
request.send(formData);
}

win.compareSos = function compareSos(a, b) {
if (a.fighttime < b.fighttime) {
return -1;
}
if (a.fighttime > b.fighttime) {
return 1;
}
return 0;
}

win.getAttackDetailsDiv = function getAttackDetailsDiv(data) {
var attackDetailsDiv = "";
data.sos.sort(win.compareSos);

for (let index = 0; index < data.sos.length; index++) {
const element = data.sos[index];
var incType = element.type == 1 ? "Fake" : element.type == 2 ? "mögliche Off" : element.type == 3 ? "Off" : element.type == 4 ? "AG" : "Unbekannt";
let params = new URLSearchParams(document.location.search);
var actualLink = document.location.protocol + "//" + document.location.host + document.location.pathname + "?village=" + params.get("village") + "&screen=info_village&id=" + element.attacker_koords_id;
attackDetailsDiv += "<div><b>" + (index + 1) + ": </b>Von <b><a href='" + actualLink + "'>" + element.attacker_koords + "</a></b> um <b>" + win.timeConverter(element.fighttime) + "</b> Typ: <b>" + incType + "</b> Grund: <b>" + element.reason + "</b></div>";
}

return attackDetailsDiv;
}

win.generateIncTable = function generateIncTable(data) {
data.sos.sort(win.compareSos);
var rows = "";

for (let index = 0; index < data.sos.length; index++) {
const element = data.sos[index];
var incType = element.type == 1 ? "Fake" : element.type == 2 ? "mögliche Off" : element.type == 3 ? "Off" : element.type == 4 ? "AG" : "Unbekannt";
let params = new URLSearchParams(document.location.search);
var actualLink = document.location.protocol + "//" + document.location.host + document.location.pathname + "?village=" + params.get("village") + "&screen=info_village&id=" + element.attacker_koords_id;

rows += win.incTableRow
.replace('$rowType', (index % 2) == 0 ? "a" : "b")
.replace('$id', (index + 1))
.replace('$from', "<a href='" + actualLink + "'>" + element.attacker_koords + "</a>")
.replace('$arrival', win.timeConverter(element.fighttime))
.replace('$arrivalTimer', win.getFormatedTimeLeftToUnixTimestamp(element.fighttime))
.replace('$type', incType)
.replace('$reason', element.reason);
}

return win.incTableBoilerPlate.replace('$rows', rows);
}

win.open = function open(data, x, y) {
var villagetype = data.type == 0 ? "Deff" : data.type == 1 ? "Off" : "Unbekannt";
var linkToAllVillageDetails = "https://diestaemmedb.de/pages/search/villageinfo.php?dorfid=" + data.village_id;
var villageTypeDiv = "<div>Dorftyp: <b> " + villagetype + "</b></br><a href='" + linkToAllVillageDetails + "' target='_blank'>Datenbank Dorfakte</a></div></br>";
var incommingAttacks = "<div><p>Eingehende Angriffe: <b>" + data.attacks + "</b></p></div>";

// var attackDetailsDiv = win.getAttackDetailsDiv(data);

var attackDetailsDiv = win.generateIncTable(data);

var lastReport = "";
if (data.report_id) {
var linkToReport = "https://diestaemmedb.de/pages/report/report.php?id=" + data.report_id;
lastReport = "</br><div>Letzter Report: <a href='" + linkToReport + "' target='_blank'>" + win.timeConverter(data.report_fighttime) + "</a></div>"
}
var linkToAllReports = "https://diestaemmedb.de/pages/reports/showreports.php?dorfcoords=(" + x + "|" + y + ")";
var allReports = "<div><a href='" + linkToAllReports + "' target='_blank'>Alle Reports</a> </div>"

var output = "</br><div><h3>Datenbankinfo:</h3></div>";
var rowCount = 0;
var rows = "";
if (win.dbmode === "SF" && data.unit_time) {
rows += win.unitTableRow
.replace('$rowType', (rowCount % 2) == 0 ? "a" : "b")
.replace('$spear', data.spear)
.replace('$sword', data.sword)
.replace('$axe', data.axe)
.replace('$archer', data.archer)
.replace('$spy', data.spy)
.replace('$light', data.light)
.replace('$marcher', data.marcher)
.replace('$heavy', data.heavy)
.replace('$ram', data.ram)
.replace('$catapult', data.catapult)
.replace('$snob', data.snob)
.replace('$type', "Im Dorf")
.replace('$date', win.timeConverter(data.unit_time));
rowCount++;
}

if (data.attack_report.fighttime) {
var linkToAttackReport = "https://diestaemmedb.de/pages/report/report.php?id=" + data.attack_report.report_id;
rows += win.unitTableRow
.replace('$rowType', (rowCount % 2) == 0 ? "a" : "b")
.replace('$spear', data.attack_report.spear)
.replace('$sword', data.attack_report.sword)
.replace('$axe', data.attack_report.axe)
.replace('$archer', data.attack_report.archer)
.replace('$spy', data.attack_report.spy)
.replace('$light', data.attack_report.light)
.replace('$marcher', data.attack_report.marcher)
.replace('$heavy', data.attack_report.heavy)
.replace('$ram', data.attack_report.ram)
.replace('$catapult', data.attack_report.catapult)
.replace('$snob', data.attack_report.snob)
.replace('$type', "<a href='" + linkToAttackReport + "' target='_blank'>Letzter Angriff</a>")
.replace('$date', win.timeConverter(data.attack_report.fighttime));

rowCount++;

rows += win.unitTableRow
.replace('$rowType', (rowCount % 2) == 0 ? "a" : "b")
.replace('$spear', data.attack_report.loss_spear)
.replace('$sword', data.attack_report.loss_sword)
.replace('$axe', data.attack_report.loss_axe)
.replace('$archer', data.attack_report.loss_archer)
.replace('$spy', data.attack_report.loss_spy)
.replace('$light', data.attack_report.loss_light)
.replace('$marcher', data.attack_report.loss_marcher)
.replace('$heavy', data.attack_report.loss_heavy)
.replace('$ram', data.attack_report.loss_ram)
.replace('$catapult', data.attack_report.loss_catapult)
.replace('$snob', data.attack_report.loss_snob)
.replace('$type', "<a href='" + linkToAttackReport + "' target='_blank'>Verlust Angriff</a>")
.replace('$date', win.timeConverter(data.attack_report.fighttime));

rowCount++;
}

if (data.defend_report.fighttime) {
var linkToDefendReport = "https://diestaemmedb.de/pages/report/report.php?id=" + data.defend_report.report_id;
rows += win.unitTableRow
.replace('$rowType', (rowCount % 2) == 0 ? "a" : "b")
.replace('$spear', data.defend_report.spear)
.replace('$sword', data.defend_report.sword)
.replace('$axe', data.defend_report.axe)
.replace('$archer', data.defend_report.archer)
.replace('$spy', data.defend_report.spy)
.replace('$light', data.defend_report.light)
.replace('$marcher', data.defend_report.marcher)
.replace('$heavy', data.defend_report.heavy)
.replace('$ram', data.defend_report.ram)
.replace('$catapult', data.defend_report.catapult)
.replace('$snob', data.defend_report.snob)
.replace('$type', "<a href='" + linkToDefendReport + "' target='_blank'>Letzte Verteidigung</a>")
.replace('$date', win.timeConverter(data.defend_report.fighttime));

rowCount++;
}

output += villageTypeDiv;

if (rows) {
output += win.unitTableBoilerPlate.replace('$rows', rows) + "</br>";
}


output += incommingAttacks;
if (data.attacks > 0) {
output += attackDetailsDiv;
}
output += lastReport;
output += allReports;

var dbInfo = document.createElement('tr');
var td = document.createElement('td');
td.setAttribute("id", "DbInfoMain");
dbInfo.appendChild(td);
$('.vis')[2].children[0].appendChild(dbInfo);
$("#DbInfoMain").html(output);
Timing.resetTickHandlers();

}

win.openWidget = function openWidget(data, x, y) {

var output = "<div id='db_widget' class='vis widget'><h4 class='head'>Datenbank</h4>$content</div>";

var attackDetailsDiv = win.getAttackDetailsDiv(data);

var content = "<div class='widget_content' style='display: block;'>" + attackDetailsDiv + "</div>";

var div = document.createElement('div');
div.innerHTML = output.replace('$content', content);
console.log(div);
var left = $('#leftcolumn')[0];
left.insertBefore(div, left.children[1]);
}

win.openAttackDetail = function openAttackDetail() {
var incTable = $('#incomings_table')[0].children[1];
for (let index = 1; index < incTable.children.length - 1; index++) {
const element = incTable.children[index];

var regEx = new RegExp("[0-9]{3}[|][0-9]{3}");
regEx.global = true;
var raw = element.children[1].innerText;
var result = regEx.exec(raw);
var coords = result[0].split('|');

var arrivalRaw = element.children[5].innerText;
var arrivalRegEx = new RegExp("[0-9]{2}[:][0-9]{2}[:][0-9]{2}");
var regexArrivalResult = arrivalRegEx.exec(arrivalRaw);
var arrival = regexArrivalResult[0];
console.log(arrival);


var output = "<div><img src='https://dsde.innogamescdn.com/graphic/overview/note.png' onclick='win.showDatabaseDetails(" + coords[0] + ", " + coords[1] + ", " + win.showAttackDetail + ", \"" + arrival + "\");'></div>"
var td = document.createElement('td');
td.innerHTML = output;
element.appendChild(td);
}
}

win.showAttackDetail = function showAttackDetail(data, x, y, arrival) {
var possibleAttacks = [];
for (let index = 0; index < data.sos.length; index++) {
const element = data.sos[index];
if (win.timeConverter(element.fighttime).includes(arrival)) {
possibleAttacks.push(element);
}
}

var message = "";

for (let index = 0; index < possibleAttacks.length; index++) {
const element = possibleAttacks[index];
var incType = element.type == 1 ? "Fake" : element.type == 2 ? "mögliche Off" : element.type == 3 ? "Off" : element.type == 4 ? "AG" : "Unbekannt";
message = "Angriff von: " + element.attacker_koords + " Typ: " + incType + " Grund: " + element.reason + "\n";
}
if (message) {
UI.SuccessMessage(message, 4000);
} else {
UI.ErrorMessage("Bitte erst Angriffe in Datenbank einlesen.", 4000);
}
}

if (document.location.search.includes('screen=info_village')) {

var coords = $('.vis')[0].children[0].children[2].children[1].innerText.split('|');
if (!win.autoLoadVillageDetails) {
var table = $('.vis')[1].children[0];
var tr = document.createElement('tr');
tr.innerHTML = "<td colspan=\"2\"><a onClick='win.showDatabaseDetails(" + coords[0] + "," + coords[1] + "," + win.open + ",null);'>Datenbankdetails anzeigen</a></td>";
table.appendChild(tr);
} else {
win.showDatabaseDetails(coords[0], coords[1], win.open, null);
}
} else if (document.location.search.includes('screen=overview') && !document.location.search.includes('screen=overview_villages') && win.showVillageOverviewWideget) {
var coords = $('.box')[0].children[0].children[0].children[3].innerText.substring(1, 8).split('|');
if (coords.length != 2) {
//bonusdörfer
coords = $('.box')[0].children[0].children[0].children[4].innerText.substring(1, 8).split('|');
}
win.showDatabaseDetails(coords[0], coords[1], win.openWidget, null);
} else if (document.location.search.includes('screen=overview_villages&mode=incomings&subtype=attacks') && win.showButtonOnAttackScreen) {
win.openAttackDetail();
}

Back to list