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) { function foundProto(func) {
return function(loc) { return loc => {
return others.some(function(other) { return others.some(other => {
return func(other, loc); return func(other, loc);
}); });
} };
} }
function connect() { function connect() {
var prefixes = consts.PREFIXES.split(" "); var prefixes = consts.PREFIXES.split(" ");
var names = consts.NAMES.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(" "); 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) { if (!success) {
console.error(msg); console.error(msg);
setTimeout(connect, 1000); setTimeout(connect, 1000);

View File

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

View File

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

View File

@ -4,18 +4,18 @@ exports.Color = require("./color");
exports.Grid = require("./grid"); exports.Grid = require("./grid");
exports.Player = require("./player"); exports.Player = require("./player");
exports.initPlayer = function(grid, player) { exports.initPlayer = (grid, player) => {
for (var dr = -1; dr <= 1; dr++) { for (var dr = -1; dr <= 1; dr++) {
for (var dc = -1; dc <= 1; dc++) { 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); 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 = []; var adead = [];
if (dead instanceof Array) adead = dead; if (dead instanceof Array) adead = dead;
var kill = (!notifyKill) ? function() {} : function(killer, other) { var kill = (!notifyKill) ? () => {} : (killer, other) => {
if (!removing[other]) 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...*/) { function defineAccessorProperties(thisobj, data /*, names...*/) {
var descript = {}; var descript = {};
function getAt(name) { function getAt(name) {
return function() { return () => {
return data[name]; return data[name];
} };
} }
for (var i = 2; i < arguments.length; i++) { for (var i = 2; i < arguments.length; i++) {
descript[arguments[i]] = defineGetter(getAt(arguments[i])); descript[arguments[i]] = defineGetter(getAt(arguments[i]));
@ -321,7 +321,7 @@ function Player(grid, sdata) {
//Instance methods //Instance methods
this.move = move.bind(this, data); this.move = move.bind(this, data);
this.die = function() { data.dead = true; }; this.die = () => { data.dead = true; };
this.serialData = function() { this.serialData = function() {
return { return {
base: this.baseColor, base: this.baseColor,

View File

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

View File

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

View File

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