跳转到内容

MediaWiki:Common.js:修订间差异

来自Minimalist Tower Defense Wiki
Jerry c9​(留言 | 贡献)
添加灰白里程有尽矿井计算
 
Jerry c9​(留言 | 贡献)
更新计算器
第1行: 第1行:
/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */
/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */
/* 有尽矿井计算器 */
/* 灰白里程有尽矿井计算器 */
(function () {
( function () {
   'use strict';
   'use strict';
  var state = { x: 0 };


   // 向下取整辅助函数
   function toJS( raw ) {
  function f36(x) {
    var s = String( raw ).trim();
    return Math.floor(36 * x / 10);
    s = s.replace( /×/g, '*' ).replace( /÷/g, '/' )
        .replace( /(/g, '(' ).replace( /)/g, ')' )
        .replace( /[-—–−]/g, '-' ).replace( /\s+/g, '' );
    s = s.replace( /(\d)(x)/g, '$1*$2' ).replace( /(x)(\d)/g, '$1*$2' )
        .replace( /(\d)\(/g, '$1*(' ).replace( /\)(\d)/g, ')*$1' )
        .replace( /\)(x)/g, ')*$1' ).replace( /(x)\(/g, '$1*(' );
    s = s.replace( /⌊([^⌋]*)⌋/g, function ( m, inner ) {
      return 'Math.floor(' + inner + ')';
    } );
    return s;
   }
   }
   function f66(x) {
 
     return Math.floor(66 * x / 10);
   function evalExpr( raw, x ) {
     try {
      var fn = new Function( 'x', 'return (' + toJS( raw ) + ');' );
      var v = fn( x );
      if ( typeof v !== 'number' || !isFinite( v ) ) return null;
      return Math.round( v );
    } catch ( e ) { return null; }
   }
   }


   // 公式表:key -> 函数
   function sanitizeX( v ) {
  var formulas = {
     var n = Math.floor( Number( v ) );
    deploy:    function (x) { return 30 * x + 50; },
     if ( !isFinite( n ) || n < 0 ) n = 0;
     baseUp:    function (x) { return 20 + 10 * x; },
     return n;
    advUp:    function (x) { return 10 * f36(x) + 60; },
  }
     fastUp:    function (x) { return 10 * f66(x) + 110; },
     maxUp:    function (x) { return 210 * x + 140; },


    baseTotal: function (x) { return 40 * x + 70; },
  function eachNode( sel, fn ) {
    advTotal:  function (x) { return 40 * x + 10 * f36(x) + 130; },
     Array.prototype.forEach.call( document.querySelectorAll( sel ), fn );
     fastTotal: function (x) { return 40 * x + 10 * f36(x) + 10 * f66(x) + 240; },
   }
    maxTotal:  function (x) { return 250 * x + 10 * f36(x) + 10 * f66(x) + 380; }
   };


  // 更新页面上所有带 sw-egm-value 的元素
   function initOutputs() {
   function updateAllValues(x) {
     eachNode( '.calc-out', function ( el ) {
     var elements = document.querySelectorAll('.sw-egm-value');
       if ( el.__expr === undefined ) el.__expr = el.textContent.trim();
    for (var i = 0; i < elements.length; i++) {
     } );
       var el = elements[i];
      var key = el.getAttribute('data-key');
      var suffix = el.getAttribute('data-suffix') || '';
      if (formulas[key]) {
        el.textContent = formulas[key](x) + suffix;
      }
     }
   }
   }


  // 初始化有尽矿井计算器
   function renderOutputs() {
   function initEndlessGoldMine() {
     eachNode( '.calc-out', function ( el ) {
     var container = document.querySelector('.sw-calculator[data-calculator="SWEndlessGoldMineSpend"]');
      if ( el.__expr === undefined ) el.__expr = el.textContent.trim();
    if (!container) return;
      var v = evalExpr( el.__expr, state.x );
      if ( v !== null ) el.textContent = String( v );
    } );
  }


    // 避免重复初始化
  function buildInput( box, xValue ) {
     if (container.querySelector('input')) return;
     if ( box.querySelector( '.calc-input' ) ) return;
 
    var labelText = box.textContent.trim() || '数量 x';
     // 创建输入框
     box.textContent = '';
     var label = document.createElement('label');
     var label = document.createElement( 'label' );
     label.textContent = '当前已部署的有尽矿井数量 x:';
     label.className = 'calc-label';
     var input = document.createElement('input');
    label.appendChild( document.createTextNode( labelText.replace( /[::]\s*$/, '' ) + ':' ) );
     var input = document.createElement( 'input' );
     input.type = 'number';
     input.type = 'number';
     input.min = '0';
     input.min = '0';
     input.step = '1';
     input.step = '1';
     input.value = '0';
     input.value = String( xValue );
     input.style.width = '6em';
    input.className = 'calc-input';
     input.style.marginLeft = '0.5em';
     input.setAttribute( 'inputmode', 'numeric' );
     label.appendChild(input);
    input.setAttribute( 'aria-label', labelText );
     input.addEventListener( 'input', function () {
      state.x = sanitizeX( input.value );
      renderOutputs();
    } );
    input.addEventListener( 'blur', function () {
      state.x = sanitizeX( input.value );
      input.value = String( state.x );
      renderOutputs();
    } );
     label.appendChild( input );
    box.appendChild( label );
  }


     var hint = document.createElement('p');
  function initBoxes() {
    hint.textContent = '修改 x 后,本页所有与 x 有关的金币花费会自动更新。';
     eachNode( '.calc-box', function ( box ) { buildInput( box, state.x ); } );
    hint.style.fontSize = '0.9em';
  }
    hint.style.color = '#666';


    container.appendChild(label);
  function start() { initOutputs(); initBoxes(); renderOutputs(); }
    container.appendChild(hint);


    // 读取并校验 x
   if ( document.readyState === 'loading' ) {
    function getX() {
     document.addEventListener( 'DOMContentLoaded', start );
      var raw = input.value.trim();
   } else { start(); }
      if (raw === '' || !/^\d+$/.test(raw)) {
} )();
        return 0; // 非法输入回退为 0
      }
      return parseInt(raw, 10);
    }
 
    // 更新所有值
    function refresh() {
      var x = getX();
      // 如果输入非法,把输入框也重置为 0
      if (input.value.trim() !== '' && !/^\d+$/.test(input.value.trim())) {
        input.value = '0';
        x = 0;
      }
      updateAllValues(x);
    }
 
    input.addEventListener('input', refresh);
 
    // 页面加载时立即计算一次(默认 x=0)
    refresh();
  }
 
  // DOM 就绪后执行
   if (document.readyState === 'loading') {
     document.addEventListener('DOMContentLoaded', initEndlessGoldMine);
   } else {
    initEndlessGoldMine();
  }
})();

2026年10月13日 (二) 07:35的版本

/* 这里的任何JavaScript将为所有用户在每次页面加载时加载。 */
/* 灰白里程有尽矿井计算器 */
( function () {
  'use strict';
  var state = { x: 0 };

  function toJS( raw ) {
    var s = String( raw ).trim();
    s = s.replace( /×/g, '*' ).replace( /÷/g, '/' )
         .replace( /(/g, '(' ).replace( /)/g, ')' )
         .replace( /[-—–−]/g, '-' ).replace( /\s+/g, '' );
    s = s.replace( /(\d)(x)/g, '$1*$2' ).replace( /(x)(\d)/g, '$1*$2' )
         .replace( /(\d)\(/g, '$1*(' ).replace( /\)(\d)/g, ')*$1' )
         .replace( /\)(x)/g, ')*$1' ).replace( /(x)\(/g, '$1*(' );
    s = s.replace( /⌊([^⌋]*)⌋/g, function ( m, inner ) {
      return 'Math.floor(' + inner + ')';
    } );
    return s;
  }

  function evalExpr( raw, x ) {
    try {
      var fn = new Function( 'x', 'return (' + toJS( raw ) + ');' );
      var v = fn( x );
      if ( typeof v !== 'number' || !isFinite( v ) ) return null;
      return Math.round( v );
    } catch ( e ) { return null; }
  }

  function sanitizeX( v ) {
    var n = Math.floor( Number( v ) );
    if ( !isFinite( n ) || n < 0 ) n = 0;
    return n;
  }

  function eachNode( sel, fn ) {
    Array.prototype.forEach.call( document.querySelectorAll( sel ), fn );
  }

  function initOutputs() {
    eachNode( '.calc-out', function ( el ) {
      if ( el.__expr === undefined ) el.__expr = el.textContent.trim();
    } );
  }

  function renderOutputs() {
    eachNode( '.calc-out', function ( el ) {
      if ( el.__expr === undefined ) el.__expr = el.textContent.trim();
      var v = evalExpr( el.__expr, state.x );
      if ( v !== null ) el.textContent = String( v );
    } );
  }

  function buildInput( box, xValue ) {
    if ( box.querySelector( '.calc-input' ) ) return;
    var labelText = box.textContent.trim() || '数量 x';
    box.textContent = '';
    var label = document.createElement( 'label' );
    label.className = 'calc-label';
    label.appendChild( document.createTextNode( labelText.replace( /[::]\s*$/, '' ) + ':' ) );
    var input = document.createElement( 'input' );
    input.type = 'number';
    input.min = '0';
    input.step = '1';
    input.value = String( xValue );
    input.className = 'calc-input';
    input.setAttribute( 'inputmode', 'numeric' );
    input.setAttribute( 'aria-label', labelText );
    input.addEventListener( 'input', function () {
      state.x = sanitizeX( input.value );
      renderOutputs();
    } );
    input.addEventListener( 'blur', function () {
      state.x = sanitizeX( input.value );
      input.value = String( state.x );
      renderOutputs();
    } );
    label.appendChild( input );
    box.appendChild( label );
  }

  function initBoxes() {
    eachNode( '.calc-box', function ( box ) { buildInput( box, state.x ); } );
  }

  function start() { initOutputs(); initBoxes(); renderOutputs(); }

  if ( document.readyState === 'loading' ) {
    document.addEventListener( 'DOMContentLoaded', start );
  } else { start(); }
} )();