Created
July 15, 2015 07:29
-
-
Save anonymous/072d23c5e213872460fb to your computer and use it in GitHub Desktop.
A game based on word scramble , the idea is to find out what the scrambled word is.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name DuoJumbles | |
// @namespace http://your.homepage/ | |
// @version 1.0a | |
// @description A game based on word scramble , the idea is to find out what the scrambled word is. | |
// @author Dessamator | |
// @include https://www.duolingo.com/* | |
// @grant none | |
// ==/UserScript== | |
//Add code to leave double words alone or "au secours".gmatch(/(\S+)\s+(\S+)/) | |
function inject(f) { //Inject the script into the document | |
var script = document.createElement('script'); | |
script.type = 'text/javascript'; | |
script.setAttribute('name', 'DuoJumbles'); | |
script.textContent = '(' + f.toString() + ')(jQuery)'; | |
document.head.appendChild(script); | |
} | |
inject(f); | |
function f($) { | |
var bLoading = false; | |
var bNewGame = false; | |
var mSndRight = new Audio("https://s3.amazonaws.com/duolingo-data/s3/sounds/right_answer.mp3"); | |
var mSndWrong = new Audio("https://s3.amazonaws.com/duolingo-data/s3/sounds/wrong_answer.mp3"); | |
var arrWords ; | |
var arrWordsOriginal ; | |
var iMaxWordsRound =2; | |
var iMaxWordsVal = 6; | |
$(document).ajaxComplete(function () { | |
try{ | |
if (!$('#app').hasClass('home')) { | |
bLoading = false; | |
} | |
main(); | |
if (!$("#DuoJumblesStore").length && $(".store-section").length ) { | |
var $StoreItem = '<div class="store-section" id="DuoJumblesStore">'+ | |
'<h5 class="gray">Games</h5><ul class="store-shelf">'+ | |
'<li><span id="jumblesowlpic2" style="float:left; background-position: -650px 0px;"'+ | |
'class="store-icon owl owl-mini"></span><a id="StoreJumblesPurchase" class="item-purchase '+ | |
'btn btn-green right" data-type="duojumbles"><span class="margin-right">Get for:</span><span'+ | |
'class="icon icon-lingot-small"></span><span class="price">1</span>'+ | |
'</a><h4>DuoJumbles</h4><p>Jumbles is a word scramble game.</p></li></ul></div>'; | |
$(".store-section").parent().prepend($StoreItem); | |
$("#StoreJumblesPurchase").on("click", function() { | |
if (duo.user.get("rupees")>1 && $("#jumbles_game").length){ | |
var $PiperPayment = payPiper(1,true); | |
$PiperPayment.then(function(data){ | |
$("#num_lingots").text($("#num_lingots").text()-1); | |
$("#num_lingots").text( duo.user.get("rupees")); | |
$('#btnjumblesgame').removeAttr("disabled"); | |
$("#StoreJumblesPurchase").attr("disabled",""); | |
$("#jumbles_game").show(); | |
},function(data){ | |
//Piper not paid | |
$("#BotMsg").html("Error. Something went wrong while paying."); | |
return ; | |
}); | |
} else if (!$("#jumbles_game").length){ | |
alert("Please refresh the page before playing (you may have switched languages)."); | |
} | |
else{ | |
alert("You do not have enough $£€¥."); | |
} | |
}); | |
} | |
} | |
catch(e){ | |
console.log(e); | |
} | |
}); | |
function main(){ | |
if ($('#app').hasClass('home') && (!$("#jumbles_game").length ) && !bLoading) { | |
var secondsSince = 0; | |
var startCounterVar; | |
var iTry =3; | |
var bTimerOn=false; | |
var sVer = "1"; | |
var arrGameWords ={currword:"",scrambledword:""}; | |
var arrGameStatus; | |
var $vocabAJax =$.ajax({dataType: "json", url: "https://www.duolingo.com/vocabulary/overview"}); | |
bLoading = true; | |
bNewGame = false; | |
if (!$("#jumbles_game").length ) { | |
$("section.sidebar-left > div.inner") | |
.prepend('<div id="jumbles_game" class="box-colored bg-white" style="display:none">'+ | |
'<span id="jumblesowlpic" style="float:right" class="owl owl-mini"></span>'+ | |
'<h2><span data-toggle="tooltip" title="" class="right" data-original-title="Close"'+ | |
'id="jumbles_close">x</span>Jumbles</h2><div id="newjumbledword"></div><div id="BotMsg"></div>'+ | |
'<a id="btnjumblesgame" href="javascript:;" class="btn btn-primary ">New game</a>'+ | |
'<textarea type="text" name="jumblesinputbox" value="" style="display:none ; overflow: hidden;'+ | |
'word-wrap: break-word; resize: none; height: 30px;" class="textarea-block" id="jumblesinput"></textarea>'+ | |
'<a id="btncheckanswer" href="javascript:;" class="btn btn-primary" style="display:none">Check</span></div>'); | |
} | |
$('#btnjumblesgame').on("click", function(){ | |
setOwl("sporty"); | |
$('#btnjumblesgame').hide(); | |
$("#jumblesinput").val(""); | |
$("#jumblesinput").show(); | |
bLoading = true; | |
main(); | |
arrGameStatus = {iScore:0,iWordCount:0,iMaxWords:iMaxWordsVal,iTries:iTry,iRound:1,iTotRounds:3}; | |
$("#newjumbledword").html("Loading words ..."); | |
if (!arrWords) { | |
$vocabAJax.then(function(data){ | |
arrWordsOriginal = data.vocab_overview; | |
arrWords = arrWordsOriginal.slice(); | |
if (arrWords.length>14){ | |
arrGameStatus.iTotRounds = 3; | |
} | |
arrGameWords=startGame(arrGameWords,arrGameStatus); | |
$("#btncheckanswer").show(); | |
}); | |
} | |
else{ | |
arrWords = arrWordsOriginal.slice(); | |
if (arrWords.length>14){ | |
arrGameStatus.iTotRounds = 3; | |
} | |
arrGameWords=startGame(arrGameWords,arrGameStatus); | |
$("#btncheckanswer").show(); | |
} | |
$("#jumblesinput").focus(); | |
$("#BotMsg").html(""); | |
}); | |
$("#jumbles_close").on("click",function(){ | |
$("#StoreJumblesPurchase").removeAttr("disabled"); | |
$("#jumbles_game").fadeOut(); | |
}); | |
$('#btncheckanswer').on("click", function(){ | |
checkAnswer(arrGameWords,arrGameStatus); | |
}); | |
$('#jumblesinput').keydown(function(e) { | |
try{ | |
if (e.which === 13) { | |
var sMsg = $('#jumblesinput').val(); | |
if (!arrGameWords || sMsg.length<1 || sMsg==="\n") { | |
$('#jumblesinput').val(""); | |
return ; | |
} | |
checkAnswer(arrGameWords,arrGameStatus); | |
event.preventDefault(); | |
return; | |
} | |
} | |
catch(error){ | |
console.log(error); | |
} | |
}); | |
} | |
} | |
function startGame(arrGameWords,arrGameStatus){ | |
sendUserMsg("",arrGameStatus); | |
return newWord(arrGameWords,arrGameStatus); | |
} | |
function setOwl (sType){ | |
if (sType=="sad"){ | |
$("#jumblesowlpic.owl").css("background-position","-80px -300px"); | |
} | |
else if (sType=="sporty"){ | |
$("#jumblesowlpic.owl").css("background-position","-650px 0"); | |
} | |
else if (sType=="winner"){ | |
$("#jumblesowlpic.owl").css("background-position","-80px -80px"); | |
} | |
} | |
function newWord(arrGameWords,arrGameStatus){ | |
if (arrGameWords.currword !=="") { | |
sendUserMsg("The last word was: "+arrGameWords.currword,arrGameStatus ); | |
} | |
arrGameWords = scramble(arrWords,arrGameWords,arrGameStatus); | |
$("#newjumbledword").html("New word is: <strong>"+ arrGameWords.scrambledword+"</strong>"); | |
return arrGameWords; | |
} | |
function checkAnswer(arrGameWords,arrGameStatus){ | |
if (arrWords && arrWords.length>0){ | |
var sMsg = $('#jumblesinput').val(); | |
if (sMsg===arrGameWords.currword ) { | |
if (arrGameStatus.iWordCount<arrGameStatus.iMaxWords){ | |
arrGameStatus.iTries=3; | |
} | |
$('#jumblesinput').val(""); | |
mSndRight.play(); | |
setOwl("sporty"); | |
arrGameStatus.iScore =arrGameStatus.iScore + 1; | |
arrGameWords = newWord(arrGameWords,arrGameStatus); | |
sendUserMsg("Right!",arrGameStatus); | |
if (arrGameStatus.iWordCount % iMaxWordsRound ===0 && | |
arrGameStatus.iScore <arrGameStatus.iMaxWords -1) { | |
arrGameStatus.iRound++; | |
} | |
}else{ | |
setOwl("sad"); | |
arrGameStatus.iTries= arrGameStatus.iTries-1; | |
if (arrGameStatus.iTries<1 && (arrGameStatus.iWordCount<arrGameStatus.iMaxWords)){ | |
arrGameStatus.iTries=3; | |
arrGameWords = newWord(arrGameWords,arrGameStatus); | |
} | |
else{ | |
sendUserMsg(arrGameStatus.iTries + " tries left.",arrGameStatus ); | |
} | |
mSndWrong.play(); | |
} | |
if (arrGameStatus.iWordCount > arrGameStatus.iMaxWords ){ | |
var iTotScore = arrGameStatus.iScore / arrGameStatus.iMaxWords ; | |
sendUserMsg("Game over!",arrGameStatus); | |
$("#jumblesinputbox").html(""); | |
$("#newjumbledword").html(""); | |
$("#jumblesinput").hide(); | |
$("#btncheckanswer").hide(); | |
$('#btnjumblesgame').attr("disabled","disabled"); | |
$('#btnjumblesgame').show(); | |
if(iTotScore== 1){ | |
setOwl("winner"); | |
sendUserMsg("Perfection, well done.",arrGameStatus); | |
}else if(iTotScore> 0.5){ | |
setOwl("winner"); | |
sendUserMsg("Not bad!",arrGameStatus); | |
} | |
bLoading =false; | |
$("#StoreJumblesPurchase").removeAttr("disabled"); | |
return arrGameWords; | |
} | |
$("#jumblesinput").val(""); | |
return arrGameWords; | |
} | |
} | |
function scramble(arrWords,arrGameWords,arrGameStatus) { | |
var x= Math.floor(Math.random() * arrWords.length); | |
var sSentence=""; | |
arrGameStatus.iWordCount += 1; | |
if (x>arrWords.length){ | |
x=0; | |
} | |
else if (arrWords.length===0){ | |
return ; | |
} | |
arrGameWords.currword =arrWords[x].word_string.toLowerCase(); | |
var arrSentenceWords = arrGameWords.currword.match(/(\S+)/g); | |
arrWords.splice(x, 1); | |
function scrambleEachWord(currword) { | |
var sText =[]; | |
var sTmp,sTmp1 =""; | |
sText = currword.split(""); | |
for (var i=0;i<sText.length;i++ ) { | |
var iRand = Math.floor((Math.random() * sText.length) +1); | |
if(sText[iRand]){ | |
sTmp = sText[i]; | |
sTmp1 = sText[iRand]; | |
sText[i] = sTmp1; | |
sText[iRand] = sTmp; | |
} | |
} | |
var oTmp = sText.toString(); | |
var scrambledword=oTmp.replace(/,/g, "").toLowerCase(); | |
return scrambledword; | |
} | |
for (var j = 0; j < arrSentenceWords.length; j ++){ | |
sSentence =sSentence+scrambleEachWord(arrSentenceWords[j]) + " "; | |
} | |
arrGameWords.scrambledword = sSentence; | |
return arrGameWords; | |
} | |
function sendUserMsg(sMsg,arrGameStatus){ | |
var sScoreText = "Score :"+arrGameStatus.iScore +"/"+arrGameStatus.iMaxWords +"<br>"+ | |
"Round: "+ arrGameStatus.iRound +"<br>"; | |
$("#BotMsg").html(sScoreText + sMsg); | |
} | |
function payPiper(iLingots,bDebug){ | |
var URL = "https://www.duolingo.com/comments/1059877/love"; | |
var $PiperPayment =$.ajax({dataType: "json",type:"post", url: URL}); | |
return $PiperPayment; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment