Advertisement
Henryjaw

Saliens_Script_new

Jul 3rd, 2018
117
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 currentPlanetID
  13.     let fireTarget = null
  14.     let planetList
  15.     let planetTimer
  16.     let inBoss = false
  17.     let bossOveredList = []
  18.     let bossSound = localStorage.getItem('dogeBossSound') === '0' ? false : true
  19.  
  20.     if ($output.length === 0) {
  21.         let $dogeBody = $('<div>').css({
  22.             boxSizing: 'border-box', position: 'fixed', bottom: 0, left: '20px', right: '20px', zIndex: 999999,
  23.             padding: '10px', borderRadius: '5px 5px 0 0', background: '#171a21', color: '#b8b6b4',
  24.             boxShadow: '0 0 20px #000'
  25.         }).appendTo($('body'))
  26.         $output = $('<div id="dogeOutput">').css({ height: '200px', overflow: 'auto', margin: '0 0 10px' }).appendTo($dogeBody)
  27.         $(`<div class="global_header_toggle_button">`).text(' START ').click(() => { window.superDoge.start() }).appendTo($dogeBody)
  28.         $(`<div class="global_header_toggle_button">`).text(' STOP ').click(() => { window.superDoge.stop() }).appendTo($dogeBody)
  29.         $(`<div class="global_header_toggle_button">`).text(' SOUND ').click(() => { window.superDoge.sound() }).appendTo($dogeBody)
  30.     }
  31.  
  32.     async function joinGame() {
  33.         clearTimeout(gameTimer)
  34.         log(`==============================================================`)
  35.         errorTime = 0
  36.         try {
  37.             log(`Fetch info...`)
  38.             !planetList && await getPlanetList()
  39.             var { response } = await $.post(`${gameUrlPrefix}/GetPlayerInfo/v0001/`, `access_token=${token}`)
  40.             const { active_planet, level, score: _score, next_level_score, active_zone_game, clan_info } = response
  41.             if (active_zone_game) {
  42.                 if (score) {
  43.                     log('Alreay in a game? Try send score...')
  44.                     return sendScore()
  45.                 }
  46.                 log('Alreay in a game, try to leave...')
  47.                 await $.post(`https://community.steam-api.com/IMiniGameService/LeaveGame/v0001/`, `access_token=${token}&gameid=${active_zone_game}`)
  48.             }
  49.             score = 0
  50.             if (forSTCN && (!clan_info || clan_info.accountid !== stcnId)) {
  51.                 await $.post(`${gameUrlPrefix}/RepresentClan/v0001/`, `clanid=103582791429777370&access_token=${token}`)
  52.             }
  53.             let currentPlanet = planetList.find(({ id }) => id === active_planet)
  54.             currentPlanetID = active_planet
  55.             if (!active_planet) {
  56.                 log(`Joining planet...`)
  57.                 fireTarget = null
  58.                 currentPlanetID = planetList[0].id
  59.                 await $.post(`${gameUrlPrefix}/JoinPlanet/v0001/`, `id=${currentPlanetID}&access_token=${token}`)
  60.             } else if (fireTarget === null && ((planetList[0].zonesBoss.length > 0 || currentPlanet.zonesD3.length < 3) && active_planet !== planetList[0].id)) {
  61.                 log(`Change planet...`)
  62.                 await $.post(`https://community.steam-api.com/IMiniGameService/LeaveGame/v0001/`, `access_token=${token}&gameid=${active_planet}`)
  63.                 currentPlanetID = planetList[0].id
  64.                 await $.post(`${gameUrlPrefix}/JoinPlanet/v0001/`, `id=${currentPlanetID}&access_token=${token}`)
  65.             }
  66.             const planet = await getPlanetInfo(currentPlanetID)
  67.             log(`Planet: ${planet.state.name}  Level: ${level}  Exp: ${_score}/${next_level_score}  Team: ${clan_info ? clan_info.name : 'None'}`)
  68.             let zones = planet.zones.filter(({ captured }) => !captured)
  69.             let targetZone = zones.find(({ zone_position }) => zone_position === fireTarget)
  70.             if (targetZone) {
  71.                 log(`>>> FIRE ZONE ${fireTarget} <<<`)
  72.             } else if (planet.zonesBoss.length > 0) {
  73.                 log(`Joining boss zone...`)
  74.                 if (bossSound && $audio[0].paused) {
  75.                     $audio[0].loop = true
  76.                     $audio[0].play()
  77.                 }
  78.                 var { response } = await $.post(`${gameUrlPrefix}/JoinBossZone/v0001/`, `zone_position=${planet.zonesBoss[0].zone_position}&access_token=${token}`)
  79.                 console.log(response)
  80.                 currentZone = planet.zonesBoss[0]
  81.                 if (Object.keys(response).length === 0) { throw 'Join boss zone failed.' }
  82.                 log('Join boss zone success!')
  83.                 inBoss = true
  84.                 return bossFight()
  85.             } else {
  86.                 fireTarget = null
  87.                 if (forSTCN && clan_info && clan_info.accountid === stcnId) {
  88.                     targetZone = findTarget(planet)
  89.                 } else {
  90.                     zones = sortZones(zones, 2)
  91.                     targetZone = zones.find(({ difficulty }) => difficulty === 3) || zones.find(({ difficulty }) => difficulty === 2) || zones[0]
  92.                 }
  93.             }
  94.             currentZone = targetZone
  95.             const { zone_position, difficulty, capture_progress: progress } = targetZone
  96.             score = difficulty === 1 ? 600 : difficulty === 2 ? 1200 : 2400
  97.             log(`Joining zone...`)
  98.             var { response: { zone_info } } = await $.post(`${gameUrlPrefix}/JoinZone/v0001/`, `zone_position=${zone_position}&access_token=${token}`)
  99.             if (zone_info) {
  100.                 log(`Join zone ${zone_position}(${zone_position % 12 + 1 | 0},${zone_position / 12 + 1 | 0}) success.`)
  101.                 log(`Progress: ${(progress * 100).toFixed(2)}%, wait 110s to send score ${score}...`)
  102.                 currentGame = zone_info.gameid
  103.                 gameTimer = setTimeout(sendScore, 110000)
  104.             } else {
  105.                 throw 'Service reject.'
  106.             }
  107.         } catch (e) {
  108.             console.error(e)
  109.             log(`Join zone fail, wait 2.5s...`)
  110.             gameTimer = setTimeout(joinGame, 2500)
  111.         }
  112.     }
  113.  
  114.     async function sendScore() {
  115.         clearTimeout(gameTimer)
  116.         log(`Sending score...`)
  117.         try {
  118.             var { response } = await $.post(`${gameUrlPrefix}/ReportScore/v0001/`, `access_token=${token}&score=${score}&language=schinese`)
  119.             if (response['new_score']) {
  120.                 log(`Send score success, new score: ${response['new_score']}.`)
  121.                 gameTimer = setTimeout(joinGame, 100)
  122.             } else {
  123.                 throw 'Service reject.'
  124.             }
  125.         } catch (e) {
  126.             if (errorTime++ < 5) {
  127.                 console.error(e)
  128.                 log(`Send score fail ${errorTime} times, wait 2s...`)
  129.                 gameTimer = setTimeout(sendScore, 2000)
  130.             } else {
  131.                 log(`Send score fail ${errorTime - 1} times, reset...`)
  132.                 score = 0
  133.                 gameTimer = setTimeout(joinGame, 100)
  134.                 errorTime = 0
  135.             }
  136.         }
  137.     }
  138.  
  139.  
  140.     async function getPlanetInfo(id) {
  141.         let planet
  142.         try {
  143.             const { response: { planets } } = await $.get(`${gameUrlPrefix}/GetPlanet/v0001/?id=${id}&language=schinese`);
  144.             planet = planets[0]
  145.         } catch (e) {
  146.             console.error(e)
  147.             return getPlanetInfo(id)
  148.         }
  149.         planet.zones = planet.zones.filter(({ captured }) => !captured)
  150.         planet.zonesD3 = planet.zones.filter(({ difficulty, boss_active }) => difficulty === 3 && !boss_active)
  151.         planet.zonesD2 = planet.zones.filter(({ difficulty, boss_active }) => difficulty === 2 && !boss_active)
  152.         planet.zonesD1 = planet.zones.filter(({ difficulty, boss_active }) => difficulty === 1 && !boss_active)
  153.         planet.zonesBoss = planet.zones.filter(({ type, boss_active, zone_position }) => type === 4 && boss_active && !bossOveredList.some(({ pid, zp }) => pid === currentPlanetID && zp === zone_position))
  154.         planet.important = planet.zonesBoss.length * 1000000 + planet.zonesD3.length * 10000 + planet.zonesD2.length * 100 + planet.zonesD1.length
  155.         return planet
  156.     }
  157.  
  158.     async function getPlanetList() {
  159.         clearTimeout(planetTimer)
  160.         try {
  161.             let { response: { planets } } = await $.get(`${gameUrlPrefix}/GetPlanets/v0001/?active_only=1&language=schinese`)
  162.             const _planetList = []
  163.             await Promise.all(planets.map(async ({ id }) => _planetList.push(await getPlanetInfo(id))))
  164.             planetList = _planetList.sort((a, b) => b.important - a.important)
  165.             planetTimer = setTimeout(getPlanetList, 180000)
  166.             log(`******************************`)
  167.             log(`*  Plant info`)
  168.             log(`******************************`)
  169.             planetList.forEach(({ state: { name, capture_progress }, zonesD3, zonesD2, zonesD1 }) => log(`*  ${(capture_progress * 100).toFixed(2)}%\tH:${zonesD3.length} M:${zonesD2.length} E:${zonesD1.length}\t${name}`))
  170.             log(`******************************`)
  171.             if (!inBoss && planetList[0].zonesBoss.length > 0) {
  172.                 stop(true)
  173.             }
  174.             return planetList
  175.         } catch (e) {
  176.             console.log(e)
  177.             planetTimer = setTimeout(getPlanetList, 2000)
  178.         }
  179.     }
  180.  
  181.     function sortZones(zones, type = 0) {
  182.         switch (type) {
  183.             case 0: return zones.sort(({ capture_progress: a }, { capture_progress: b }) => b - a)
  184.             case 1: return zones.sort(({ capture_progress: a }, { capture_progress: b }) => a - b)
  185.             case 2: return zones.sort(({ zone_position: a }, { zone_position: b }) => Math.abs(48 - a) - Math.abs(48 - b))
  186.         }
  187.     }
  188.  
  189.     function getLeaderZone(zones, min = 1, k = 0) {
  190.         for (let i = 0; i < min; i++) {
  191.             let target = zones.find(({ top_clans, capture_progress }) => top_clans && (capture_progress < 1 / (i * k + 1)) && top_clans[i] && top_clans[i].accountid === stcnId)
  192.             if (target) {
  193.                 return target
  194.             }
  195.         }
  196.     }
  197.  
  198.     function findTarget({ zonesD3, zonesD2, zonesD1 }) {
  199.         let target
  200.         if (zonesD3.length > 0) {
  201.             target = getLeaderZone(zonesD3, 2, 2) || sortZones(zonesD3, 2)[0]
  202.             return target
  203.         } else if (zonesD2.length > 0) {
  204.             target = getLeaderZone(zonesD3, 2, 2) || sortZones(zonesD2, 2)[0]
  205.             return target
  206.         }
  207.         else {
  208.             target = getLeaderZone(zonesD1, 3, 1) || sortZones(zonesD1, 2)[0]
  209.             return target
  210.         }
  211.     }
  212.  
  213.     async function fire(x, y) {
  214.         fireTarget = 12 * (y - 1) + (x - 1)
  215.         log(`>>> SET TARGET: ZONE ${fireTarget} <<<`)
  216.         if (fire !== currentZone.zone_position) {
  217.             log(`Restart to change target...`)
  218.             await stop()
  219.             start()
  220.         }
  221.     }
  222.  
  223.     function endFire() {
  224.         log(`>>> CANCLE TARGET: ZONE ${fireTarget} <<<`)
  225.         fireTarget = null
  226.     }
  227.  
  228.     let $audio = $('#dogeAudio')
  229.     if ($audio.length === 0) {
  230.         $audio = $('<audio src="https://steamcdn-a.akamaihd.net/steamcommunity/public/assets/saliengame/sfx/SS2018_Saliens_Intro2.ogg?v=1550bld">').appendTo($('body'))
  231.         $audio[0].volume = .5
  232.     }
  233.  
  234.     let healTime = 0
  235.     async function bossFight() {
  236.         clearTimeout(gameTimer)
  237.         await new Promise((resolve) => { gameTimer = setTimeout(resolve, 4000) })
  238.         let use_heal_ability = 0, damage_to_boss = 0, damage_taken = 0
  239.         if (healTime) {
  240.             if (Date.now() > healTime) {
  241.                 use_heal_ability = 1
  242.                 log('Using heal ability, but why?')
  243.                 healTime = Date.now() + 120000
  244.             }
  245.             damage_to_boss = 20 + Math.random() * 100 | 0
  246.         } else {
  247.             healTime = Date.now() + 120000 + Math.random(60) | 0
  248.         }
  249.         try {
  250.             var { response } = await $.post(`${gameUrlPrefix}/ReportBossDamage/v0001/`, `access_token=${token}&use_heal_ability=${use_heal_ability}&damage_to_boss=${damage_to_boss}&damage_taken=${damage_taken}`)
  251.             if (!response.boss_status) {
  252.                 log('Report Boss Damage failed, retry...')
  253.                 healTime = 0
  254.                 inBoss = false
  255.                 return joinGame()
  256.             }
  257.             $audio[0].loop = false
  258.             $audio[0].pause()
  259.             $audio[0].currentTime = 0
  260.             log('Report Boss Damage success.')
  261.             let playerInfo
  262.             if (typeof gAccountID !== 'undefined') {
  263.                 playerInfo = response.boss_status.boss_players.find(({ accountid }) => accountid === gAccountID)
  264.             }
  265.             const { boss_status: { boss_hp, boss_max_hp } } = response
  266.             playerInfo && log(`Boss HP: ${boss_hp}/${boss_max_hp} ${(boss_hp / boss_max_hp * 100).toFixed(2)}%`)
  267.             if (playerInfo) {
  268.                 log(`EXP:${parseInt(playerInfo.score_on_join) + playerInfo.xp_earned}`)
  269.             }
  270.             if (response.game_over) {
  271.                 log('Boss is over~')
  272.                 healTime = 0
  273.                 inBoss = false
  274.                 planetList = null
  275.                 bossOveredList.push({ pid: currentPlanetID, zp: currentZone.zone_position })
  276.                 return joinGame()
  277.             } else if (response.waiting_for_players) {
  278.                 log('Waiting for players...')
  279.                 return bossFight()
  280.             }
  281.             return bossFight()
  282.         } catch (e) {
  283.             log('Report Boss Damage failed, retry...')
  284.             healTime = 0
  285.             inBoss = false
  286.             return joinGame()
  287.         }
  288.     }
  289.  
  290.     function log() {
  291.         const date = new Date()
  292.         const time = `[${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}]\t`
  293.         console.log(time, ...arguments)
  294.         $output.append($('<div>').text(`${time}\t ${arguments[0]}`))
  295.         requestAnimationFrame(() => { $output[0].scrollTop = 10e10 })
  296.     }
  297.     async function start() {
  298.         if (running) {
  299.             return
  300.         }
  301.         errorTime = 0
  302.         try {
  303.             token = (await $.get('https://steamcommunity.com/saliengame/gettoken')).token
  304.         } catch (e) {
  305.             console.error(e)
  306.             log('Get token failed, wait 2s...')
  307.             gameTimer = setTimeout(start, 2000)
  308.             return
  309.         }
  310.         running = true
  311.         log('Script is running.')
  312.         joinGame()
  313.         return
  314.     }
  315.  
  316.     async function stop(goBoss) {
  317.         clearTimeout(gameTimer)
  318.         if (!goBoss) {
  319.             clearTimeout(planetTimer)
  320.             planetList = null
  321.             log('Script is ended.')
  322.         } else {
  323.             log('Stop and go to boss...')
  324.         }
  325.         running = false
  326.         await $.post(`https://community.steam-api.com/IMiniGameService/LeaveGame/v0001/`, `access_token=${token}&gameid=${currentGame}`)
  327.         goBoss && start()
  328.     }
  329.  
  330.     log(`Boss alert sound is ${bossSound ? 'on' : 'off'}.`)
  331.     function sound() {
  332.         bossSound = !bossSound;
  333.         localStorage.setItem('dogeBossSound', bossSound ? '1' : '0')
  334.         log(`Boss alert sound is ${bossSound ? 'on, playing preview' : 'off'}.`)
  335.         if (bossSound) {
  336.             $audio[0].play()
  337.         } else {
  338.             $audio[0].loop = false
  339.             $audio[0].pause()
  340.             $audio[0].currentTime = 0
  341.         }
  342.  
  343.     }
  344.     window.superDoge && window.superDoge.stop()
  345.     window.superDoge = { start, stop, fire, endFire, sound }
  346.     start()
  347. }
  348. )(jQuery, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement