SHARE
TWEET

Untitled

a guest May 20th, 2019 97 in 217 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. javascript:
  2. (function() {
  3.     /**
  4.      Author: Meiderac
  5.      Date: 20.05.2019
  6.      Version: 1.0.0
  7.     **/
  8.    
  9.     const types = [
  10.         { name: "Własne", id: 0 },
  11.         { name: "W wiosce", id: 1 },
  12.         { name: "Poza wioską", id: 2 },
  13.         { name: "W drodze", id: 3 },
  14.         { name: "Razem", id: 4 },
  15.     ]
  16.  
  17.     const find = name => el => Array.from($(el).find(name))
  18.     const sumArray = a1 => a2 => a1.map((n, i) => n + a2[i])
  19.     const accSumArray = (a1, a2) => sumArray(a1)(a2)
  20.     const initial = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  21.     const units = ['spear', 'sword', 'axe', 'archer', 'spy', 'light', 'marcher', 'heavy', 'ram', 'catapult', 'knight', 'snob']
  22.     const getGroupId = url => url ? +url.match(/group\=([\d]{1,})\&/)[1] : -1
  23.     const discardLast = ar => ar.slice(0, -1)
  24.  
  25.     const sum = perVillage => n =>
  26.         perVillage
  27.             .map(rows => rows[n])
  28.             .reduce(accSumArray, initial)
  29.  
  30.     const rowsPerVillage = villages =>
  31.         villages
  32.             .map(find("tr"))
  33.             .map(rows => rows.map(row =>
  34.                 find("td")(row)
  35.                     .map(td => +$(td).text())
  36.                     .filter(_ => !Number.isNaN(_))).map(discardLast))
  37.  
  38.     const getGroups = el =>
  39.         find("a, strong")(el)
  40.             .map(a => ({ name: $(a).text(), id: getGroupId($(a).attr("href")) }))
  41.  
  42.     const calculate = table => n => {
  43.         const villages = rowsPerVillage(find("tbody")(table))
  44.  
  45.         return { villages: villages.length, units: sum(villages)(n) }
  46.     }
  47.  
  48.     const fetch = groupId => type => {
  49.         const url = `/game.php?&village=${game_data.village.id}&type=complete&mode=units&group=0&page=-1&screen=overview_villages`
  50.         const urlWithGroup = groupId !== -1 ? `${url}&group=${groupId}` : url
  51.  
  52.         $.get(urlWithGroup, data => {
  53.             const table = $(data).find("#units_table")
  54.             const groups = $(data).find(".vis_item")
  55.  
  56.             display(+groupId, +type)(getGroups(groups))(calculate(table)(+type))
  57.         })
  58.     }
  59.  
  60.     const changeGroup = (group, type) => function (e) {
  61.         fetch(this.value)(type)
  62.     }
  63.     const changeType = (group, type) => function (e) {
  64.         fetch(group)(this.value)
  65.     }
  66.  
  67.     const display = (selectedGroup, selectedType) => groups => calculation => {
  68.         const groupOptions = groups.map(_ => `<option value='${_.id}'${_.id === -1 ? ' selected="selected"' : ''}'>${_.name}</option>`)
  69.         const groupsHtml = "<select id='sw_groups'>" + groupOptions + "</select>"
  70.  
  71.         const typeOptions = types.map(_ => `<option value='${_.id}'${_.id === selectedType ? ' selected="selected"' : ''}'>${_.name}</option>`)
  72.         const typesHtml = "<select id='sw_types'>" + typeOptions + "</select>"
  73.  
  74.         const unitsHtml = calculation
  75.             .units
  76.             .map((unit, i) => `<th width='20'><img src='${image_base}/unit/unit_${units[i]}.png' /></th><td bgcolor='#fff5da'>${unit}</td>`)
  77.             .map((h, i) => i % 2 === 0 ? `<tr>${h}` : `${h}</tr>`)
  78.             .reduce((acc, curr) => acc + curr, "")
  79.        
  80.         const unitsBB = calculation
  81.             .units
  82.             .map((unit, i) => `[unit]${units[i]}[/unit]  ${unit} \t`)
  83.             .map((h, i) => i % 2 === 0 ? `${h}` : `${h}\n\r`)
  84.             .reduce((acc, curr) => acc + curr, "")
  85.        
  86.         const html = "<h2 align='center'>Stan wojska</h2>" +
  87.         "<table width='100%'>" +
  88.         "<tr><th>Grupa: "+ groupsHtml +"</th></tr>" +
  89.         "<tr><td>" +
  90.         "<table width='100%'>" +
  91.         "<tbody>" +
  92.         "<tr><th colspan='4'>Rodzaj: " + typesHtml + "</th></tr>" +
  93.         "</tbody>" +
  94.         "<tbody>" +
  95.         "<tr></tr>" +
  96.         unitsHtml +
  97.         "</tbody>" +
  98.         "</table></td></tr>" +
  99.         "<tr><td>" +
  100.         "<b>Suma wiosek: " + calculation.villages + "</b>" +
  101.         "<a href='#' style='float: right;' onclick='eksportuj();'>Eksportuj</a>" +
  102.         "</td></tr>" +
  103.         "<tr id='eksportRow' style='display:none;'><td id='eksport'><textarea rows='7' style='width:95%;'>" + unitsBB + "</textarea>" +
  104.         "</td></tr>" +
  105.         "</table>"
  106.  
  107.         Dialog.show("sw_new", html)
  108.  
  109.         setTimeout(() => {
  110.             $("#sw_groups").on("change", changeGroup(selectedGroup, selectedType))
  111.             $("#sw_types").on("change", changeType(selectedGroup, selectedType))
  112.         }, 100)
  113.        
  114.     }
  115.  
  116.     fetch(0)(4)
  117.  
  118.  
  119. })();
  120.  
  121. function eksportuj() {
  122.     if ($("#eksportRow").css('display') == 'none') {
  123.        
  124.         $("#eksportRow").show();
  125.     }
  126.     else {
  127.         $("#eksportRow").hide();
  128.     }
  129. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top