Last active
May 4, 2017 13:01
-
-
Save Fuzzyma/1ab0c535111a6c1a6c04b51da23e74f7 to your computer and use it in GitHub Desktop.
Report inline formatter for tribalwars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Berichte | |
// @namespace tribalswars | |
// @description Fügt einen BB-Code Button hinzu, der es ermöglicht Berichte direkt formatiert in den Post einzufügen | |
// @include https://*.die-staemme.de/*screen=forum* | |
// @version 1.1.3 | |
// @grant none | |
// ==/UserScript== | |
var mouse = null | |
$('#bb_bar').parent().prev().append( | |
$('#bb_button_report_display') | |
.clone(true) | |
.attr('onclick', null) | |
.off('click') | |
.on('click', function(e) { | |
mouse = e | |
$.get('game.php?screen=report&group_id=0s', function(data) { | |
var locations = $(data).find('#report_list').parent() | |
locations.find('a').attr('onclick', null).on('click', function(e) { | |
e.preventDefault() | |
$.get(this.href, function(data) { | |
buildOutput(extract($(data))) | |
}) | |
inlinePopupClose() | |
}) | |
showPopup(locations) | |
}) | |
}) | |
) | |
function buildOutput(input) { | |
var str = bb('quote', [ | |
[ | |
ib('Betreff:'), | |
playerBB(input.sideA.player), | |
'greift', | |
villageBB(input.sideB.village), | |
'an' | |
].join(' '), | |
[ | |
ib('Kampfzeit:'), | |
input.date | |
].join(' '), | |
'', | |
[ | |
b(input.text) | |
].join(' '), | |
'', | |
[ | |
ib('Glück:'), | |
input.luck | |
].join(' '), | |
'', | |
[ | |
ib('Angreiger:'), | |
playerBB(input.sideA.player) | |
].join(' '), | |
[ | |
ib('Herkunft:'), | |
villageBB(input.sideA.village) | |
].join(' '), | |
tableBB([input.sideA.icons, input.sideA.units, input.sideA.lost]) + ( | |
input.sideA.effects ? '\n' + [ | |
ib('Effekte:'), | |
input.sideA.effects | |
].join(' ') : '' | |
), | |
[ | |
ib('Glauben:'), | |
input.sideA.believe | |
].join(' '), | |
'', | |
[ | |
ib('Verteidiger:'), | |
playerBB(input.sideB.player) | |
].join(' '), | |
[ | |
ib('Ziel:'), | |
villageBB(input.sideB.village) | |
].join(' ') | |
].concat(input.sideB.yes ? [ | |
tableBB([input.sideB.icons, input.sideB.units, input.sideB.lost]) + ( | |
input.sideB.effects ? '\n' + [ | |
ib('Effekte:'), | |
input.sideB.effects | |
].join(' ') : '' | |
), | |
[ | |
ib('Glauben:'), | |
input.sideB.believe | |
].join(' ') | |
] : [i('\nKeiner deiner Kämpfer ist lebend zurückgekehrt.\nEs konnten keine Informationen über die Truppenstärke des Gegners erlangt werden.')]).concat(input.spy.yes ? [ | |
'', | |
'', | |
ub('Spionage'), | |
'', | |
[ | |
ib('Erspähte Rohstoffe:'), | |
generateRessourceBB(input.spy.ressources) | |
].join(' '), | |
'', | |
ib('Gebäude:'), | |
'', | |
generateBuildingsBB(input.spy.buildings), | |
'', | |
ib('Einheiten außerhalb:'), | |
'', | |
tableBB([input.spy.icons, input.spy.units]) | |
] : []).concat(input.loot.yes ? [ | |
'', | |
[ | |
ib('Beute:'), | |
generateRessourceBB(input.loot), | |
'('+b(input.loot.overall)+')' | |
].join(' '), | |
] : []).concat([ | |
input.wall ? [ | |
ib('Rammen:'), | |
input.wall | |
].join(' ') : '', | |
input.damage ? [ | |
ib('Katapulte:'), | |
input.damage | |
].join(' ') : '', | |
input.approval ? [ | |
ib('Zustimmung:'), | |
input.approval | |
].join(' ') : '' | |
]).join('\n')) | |
insertAtCaret(str) | |
} | |
function generateRessourceBB(res) { | |
return [ | |
wood() + (res.wood||0), | |
stone() + (res.stone||0), | |
iron() + (res.iron||0) | |
].join(' ') | |
} | |
function generateBuildingsBB(buildings) { | |
var ret = ['[table]'] | |
var split = Math.ceil(buildings.icons.length / 2) | |
ret.push(bb('**', ['Gebäude', 'Stufe', 'Gebäude', 'Stufe'].join('[||]'))) | |
for(var i = 0; i < split; ++i) { | |
var temp = [bb('building', buildings.icons[i]) + ' ' + b(buildings.names[i].trim()), buildings.level[i].trim()] | |
if(buildings.icons[i+split]){ | |
temp = temp.concat([bb('building', buildings.icons[i+split]) + ' ' + b(buildings.names[i+split].trim()), buildings.level[i+split].trim()]) | |
} | |
ret.push(bb('*', temp.join('[|]'))) | |
} | |
ret.push('[/table]') | |
return ret.join('\n') | |
} | |
function tableBB(cols) { | |
var first = bb('**', [''].concat(cols[0].map(function(el){ return bb('unit', el) })).join('[||]')) | |
var second = bb('**', ['Anzahl'].concat(cols[1]).join('[|]')) | |
var third = '' | |
if(cols[2]) | |
third = bb('**', ['Verluste'].concat(cols[2]).join('[|]')) | |
return bb('table', [first, second, third].join('\n')) | |
} | |
var bb = function(wrapper, text) { | |
return '[' + wrapper + ']' + text + '[/' + wrapper + ']' | |
} | |
var playerBB = bb.bind(null, 'player') | |
var villageBB = bb.bind(null, 'coord') | |
var i = bb.bind(null,'i') | |
var b = bb.bind(null, 'b') | |
var u = bb.bind(null, 'u') | |
var img = bb.bind(null, 'img') | |
var wood = img.bind(null, 'http://cdn.tribalwars.net/graphic/holz.png') | |
var stone = img.bind(null, 'http://cdn.tribalwars.net/graphic/lehm.png') | |
var iron = img.bind(null, 'http://cdn.tribalwars.net/graphic/eisen.png') | |
var ib = function(txt) { return i(b(txt)) } | |
var ub = function(txt) { return u(b(txt)) } | |
function insertAtCaret(str) { | |
var $textarea = $("#message") | |
var msg = $textarea.val() | |
$textarea.val( | |
msg.substring(0, $textarea[0].selectionStart) + | |
str + | |
msg.substring($textarea[0].selectionEnd) | |
) | |
} | |
function showPopup(locations) { | |
inlinePopup( | |
mouse, // position automatically | |
false, // unused param | |
false, // url | |
{offset_x:0, offset_y:0}, | |
locations, // content of pupup (loaded dynamically) | |
'Berichte' // title | |
) | |
$('#inline_popup').width(700) | |
} | |
function key(path){ | |
return path.substring(path.lastIndexOf('/')+1, path.lastIndexOf('.')) | |
} | |
function extract($html) { | |
return { | |
date: $html.find('td.report_ReportAttack').parent().parent().find('tr:nth-child(2) td:nth-child(2)').eq(0).text().trim(), | |
text: $html.find('td.report_ReportAttack > h3').text(), | |
luck: $html.find('#attack_luck tr:first td:first, #attack_luck tr:first td:last').text().replace(/\s+/g).trim(), | |
sideA: { | |
player: $html.find('#attack_info_att tr:first th a').text(), | |
village: $html.find('#attack_info_att tr:nth-child(2) td span a:first').text().match(/\d\d\d\|\d\d\d/)[0], | |
icons: $html.find('#attack_info_att_units tr:nth-child(1) td:not(:first-child) a').map(function(){ return $(this).data('unit') }).get(), | |
units: $html.find('#attack_info_att_units tr:nth-child(2) td:not(:first-child)').map(function(){ return $(this).text() }).get(), | |
lost: $html.find('#attack_info_att_units tr:nth-child(3) td:not(:first-child)').map(function(){ return $(this).text() }).get(), | |
effects: $html.find('#attack_info_att tr td:contains("Effekte")').next().text().replace(/\s+/g, ' ').trim(), | |
believe: $html.find('#attack_info_att tr td:contains("Glauben")').text().replace('Glauben:', '').replace(/\s+/g, ' ').trim() | |
}, | |
sideB: { | |
yes: $html.find('#attack_info_def_units').length, | |
player: $html.find('#attack_info_def tr:first th a').text(), | |
village: $html.find('#attack_info_def tr:nth-child(2) td span a:first').text().match(/\d\d\d\|\d\d\d/)[0], | |
icons: $html.find('#attack_info_def_units tr:nth-child(1) td:not(:first-child) a').map(function(){ return $(this).data('unit') }).get(), | |
units: $html.find('#attack_info_def_units tr:nth-child(2) td:not(:first-child)').map(function(){ return $(this).text() }).get(), | |
lost: $html.find('#attack_info_def_units tr:nth-child(3) td:not(:first-child)').map(function(){ return $(this).text() }).get(), | |
effects: $html.find('#attack_info_def tr td:contains("Effekte")').next().text().replace(/\s+/g, ' ').trim(), | |
believe: $html.find('#attack_info_def tr td:contains("Glauben")').text().replace('Glauben:', '').replace(/\s+/g, ' ').trim() | |
}, | |
spy: { | |
yes: $html.find('#attack_spy_resources').length, | |
ressources: { | |
wood: $html.find('#attack_spy_resources tr:first td .wood').parent().text(), | |
stone: $html.find('#attack_spy_resources tr:first td .stone').parent().text(), | |
iron: $html.find('#attack_spy_resources tr:first td .iron').parent().text() | |
}, | |
buildings: { | |
icons: $html.find('#attack_spy_buildings_left tr td:nth-child(1) img, #attack_spy_buildings_right tr td:nth-child(1) img').map(function(){ return key($(this).attr('src')) }).get(), | |
names: $html.find('#attack_spy_buildings_left tr td:nth-child(1), #attack_spy_buildings_right tr td:nth-child(1)').map(function(){ return $(this).text() }).get(), | |
level: $html.find('#attack_spy_buildings_left tr td:nth-child(2), #attack_spy_buildings_right tr td:nth-child(2)').map(function(){ return $(this).text() }).get(), | |
}, | |
icons: $html.find('#attack_spy_away table tr:first th a').map(function(){ return $(this).data('unit') }).get(), | |
units: $html.find('#attack_spy_away table tr:last td').map(function(){ return $(this).text() }).get() | |
}, | |
loot: { | |
yes: $html.find('#attack_results tr:first td .wood').length, | |
wood: $html.find('#attack_results tr:first td .wood').parent().text(), | |
stone: $html.find('#attack_results tr:first td .stone').parent().text(), | |
iron: $html.find('#attack_results tr:first td .iron').parent().text(), | |
overall: $html.find('#attack_results tr:first td:last').text() | |
}, | |
wall: htmlToBB($html.find('#attack_results tr th:contains("Schaden durch Rammböcke:")').next()), | |
damage: htmlToBB($html.find('#attack_results tr th:contains("Katapult")').next()), | |
approval: htmlToBB($html.find('#attack_results tr th:contains("Zustimmung")').next()) | |
} | |
} | |
function htmlToBB($html) { | |
return $html.contents().map(function() { | |
if(this.nodeName == '#text') return this.data | |
return wrap(this.nodeName) + this.textContent + wrap(this.nodeName, true) | |
}).get().join('') | |
} | |
function wrap(str, slash) { | |
if(!str) return '' | |
return '[' + (slash ? '/' : '') + str + ']' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment