Town Star 自动售卖挂机脚本--没有编程基础也能轻松上手

发布于 2021-09-21 11:21:50

Town Star 这款游戏伤肝,所以很多人玩到一般就放弃了。其实国外供应商是有相关NFT售卖,有一个NFT可以自动卖货,该NFT价值0.11eth。如下图:

Town Star交流群200号人,我不想让200号人为了周赛的奖励,天天一起“肝”他。

所以狗哥将自己珍藏许久的脚本拿出来,祝大家在新的赛季中获得更高的奖励,同时也保护好大家的身体。脚本搭建如下:

安装暴力猴子

在浏览器中搜索tampermonkey(自行在后面加“.net”),设置语言为“zh”,点击“下载”

编辑脚本

在谷歌插件中找到“tampermonkey”,点开,点击“添加新脚本”。

在编辑器中输入以下代码保存一定要一字不漏):

// ==UserScript==

// @name         Town Star Auto-Sell

// @namespace    http://tampermonkey.net/

// @version      2.1

// @description  Automatically sell crafted items.

// @author       Groove

// @match        https://townstar.sandbox-games.com/launch/

// @grant        none

// @run-at       document-start

// ==/UserScript==

(function() {

'use strict';

// keepAmt is the amount that you do not want to sell

// sellMin is the minimum amount needed before attempting to sell

//    setting a sellMin of 100 will ensure that the item is only sold in batches of 100 (e.g. via Freight Ship)

const craftedItems = [

{item: 'Silica', keepAmt: 10, sellMin: 100},

{item: 'Pinot_Noir_Grapes', keepAmt: 10, sellMin: 10},

{item: 'Feed', keepAmt: 15, sellMin: 10},

{item: 'Wheat', keepAmt: 30, sellMin: 10},

{item: 'Flour', keepAmt: 5, sellMin: 10},

{item: 'Salt', keepAmt: 10, sellMin: 10},

{item: 'Sugar', keepAmt: 30, sellMin: 10},

{item: 'Energy', keepAmt: 40, sellMin: 10},

{item: 'Petroleum', keepAmt: 60, sellMin: 10},

{item: 'Gasoline', keepAmt: 80, sellMin: 10},

{item: 'Crude_Oil', keepAmt: 80, sellMin: 10},

{item: 'Lumber', keepAmt: 15, sellMin: 10},

{item: 'Wood', keepAmt: 70, sellMin: 10},

{item: 'Brine', keepAmt: 20, sellMin: 10},

{item: 'Sugarcane', keepAmt: 20, sellMin: 10},

{item: 'Eggs', keepAmt: 5, sellMin: 10},

{item: 'Butter', keepAmt: 9, sellMin: 10},

{item: 'Water_Drum', keepAmt: 30, sellMin: 10},

{item: 'Milk', keepAmt: 8, sellMin: 10},

{item: 'Wool', keepAmt: 5, sellMin: 10},

{item: 'Batter', keepAmt: 0, sellMin: 10},

{item: 'Silica', keepAmt: 0, sellMin: 10},

{item: 'Iron', keepAmt: 5, sellMin: 10},

{item: 'Steel', keepAmt: 5, sellMin: 10},

{item: 'Cotton_Yarn', keepAmt: 5, sellMin: 10},

{item: 'Wool_Yarn', keepAmt: 5, sellMin: 10},

{item: 'Cotton', keepAmt: 5, sellMin: 10},

]

new MutationObserver(function(mutations) {

let airdropcollected = 0;

if(document.getElementsByClassName('hud-jimmy-button')[0] && document.getElementsByClassName('hud-jimmy-button')[0].style.display != 'none'){

document.getElementsByClassName('hud-jimmy-button')[0].click();

document.getElementById('Deliver-Request').getElementsByClassName('yes')[0].click();

document.getElementById('Deliver-Request').getElementsByClassName('close-button')[0].click();

}

if(document.getElementsByClassName('hud-airdrop-button')[0] && document.getElementsByClassName('hud-airdrop-button')[0].style.display != 'none'){

if(airdropcollected == 0){

airdropcollected = 1;

document.getElementsByClassName('hud-airdrop-button')[0].click();

document.getElementsByClassName('air-drop')[0].getElementsByClassName('yes')[0].click();

}

}

if (document.getElementById("playnow-container") && document.getElementById("playnow-container").style.visibility !== "hidden") {

if(typeof Game == 'undefined' || (Game && Game.gameData == null)) {

window.location.reload();

} else {

document.getElementById("playButton").click();

console.log(Date.now() + ' ---===ACTIVATING AUTO SELL===---');

ActivateAutoSell();

}

}

}).observe(document, {childList: true, subtree: true});

function GetAvailableTradeObject(capacity) {

return Object.values(Game.town.objectDict).filter(tradeObj => tradeObj.logicType === 'Trade')

.find(tradeObj =>

Game.unitsData[tradeObj.objData.UnitType].Capacity == capacity

&& !Game.town.tradesList.find(activeTrade => activeTrade.source.x == tradeObj.townX && activeTrade.source.z == tradeObj.townZ)

)

}

function CloseWindows(elements, checkParent) {

for (let i=0, n=elements.length; i < n; i++) {

let el = checkParent ? elements[i].closest('.container') : elements[i];

let elVis = el.currentStyle ? el.currentStyle.visibility : getComputedStyle(el, null).visibility;

let elDis = el.currentStyle ? el.currentStyle.display : getComputedStyle(el, null).display;

if (!(elVis === 'hidden' || elDis === 'none')) {

el.querySelector('.close-button') && el.querySelector('.close-button').click();

}

}

}

async function WaitForCompletion(selector) {

while (document.querySelector(selector) !== null) {

await new Promise( resolve => requestAnimationFrame(resolve) )

}

return document.querySelector(selector);

}

async function WaitForTradeLoad(targetTradeObj) {

return await new Promise(resolve => {

const waitForUpdate = setInterval(() => {

let tradeUiObj = Game.app.root.findByName('TradeUi').script.trade.townObject;

if (tradeUiObj && tradeUiObj.townX == targetTradeObj.townX && tradeUiObj.townZ == targetTradeObj.townZ && Game.app.root.findByName('TradeUi').script.trade.cityPaths[0].gasCost) {

resolve('Loaded');

clearInterval(waitForUpdate);

};

}, 500);

});

}

async function WaitForElement(selector) {

while (document.querySelector(selector) === null) {

await new Promise( resolve => requestAnimationFrame(resolve) )

}

await new Promise(resolve => setTimeout(resolve, 1000));

return document.querySelector(selector);

}

async function CheckCrafts() {

let allTradeObjects = Object.values(Game.town.objectDict).filter(tradeObj => tradeObj.logicType === 'Trade');

for (let i=0, n=allTradeObjects.length; i < n; i++) {

if (allTradeObjects[i].logicObject.tapToCollectEntity.enabled) {

allTradeObjects[i].logicObject.OnTapped();

}

}

if (Game.town.GetStoredCrafts()['Gasoline'] >= 1) {

for (let i=0, n=craftedItems.length; i < n; i++) {

if (Game.town.GetStoredCrafts()[craftedItems[i].item] >= craftedItems[i].keepAmt + 10) {

let targetTradeObj;

if (Game.town.GetStoredCrafts()[craftedItems[i].item] >= 100 + craftedItems[i].keepAmt) {

targetTradeObj = GetAvailableTradeObject(100);

}

if (!targetTradeObj && Game.town.GetStoredCrafts()[craftedItems[i].item] >= 50 + craftedItems[i].keepAmt && craftedItems[i].sellMin <= 50){

targetTradeObj = GetAvailableTradeObject(50);

}

if (!targetTradeObj && Game.town.GetStoredCrafts()[craftedItems[i].item] >= 10 + craftedItems[i].keepAmt && craftedItems[i].sellMin <= 10){

targetTradeObj = GetAvailableTradeObject(10);

}

if (targetTradeObj){

CloseWindows(document.querySelectorAll('body > .container > .player-confirm .dialog-cell'), false);

CloseWindows(document.querySelectorAll('.container > div:not(.hud):not(.player-confirm)'), true);

Game.app.fire('SellClicked', {x: targetTradeObj.townX, z: targetTradeObj.townZ});

await WaitForCompletion('.LoadingOrders');

document.querySelector('#trade-craft-target [data-name="' + craftedItems[i].item + '"]').click();

await WaitForTradeLoad(targetTradeObj);

if (Game.town.GetStoredCrafts()['Gasoline'] >= Game.app.root.findByName('TradeUi').script.trade.cityPaths[0].gasCost) {

document.querySelector('#destination-target .destination .sell-button').click();

await WaitForCompletion('.trade-connection .compass');

} else {

console.log('Whoops! You have run out of gas.');

document.querySelector('#autosell-status .bank').textContent = 'ALERT: Out of gas!'

document.querySelector('.container > .trade .close-button').click();

}

}

}

}

} else {

console.log('Whoops! You have run out of gas.');

document.querySelector('#autosell-status .bank').textContent = 'ALERT: Out of gas!'

}

setTimeout(CheckCrafts, 5000);

}

async function ActivateAutoSell() {

let autoSellStatus = document.createElement('div');

autoSellStatus.id = 'autosell-status';

autoSellStatus.style.cssText = 'pointer-events: all; position: absolute; left: 50%; transform: translate(-50%, 0);';

autoSellStatus.addEventListener( 'click', function(){this.children[0].textContent = 'Auto-Sell Active';})

let autoSellContent = document.createElement('div');

autoSellContent.classList.add('bank');

autoSellContent.style.cssText = 'background-color: #fde7e3; padding-left: 10px; padding-right: 10px';

autoSellContent.textContent = 'Auto-Sell Active';

autoSellStatus.appendChild(autoSellContent);

await WaitForElement('.hud');

document.querySelector('.hud').prepend(autoSellStatus);

CheckCrafts();

}

})();

调试脚本

脚本保存后,关闭设置页面,进入Town Star 游戏界面,刷新,这时脚本便自动运行了。

PS:每次修改脚本,都要刷新,否则脚本不工作。

脚本修改

上文中提供的脚本,是我自己用的,也就是只适合我自己的经济模式,所以玩家可能会修改脚本,修改原理如下:

单引号中单词是售卖的产品名

keepAmt:表示想保留多少库存;

sellMin:表示想售卖多少产品;

两个单词中间用“_”隔开。

Town Star往期教程

今天就分享到这里了,后期我会不定期测试其他游戏,和大家一起在这混浊的世界,一起做一个纯种撸狗,稳健的赚钱。

0 条评论

发布
问题