Last active
January 31, 2017 19:50
-
-
Save alirezamirian/4dea44da5d3c647ee108b16be1637fc3 to your computer and use it in GitHub Desktop.
Creates an object for automatically responding to your opponent's guesses in 4-harfi game using Airdroid
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
/** | |
* Creates an object for automatically responding to your opponent's guesses in 4-harfi game using Airdroid. | |
* If you don't have an idea about what 4-harfi game is that's absolutely normal! However if you don't know Airdroid, | |
* you are missing a really useful android app. | |
* | |
* @param word your guess | |
* @param prevGuess previously guessed word, use it if you want to start automation while already started game. | |
* @returns {{stop: stop, start: start}} | |
* start: starts auto responding | |
* stop: stops auto responding | |
* @constructor | |
*/ | |
function FourLetterGameAutoResponder(word, prevGuess){ | |
prevGuess = prevGuess || []; | |
if(typeof prevGuess === 'string'){ | |
prevGuess = prevGuess.split(''); | |
} | |
var timer; | |
var thread = $('.mod-messageList-chat-item.active'); | |
function processLastSms(){ | |
var lastGuess = thread.find('.mod-multiChat-smsItemType1:last .mod-multiChat-smsItemBody').text().trim().split(""); | |
if(lastGuess.length === 4 && prevGuess.join('') != lastGuess.join('')){ | |
prevGuess = lastGuess; | |
var score = lastGuess.reduce(function(result, letter, index){ | |
return [result[0] + (word.indexOf(letter) >= 0 ? 1 : 0), result[1] + (word[index] === letter ? 1 : 0)]; | |
}, [0, 0]); | |
console.log(`score for ${lastGuess.join('')} is: ${score}`); | |
var responseText = score.map(toPersianDigit).join(' '); | |
if(score[0] === 4 && score[1] === 4){ | |
responseText += "\n آفرییییین"; | |
} | |
thread.find('.mod-chat-sendContent').text(responseText); | |
thread.find('.mod-chat-sendBtn').removeAttr('disabled').click(); | |
} | |
} | |
function toPersianDigit(digitChar){ return String.fromCharCode('۰'.charCodeAt(0) + parseInt(digitChar))} | |
return { | |
stop: function(){ | |
clearInterval(timer); | |
}, | |
start: function(){ | |
timer = setInterval(processLastSms, 200); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment