<commit_before>/**
 * Copyright (c) 2014 Virtue Studios
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions\:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
/* globals EventType, ENGINE, Inv */
var widget = require('../../core/widget');
var config = require('../../core/config');
var util = require('../../core/util');
var inv = require('../../core/inv');
var stat = require('../logic/stat');
var quest = require('../../core/quest');

/**
 * @author <NAME> <skype:kfriz1998>
 * @author <NAME> <skype:travis.mccorkle>
 * @author Arthur <skype:arthur.behesnilian>
 * @author Sundays211
 * @since 19/11/2014
 */
module.exports = (function () {
	return {
		init : init,
		selectProduct : selectProduct,
		structContainsItem : structContainsItem
	};
	
	function init (scriptManager) {
		scriptManager.bind(EventType.IF_OPEN, 1370, function (ctx) {
			openDialog(ctx.player);
		});
		
		scriptManager.bind(EventType.IF_DRAG, widget.getHash(1371, 36), function (ctx) {
			if (ctx.toslot < ENGINE.getVarBit(ctx.player, 1002) && ctx.toslot >= 0) {
				ENGINE.setVarBit(ctx.player, 1003, ctx.toslot+1);
			}
		});
		
		scriptManager.bind(EventType.IF_BUTTON, 1371, function (ctx) {
			var player = ctx.player;
			
			var mainCategory = ENGINE.getVarp(player, 1168);
			if (mainCategory < 0) {
				ENGINE.sendMessage(player, "No main category selected!");
				return;
			}
			switch (ctx.component) {
			case 62://Select product category
				if (ctx.slot >= 0 && ctx.slot < config.enumSize(mainCategory)) {
					var newCategory = config.enumValue(mainCategory, ctx.slot);
					ENGINE.setVarp(player, 1169, newCategory);
					var slotCount = config.enumSize(newCategory) * 4;
					widget.setEvents(player, 1371, 44, 0, slotCount, 2);
					setSelectedProduct(player, 0);
				}
				return;
			case 44://Select product
				setSelectedProduct(player, ctx.slot);
				return;
			case 29://Increase count
				if (ENGINE.getVarBit(player, 1003) < ENGINE.getVarBit(player, 1002)) {
					ENGINE.incrementVarBit(player, 1003, 1);
				}
				return;
			case 31://Decrease count
				if (ENGINE.getVarBit(player, 1003) > 0) {
					ENGINE.incrementVarBit(player, 1003, -1);
				}
				return;
			case 143://Click count
				if (ctx.slot < ENGINE.getVarBit(player, 1002) && ctx.slot >= 0) {
					ENGINE.setVarBit(player, 1003, ctx.slot+1);
				}
				return;
			default:
				util.defaultHandler(ctx, "make-x dialog");
				return;
			}
		});
	}
	
	function selectProduct (player, rootCategory, rootCategoryNames, category, productId) {
		ENGINE.setVarp(player, 1168, rootCategory);
		ENGINE.setVarc(player, 2222, rootCategoryNames);
		ENGINE.setVarp(player, 1169, category);
		if (typeof(productId) === 'number') {
			ENGINE.setVarp(player, 1170, productId);
		}
		widget.openCentral(player, 1370, false);
	}
	
	function openDialog (player) {
		ENGINE.runClientScript(player, 6946, []);
		widget.hide(player, 1371, 20, false);
				
		ENGINE.setVarc(player, 2225, 0);
		ENGINE.setVarc(player, 2689, 0);
		ENGINE.setVarc(player, 2690, 0);

		var categoryId = ENGINE.getVarp(player, 1169);		
		var selectedItemId = ENGINE.getVarp(player, 1170);
		
		if (!config.enumHasValue(categoryId, selectedItemId)) {
			selectedItemId = -1;//Only select items actually in the category
		}
		
		if (selectedItemId == -1) {//Auto-select item
			var productId;
			for (var slot = 0; slot < config.enumSize(categoryId); slot++) {
				productId = config.enumValue(categoryId, slot);
				if (getMaxAmount(player, productId) > 1) {
					ENGINE.setVarp(player, 1170, productId);
					selectedItemId = productId;
					break;
				}
			}
		}
		if (selectedItemId == -1) {
			selectedItemId = config.enumValue(categoryId, 0);
			ENGINE.setVarp(player, 1170, selectedItemId);
		}
		var invCount = getMaxAmount(player, selectedItemId);//The maximum amount of the item the player can produce
		ENGINE.setVarBit(player, 1002, invCount);//Product select max amount
		ENGINE.setVarBit(player, 1003, invCount);//The amount currently selected
		
		//Information about the selected item
		ENGINE.setVarc(player, 2391, config.objDesc(selectedItemId));//Description
		ENGINE.setVarc(player, 2223, 1);
		ENGINE.setVarc(player, 2224, ENGINE.getExchangeCost(selectedItemId));//Exchange guide price
		
		widget.open(player, 1370, 62, 1371, true);//The inner interface
		
		widget.setEvents(player, 1371, 62, 0, 12, 2);//Allows an option to be selected in the category drop-down menu
		widget.setEvents(player, 1371, 36, 0, invCount, 2359296);//Activates the amount selection dragger
		widget.setEvents(player, 1371, 143, 0, invCount, 2);//Clicks on the amount selection dragger
		var slotCount = config.enumSize(categoryId) * 4;
		widget.setEvents(player, 1371, 44, 0, slotCount, 2);//Allows the items to be clicked
	}
	
	/**
	 * Sets the selected product and updates the information for it
	 * @param player The player
	 * @param slot The selected slot ID
	 * @return True if the product was selected successfully, false if an error occured
	 */
	function setSelectedProduct (player, slot) {
		slot = slot/4;
		var subCategory = ENGINE.getVarp(player, 1169);
		if (subCategory < 0) {
			return false;<commit_msg>Prevent players from crafting products they don't have the correct level for<commit_after>/**
 * Copyright (c) 2014 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions\:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
/* globals EventType, ENGINE, Inv */
var widget = require('../../core/widget');
var config = require('../../core/config');
var util = require('../../core/util');
var inv = require('../../core/inv');
var stat = require('../logic/stat');
var quest = require('../../core/quest');

/**
 * @author <NAME> <skype:kfriz1998>
 * @author <NAME> <skype:travis.mccorkle>
 * @author Arthur <skype:arthur.behesnilian>
 * @author Sundays211
 * @since 19/11/2014
 */
module.exports = (function () {
	return {
		init : init,
		selectProduct : selectProduct,
		structContainsItem : structContainsItem
	};
	
	function init (scriptManager) {
		scriptManager.bind(EventType.IF_OPEN, 1370, function (ctx) {
			openDialog(ctx.player);
		});
		
		scriptManager.bind(EventType.IF_DRAG, widget.getHash(1371, 36), function (ctx) {
			if (ctx.toslot < ENGINE.getVarBit(ctx.player, 1002) && ctx.toslot >= 0) {
				ENGINE.setVarBit(ctx.player, 1003, ctx.toslot+1);
			}
		});
		
		scriptManager.bind(EventType.IF_BUTTON, 1371, function (ctx) {
			var player = ctx.player;
			
			var mainCategory = ENGINE.getVarp(player, 1168);
			if (mainCategory < 0) {
				ENGINE.sendMessage(player, "No main category selected!");
				return;
			}
			switch (ctx.component) {
			case 62://Select product category
				if (ctx.slot >= 0 && ctx.slot < config.enumSize(mainCategory)) {
					var newCategory = config.enumValue(mainCategory, ctx.slot);
					ENGINE.setVarp(player, 1169, newCategory);
					var slotCount = config.enumSize(newCategory) * 4;
					widget.setEvents(player, 1371, 44, 0, slotCount, 2);
					setSelectedProduct(player, 0);
				}
				return;
			case 44://Select product
				setSelectedProduct(player, ctx.slot);
				return;
			case 29://Increase count
				if (ENGINE.getVarBit(player, 1003) < ENGINE.getVarBit(player, 1002)) {
					ENGINE.incrementVarBit(player, 1003, 1);
				}
				return;
			case 31://Decrease count
				if (ENGINE.getVarBit(player, 1003) > 0) {
					ENGINE.incrementVarBit(player, 1003, -1);
				}
				return;
			case 143://Click count
				if (ctx.slot < ENGINE.getVarBit(player, 1002) && ctx.slot >= 0) {
					ENGINE.setVarBit(player, 1003, ctx.slot+1);
				}
				return;
			default:
				util.defaultHandler(ctx, "make-x dialog");
				return;
			}
		});
	}
	
	function selectProduct (player, rootCategory, rootCategoryNames, category, productId) {
		ENGINE.setVarp(player, 1168, rootCategory);
		ENGINE.setVarc(player, 2222, rootCategoryNames);
		ENGINE.setVarp(player, 1169, category);
		if (typeof(productId) === 'number') {
			ENGINE.setVarp(player, 1170, productId);
		}
		widget.openCentral(player, 1370, false);
	}
	
	function openDialog (player) {
		ENGINE.runClientScript(player, 6946, []);
		widget.hide(player, 1371, 20, false);
				
		ENGINE.setVarc(player, 2225, 0);
		ENGINE.setVarc(player, 2689, 0);
		ENGINE.setVarc(player, 2690, 0);

		var categoryId = ENGINE.getVarp(player, 1169);		
		var selectedObjId = ENGINE.getVarp(player, 1170);
		
		if (!config.enumHasValue(categoryId, selectedObjId)) {
			selectedObjId = -1;//Only select items actually in the category
		}
		
		if (selectedObjId == -1) {//Auto-select item
			var productId;
			for (var slot = 0; slot < config.enumSize(categoryId); slot++) {
				productId = config.enumValue(categoryId, slot);
				if (getMaxAmount(player, productId) > 1) {
					ENGINE.setVarp(player, 1170, productId);
					selectedObjId = productId;
					break;
				}
			}
		}
		if (selectedObjId == -1) {
			selectedObjId = config.enumValue(categoryId, 0);
			ENGINE.setVarp(player, 1170, selectedObjId);
		}
		var invCount = canCraft(player, selectedObjId) ? getMaxAmount(player, selectedObjId) : 0;//The maximum amount of the item the player can produce
		ENGINE.setVarBit(player, 1002, invCount);//Product select max amount
		ENGINE.setVarBit(player, 1003, invCount);//The amount currently selected
		
		//Information about the selected item
		ENGINE.setVarc(player, 2391, config.objDesc(selectedObjId));//Description
		ENGINE.setVarc(player, 2223, 1);
		ENGINE.setVarc(player, 2224, ENGINE.getExchangeCost(selectedObjId));//Exchange guide price
		
		widget.open(player, 1370, 62, 1371, true);//The inner interface
		
		widget.setEvents(player, 1371, 62, 0, 12, 2);//Allows an option to be selected in the category drop-down menu
		widget.setEvents(player, 1371, 36, 0, invCount, 2359296);//Activates the amount selection dragger
		widget.setEvents(player, 1371, 143, 0, invCount, 2);//Clicks on the amount selection dragger
		var slotCount = config.enumSize(categoryId) * 4;
		widget.setEvents(player, 1371, 44, 0, slotCount, 2);//Allows the items to be clicked
	}
	
	/**
	 * Sets the selected product and updates the information for it
	 * @param player The player
	 * @param slot The selected slot ID
	 * @return True if the product was selected successfully, false if an error occured
	 */
	function setSelectedProduct (player, slot) {
		slot = slot/4;
		var subCategory = ENGINE.getVarp(player, 1169);
		if (subCategory < 0) {
			return false;
{'fraction_non_alphanumeric': 0.12238273007628632, 'fraction_numerical': 0.04114591787047557, 'mean_word_length': 3.8477576711250983, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1778392', 'n_tokens_mistral': 4830, 'n_tokens_neox': 3838, 'n_words': 1168}