Use arrow functions

This commit is contained in:
Mimi 2020-03-04 20:07:38 +08:00
parent c44446eb65
commit 0005563b46
9 changed files with 37 additions and 37 deletions

8
bot.js
View File

@ -87,18 +87,18 @@ var LAND_CLAIMS = {
}
function foundProto(func) {
return function(loc) {
return others.some(function(other) {
return loc => {
return others.some(other => {
return func(other, loc);
});
}
};
}
function connect() {
var prefixes = consts.PREFIXES.split(" ");
var names = consts.NAMES.split(" ");
var name = process.argv[3] || [prefixes[Math.floor(Math.random() * prefixes.length)], names[Math.floor(Math.random() * names.length)]].join(" ");
client.connectGame(process.argv[2], "[BOT] " + name, function(success, msg) {
client.connectGame(process.argv[2], "[BOT] " + name, (success, msg) => {
if (!success) {
console.error(msg);
setTimeout(connect, 1000);

View File

@ -6,7 +6,7 @@ var config = require("./config.json");
function run(flag) {
client.renderer = flag ? require("./src/mode/god") : require("./src/mode/player");
client.connectGame("//" + location.host, $("#name").val(), function(success, msg) {
client.connectGame("//" + location.host, $("#name").val(), (success, msg) => {
if (success) {
$("#main-ui").fadeIn(1000);
$("#begin, #wasted").fadeOut(1000);
@ -17,42 +17,42 @@ function run(flag) {
}, flag);
}
$(document).ready(function() {
$(document).ready(() => {
var err = $("#error");
if (!window.WebSocket) {
err.text("Your browser does not support WebSockets!");
return;
}
err.text("Loading... Please wait"); //TODO: show loading screen
(function() {
(() => {
var socket = io(`//${location.host}`, {
forceNew: true,
upgrade: false,
transports: ["websocket"]
});
socket.on("connect", function() {
socket.on("connect", () => {
socket.emit("pings");
});
socket.on("pongs", function() {
socket.on("pongs", () => {
socket.disconnect();
err.text("All done, have fun!");
$("#name").keypress(function(evt) {
$("#name").keypress(evt => {
if (evt.which === 13) run();
});
$(".start").removeAttr("disabled").click(function(evt) {
$(".start").removeAttr("disabled").click(evt => {
run();
});
$(".spectate").removeAttr("disabled").click(function(evt) {
$(".spectate").removeAttr("disabled").click(evt => {
run(true);
});
});
socket.on("connect_error", function() {
socket.on("connect_error", () => {
err.text("Cannot connect with server. This probably is due to misconfigured proxy server. (Try using a different browser)");
});
})();
});
//Event listeners
$(document).keydown(function(e) {
$(document).keydown(e => {
var newHeading = -1;
switch (e.key) {
case "w": case "ArrowUp":
@ -69,14 +69,14 @@ $(document).keydown(function(e) {
//e.preventDefault();
});
$(document).on("touchmove", function(e) {
$(document).on("touchmove", e => {
e.preventDefault();
});
$(document).on("touchstart", function (e1) {
$(document).on("touchstart", e1 => {
var x1 = e1.targetTouches[0].pageX;
var y1 = e1.targetTouches[0].pageY;
$(document).one("touchend", function (e2) {
$(document).one("touchend", e2 => {
var x2 = e2.changedTouches[0].pageX;
var y2 = e2.changedTouches[0].pageY;
var deltaX = x2 - x1;
@ -90,12 +90,12 @@ $(document).on("touchstart", function (e1) {
});
});
$(".menu").on("click", function() {
$(".menu").on("click", () => {
client.disconnect();
$("#main-ui, #wasted").fadeOut(1000);
$("#begin").fadeIn(1000);
});
$(".toggle").on("click", function() {
$(".toggle").on("click", () => {
$("#settings").slideToggle();
});

View File

@ -47,7 +47,7 @@ function Color(h, s, l, a) {
},
});
}
Color.fromData = function(data) {
Color.fromData = data => {
return new Color(data.hue, data.sat, data.lum, data.alpha);
};
Color.prototype.interpolateToString = function(color, amount) {
@ -86,7 +86,7 @@ Color.prototype.rgbString = function() {
rgb[3] = this.a;
return `rgba(${rgb[0]}, ${rgb[1]}, ${rgb[2]}, ${this.alpha})`;
};
Color.possColors = function() {
Color.possColors = () => {
var SATS = [192, 150, 100].map(val => val / 240);
var HUES = [0, 10, 20, 25, 30, 35, 40, 45, 50, 60, 70, 100, 110, 120, 125, 130, 135, 140, 145, 150, 160, 170, 180, 190, 200, 210, 220].map(val => val / 240);
var possColors = new Array(SATS.length * HUES.length);

View File

@ -6,11 +6,11 @@ function Grid(size, changeCallback) {
size
};
this.get = function(row, col) {
this.get = (row, col) => {
if (isOutOfBounds(data, row, col)) throw new RangeError("Row or Column value out of bounds");
return grid[row] && grid[row][col];
}
this.set = function(row, col, value) {
this.set = (row, col, value) => {
if (isOutOfBounds(data, row, col)) throw new RangeError("Row or Column value out of bounds");
if (!grid[row]) grid[row] = new Array(size);
var before = grid[row][col];
@ -19,7 +19,7 @@ function Grid(size, changeCallback) {
modified = true;
return before;
}
this.reset = function() {
this.reset = () => {
if (modified) {
grid = new Array(size);
modified = false;

View File

@ -4,18 +4,18 @@ exports.Color = require("./color");
exports.Grid = require("./grid");
exports.Player = require("./player");
exports.initPlayer = function(grid, player) {
exports.initPlayer = (grid, player) => {
for (var dr = -1; dr <= 1; dr++) {
for (var dc = -1; dc <= 1; dc++) {
if (!grid.isOutOfBounds(dr + player.row, dc + player.col)) grid.set(dr + player.row, dc + player.col, player);
}
}
};
exports.updateFrame = function(grid, players, dead, notifyKill) {
exports.updateFrame = (grid, players, dead, notifyKill) => {
var adead = [];
if (dead instanceof Array) adead = dead;
var kill = (!notifyKill) ? function() {} : function(killer, other) {
var kill = (!notifyKill) ? () => {} : (killer, other) => {
if (!removing[other]) notifyKill(killer, other);
};

View File

@ -19,9 +19,9 @@ function defineInstanceMethods(thisobj, data /*, methods...*/) {
function defineAccessorProperties(thisobj, data /*, names...*/) {
var descript = {};
function getAt(name) {
return function() {
return () => {
return data[name];
}
};
}
for (var i = 2; i < arguments.length; i++) {
descript[arguments[i]] = defineGetter(getAt(arguments[i]));
@ -321,7 +321,7 @@ function Player(grid, sdata) {
//Instance methods
this.move = move.bind(this, data);
this.die = function() { data.dead = true; };
this.die = () => { data.dead = true; };
this.serialData = function() {
return {
base: this.baseColor,

View File

@ -1,7 +1,7 @@
function Stack(initSize) {
var len = 0;
var arr = [];
this.ensureCapacity = function(size) {
this.ensureCapacity = size => {
arr.length = Math.max(arr.length, size || 0);
};
this.push = function(ele) {
@ -15,7 +15,7 @@ function Stack(initSize) {
this[len] = undefined;
return tmp;
};
this.isEmpty = function() {
this.isEmpty = () => {
return len === 0;
}
this.ensureCapacity(initSize);

View File

@ -23,11 +23,11 @@ try {
|| window.mozRequestAnimationFrame
|| window.oRequestAnimationFrame
|| window.msRequestAnimationFrame
|| function(callback) { window.setTimeout(callback, 1000 / 30) };
|| (callback => { window.setTimeout(callback, 1000 / 30) });
}
}
catch (e) {
mimiRequestAnimationFrame = function(callback) { setTimeout(callback, 1000 / 30) };
mimiRequestAnimationFrame = callback => { setTimeout(callback, 1000 / 30) };
}
//Public API

View File

@ -18,7 +18,7 @@ function Game(id) {
}
});
this.id = id;
this.addPlayer = function(client, name) {
this.addPlayer = (client, name) => {
if (players.length >= consts.MAX_PLAYERS) return false;
var start = findEmpty(grid);
if (!start) return false;
@ -55,7 +55,7 @@ function Game(id) {
errorHan(false, "No data supplied.");
return;
}
if (typeof errorHan !== "function") errorHan = function() {};
if (typeof errorHan !== "function") errorHan = () => {};
if (!data) errorHan(false, "No data supplied.");
else if (!checkInt(data.frame, 0, Infinity)) errorHan(false, "Requires a valid non-negative frame integer.");
else if (data.frame > frame) errorHan(false, "Invalid frame received.");
@ -76,7 +76,7 @@ function Game(id) {
});
return true;
};
this.addGod = function(client) {
this.addGod = client => {
var g = {
client,
frame