User:Matthew Yeager/renderboard.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.

importScript('User:AzaToth/morebits.js');
 
if(typeof(RenderBoard) == 'undefined'){RenderBoard= function(){};}
 
function brender() {
  addPortletLink( 'p-personal', "javascript:brender.generate()", "Render Game Board", "", "Render Game Board", "");
}
addOnloadHook(brender);
 
brender.generate = function brendergenerate() {
  var Window = new SimpleWindow(350,500);
  Window.setTitle("Render Game Board");
  var form = new QuickForm(brender.generate.code);
  form.append({
      type:'select',
      name:'seltype',
      label:'Game Selection: ',
      align:'left',
      event: brender.generate.logic,
      list:[{
	  label:'Chess',
	  list:[
            {label:'5x5 Blank (22x22px)', value:'10'},
            {label:'5x5x5 Raumschach (22x22px)', value:'7'},
            {label:'6x6 Blank (22x22px)', value:'11'},
	    {label:'8x8 Standard (26x26px)', value:'1'},
	    {label:'8x8 Standard Small (22x22px)', value:'2'},
	    {label:'8x8 Chaturanga (uncheckered)(26x26px)', value:'4'},
	    {label:'8x8 Mutation (colored squares) (26x26px)', value:'5'},
	    {label:'8x8x2 Bughouse (22x22px)', value:'8'},
            {label:'8x8x2 Alice Chess (22x22px)', value:'9'},
            {label:'8x10 Casablanca (10 columns) (26x26px)', value:'3' },
	    {label:'10x10 Omega (22x22px)', value:'6'},
            {label:'Please Choose A Game', selected:true, value:'-1'}
	  ]
      }]
  });
 
  var specs = form.append({type: 'field',label: 'Board Specifications'});
  specs.append({
      type: 'input',
      name: 'txtalign',
      label: 'Alignment:  ',
      tooltip: 'Default is empty, but may be tright or tleft.'
  });
  specs.append({
      type: 'input',
      name: 'txtheader',
      label: 'Heading: ',
      tooltip: 'The title of this image.'
  });
  specs.append({
      type: 'input',
      name: 'txtfooter',
      label: 'Footer: ',
      tooltip: 'Text that appears below image.'
  });
  specs.append({
      type: 'input',
      name: 'txtFENparam',
      label: 'FEN Parameters: ',
      tooltip: 'Only applicable to certain templates.'
  });
  specs.append({
      type: 'input',
      name: 'txtBughouse',
      label: 'Bughouse Boards : ',
      tooltip: '(Example: \'Team 1, Board A|Team 2, Board A|Team 1, Board B |Team 2, Board B\')'
  });
 
  var inputsec = form.append({type: 'field',label: 'Input'});
  inputsec.append({
      type:'checkbox',
      list:[{
          name: 'chkstnd',
          label: 'Render Starting Position',
          tooltip: 'Will create code with pieces set up in starting position.',
        }
      ]
  });
  inputsec.append({
      type: 'input',
      name: 'txtposition',
      label: 'Position String',
      tooltip: 'This would be black\'s starting position: \'rd|nd|bd|qd|kd|bd|nd|rd|pd|pd|pd|pd|pd|pd|pd|pd\''
  });
  inputsec.append({
      type: 'input',
      name: 'txtFEN',
      label: 'FEN String',
      tooltip: 'This would be starting position: \'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR\''
  });
 
  var resultdisplay = form.append({type: 'field',label: 'Rendered Code'});
  resultdisplay.append({type: 'textarea',name: 'txtresult',label: 'Result: '});
 
  form.append({type:'submit',name: 'Render Code',event: brender.generate.code});
 
  var result = form.render();
  Window.setContent(result);
  Window.display();
}
 
brender.generate.logic = function brendergeneratelogic(e){
  var form = e.target.form;
 
  if(form.seltype.value < 7 || form.seltype.value > 9){form.txtFEN.disabled = false;}
  else{form.txtFEN.disabled = true;}
 
  if(form.seltype.value == 1 || form.seltype.value == 5){form.txtFENparam.disabled = false;}
  else{form.txtFENparam.disabled = true;}
 
  if(form.seltype.value == 8){form.txtBughouse.disabled = false;}
  else{form.txtBughouse.disabled = true;}
}
 
brender.generate.code = function brendergeneratecode(e){
  var form = e.target;
  if(form.seltype.value < 0){
    alert('Please Select A Game Type');
    return;
  }
  var InputLogic = 'Please note that code will be determined in this order:\n\t1. Starting Position Checkbox (Overrides other inputs)';
  InputLogic += '\n\t2. Position String (Overrides FEN String)\n\t3. FEN String';
  alert(InputLogic);
  var RenderedBoard = 'MYEA';
  var params = {
    type: form.seltype.value,
    align: form.txtalign.value,
    header: form.txtheader.value,
    startpos: form.chkstnd.checked,
    footer: form.txtfooter.value,
    FENparam: form.txtFENparam.value,
    position: form.txtposition.value,
    FEN: form.txtFEN.value,
    bughouse: form.txtBughouse.value
  }
 
  var boardsize = 64;var modulus = 8;var supermod = 99999;
 
  if(params.type == 1){
    RenderedBoard = '{{User:Matthew Yeager/Chess Render FEN|=\n';
  }else if(params.type == 2){
    RenderedBoard = '{{Template:Chess diagram small|=\n';
  }else if(params.type == 3){
    RenderedBoard = '{{Template:Chess diagram 8x10|=\n';
    boardsize = 80;modulus = 10;
  }else if(params.type == 4){
    RenderedBoard = '{{Template:Chaturanga diagram|=\n';
  }else if(params.type == 5){
    RenderedBoard = '{{Template:Chess mutation diagram|=\n';
  }else if(params.type == 6){
    RenderedBoard = '{{Template:Omega Chess diagram|=\n';
    boardsize = 100;modulus = 10;
  }else if(params.type == 7){
    RenderedBoard = '{{Raumschach diagram|=\n';
    boardsize = 125;modulus = 5;supermod = 25;
  }else if(params.type == 8){
    RenderedBoard = '{{bughouse chess diagram|=\n';
    var avbugteams = params.bughouse.split('|');
    for(var i = 0;i<4;i++){
      if(i == 0){RenderedBoard += '|A1=';}
      else if(i == 1){RenderedBoard += '|A2=';}
      else if(i == 2){RenderedBoard += '|B1=';}
      else{RenderedBoard += '|B2=';}
 
      if(i <= avbugteams.length){RenderedBoard += avbugteams[i] + '\n';}
      else{RenderedBoard += '\n';}
    }      
    boardsize *= 2;supermod = 64;
  }else if(params.type == 9){
    RenderedBoard = '{{alice chess diagram|=\n';
    boardsize *= 2;supermod = 64;
  }else if(params.type == 10){
    RenderedBoard = '{{Chess diagram 5x5|=\n';
    boardsize = 25;modulus = 5;
  }else if(params.type == 11){
    RenderedBoard = '{{Chess diagram 6x6|=\n';
    boardsize = 36;modulus = 6;
  }
  RenderedBoard += '|' + params.align + '\n';
  RenderedBoard += '|' + params.header + '\n';
  RenderedBoard += '|=\n';
 
  if(params.type == 6){RenderedBoard += '|wd|=|wd|=\n';}
 
  if(params.startpos == true){
    if(params.type == 1 || params.type == 2){
      RenderedBoard += '|rd|nd|bd|qd|kd|bd|nd|rd|=\n';
      RenderedBoard += '|pd|pd|pd|pd|pd|pd|pd|pd|=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|pl|pl|pl|pl|pl|pl|pl|pl|=\n';
      RenderedBoard += '|rl|nl|bl|ql|kl|bl|nl|rl|=\n';
    }else if(params.type == 3){
      RenderedBoard += '|rd|nd|ad|bd|qd|kd|bd|cd|nd|rd|=\n';
      RenderedBoard += '|pd|pd|pd|pd|pd|pd|pd|pd|pd|pd|=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|pl|pl|pl|pl|pl|pl|pl|pl|pl|pl|=\n';
      RenderedBoard += '|rl|nl|al|bl|ql|kl|bl|cl|nl|rl|=\n';
   }else if(params.type == 4){
      RenderedBoard += '|rd|nd|bd|qd|kd|bd|nd|rd|=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|pd|pd|pd|pd|pd|pd|pd|pd|=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|pl|pl|pl|pl|pl|pl|pl|pl|=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|rl|nl|bl|ql|kl|bl|nl|rl|=\n';
    }else if(params.type == 5){
      RenderedBoard += '|pd|pd|pd|pd|kd|pd|pd|pd|=\n';
      RenderedBoard += '|pd|pd|pd|pd|pd|pd|pd|pd|=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|pl|pl|pl|pl|pl|pl|pl|pl|=\n';
      RenderedBoard += '|pl|pl|pl|pl|kl|pl|pl|pl|=\n';
    }else if(params.type == 6){
      RenderedBoard += '|zd|rd|nd|bd|qd|kd|bd|nd|rd|zd|=\n';
      RenderedBoard += '|pd|pd|pd|pd|pd|pd|pd|pd|pd|pd|=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|pl|pl|pl|pl|pl|pl|pl|pl|pl|pl|=\n';
      RenderedBoard += '|zl|rl|nl|bl|ql|kl|bl|nl|rl|zl|=\n';
    }else if(params.type == 7){
      RenderedBoard += '|rd|nd|kd|nd|rd|=\n';
      RenderedBoard += '|pd|pd|pd|pd|pd|=\n';
      RenderedBoard += '|  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |=\n\n';
      RenderedBoard += '|bd|hd|qd|bd|hd|=\n';
      RenderedBoard += '|pd|pd|pd|pd|pd|=\n';
      RenderedBoard += '|  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |=\n\n';
      RenderedBoard += '|  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |=\n\n';
      RenderedBoard += '|  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |=\n';
      RenderedBoard += '|pl|pl|pl|pl|pl|=\n';
      RenderedBoard += '|bl|hl|ql|bl|hl|=\n\n';
      RenderedBoard += '|  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |=\n';
      RenderedBoard += '|pl|pl|pl|pl|pl|=\n';
      RenderedBoard += '|rl|nl|kl|nl|rl|=\n';
    }else if(params.type == 8){
      RenderedBoard += '|rd|nd|bd|qd|kd|bd|nd|rd|=\n';
      RenderedBoard += '|pd|pd|pd|pd|pd|pd|pd|pd|=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|pl|pl|pl|pl|pl|pl|pl|pl|=\n';
      RenderedBoard += '|rl|nl|bl|ql|kl|bl|nl|rl|=\n\n';
      RenderedBoard += '|rl|nl|bl|kl|ql|bl|nl|rl|=\n';
      RenderedBoard += '|pl|pl|pl|pl|pl|pl|pl|pl|=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|pd|pd|pd|pd|pd|pd|pd|pd|=\n';
      RenderedBoard += '|rd|nd|bd|kd|qd|bd|nd|rd|=\n';
    }else if(params.type == 9){
      RenderedBoard += '|rd|nd|bd|qd|kd|bd|nd|rd|=\n';
      RenderedBoard += '|pd|pd|pd|pd|pd|pd|pd|pd|=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|pl|pl|pl|pl|pl|pl|pl|pl|=\n';
      RenderedBoard += '|rl|nl|bl|ql|kl|bl|nl|rl|=\n\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |  |  |=\n';
    }else if(params.type == 10){
      RenderedBoard += '|  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |=\n';
    }else if(params.type == 11){
      RenderedBoard += '|  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |=\n';
      RenderedBoard += '|  |  |  |  |  |  |=\n';
    }
  }else if(params.position != ''){
    var avPosition = params.position.split('|');
    for(var i = 1;i <= boardsize;i++){
      if(i <= avPosition.length){RenderedBoard += '|' + avPosition[i-1];}
      else{RenderedBoard += '|  ';}
 
      if(i != 0 && i % modulus == 0){
        RenderedBoard += '|=\n';
        if(i % supermod == 0){RenderedBoard += '\n';}
      }
    }
  }else if(params.FEN != ''){
    var FEN = params.FEN;
    for(var i = 0;i <= FEN.length;i++){
      if(FEN[i] == '/'){RenderedBoard += '|=\n';}
      else{
        if(FEN[i] == '/'){RenderedBoard += '|=\n';}
        else if(FEN[i] == 'r'){RenderedBoard += '|rd';}
        else if(FEN[i] == 'n'){RenderedBoard += '|nd';}
        else if(FEN[i] == 'b'){RenderedBoard += '|bd';}
        else if(FEN[i] == 'q'){RenderedBoard += '|qd';}
        else if(FEN[i] == 'k'){RenderedBoard += '|kd';}
        else if(FEN[i] == 'p'){RenderedBoard += '|pd';}
        else if(FEN[i] == 'a'){RenderedBoard += '|ad';}
        else if(FEN[i] == 'c'){RenderedBoard += '|cd';}
        else if(FEN[i] == 'z'){RenderedBoard += '|zd';}
        else if(FEN[i] == 'g'){RenderedBoard += '|gd';}
 
        else if(FEN[i] == 'G'){RenderedBoard += '|gl';}
        else if(FEN[i] == 'Z'){RenderedBoard += '|zl';}
        else if(FEN[i] == 'C'){RenderedBoard += '|cl';}
        else if(FEN[i] == 'A'){RenderedBoard += '|al';}
        else if(FEN[i] == 'P'){RenderedBoard += '|pl';}
        else if(FEN[i] == 'K'){RenderedBoard += '|kl';}
        else if(FEN[i] == 'Q'){RenderedBoard += '|ql';}
        else if(FEN[i] == 'B'){RenderedBoard += '|bl';}
        else if(FEN[i] == 'N'){RenderedBoard += '|nl';}
        else if(FEN[i] == 'R'){RenderedBoard += '|rl';}
        else{for(var s = 0;s < FEN[i];s++){RenderedBoard += '|  ';}}
      }
    }
  }else{
    for(var i = 1;i <= boardsize;i++){
      RenderedBoard += '|  ';
      if(i != 0 && i % modulus == 0){
        RenderedBoard += '|=\n';
        if(i % supermod == 0){RenderedBoard += '\n';}
      }
    }
  }
 
  if(params.type == 6){RenderedBoard += '|wl|=|wl|=\n';}
  RenderedBoard += '|' + params.footer;
  if(params.type == 1 || params.type == 5){RenderedBoard += '\n|FEN=' + params.FENparam;}
  RenderedBoard += '}}\n';
 
  RenderedBoard += '<!--Code Generated From RenderBoard-->\n';
  RenderedBoard += '<!--Any problems please contact http://en.wikipedia.org/wiki/User_talk:Matthew_Yeager-->\n';
 
  form.txtFENparam.value = '';
  form.txtFEN.value = '';
  form.txtBughouse.value = '';
 
  form.txtresult.value = RenderedBoard;
}