Includes : Neopets : Shop Wizard

By w35l3y Last update 3 hours ago — Installed 9,791 times.


Script Summary: Wizard Function

Version: 2.0.0.2

Copyright: 2011+, w35l3y (http://gm.wesley.eti.br)

License: GNU GPL

FOR SCRIPTERS/PROGRAMMERS only

If you are neither scripter nor programmer, you're discouraged to install this script directly.

Implementing

// @require        http://userscripts.org/scripts/source/56489.user.js
// @require        http://userscripts.org/scripts/source/56503.user.js

Examples

Wizard.find({
	"text" : "plain omelette",
	"onsuccess" : function(params)
	{
		var a = [];
		for ( var ai = 0 , at = params.list.length ; ai < at ; ++ai )
		{
			a.push([
//				params.list[ai].Link,
				params.list[ai].Owner,
				params.list[ai].Item,
				params.list[ai].Stock,
				params.list[ai].Price
			].join("\t"));
		}
		alert(a.join("\n"));
	}
});
function getLowestPrices(params)
{
	if (typeof params.parameters == "undefined")
	{
		params.parameters = {};
	}

	params.parameters.list = [];

	(function recursive(params)	// creates a recursive function to keep a searching after another
	{
		list:for ( var ai = params.list.length - 1 ; ~ai ; --ai )	// this loop avoids duplicate entries
		{
			for ( var bi = params.parameters.parameters.list.length - 1 ; ~bi ; --bi )
			if ( params.parameters.parameters.list[bi] == params.list[ai] )	// the current item already exists (we may presume the other ones from the same page exist too. heuristic search!)
			break; // set label "list" here if you want to do a heuristic search (this is good for items that don't have much rotativity)

			if (!~bi)	// add the current item to the list!
			params.parameters.parameters.list.push(params.list[ai]);
		}

		if (--params.parameters.attempts < 0)	// done!
		{
			var obj = params.parameters.parameters||{};

			obj.list.sort(function(a, b)	// order items by price
			{
				return ( a.Price == b.Price ? 0 : ( a.Price < b.Price ? -1 : 1 ) );
			});

			params.parameters.onsuccess(obj);
		}
		else	// keep searching...
		{
			setTimeout(Wizard.find, ( typeof params.parameters.wait == "function" ? params.parameters.wait() : (parseInt(w, 10)||1000) ), {
				"text" : params.parameters.text,
				"onsuccess" : recursive,
				"parameters" : params
			});
		}
	})({
		"list" : [],
		"parameters" : params
	});
}

// and then...
getLowestPrices({
	"text" : "plain omelette",
	"attempts" : 5,
	"wait" : function(){return 1000+Math.random()*1000;},	// rnd time : 1-2 secs
	"onsuccess" : function(params)
	{
		var list = [];
		for ( var item in params.list )
			list.push([params.list[item].Owner, params.list[item].Price]);

		// the lowest price is params.list[0].Price
		alert(list.join("\n"));
	}
});