Benutzer:Formatierer/js/GameWR.js

Aus Wiktionary, dem freien Wörterbuch
// Game Wörterraten
(function( _public, $, undefined ) {


var WRmode=0;
var WRwortsoll="";
var WRwortist="";
var WRcount=7;
var WRreload=10;
var WRscore=0;
var WRwords=0;


_public.WRhandleResponse = function(data) {
  var t=WRextract(data,/"wgTitle"\:"(.*?)"/);

  $.get(mw.config.get("wgServer")+mw.config.get("wgScript")
      +"?title="+mw.util.wikiUrlencode(t)+"&action=raw",
      GameWR.WRhandleResponse2);
}


_public.WRhandleResponse2 = function(data) {
  var t=WRextract(data,/==(.*)\(\{\{/);
  WRwortsoll=$.trim(t.toUpperCase());
  if(!/^[A-ZÄÖÜ]*$/.exec(WRwortsoll)) {
   _public.WRselect();
   return;
  }

  var txt=WRextract(data,/\{\{Bedeutungen\}\}([\S\s]*?)\n\{\{/);
  var a=txt.split(/\n:[^:]/);
  if(a.length < 2) {
   WRselect();
   return;
  }
  var l=Math.floor(Math.random()*(a.length-1))+1;
  if (WRmode==0) {
    WRcreateButtons("QWERTZUIOPÜ","WR1");
    WRcreateButtons("ASDFGHJKLÖÄ","WR2");
    WRcreateButtons("YXCVBNM","WR3");
    WRcreateButtons("?","WR4");
  }
  WRmode=1;
  WRreload=10;
  WRcount=7;
  WRwords++;
  WRwortist=WRwortsoll.replace(/./g,'-');
  WRset("WRwort",WRwortist);
  WRset("WRwas",'['+a[l]);
  WRset("WRstatus","Erlaubte Fehler: "+(WRcount-1));
  WRset("WRpunkte",WRscore.toString());
  WRset("WRbegriffe",WRwords.toString());
}


function WRextract(t,e) {
  var a=e.exec(t);
  if (a && a.length > 1) {
    return a[1];
  }
  return '';
}


function WRset(wo,was){
  document.getElementById(wo).textContent=was;
}


function WRcreateButtons(k,wo){
  for (var i=0;i<k.length;i++) {
    WRcreateButton(k[i],wo);
  }
}


function WRcreateButton(chr,wo){
  var node = document.getElementById(wo);
  if (node) {

    var y = document.createElement("input");

    y.setAttribute("value", chr);
    y.setAttribute("title", "press");
    y.setAttribute("type", "button");
    y.setAttribute("onclick", "GameWR.WRputchar('"+chr+"')");

    node.appendChild(y);

  }
}


function WRcreateSelect(wo){
  var node = document.getElementById(wo);
  if (node) {

    var y = document.createElement("label");

    var opt = "Vorschläge für Kategorien: Wortart: <select id='WRart'>";
    opt += "<option>Adjektiv</option>";
    opt += "<option>Adverb</option>";
    opt += "<option>Interjektion</option>";
    opt += "<option selected>Substantiv</option>";
    opt += "<option>Toponym</option>";
    opt += "<option>Verb</option>";
    opt += "</select>";
    y.innerHTML = opt;
    node.appendChild(y);

    var o = document.getElementById("WRart");
    o.setAttribute("onchange", "GameWR.WRputkat('WRart')");

  }
}


function WRcreateSelectSpr(wo){
  var node = document.getElementById(wo);
  if (node) {

    var y = document.createElement("label");

    var opt = " Sprache: <select id='WRspr'>";
    opt += "<option selected>Deutsch</option>";
    opt += "<option>Englisch</option>";
    opt += "<option>Französisch</option>";
    opt += "<option>Italienisch</option>";
    opt += "<option>Katalanisch</option>";
    opt += "<option>Latein</option>";
    opt += "<option>Polnisch</option>";
    opt += "<option>Portugiesisch</option>";
    opt += "<option>Schwedisch</option>";
    opt += "<option>Tschechisch</option>";
    opt += "</select>";
    y.innerHTML = opt;
    node.appendChild(y);

    var o = document.getElementById("WRspr");
    o.setAttribute("onchange", "GameWR.WRputkat('WRspr')");
  }
}


_public.WRputchar = function(chr) {
  if (WRmode!=1) {
    return;
  }
  if (chr=='?') {
   WRcount=0;
  }
  if (WRcount<=0) {
   WRset("WRstatus"," Aufgegeben! Dafür wird dir ein Punkt abgezogen.");
   WRset("WRwort",WRwortsoll);
   WRscore--;
   WRset("WRpunkte",WRscore);
   WRmode=2;
   return;
  }
  var i,neu="";
  var found=false;
  for (i=0;i<WRwortsoll.length;i++){
   if(WRwortsoll[i]==chr){
    found=true;
    neu+=chr;
   } else {
    neu+=WRwortist[i];
   }
  }
  WRwortist=neu;
  WRset("WRwort",WRwortist);
  if (WRwortist==WRwortsoll){
   WRset("WRstatus"," Richtig, du erhältst dafür " + WRcount + " Punkt" + ((WRcount == 1) ? "." : "e."));
   WRscore+=WRcount;
   WRset("WRpunkte",WRscore);
   WRmode=3;
   return;
  }
  if (!found) {WRcount--;}
  if(WRcount<=0){
   WRset("WRstatus"," Verloren! Dafür erhältst du keine Punkte.");
   WRset("WRwort",WRwortsoll);
   WRmode=2;
  } else {
   WRset("WRstatus","Erlaubte Fehler: "+(WRcount-1));
  }
}


_public.WRputkat = function(id) {
  var o = document.getElementById(id).value;
  var pf = document.getElementById("morfegap").value;
  var a = pf.split("(");
  if (id == "WRart") {
    a[0] = o + " ";
  } else {
    a[1] = o + ")";
  }
  document.getElementById("morfegap").value = a.join("(");
}


_public.WRselect = function () {
  WRset("WRwas","");
  WRset("WRwort","");
  WRset("WRstatus","");
  WRreload--;

  if(WRmode == 1){
   WRscore--;
  }
  if(WRreload < 0){
    WRset("WRstatus","Kein passendes Wort gefunden");
    WRreload=10;
  } else {
    var pf=document.getElementById("morfegap");
    $.get(mw.config.get("wgServer")+mw.config.get("wgScript")
      +"?title="+mw.util.wikiUrlencode("Spezial:RandomInCategory/"+pf.value)
      ,
      GameWR.WRhandleResponse);
  }
}


_public.WRnewWord = function () {
  var form,node;

  form = newNode(
    'form',
    {'class':'noprint'},
    newNode('label','Zufälliges Wort aus der Kategorie ',
    newNode('input',{'name':'pagefrom','id':'morfegap','size':'35'})),
    newNode('text',' '),
    newNode('input',{'type':'button','value':'Neues Wort'})
  );

  WRcreateSelect("WRwahl");
  WRcreateSelectSpr("WRwahl");

  form.lastChild.onclick=GameWR.WRselect;
  node=document.getElementById("WRwahl");
  node.parentNode.insertBefore(form,node);
  document.getElementById("morfegap").value="Substantiv (Deutsch)";
}

}( window.GameWR = window.GameWR || {}, jQuery ));


if (mw.config.get("wgArticleId") == 432917) {
  jQuery(document).ready(GameWR.WRnewWord);
}