Sixtest Wiki:API4/TradeSystem

(function TradeSystem {	"use strict";	var extension = $API.create.extension('TradeSystem');	extension.parseOTS = function (str) {		// parse the old trade system strings; not very accurate but it doesn't matter		var regex, npc, i = 0, data = [];		regex = /(\w+)\s*\([\w\s]+\)\s*(\d+)\s*(gp|gold)?\s*(\([\w\s]+\))*/g;		while ((npc = regex.exec(str)) !== null) {			data[i] = { npc: npc[1], value: npc[2], notes: npc[4] };			i += 1;		}		return data;	};	extension.parseNTS = function (arr) {		var npcs = arr,			i = 0,			len = arr.length,			obj,			ndata = [],			tvdata = [],			data = [];		for (i; i < len; i += 1) {			ndata[i] = npcs[i].split(';');			tvdata[i] = ndata[i][0].split(':');			obj = {};			if (tvdata[i][0]) {				obj.npc = tvdata[i][0];			}			if (tvdata[i][1]) {				obj.value = tvdata[i][1];			}			if (ndata[i][1]) {				obj.notes = ndata[i][1];			}			data[data.length] = obj;		}		if (!data.length) { data = [ { npc: "--" } ]; }		return data; };	extension.parse = function (item, mode) { var func = item.newtradesystem === 'yes' ? extension.parseNTS : extension.parseOTS, m; m = mode === 'buys' ? 'sellto' : 'buyfrom'; return func(item[m] || ''); };	extension.tradesParse = function (trades) { var i = 0, len = trades.length, arr = [], str = ''; for (i; i < len; i += 1) { str = trades[i].data.npc + (trades[i].data.value !== undefined ? ': ' + trades[i].data.value : ) + (trades[i].data.notes !== undefined ? '; ' + trades[i].data.notes : ); arr[i] = str; }		return arr.join(', '); };	extension.compareNpcData = function (item, mode) { var occurrences = [], o, i, len, j, len2, out1 = [], out2 = [], occ, obj; $API.data.npc.query(function (npc) {			o = $API.ext.Array.occurrences(item.name, npc[mode], 'name');			if (o.length) {				occurrences[occurrences.length] = { npc: npc.name, occurrences: o };			}		}); i = 0; len = occurrences.length; for (i; i < len; i += 1) { j = 0; len2 = occurrences[i].occurrences.length; for (j; j < len2; j += 1) { obj = {}; occ = occurrences[i].occurrences[j]; obj.npc = occurrences[i].npc; obj.value = occ.value; obj.notes = occ.notes; out1[out1.length] = obj; }		}		out2 = extension.parse(item, mode); return $API.ext.Array.diff(out1, out2, 'npc'); };	extension.update = function (start) { var items = $API.data.item, item, next, id = start || 0, root, container, format; root = $API.ui.query(function (e) { return e.level === 0; }, 1).data[0]; container = root.child('div'); format = function (container, item) { var data = {}, elements = {}, iterate, tmp; container.removeChildren; data.buyfrom = extension.compareNpcData(item, 'sells'); data.sellto = extension.compareNpcData(item, 'buys'); elements.list_name = container.child('div'); elements.list_name.text(item.name); elements.list_yes = container.child('button'); elements.list_yes.text('Yes'); elements.list_reject = container.child('button'); elements.list_reject.text('No'); elements.list_submit = container.child('button'); elements.list_submit.text('Submit'); elements.list_submit.ele.onclick = $API.ext.Edit.submit; elements.list_yes.ele.onclick = function { item.twEdit('buyfrom', extension.tradesParse(data.buyfrom)); item.twEdit('sellto', extension.tradesParse(data.sellto)); next; };			elements.list_reject.ele.onclick = function { next; };			elements.list_sellto_header = container.child('div').text('Sell this item to'); elements.list_sellto = container.child('ul'); elements.list_sellto_header = container.child('div').text('Buy this item from'); elements.list_buyfrom = container.child('ul'); iterate = function (mode) { var i = 0, arr = data[mode], len = arr.length, e;				for (i; i < len; i += 1) { e = elements['list_' + mode].child('li'); e.addClass(arr[i].type === '+' ? 'added' : (arr[i].type === '-' ? 'removed' : 'nochange')); tmp = arr[i].data; e.text(tmp.npc + (tmp.value !== undefined && tmp.value !== 0 ? ': ' + arr[i].data.value : '') + (tmp.notes !== undefined ? '; ' + arr[i].data.notes : '')); }			};			iterate('sellto'); iterate('buyfrom'); };		next = function { item = items.getId(id) || null; if (item !== null) { id += 1; }			format(container, item.data[0]); };		next; }; });