Advertisement
Henryjaw

Saliens_Script

Jun 24th, 2018
915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (async function ($, forSTCN) {
  2.     const gameUrlPrefix = 'https://community.steam-api.com/ITerritoryControlMinigameService'
  3.     const stcnId = 255962
  4.     let token
  5.     let gameTimer
  6.     let errorTime = 0
  7.     let score
  8.     let currentGame
  9.     let running = false
  10.     let $output = $('#dogeOutput')
  11.     let currentZone
  12.     let fireTarget
  13.  
  14.     if ($output.length === 0) {
  15.         let $dogeBody = $('<div>').css({
  16.             boxSizing: 'border-box', position: 'fixed', bottom: 0, left: '20px', right: '20px', zIndex: 999999,
  17.             padding: '10px', borderRadius: '5px 5px 0 0', background: '#171a21', color: '#b8b6b4',
  18.             boxShadow: '0 0 20px #000'
  19.         }).appendTo($('body'))
  20.         $output = $('<div id="dogeOutput">').css({ height: '200px', overflow: 'auto', margin: '0 0 10px' }).appendTo($dogeBody)
  21.         $(`<div class="global_header_toggle_button">`).text(' START ').click(() => { window.superDoge.start() }).appendTo($dogeBody)
  22.         $(`<div class="global_header_toggle_button">`).text(' STOP ').click(() => { window.superDoge.stop() }).appendTo($dogeBody)
  23.     }
  24.  
  25.     async function joinGame() {
  26.         clearTimeout(gameTimer)
  27.         log(`==============================================================`)
  28.         errorTime = 0
  29.         try {
  30.             log(`Fetch info...`)
  31.             var { response } = await $.post(`${gameUrlPrefix}/GetPlayerInfo/v0001/`, `access_token=${token}`)
  32.             const { active_planet, level, score: _score, next_level_score, active_zone_game, clan_info } = response
  33.             if (active_zone_game) {
  34.                 log('Alreay in a game, try to leave...')
  35.                 await $.post(`https://community.steam-api.com/IMiniGameService/LeaveGame/v0001/`, `access_token=${token}&gameid=${active_zone_game}`)
  36.             }
  37.             if (forSTCN && (!clan_info || clan_info.accountid !== stcnId)) {
  38.                 await $.post(`${gameUrlPrefix}/RepresentClan/v0001/`, `clanid=103582791429777370&access_token=${token}`)
  39.             }
  40.  
  41.             let planet
  42.             if (!active_planet) {
  43.                 log(`Joining planet...`)
  44.                 fireTarget = null
  45.                 var { response: { planets } } = await $.get(`${gameUrlPrefix}/GetPlanets/v0001/?active_only=1&language=schinese`)
  46.                 planet = planets.sort((a, b) => a.state.capture_progress - b.state.capture_progress)[0]
  47.                 await $.post(`${gameUrlPrefix}/JoinPlanet/v0001/`, `id=${planet.id}&access_token=${token}`)
  48.             }
  49.             var { response: { planets } } = await $.get(`${gameUrlPrefix}/GetPlanet/v0001/?id=${active_planet}&language=schinese`)
  50.             planet = planets[0]
  51.             log(`Planet: ${planet.state.name}  Level: ${level}  Exp: ${_score}/${next_level_score}  Team: ${clan_info ? clan_info.name : 'None'}`)
  52.  
  53.             let zones = planet.zones.filter(({ captured }) => !captured)
  54.             let targetZone = zones.find(({ zone_position }) => zone_position === fireTarget)
  55.             if (targetZone) {
  56.                 log(`>>> FIRE ZONE ${fireTarget} <<<`)
  57.             }
  58.             else if (forSTCN && clan_info && clan_info.accountid === stcnId) {
  59.                 targetZone = findTarget(zones, planet)
  60.             } else {
  61.                 zones = sortZones(zones, 2)
  62.                 targetZone = zones.find(({ difficulty }) => difficulty === 3) || zones.find(({ difficulty }) => difficulty === 2) || zones[0]
  63.             }
  64.             currentZone = targetZone
  65.             const { zone_position, difficulty, capture_progress: progress } = targetZone
  66.             score = difficulty === 1 ? 600 : difficulty === 2 ? 1200 : 2400
  67.             log(`Joining zone...`)
  68.             var { response: { zone_info } } = await $.post(`${gameUrlPrefix}/JoinZone/v0001/`, `zone_position=${zone_position}&access_token=${token}`)
  69.             if (zone_info) {
  70.                 log(`Join zone ${zone_position}(${zone_position % 12 + 1 | 0},${zone_position / 12 + 1 | 0}) success.`)
  71.                 log(`Progress: ${(progress * 100).toFixed(2)}%, wait 110s to send score ${score}...`)
  72.                 currentGame = zone_info.gameid
  73.                 gameTimer = setTimeout(sendScore, 110000)
  74.             } else {
  75.                 throw 'Service reject.'
  76.             }
  77.         } catch (e) {
  78.             console.error(e)
  79.             log(`Join zone fail, wait 2.5s...`)
  80.             gameTimer = setTimeout(joinGame, 2500)
  81.         }
  82.     }
  83.  
  84.     async function sendScore() {
  85.         clearTimeout(gameTimer)
  86.         log(`Sending score...`)
  87.         try {
  88.             var { response } = await $.post(`${gameUrlPrefix}/ReportScore/v0001/`, `access_token=${token}&score=${score}&language=schinese`)
  89.             if (response['new_score']) {
  90.                 log(`Send score success, new score: ${response['new_score']}.`)
  91.                 gameTimer = setTimeout(joinGame, 100)
  92.             } else {
  93.                 throw 'Service reject.'
  94.             }
  95.         } catch (e) {
  96.             if (errorTime++ < 5) {
  97.                 console.error(e)
  98.                 log(`Send score fail ${errorTime} times, wait 2s...`)
  99.                 gameTimer = setTimeout(sendScore, 2000)
  100.             } else {
  101.                 log(`Send score fail ${errorTime - 1} times, reset...`)
  102.                 gameTimer = setTimeout(joinGame, 100)
  103.                 errorTime = 0
  104.             }
  105.         }
  106.     }
  107.  
  108.     async function start() {
  109.         if (running) {
  110.             return
  111.         }
  112.         errorTime = 0
  113.         try {
  114.             token = (await $.get('https://steamcommunity.com/saliengame/gettoken')).token
  115.         } catch (e) {
  116.             console.error(e)
  117.             log('Get token failed, wait 2s...')
  118.             gameTimer = setTimeout(start, 2000)
  119.             return
  120.         }
  121.         running = true
  122.         log('Script is running.')
  123.         joinGame()
  124.         return
  125.     }
  126.  
  127.     async function stop() {
  128.         clearTimeout(gameTimer)
  129.         running = false
  130.         log('Script is ended.')
  131.         await $.post(`https://community.steam-api.com/IMiniGameService/LeaveGame/v0001/`, `access_token=${token}&gameid=${currentGame}`)
  132.     }
  133.  
  134.     function sortZones(zones, type = 0) {
  135.         switch (type) {
  136.             case 0: return zones.sort(({ capture_progress: a }, { capture_progress: b }) => b - a)
  137.             case 1: return zones.sort(({ capture_progress: a }, { capture_progress: b }) => a - b)
  138.             case 2: return zones.sort(({ zone_position: a }, { zone_position: b }) => Math.abs(48 - a) - Math.abs(48 - b))
  139.         }
  140.     }
  141.  
  142.     function getLeaderZone(zones, min = 1, k = 0) {
  143.         for (let i = 0; i < min; i++) {
  144.             let target = zones.find(({ top_clans, capture_progress }) => (capture_progress < 1 / (i * k + 1)) && top_clans[i] && top_clans[i].accountid === stcnId)
  145.             if (target) {
  146.                 return target
  147.             }
  148.         }
  149.     }
  150.  
  151.     function findTarget(_zones, planet) {
  152.         let target
  153.         let zonesD3 = _zones.filter(({ difficulty }) => difficulty === 3)
  154.         let zonesD2 = _zones.filter(({ difficulty }) => difficulty === 2)
  155.         let zonesD1 = _zones.filter(({ difficulty }) => difficulty === 1)
  156.         if (zonesD3.length > 0) {
  157.             target = getLeaderZone(zonesD3, 2, 2) || sortZones(zonesD3, 2)[0]
  158.             return target
  159.         } else if (zonesD2.length > 0) {
  160.             target = getLeaderZone(zonesD3, 2, 2) || sortZones(zonesD2, 2)[0]
  161.             return target
  162.         }
  163.         else {
  164.             target = getLeaderZone(zonesD1, 3, 1) || sortZones(zonesD1, 2)[0]
  165.             return target
  166.         }
  167.     }
  168.  
  169.     async function fire(x, y) {
  170.         fireTarget = 12 * (y - 1) + (x - 1)
  171.         log(`>>> SET TARGET: ZONE ${fireTarget} <<<`)
  172.         if (fire !== currentZone.zone_position) {
  173.             log(`Restart to change target...`)
  174.             await stop()
  175.             start()
  176.         }
  177.     }
  178.  
  179.     function endFire() {
  180.         log(`>>> CANCLE TARGET: ZONE ${fireTarget} <<<`)
  181.         fireTarget = null
  182.     }
  183.  
  184.     function log() {
  185.         const date = new Date()
  186.         const time = `[${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}]\t`
  187.         console.log(time, ...arguments)
  188.         $output.append($('<div>').text(`${time}\t ${arguments[0]}`))
  189.         requestAnimationFrame(() => { $output[0].scrollTop = 10e10 })
  190.     }
  191.  
  192.     window.superDoge && window.superDoge.stop()
  193.     window.superDoge = { start, stop, fire, endFire }
  194.     start()
  195. }
  196. )(jQuery, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement