Skip to content
This repository was archived by the owner on Oct 2, 2021. It is now read-only.
90 changes: 84 additions & 6 deletions bot.user.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
Copyright (c) 2016 Ermiya Eskandary & Théophile Cailliau and other contributors
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -60,6 +60,31 @@ window.log = function() {
}
};

// Add standard deviation function to array prototype
Array.prototype.stats = function() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array prototype is read only, properties should not be added.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, what do you think of this?

var i;
var j;
var total = 0;
var mean = 0;
var diffSqredArr = [];
for (i = 0; i < this.length; i += 1) {
total += this[i];
}
mean = total / this.length;
for (j = 0; j < this.length; j += 1) {
diffSqredArr.push(Math.pow((this[j] - mean), 2));
}
return {
mean: mean,
stdev: (Math.sqrt(diffSqredArr.reduce(
function(firstEl, nextEl) { return firstEl + nextEl; }
) / this.length)),
min: Math.min.apply(null, this),
max: Math.max.apply(null, this)
};
};


var canvasUtil = window.canvasUtil = (function() {
return {
// Ratio of screen size divided by canvas size.
Expand Down Expand Up @@ -357,6 +382,42 @@ var canvasUtil = window.canvasUtil = (function() {
};
})();

var logUtil = window.logUtil = (function() {
return {
functionData: [],
startTimes: [],

startTime: function(functionName) {
logUtil.startTimes[functionName] = performance.now();
},

endTime: function(functionName) {
// No sense recording end time if start wasn't called.
if (!(functionName in logUtil.startTimes)) {
window.log('logUtil.endTime called for "' + functionName + '" without start');
return;
}
var duration = performance.now() - logUtil.startTimes[functionName];

if (!(functionName in logUtil.functionData)) {
logUtil.functionData[functionName] = [];
}
if (logUtil.functionData[functionName].push(duration) > window.bot.opt.logFrames) {
logUtil.functionData[functionName].shift();
}
},

functionStats: function(functionName) {
if (functionName in logUtil.functionData) {
return logUtil.functionData[functionName].stats();
} else {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexpected 'else' after 'return'.

window.log('No function data for ' + functionName);
return false;
}
}
};
})();

var bot = window.bot = (function() {
return {
isBotRunning: false,
Expand Down Expand Up @@ -386,7 +447,8 @@ var bot = window.bot = (function() {
rearHeadAngle: 3 * Math.PI / 4,
rearHeadDir: Math.PI / 2,
radiusApproachSize: 5,
radiusAvoidSize: 25
radiusAvoidSize: 25,
logFrames: 200
},
MID_X: 0,
MID_Y: 0,
Expand Down Expand Up @@ -844,7 +906,8 @@ var bot = window.bot = (function() {
window.snake.xx - (bot.sectorBoxSide / 2),
window.snake.yy - (bot.sectorBoxSide / 2),
bot.sectorBoxSide, bot.sectorBoxSide);
// if (window.visualDebugging) canvasUtil.drawRect(bot.sectorBox, '#c0c0c0', true, 0.1);
// if (window.visualDebugging)
// canvasUtil.drawRect(bot.sectorBox, '#c0c0c0', true, 0.1);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to delete this if it's unused.


bot.cos = Math.cos(window.snake.ang);
bot.sin = Math.sin(window.snake.ang);
Expand Down Expand Up @@ -899,7 +962,9 @@ var bot = window.bot = (function() {
foodTimer: function() {
if (window.playing && bot.lookForFood &&
window.snake !== null && window.snake.alive_amt === 1) {
logUtil.startTime('cfg');
bot.computeFoodGoal();
logUtil.endTime('cfg');
window.goalCoordinates = bot.currentFood;
canvasUtil.setMouseCoordinates(canvasUtil.mapToMouse(window.goalCoordinates));
}
Expand Down Expand Up @@ -1229,10 +1294,14 @@ var userInterface = window.userInterface = (function() {
median = Math.round((bot.scores[Math.floor((bot.scores.length - 1) / 2)] +
bot.scores[Math.ceil((bot.scores.length - 1) / 2)]) / 2);

var stats = bot.scores.stats();

avg = Math.round(stats.mean);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'avg' is not defined.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be var avg = Math.round(stats.mean);?

stdev = Math.round(stats.stdev);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'stdev' is not defined.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here... var stdev =


oContent.push('games played: ' + bot.scores.length);
oContent.push('a: ' + Math.round(
bot.scores.reduce(function(a, b) { return a + b; }) / (bot.scores.length)) +
' m: ' + median);
oContent.push('avg: ' + avg + ' stdev: ' + stdev + '<br/>' +
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'avg' is not defined.
'stdev' is not defined.

'med: ' + median);

for (var i = 0; i < bot.scores.length && i < 10; i++) {
oContent.push(i + 1 + '. ' + bot.scores[i]);
Expand Down Expand Up @@ -1292,6 +1361,15 @@ var userInterface = window.userInterface = (function() {
userInterface.overlays.serverOverlay.innerHTML =
window.bso.ip + ':' + window.bso.po;
}

if (window.logUtil !== undefined) {
var cfg = window.logUtil.functionStats('cfg');

if (cfg) {
oContent.push('cfg: μ' + Math.round(cfg.mean) + ' ∧' +
Math.round(cfg.min) + ' ∨' + Math.round(cfg.max));
}
}
}

userInterface.overlays.botOverlay.innerHTML = oContent.join('<br/>');
Expand Down