Skip to content

Instantly share code, notes, and snippets.

@notarikon-nz
Created August 27, 2024 06:54
Show Gist options
  • Select an option

  • Save notarikon-nz/55b39f9a5cd1dcee9b22d744ca35fbdb to your computer and use it in GitHub Desktop.

Select an option

Save notarikon-nz/55b39f9a5cd1dcee9b22d744ca35fbdb to your computer and use it in GitHub Desktop.
HackMUD Snax Purchasing Script for sn_w_usac
function(context, args) {
const MIN_SLOTS_FREE = 16
let MIN_SNAX_PURCHASED = 10
const MAX_SNACK_COST = 25000
const decolor = val => val.replace(/`\w(.*?)`/g, '$1');
const input = decolor(#hs.sys.specs());
const regex = /slots:\s*(\d+)\/(\d+)/;
const snaxRegex = /^[c|d|e].*/
const matches = input.match(regex);
const firstValue = matches[1];
const secondValue = matches[2];
let available_slots = secondValue - firstValue;
const L = #fs.scripts.lib();
const A = #fs.aperture.lib()
// CHECK IF WE ALREADY HAVE SNAX
let currentSnax = #hs.sys.upgrades( {filter:{rarity:0,tier:1,loaded:false,type:{"$ne":"tool"} }} );
if (Array.isArray(currentSnax)) {
currentSnax = currentSnax.filter(u => u.name.match(snaxRegex) );
if (currentSnax.length > MIN_SNAX_PURCHASED) {
return `Sufficient Snax (${currentSnax.length}) in Upgrade Storage\n`
} else {
MIN_SNAX_PURCHASED -= currentSnax.length
}
}
// END CHECK FOR SNAX
// if whitelisted make sure we have money to buy snax
if (A.isAuthorised(context.caller,context.calling_script)) {
let balance = #hs.accts.balance();
if (balance < 1000000) {
#ls.nokiraton.steal({ transfer: "1MGC", memo: "snax" });
}
}
const oneDay = 86400000; // ms
let yesterday = L.to_game_timestr(new Date(Date.now() - oneDay));
let snax = #fs.market.browse(
{
rarity: 0,
tier: 1,
cost: {"$lte": MAX_SNACK_COST},name: {"$regex":"^[c|d|e].*"},
// listed_after: yesterday, // previous 24 hours
}
),
count = 0,
purchased = 0,
token = null
const MARKET_SIZE = snax.length;
const MAX_SNAX_TO_BUY = Math.min(Math.min(MIN_SNAX_PURCHASED, available_slots - MIN_SLOTS_FREE), snax.length)
snax = snax.slice(0, Math.min(MAX_SNAX_TO_BUY,snax.length))
for (count = 0; count < MAX_SNAX_TO_BUY; count++) {
token = snax[count].i
let result = #ms.market.buy({i:token,count:1,confirm:1})
if (result && result.ok) {
if (result.ok == true)
purchased++
}
if (check_timeout(4500))
break;
}
// clean up
snax = null
return `Available Slots: ${available_slots}\n` +
`Purchased ${purchased}/${MAX_SNAX_TO_BUY} snax from a market pool of ${MARKET_SIZE}\n`
function check_timeout(x) {
x = x || 4500; // Adjust according to the appropriate timeout requirement
return (Date.now() - _START) > x;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment