Skip to content

Commit

Permalink
v2.2.5 - Fix Chessarena.com
Browse files Browse the repository at this point in the history
  • Loading branch information
Hakorr committed Jan 5, 2025
1 parent 4dfc825 commit 474220b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 28 deletions.
82 changes: 55 additions & 27 deletions acas.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
// @require https://greasyfork.org/scripts/470418-commlink-js/code/CommLinkjs.js
// @require https://greasyfork.org/scripts/470417-universalboarddrawer-js/code/UniversalBoardDrawerjs.js
// @icon https://raw.githubusercontent.com/Psyyke/A.C.A.S/main/assets/images/grey-logo.png
// @version 2.2.4
// @version 2.2.5
// @namespace HKR
// @author HKR
// @license GPL-3.0
Expand Down Expand Up @@ -914,14 +914,25 @@ function getPieceAmount() {

class AutomaticMove {
constructor(profile, fenMoveArr, isLegit, callback) {
this.id = getUniqueID();

// activeAutomoves is an external variable, not a child of AutomaticMove
activeAutomoves.push({ 'id': this.id, 'move': this });

this.profile = profile;
this.fenMoveArr = fenMoveArr;
this.isLegit = isLegit;

this.active = true;
this.isPromotingPawn = false;

this.onFinished = callback;
this.onFinished = function(...args) {
activeAutomoves.filter(x => x.id !== this.id); // remove the move from the active automove list

this.active = false;

callback(...args);
};

this.moveDomCoords = fenCoordArrToDomCoord(fenMoveArr);
this.isPromotion = isPawnPromotion(fenMoveArr);
Expand Down Expand Up @@ -1104,20 +1115,29 @@ class AutomaticMove {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

getRandomIntegerNearAverage(min, max) {
const mid = (min + max) / 2;
const range = (max - min) / 2;

let value = Math.floor(mid + (Math.random() - 0.5) * range * 1.5);

return Math.max(min, Math.min(max, value));
}

delay(ms) {
return this.active ? new Promise(resolve => setTimeout(resolve, ms)) : true;
}

triggerPieceClick(input, attemptNum = 0) {
async triggerPieceClick(input) {
const parentExists = activeAutomoves.find(x => x.move === this) ? true : false;

if(!parentExists && attemptNum > 3) {
if(!parentExists) {
return;
}

let clientX, clientY;

if (input instanceof Element) {
if(input instanceof Element) {
const rect = input.getBoundingClientRect();
clientX = rect.left + rect.width / 2;
clientY = rect.top + rect.height / 2;
Expand Down Expand Up @@ -1153,11 +1173,25 @@ class AutomaticMove {
switch(domain) {
case 'chess.com':
elementToTrigger.dispatchEvent(new PointerEvent('pointerdown', pointerEventOptions));

if(this.isLegit) await this.delay(this.getRandomIntegerNearAverage(35, 125));

elementToTrigger.dispatchEvent(new PointerEvent('pointerup', pointerEventOptions));

break;
case 'lichess.org':
elementToTrigger.dispatchEvent(new MouseEvent('mousedown', pointerEventOptions));

if(this.isLegit) await this.delay(this.getRandomIntegerNearAverage(35, 125));

elementToTrigger.dispatchEvent(new MouseEvent('mouseup', pointerEventOptions));

break;
case 'chessarena.com':
elementToTrigger.dispatchEvent(new MouseEvent('mousedown', pointerEventOptions));

if(this.isLegit) await this.delay(this.getRandomIntegerNearAverage(35, 125));

elementToTrigger.dispatchEvent(new MouseEvent('mouseup', pointerEventOptions));

break;
Expand Down Expand Up @@ -1256,7 +1290,7 @@ class AutomaticMove {
if(this.isLegit) {
this.playLegit();
} else {
this.finishMove(5, 5);
this.finishMove(5, 1111);
}
}

Expand All @@ -1266,22 +1300,16 @@ class AutomaticMove {
this.click(this.moveDomCoords[1]);
}

this.active = false;

this.onFinished(false);
}
}

async function makeMove(profile, fenMoveArr, isLegit) {
const id = getUniqueID();

const move = new AutomaticMove(profile, fenMoveArr, isLegit, () => {
const move = new AutomaticMove(profile, fenMoveArr, isLegit, e => {
// This is ran when the move finished

activeAutomoves.filter(x => x.id !== id); // remove the move from the active automove list
if(debugModeActivated) console.warn('Move', fenMoveArr, move.id, 'finished', 'for profile:', profile);
});

activeAutomoves.push({ id, move });
}

function getGmConfigValue(key, instanceID, profileID) {
Expand Down Expand Up @@ -1468,8 +1496,6 @@ function getSiteData(dataType, obj) {

const result = dataHandlerFunction(dataObj);

//if(debugModeActivated) console.warn('GET_SITE_DATA', '| DATA_TYPE:', dataType, '| INPUT_OBJ:', obj, '| DATA_OBJ:', dataObj, '| RESULT:', result);

return result;
}

Expand Down Expand Up @@ -1561,7 +1587,7 @@ function getBoardMatrix() {
const pieceFenCode = getPieceElemFen(pieceElem);
const pieceCoordsArr = getPieceElemCoords(pieceElem);

if(debugModeActivated) console.warn('pieceElem', pieceElem, 'pieceFenCode', pieceFenCode, 'pieceCoordsArr', pieceCoordsArr);
//if(debugModeActivated) console.warn('pieceElem', pieceElem, 'pieceFenCode', pieceFenCode, 'pieceCoordsArr', pieceCoordsArr);

try {
const [xIdx, yIdx] = pieceCoordsArr;
Expand Down Expand Up @@ -1704,22 +1730,24 @@ function onNewMove(mutationArr, bypassFenChangeDetection) {
}

function observeNewMoves() {
let lastProcessedFen = null;

const boardObserver = new MutationObserver(mutationArr => {
if(debugModeActivated) console.log(mutationArr);

if(isMutationNewMove(mutationArr))
{
if(debugModeActivated) console.warn('Mutation is a new move:', mutationArr);

if(domain === 'chess.org')
{
setTimeout(() => onNewMove(mutationArr), 250);
}
else
{
onNewMove(mutationArr);
try {
if(domain === 'chess.org' || domain === 'chessarena.com')
{
setTimeout(() => onNewMove(mutationArr), 250);
}
else
{
onNewMove(mutationArr);
}
} catch(e) {
if(debugModeActivated) console.error('Error while running onNewMove:', e);
}
}
});
Expand Down
13 changes: 12 additions & 1 deletion assets/js/acas-backend-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class BackendInstance {
this.environmentSetupRun = false;

this.lastOrientation = null;
this.lastTurn = null;

this.activeEnginesAmount = 0;
this.guiUpdaterActive = false;
Expand Down Expand Up @@ -588,7 +589,17 @@ class BackendInstance {
const playerColor = this.getPlayerColor(profile);
const turn = this.getTurnFromFenChange(lastFen, currentFen, profile);

console.error('Turn: ', turn);
if(this.lastTurn === turn) {
console.error('For some reason the turn was the same two times in a row, forcing turn to player!');

this.lastTurn = playerColor;

return true;
} else {
console.warn('Turn: ', turn);

this.lastTurn = turn;
}

return !playerColor || turn == playerColor;
}
Expand Down
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ if(typeof USERSCRIPT == 'undefined') {

if(urlParams.get('hidden') === 'true') {
hiddenSettingPanel.classList.remove('hidden');

// Disable certain settings if no hidden param found
} else {
const autoMoveCheckbox = document.querySelector('input[data-key="autoMove"]');

if(autoMoveCheckbox?.checked) {
autoMoveCheckbox.click();
}

console.log(autoMoveCheckbox.checked);
}

const MainCommLink = new USERSCRIPT.CommLinkHandler('mum', {
Expand Down

0 comments on commit 474220b

Please sign in to comment.