User:Cqm/efficiency.js

/** * Efficiency base class * * @author Cqm * @copyright © Cqm 2014  *           All rights reserved */


 * ( function ( $, mw, rswiki ) {

'use strict';

var eff = ( function {

/**            * Private functions that do not need to be interacted with directly */       var local = {

/**                * Create form for user to fill out created from template on page */               buildForm: function  {

var $form = $( '#rs-eff-form' ), content = $( '.jsCalc' ).text.split( '\n' ), i,                       conf, text, j,                       options, out = '';

// create static section of form $form.empty; $form.append(                       $( ' ' )                            .attr( { id: 'eff-form', action: '#' } )                           .on( 'submit', local.generate )                            .append( $( ' ';

$( '#rs-eff-result' ).html( out );

mw.loader.using( ['jquery.tablesorter'], function {                        $( '#efficiency-table' ).tablesorter;                        rswiki.common.autosort;                    } );

},

/**                * Pull data from hidden tables on the page * Fills out data[item].price, data[item].image and ing[ingredient].price and items array */               getData: function  {

// table containing data for data object and items array $( '#efficiency-data>tbody>tr' ).each( function {                        var $td = $( this ).children( 'td' ),                            key,                            price,                            img;                        $td.each( function ( i ) { if ( i === 0 ) { key = $( this ).text.trim; } else if ( i === 1 ) { price = $( this ).text.trim; } else if ( i === 2 ) { img = $( this ).children( 'a' ).attr( 'href' ); }                       } );                        self.items.push( key );                        self.data[key].price = parseInt( price, 10 );                        self.data[key].image = '';                    } ); // table containing list of ingredients $( '#efficiency-ingredients>tbody>tr' ).each( function {                        var $td = $( this ).children( 'td' ),                            key,                            price;                        $td.each( function ( i ) { if ( i === 0 ) { key = $( this ).text.trim; } else if ( i === 1 ) { price = $( this ).text.trim; }                       } );                        self.ing[key] = {                            price: parseInt( price, 10 )                        };                    } );

},

/**                * Check for function type *                * @param {function} obj Object to check function type for * @return {boolean} true if obj is a function, false if not */               isFunction: function ( obj ) { return !!( obj && obj.constructor && obj.call && obj.apply ); },

/**                * Parses wikitext links *                * @param {string} text Wikitext link(s) to parse * @return {string} Corresponding html for wikitext link(s) */               parseLinks: function ( text ) {

var link = /\[\[.+?\]\]/g, match = text.match( link ), replace, i;

for ( i = 0; i < match.length; i += 1 ) {

replace = match[i].replace( /(\[\[|\]\])/g, '' );

if ( replace.indexOf( '|' ) > -1 ) { replace = replace.split( '|' ); text = text.replace( match[i], '' + replace[1] + '' ); } else { text = text.replace( match[i], '' + replace + '' ); }

}

return text;

}

},

/**            * Public functions * These are a mixture of functions that are required to be global for use * or can be overwritten by individual calcs */           self = {

/**                * Filled out by individual calcs if required */               checkMods: function  {},

/**                * Data object *                * @example foo: { *             // string of ingredients *             // accepts wikilinks which are converted to links *             ingredients: 'ingredient1, 5 ingredient2', *             // level at which the item can be made *             lvl: 5, *             // profit function *             // subtracts price of ingredients from created item and returns the profit *             // if resulting item or ingredient is untradeable set that price to 0 *             // this is a function to allow for modification of prices of certain items *             // @example Scroll of cleansing uses 10% less secondary ingredients when making potions *             profit: function  { *                 return data['foo'].price - ing['ingredient1'].price - ing['ingredient2'].price; *             },                 *              // number of items that can be made per hour *             // num can be modified on a per item basis or to take account *             // of a number modifier using modNum if required *             num: 5000, *             // experience gained from making the item *             // xp can be modified on a per item basis or to take account *             // of an experience modifier using modXP if required *             xp: 50 *         }                 */                data: {},

/**                * Ingredients prices, filled out by getData */               ing: {},

/**                * Start function * Should be called from individual calcs to start script */               init: function  { console.log( 'init' ); local.getData; console.log( self.data ); local.buildForm; // local.generate; },

/**                * Item names, filled out by getData */               items: [],

/**                * Default num modification, to be altered by each calc as required *                * @param {number} num Number to be modified * @return {number} Modified number */               modNum: function ( num ) { return num; },

/**                * Default xp modification function, to be altered by each calc as required *                * @param {number} xp Experience to modify * @return {number} Modified experience */               modXp: function ( xp ) { return xp; },

/**                * Error handling function *                * @param {string} error Error to display */               throwError: function ( error ) { console.log( error ); },

/**                * Filled out by individual efficiency calc script if required */               xpMods: {}

};

return self;

} );

// expose to global scope rswiki.eff = eff;

} ( this.jQuery, this.mediaWiki, this.rswiki ) );