|
// Public Domain/CC-0 (see <https://gist.githubusercontent.com/eins78/d936ba299198b16c96116cba14eec9a5/raw/87b28242381efa70906c76d5418db06de89b025d/about-docror-stats-plugin.md>) |
|
|
|
var $ = window.$ |
|
var getCookie = window.getCookie |
|
|
|
// our stuff |
|
|
|
function showGameStats () { |
|
getAllBoards(function (boards) { |
|
var boardStates = boards.map(getBoard) |
|
|
|
var stats = { |
|
numberOfBoards: boards.length, |
|
totalPossible: totalPossiblePoints(boards), |
|
totalPoints: sumTotalPoints(boardStates), |
|
detailedStats: getDetailedStats(boardStates) |
|
} |
|
|
|
showStatsMessage(stats) |
|
}) |
|
} |
|
|
|
function showStatsMessage (stats) { |
|
$('#msgTitle').html(stats.totalPoints + '/' + stats.totalPossible) |
|
$('#msgTxt').empty() |
|
$('#msgTxt').html( |
|
'<div style="color: white; font-size: 60%; line-height: normal;"><p class="msgBoxTxt">' + |
|
decorateStats(stats) + |
|
'</p> <div class="squareLink" onclick="closeMsg()">סגור</div>' |
|
) |
|
$('#msgContainer').show().animate({opacity: 1}, 200) |
|
$('#brighten').show().animate({opacity: 0.8}, 200) |
|
} |
|
|
|
function decorateStats (s) { |
|
// You've earned 9 points (x%) and completed 1 board(s) (x%). |
|
// |
|
// There are 968 total points across 121 boards (484 rows). |
|
// In total, you've completed 5 row(s) (x%), and connected another 1 row(s) (x%). |
|
|
|
var numberOfRows = s.numberOfBoards * 4 |
|
|
|
var d = s.detailedStats |
|
|
|
return [ |
|
|
|
[ 'הרווחת ', s.totalPoints, ' נקודות (%', |
|
percent(s.totalPoints, s.totalPossible), ') והשלמת ', |
|
d.completedBoardsCount, ' לוחות (%', |
|
percent(d.completedBoardsCount, s.numberOfBoards), ').' ], |
|
|
|
[ 'סך הכל ', s.totalPossible, ' נקודות ב- ', s.numberOfBoards, ' לוחות (', numberOfRows, ' שורות).' ], |
|
|
|
[ 'בסך הכל השלמת ', d.completedRowsCount, ' שורות (%', |
|
percent(d.completedRowsCount, numberOfRows), ') וקישרת עוד ', |
|
d.partialRowsCount, ' שורות (%', |
|
percent(d.partialRowsCount, numberOfRows), ').' ] |
|
|
|
].map(function (l) { return l.join('') }).join('<br>') |
|
} |
|
|
|
function percent (a, b) { return (a * 100 / b).toFixed(2) } |
|
|
|
// game business logic |
|
|
|
function sumTotalPoints (boardStates) { |
|
return boardStates.reduce(function (sum, boardState) { |
|
// NOTE: 1. solvedWithConnection = 2 points; 2. onlyGrouped = 1 point] |
|
return sum + (boardState[0].length * 2) + (boardState[1].length) |
|
}, 0) |
|
} |
|
|
|
function getDetailedStats (boardStates) { |
|
var stats = { |
|
completedBoardsCount: 0, |
|
completedRowsCount: 0, |
|
partialRowsCount: 0, |
|
completedRows: [0, 0, 0, 0], |
|
partialRows: [0, 0, 0, 0] |
|
} |
|
|
|
// rows are "indexed" counts, total count must multiply position: |
|
function sumRows (sum, count, index) { return sum + (count * (index + 1)) } |
|
|
|
boardStates.forEach(function (boardState) { |
|
['completedRows', 'partialRows'].forEach(function (k, i) { |
|
stats[k].forEach(function (count, index) { |
|
if (boardState[i].length === (index + 1)) { |
|
stats[k][index] = (count + 1) |
|
} |
|
}) |
|
}) |
|
}) |
|
|
|
stats.partialRowsCount = stats.partialRows.reduce(sumRows, 0) |
|
stats.completedRowsCount = stats.completedRows.reduce(sumRows, 0) |
|
// solved boards is just number of boards with 4 completed rows: |
|
stats.completedBoardsCount = stats.completedRows[3] |
|
|
|
return stats |
|
} |
|
|
|
function totalPossiblePoints (allBoards) { |
|
return allBoards.length * 8 |
|
} |
|
|
|
function getAllBoards (cb) { |
|
$.get('cgi-bin/getBoards.py', {}, function (res) { |
|
cb(JSON.parse(res)['boards']) |
|
}) |
|
} |
|
|
|
function getBoard (id) { |
|
var data = getCookie('boardState' + id) |
|
if (data !== '') { |
|
return JSON.parse(data) |
|
} else { |
|
return [[], []] |
|
} |
|
} |
|
|
|
// RUN IT |
|
|
|
// paste to console… |
|
// showGameStats() |
|
|
|
// attach click handler to point button |
|
var $pointBox = $('#pointBox') |
|
$pointBox.on('click', showGameStats) |
|
$pointBox.attr('style', $pointBox.attr('style') + '; cursor: help;') |
Screenshot: