format code

This commit is contained in:
StevenJoeZhang 2019-02-22 01:50:43 +08:00
parent df1dfa3062
commit 73958b9252
7 changed files with 23 additions and 35 deletions

View File

@ -15,14 +15,14 @@ if (!(config.ws_port >= 0 && config.ws_port < 65536 && config.ws_port % 1 === 0)
const finalhandler = require("finalhandler"),
http = require("http"),
serveStatic = require("serve-static");
// Serve up public/ folder
//Serve up public/ folder
var serve = serveStatic("public/", {
"setHeaders": function(res, path) {
res.setHeader("Cache-Control", "public, max-age=0");
}
});
// Create server
//Create server
try {
http.createServer(function onRequest(req, res) {
serve(req, res, finalhandler(req, res));

View File

@ -99,7 +99,7 @@ function foundProto(func) {
}
function connect() {
client.connectGame(process.argv[2], process.argv[3], function(success, msg) { //|| "[BOT]"
client.connectGame(process.argv[2], process.argv[3] || "[BOT]", function(success, msg) {
if (!success) setTimeout(connect, 1000);
});
}

View File

@ -68,7 +68,7 @@ Color.prototype.rgbString = function() {
//https://stackoverflow.com/a/9493060/7344257
function hslToRgb(h, s, l) {
var r, g, b;
if (s == 0) r = g = b = l; // achromatic
if (s == 0) r = g = b = l; //Achromatic
else {
var hue2rgb = function hue2rgb(p, q, t) {
if (t < 0) t += 1;

View File

@ -119,18 +119,6 @@ function reposition(data, row, col) {
}
}
/*
function render2(data, ctx) {
ctx.fillStyle = data.player.tailColor.rgbString();
for (var r = 0; r < data.tailGrid.length; r++) {
if (!data.tailGrid[r]) continue;
for (var c = 0; c < data.tailGrid[r].length; c++) {
if (data.tailGrid[r][c]) ctx.fillRect(c * consts.CELL_WIDTH, r * consts.CELL_WIDTH, consts.CELL_WIDTH, consts.CELL_WIDTH);
}
}
}
*/
//Helper methods
function renderTail(data, ctx) {
if (data.tail.length === 0) return;
@ -377,9 +365,9 @@ function calcCol(data) {
//Instance methods
Player.prototype.render = function(ctx, fade) {
//Render tail.
//Render tail
this.tail.renderTail(ctx);
//Render player.
//Render player
fade = fade || 1;
ctx.fillStyle = this.shadowColor.deriveAlpha(fade).rgbString();
ctx.fillRect(this.posX, this.posY, consts.CELL_WIDTH, consts.CELL_WIDTH);
@ -407,7 +395,7 @@ function move(data) {
data.waitLag++;
return;
}
//Move to new position.
//Move to new position
var heading = this.heading;
if (this.posX % consts.CELL_WIDTH !== 0 || this.posY % consts.CELL_WIDTH !== 0) heading = data.currentHeading;
else data.currentHeading = heading;
@ -417,19 +405,19 @@ function move(data) {
case 2: data.posY += SPEED; break; //DOWN
case 3: data.posX -= SPEED; break; //LEFT
}
//Check for out of bounds.
//Check for out of bounds
var row = this.row, col = this.col;
if (data.grid.isOutOfBounds(row, col)) {
data.dead = true;
return;
}
//Update tail position.
//Update tail position
if (data.grid.get(row, col) === this) {
//Safe zone!
this.tail.fillTail();
this.tail.reposition(row, col);
}
//If we are completely in a new cell (not in our safe zone), we add to the tail.
//If we are completely in a new cell (not in our safe zone), we add to the tail
else if (this.posX % consts.CELL_WIDTH === 0 && this.posY % consts.CELL_WIDTH === 0) this.tail.addTail(heading);
}

View File

@ -8,7 +8,7 @@ var kills;
var timeout = undefined;
var dirty = false;
var deadFrames = 0;
var requesting = -1; //frame that we are requesting at
var requesting = -1; //Frame that we are requesting at
var frameCache = []; //Frames after our request
var allowAnimation = true;
var grid = new core.Grid(consts.GRID_COUNT, function(row, col, before, after) {
@ -52,11 +52,11 @@ function connectGame(url, name, callback) {
});
socket.on("game", function(data) {
if (timeout != undefined) clearTimeout(timeout);
//Initialize game.
//Initialize game
//TODO: display data.gameid --- game id #
frame = data.frame;
reset();
//Load players.
//Load players
data.players.forEach(function(p) {
var pl = new core.Player(grid, p);
addPlayer(pl);
@ -64,7 +64,7 @@ function connectGame(url, name, callback) {
user = allPlayers[data.num];
if (!user) throw new Error();
setUser(user);
//Load grid.
//Load grid
var gridData = new Uint8Array(data.grid);
for (var r = 0; r < grid.size; r++) {
for (var c = 0; c < grid.size; c++) {
@ -75,7 +75,7 @@ function connectGame(url, name, callback) {
invokeRenderer("paint", []);
frame = data.frame;
if (requesting !== -1) {
//Update those cache frames after we updated game.
//Update those cache frames after we updated game
var minFrame = requesting;
requesting = -1;
while (frameCache.length > frame - minFrame) processFrame(frameCache[frame - minFrame]);

View File

@ -19,7 +19,7 @@ function Game(id) {
possColors[i++] = new core.Color(HUES[h], SATS[s], .5, 1);
}
}
//Shuffle the colors.
//Shuffle the colors
for (var i = 0; i < possColors.length * 50; i++) {
var a = Math.floor(Math.random() * possColors.length);
var b = Math.floor(Math.random() * possColors.length);
@ -63,7 +63,7 @@ function Game(id) {
if (p.name.indexOf("BOT") == -1) log((p.name || "Unnamed") + " (" + p.num + ") joined.");
client.on("requestFrame", function() {
if (p.frame === frame) return;
p.frame = frame; //Limit number of requests per frame. (One per frame);
p.frame = frame; //Limit number of requests per frame (One per frame)
var splayers = players.map(function(val) {
return val.serialData();
});
@ -75,7 +75,7 @@ function Game(id) {
"grid": gridSerialData(grid, players)
});
});
//Verifies that this client has executed this frame properly.
//Verifies that this client has executed this frame properly
client.on("verify", function(data, resp) {
if (typeof resp !== "function") return;
if (!data.frame) resp(false, false, "No frame supplied");
@ -104,7 +104,7 @@ function Game(id) {
}
});
client.on("disconnect", function() {
p.die(); //Die immediately if not already.
p.die(); //Die immediately if not already
p.disconnected = true;
if (p.name.indexOf("BOT") == -1) log((p.name || "Unnamed") + " (" + p.num + ") left.");
});
@ -117,7 +117,7 @@ function Game(id) {
locs[p.num] = [p.posX, p.posY, p.waitLag];
}
locs.frame = frame;
if (frameLocs.length >= 300) frameLocs.shift(); //Give it 5 seconds of lag.
if (frameLocs.length >= 300) frameLocs.shift(); //Give it 5 seconds of lag
frameLocs.push(locs);
}
@ -147,7 +147,7 @@ function Game(id) {
}
function tick() {
//TODO: notify those players that this server automatically drops out.
//TODO: notify those players that this server automatically drops out
var splayers = players.map(function(val) {
return val.serialData();
});
@ -163,7 +163,7 @@ function Game(id) {
return val.serialData();
});
var moves = players.map(function(val) {
//Account for race condition (when heading is set after emitting frames, and before updating).
//Account for race condition (when heading is set after emitting frames, and before updating)
val.heading = val.tmpHeading;
return {
num: val.num,

View File

@ -24,7 +24,7 @@ function mod(x) {
}
function connect() {
client.connectGame(process.argv[2], process.argv[3], function(success, msg) { //|| "[PAPER-IO-BOT]"
client.connectGame(process.argv[2], process.argv[3] || "[BOT]", function(success, msg) {
if (!success) setTimeout(connect, 1000);
});
}