MediaWiki:Common.js/shops.js

//

if (wgCanonicalNamespace == 'Marketplace' && !getCookie('ign').length) { setCookie('ign',prompt('What is your in-game name?'),1000) }

$(function{ $('.shoptable th b').click(function { if ($(this).parents('.shoptable').is('.collapsed')) { $(this).parents('.shoptable').removeClass('collapsed'); $(this).html('Close window') } else { $(this).parents('.shoptable').addClass('collapsed'); $(this).html('Open shop') } })

$('.money').attr('contenteditable','true').html(getCookie('shopMoney').length?getCookie('shopMoney'):'0').keyup(function {	var cookieval = parseInt($(this).html.replace(/\s/g,));	cookieval = cookieval?cookieval:;	setCookie('shopMoney',cookieval,1000); }); $('.info td+td+td').click(function {$(this).find('.money').focus;}).attr('title','click to change the amount of money you have.'); if (wgCanonicalNamespace == 'Marketplace') { $('h2').attr('data-section', function {		return $(this).find('.editsection a').attr('href').replace(/.*?(\d+)$/, '$1');	}) } })

$(function{ $('td[colspan="2"][title]').click(function { $('.select').removeClass('select'); $(this).addClass('select'); var item = $(this).children('img').attr('alt').replace(/\.\w{2,4}$/,''); var price = $(this).attr('title').replace('gp',''); var amount = parseInt($(this).children('.stock').html); if (amount != 0) { var amounts = '1'+(amount>=5?'5':)+(amount>=10?'10':)+(amount>=50?'50':''); $('.buy').html(''+item+': buy for '+price+'gp each Buy:'+amounts+'X  '); $('.sell').html(' You do not have any of this item to sell ') } else { amount = parseInt($(this).find('.carried').html); var amounts = '1'+(amount>=5?'5':)+(amount>=10?'10</b>':)+(amount>=50?'50</b>':''); $('.sell').html('<td colspan="12" style="text-align:left;">'+item+': sell for '+price+'gp each Sell:'+amounts+'X</b>  '); $('.buy').html(' This item is currently not available to buy '); }	var itemtd = this; if (wgCanonicalNamespace == 'Marketplace' && getCookie('ign').length) { $('.buy b, .sell b').click(function {			var thisb = this;			submitOffer(itemtd,thisb)		}); } })

$('.removeOffer').each(function {	if (wgUserName == $(this).parents('table.offers').prev('.shoptable').prev('h2').children('.mw-headline').html || $(this).is('tr.'+wgUserName+' .removeOffer')) {		$('.removeOffer').html('<a href="#" onclick="event.preventDefault;removeOffer(this);" title="Remove this offer (for example when the offer is completed or you reject the offer).">(remove)</a>')	} }) if (wgCanonicalNamespace == 'Marketplace') { var trs = $('table.offers tr+tr') for (i=0;i<trs.length;i++) { trs.eq(i).attr('data-pos',i); } } })

function submitOffer(itemtd,thisb) { var thistd = $(itemtd); var amount = parseInt($(thisb).attr('data-amount'));console.log(amount); amount = amount?amount:prompt('Type the number of items to buy and press enter'); if (!amount) {return false;} amount = parseInt(amount); if (!amount) { alert('The amount you entered is not a number.'); return false; } var stock = parseInt(thistd.children('.stock').html); stock = stock==0?parseInt(thistd.children('.carried').html):stock; var type = $(thisb).is('.buy b')?'buy':'sell'; if (amount > stock) { alert('The amount you entered is bigger than the '+(type=='buy'?'shop stocks':'buy offer')+'. Please enter an amount smaller than '+stock+'.'); return false;} var item = thistd.children('img').attr('alt').replace(/\.\w{2,4}$/,''); if (!confirm('Are you sure you want to place an offer to '+type+' '+amount+' '+item+'s?')) {return false;} var section = thistd.parents('table.shoptable').prev('h2').attr('data-section'); var sectname = thistd.parents('table.shoptable').prev('h2').children('.mw-headline').html; var sectid = thistd.parents('table.shoptable').prev('h2').children('.mw-headline').attr('id'); var price = parseInt(thistd.attr('title').replace('gp','')); var template = ''; callAPI({		'action':'query',		'titles':wgPageName,		'prop':'info|revisions',		'intoken':'edit',		'rvprop':'content|ids',		'rvlimit':'1',		'indexpageids':'true',		'rvsection':section,	}, 'GET', function(response) {		var page = response.query.pages[response.query.pageids[0]];		var content = typeof(page.revisions)!="undefined"?page.revisions[0]['*']:'';		content = content.replace(/\|\}\s*$/, template + '\n|}');		callAPI({ 'action':'edit', 'title':wgPageName, 'section':section, 'token':page.edittoken, 'text':content, 'summary':'/*'+sectname+'*/ Adding offer to '+type+' items.', 'minor':'true', }, 'POST', function(response) { document.location.hash = sectid; document.location.reload })	}) }

function removeOffer(thisrow) { var pos = $(thisrow).parents('tr').attr('data-pos'); var section = $(thisrow).parents('table.offers').prev('table.shoptable').prev('h2').attr('data-section'); var sectid = $(thisrow).parents('table.offers').prev('table.shoptable').prev('h2').children('.mw-headline').attr('id'); var regex = new RegExp('(\n(.*\n){'+pos+'}).*\n'); callAPI({		'action':'query',		'titles':wgPageName,		'prop':'info|revisions',		'intoken':'edit',		'rvprop':'content|ids',		'rvlimit':'1',		'indexpageids':'true',		'rvsection':section,	}, 'GET', function(response) {		var page = response.query.pages[response.query.pageids[0]];		var content = typeof(page.revisions)!="undefined"?page.revisions[0]['*']:'';		content = content.replace(regex, '$1');		callAPI({ 'action':'edit', 'title':wgPageName, 'section':section, 'token':page.edittoken, 'text':content, 'summary':'Removing completed/rejected offer', 'minor':'true', }, 'POST', function(response) { document.location.hash = sectid; document.location.reload })	}) }

/* CODE TO ADD NEW MARKETPLACE SHOPS */

$(function {	$('#addnew').html('Add or edit personal shop '); })

function openAddShopForm { var selector = '.mw-headline:contains('+wgUserName+')' if ($(selector).length) { document.location.hash = $(selector).attr('id'); openEditShopForm($(selector).parent.attr('data-section')); return false; }	$('#addnew').after('<form action="javascript:addNewShop" class="shopaddform"> Item name: <input type="text" name="item1"> Amount: <input type="text" name="amount1">  Price: <input type="text" name="price1"> <a href="javascript:removeLine(1)">Remove</a> <input type="submit" value="Submit changes">Add another item  ') $('#addnew').remove; }

function addNewShop { var content = '\n\n\n|}'; callAPI({		'action':'query',		'titles':wgPageName,		'prop':'info',		'intoken':'edit',		'indexpageids':'true',	}, 'GET', function(response) {		var edittoken = response.query.pages[response.query.pageids[0]].edittoken;		callAPI({ 'action':'edit', 'section':'new', 'title':wgPageName, 'token':edittoken, 'summary':wgUserName, 'minor':'true', 'text':content }, 'POST', function(response) { console.log(response) });	}); }

function openEditShopForm(section) { callAPI({		'action':'query',		'titles':wgPageName,		'prop':'info|revisions',		'intoken':'edit',		'rvprop':'content|ids',		'rvlimit':'1',		'indexpageids':'true',		'rvsection':section,	}, 'GET', function(response) {		var page = response.query.pages[response.query.pageids[0]];		var content = typeof(page.revisions)!="undefined"?page.revisions[0]['*']:;		var template = content.replace(/^(.*\n+(.|\n)*)$/, '$2');		var params;		var lines = template.replace(/^\n+|\n+$/g,).split('\n');		var divs = []		for (i=0;i<lines.length;i++) {			params = lines[i].replace(/^\|/,'').replace(/\|..=/g,'|').split('\|');			divs[i] = '<div data-pos="'+(i+1)+'"> Item name: <input type="text" name="item" value="'+params[0]+'"> Amount: <input type="text" name="amount" value="'+params[1]+'">  Price: <input type="text" name="price" value="'+params[2]+'"> <a href="javascript:removeLine('+(i+1)+')">Remove</a> ';		}		$('[data-section="'+section+'"]').after('<form action="javascript:editShop('+section+')" class="shopaddform">\n'+divs.join('\n')+'\n  <input type="submit" value="Submit changes"> Add another item  '); }); }

function removeLine(pos) { $('.shopaddform div[data-pos="'+pos+'"]').remove; $('.shopaddform div[data-pos]').each(function(i) {		$(this).attr('data-pos',i+1);	}) }

function addItemRow { var lastchild = $('.shopaddform div:last-child'); if (parseInt(lastchild.prev.attr('data-pos')) == 40) { alert('You can only have a maximum of 40 items in a shop.'); return false; }	if (lastchild.prev.find('input[name="item"]').val !=  && lastchild.prev.find('input[name="amount"]').val !=  && lastchild.prev.find('input[name="price"]').val != '') { lastchild.before('<div data-pos="'+ (parseInt(lastchild.prev.attr('data-pos'))+1) +'"> Item name: <input type="text" name="item"> Amount: <input type="text" name="amount">  Price: <input type="text" name="price"> <a href="javascript:removeLine('+ (parseInt(lastchild.prev.attr('data-pos'))+1)+ ')">Remove</a> '); } else { alert('Please fill in all fields of the previous item before adding another row.') } }

function editShop(section, edittoken) { var template = ''; callAPI({		'action':'query',		'titles':wgPageName,		'prop':'info|revisions',		'intoken':'edit',		'rvprop':'content|ids',		'rvlimit':'1',		'indexpageids':'true',		'rvsection':section,	}, 'GET', function(response) {		var page = response.query.pages[response.query.pageids[0]];		var content = typeof(page.revisions)!="undefined"?page.revisions[0]['*']:'';		content = content.replace(//, template);		callAPI({ 'action':'edit', 'title':wgPageName, 'section':section, 'token':page.edittoken, 'text':content, 'summary':'Editing shop stocks', 'minor':'true' }, 'POST', function(response) { document.location.reload })	}) }