Skip to content
This repository has been archived by the owner on Oct 2, 2021. It is now read-only.

Commit

Permalink
Codestyle + docs #275 + PR #292
Browse files Browse the repository at this point in the history
  • Loading branch information
clemens-tolboom committed Jul 11, 2016
1 parent 65d8bee commit 7d88587
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions bot.user.js
Expand Up @@ -887,14 +887,14 @@ var scheduler = window.scheduler = (function() {
active: true,
description: 'Avoid collision with other (enemy) snakes',

getPriority: function () {
getPriority: function() {
if (bot.checkCollision()) {
return 500;
} else {
return 0;
}
},
execute: function () {
execute: function() {
// NOP
// TODO: checkCollision() needs refactoring; it should return but not set direction
}
Expand All @@ -904,10 +904,10 @@ var scheduler = window.scheduler = (function() {
active: true,
description: 'Eat what is detected by CheckForFood',

getPriority: function () {
getPriority: function() {
return 300;
},
execute: function () {
execute: function() {
if (bot.currentFood) {
window.setAcceleration(bot.foodAccel());
}
Expand All @@ -926,7 +926,7 @@ var scheduler = window.scheduler = (function() {
// to maximum priority
triggerPriority: 400,

getPriority: function () {
getPriority: function() {
var currentPriority = this.priority;

if (this.priority < this.triggerPriority) {
Expand All @@ -936,7 +936,7 @@ var scheduler = window.scheduler = (function() {
}
return this.startPriority;
},
execute: function () {
execute: function() {
bot.computeFoodGoal();
if (bot.currentFood) {
window.setAcceleration(bot.foodAccel());
Expand All @@ -961,13 +961,14 @@ var scheduler = window.scheduler = (function() {
},

getPriority: function () {
if (canvasUtil.getDistance2(window.snake.xx, window.snake.yy, this.point.x, this.point.y) > 1000) {
if (canvasUtil.getDistance2(window.snake.xx, window.snake.yy,
this.point.x, this.point.y) > 1000) {
return this.defaultPriority;
} else {
this.active = false;
}
},
execute: function () {
execute: function() {
window.goalCoordinates = this.point;
canvasUtil.setMouseCoordinates(canvasUtil.mapToMouse(window.goalCoordinates));
}
Expand All @@ -977,14 +978,14 @@ var scheduler = window.scheduler = (function() {
active: true,
description: 'This is the default task which cannot be deactivated.',

getPriority: function () {
getPriority: function() {
// Always active
this.active = true;

// Lowest priority
return 0;
},
execute: function () {
execute: function() {
window.log(this.id, 'nothing to do');
}
}
Expand Down Expand Up @@ -1090,6 +1091,7 @@ var scheduler = window.scheduler = (function() {
* Existing tasks can be adjusted.
*/
executeTasks: function() {
// Get new priorities
scheduler.tasks.forEach(function(v) {
v.priority = v.getPriority();
});
Expand All @@ -1115,7 +1117,13 @@ var scheduler = window.scheduler = (function() {
task.execute();
},

// Sort tasks by active
/**
* Sort tasks by active then priority.
*
* @param {Task} a Task
* @param {Task} b Task
* @returns {boolean} a < b
*/
sortTasks: function(a, b) {

if (a.active !== b.active) {
Expand Down Expand Up @@ -1542,8 +1550,8 @@ var userInterface = window.userInterface = (function() {
*
* This allows for keys 1-9 to toggle task.
*
* @param keycode
* @returns {null|string}
* @param {integer} keycode the key pressed
* @returns {null|string} Task ID selected
*/
getTaskIdByKeyBinding: function(keycode) {
// http://www.cambiaresearch.com/articles/15/javascript-key-codes
Expand Down

0 comments on commit 7d88587

Please sign in to comment.