Skip to content

Instantly share code, notes, and snippets.

@eins78
Last active February 26, 2017 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eins78/d936ba299198b16c96116cba14eec9a5 to your computer and use it in GitHub Desktop.
Save eins78/d936ba299198b16c96116cba14eec9a5 to your computer and use it in GitHub Desktop.
doctor stats plugin. install it on <professor.amiacyb.org> then click the "נקודות" box.

doctor stats plugin.

install it on http://professor.amiacyb.org then click the "נקודות" box.

install with bookmarklet here

or paste the JS code in the browser console, use a "userscripts" or "greasmonkey" browser extension, etc.

if installation was successful, the "נקודות" box will show a "?" pointer icon on mouse hover.

screenshot

Screenshot

license

CC0
To the extent possible under law, Max Albrecht and Naomi T. Salmon have waived all copyright and related or neighboring rights to doctor stats plugin. This work is published from: Switzerland.

<!DOCTYPE html>
bookmarklet: <a style="font-size:2em;border-radius: 0.3m" href="javascript:void%20function(){function%20t(){s(function(t){var%20n=t.map(l),e={numberOfBoards:t.length,totalPossible:i(t),totalPoints:a(n),detailedStats:r(n)};o(e)})}function%20o(t){c(%22%23msgTitle%22).html(t.totalPoints+%22/%22+t.totalPossible),c(%22%23msgTxt%22).empty(),c(%22%23msgTxt%22).html('%3Cdiv%20style=%22color:%20white;%20font-size:%2060%25;%20line-height:%20normal;%22%3E%3Cp%20class=%22msgBoxTxt%22%3E'+n(t)+'%3C/p%3E%20%3Cdiv%20class=%22squareLink%22%20onclick=%22closeMsg()%22%3Eסגור%3C/div%3E'),c(%22%23msgContainer%22).show().animate({opacity:1},200),c(%22%23brighten%22).show().animate({opacity:.8},200)}function%20n(t){var%20o=4*t.numberOfBoards,n=t.detailedStats;return[[%22הרווחת%20%22,t.totalPoints,%22%20נקודות%20(%25%22,e(t.totalPoints,t.totalPossible),%22)%20והשלמת%20%22,n.completedBoardsCount,%22%20לוחות%20(%25%22,e(n.completedBoardsCount,t.numberOfBoards),%22).%22],[%22סך%20הכל%20%22,t.totalPossible,%22%20נקודות%20ב-%20%22,t.numberOfBoards,%22%20לוחות%20(%22,o,%22%20שורות).%22],[%22בסך%20הכל%20השלמת%20%22,n.completedRowsCount,%22%20שורות%20(%25%22,e(n.completedRowsCount,o),%22)%20וקישרת%20עוד%20%22,n.partialRowsCount,%22%20שורות%20(%25%22,e(n.partialRowsCount,o),%22).%22]].map(function(t){return%20t.join(%22%22)}).join(%22%3Cbr%3E%22)}function%20e(t,o){return(100*t/o).toFixed(2)}function%20a(t){return%20t.reduce(function(t,o){return%20t+2*o[0].length+o[1].length},0)}function%20r(t){function%20o(t,o,n){return%20t+o*(n+1)}var%20n={completedBoardsCount:0,completedRowsCount:0,partialRowsCount:0,completedRows:[0,0,0,0],partialRows:[0,0,0,0]};return%20t.forEach(function(t){[%22completedRows%22,%22partialRows%22].forEach(function(o,e){n[o].forEach(function(a,r){t[e].length===r+1%26%26(n[o][r]=a+1)})})}),n.partialRowsCount=n.partialRows.reduce(o,0),n.completedRowsCount=n.completedRows.reduce(o,0),n.completedBoardsCount=n.completedRows[3],n}function%20i(t){return%208*t.length}function%20s(t){c.get(%22cgi-bin/getBoards.py%22,{},function(o){t(JSON.parse(o).boards)})}function%20l(t){var%20o=u(%22boardState%22+t);return%22%22!==o%3FJSON.parse(o):[[],[]]}var%20c=window.$,u=window.getCookie,d=c(%22%23pointBox%22);d.on(%22click%22,t),d.attr(%22style%22,d.attr(%22style%22)+%22;%20cursor:%20help;%22)}();">doctor stats</a>
// 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;')
@eins78
Copy link
Author

eins78 commented Feb 26, 2017

Screenshot: bildschirmfoto 2017-02-26 um 15 38 55

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment