Skip to content

Commit

Permalink
Merge branch 'commonjs-modules'
Browse files Browse the repository at this point in the history
  • Loading branch information
everyonesdesign committed Jul 27, 2018
2 parents c388a4a + d667c4c commit 56a6a93
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 200 deletions.
65 changes: 65 additions & 0 deletions app/src/analytics.js
@@ -0,0 +1,65 @@
/**
* Init google analytics for the app
*/
function initAnalytics() {
/* eslint-disable */
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','cchGa');
/* eslint-enable */

window.cchGa('create', 'UA-110216390-1', 'auto', 'chessHelper');
sendDataToAnalytics({
category: 'init',
action: 'init',
});
}

/**
* There is a tricky layout bug: https://trello.com/c/aT95jsv5
* Fixing it may require applying changes to the layout of the app
* It's better to avoid this changes
*
* This function allows to register amount of such bug events
* and will help us decide whether we need to fix that
*/
function sendLayoutOverlappingStatus() {
const isLive = !!document.getElementById('live-app');
if (!isLive) return;

const input = document.getElementById('ccHelper-input');
const board = document.querySelector('.chessboard');
const inputRect = input.getBoundingClientRect();
const boardRect = board.getBoundingClientRect();

const isOverlapping = (boardRect.top + boardRect.height + 40) > inputRect.top;

sendDataToAnalytics({
category: 'layout-bug-aT95jsv5',
action: 'view',
label: String(isOverlapping),
});
}

/**
* Send data to google analytics to make the extension better
* @param {String} category
* @param {String} action
*/
function sendDataToAnalytics({category, action, label}) {
try {
window.cchGa('chessHelper.send', {
hitType: 'event',
eventCategory: category,
eventAction: action,
eventLabel: label,
});
} catch (e) {}
}

module.exports = {
initAnalytics,
sendLayoutOverlappingStatus,
sendDataToAnalytics,
};
125 changes: 125 additions & 0 deletions app/src/chess.js
@@ -0,0 +1,125 @@
const {
sendDataToAnalytics,
} = require('./analytics');
const {
postMessage,
} = require('./utils');

/**
* Check if input is valid square name
* @param {String} input
* @return {Boolean}
*/
function validateSquareName(input) {
return /^[a-h][1-8]$/.test(input);
}

/**
* Parse message input by user
* @param {String} input - input, in format 'e2e4'
* @return {Array?} - array of two elemens: from and to; or null if there's no move
*/
function parseMoveText(input) {
const filteredSymbols = input.replace(/( |-)+/g, '');
const fromSquare = filteredSymbols.slice(0, 2);
const toSquare = filteredSymbols.slice(2, 4);

if (validateSquareName(fromSquare) && validateSquareName(toSquare)) {
return [fromSquare, toSquare];
}

return null;
}

/**
* Get active board instance
* @return {ChessBoard?}
*/
function getBoard() {
// board for training with computer
const computerBoard = window.myEvent.capturingBoard;
if (computerBoard) {
return computerBoard;
}

// old live mode: probably not working anywhere now
if (window.boardsService && window.boardsService.getSelectedBoard) {
const activeBoard = window.boardsService.getSelectedBoard();

if (activeBoard) {
return activeBoard.chessboard;
}
}

// new live mode
const lc = window.liveClient;
if (
lc &&
lc.controller &&
lc.controller.activeBoard &&
lc.controller.activeBoard.chessboard
) {
return lc.controller.activeBoard.chessboard;
}


return null;
}

/**
* Handle user input and act in appropriate way
* The function uses active board on the screen if there's any
* @param {String} input - input, in format 'e2e4'
*/
function go(input) {
const board = getBoard();
if (board) {
const move = parseMoveText(input);
if (move) {
makeMove(move[0], move[1]);
} else {
sendDataToAnalytics({
category: 'incorrect',
action: 'input',
label: input,
});

postMessage('Incorrect move: ' + input);
}
}
}

/**
* Check move and make it if it's legal
* This function relies on chess.com chessboard interface
* @param {String} fromField - starting field, e.g. 'e2'
* @param {String} toField - ending field, e.g. 'e4'
*/
function makeMove(fromField, toField) {
const board = getBoard();
if (board.gameRules.isLegalMove(board.gameSetup, fromField, toField)) {
board._clickedPieceElement = fromField;
board.fireEvent('onDropPiece', {
fromAreaId: fromField,
targetAreaId: toField,
});
} else {
const move = fromField + '-' + toField;

sendDataToAnalytics({
category: 'illegal',
action: 'input',
label: move,
});

postMessage('Move "' + move + '" is illegal');
}
}

module.exports = {
validateSquareName,
parseMoveText,
getBoard,
go,
makeMove,
};

0 comments on commit 56a6a93

Please sign in to comment.