forked from ilikecats/papercats
format code
This commit is contained in:
parent
df1dfa3062
commit
73958b9252
@ -15,14 +15,14 @@ if (!(config.ws_port >= 0 && config.ws_port < 65536 && config.ws_port % 1 === 0)
|
|||||||
const finalhandler = require("finalhandler"),
|
const finalhandler = require("finalhandler"),
|
||||||
http = require("http"),
|
http = require("http"),
|
||||||
serveStatic = require("serve-static");
|
serveStatic = require("serve-static");
|
||||||
// Serve up public/ folder
|
//Serve up public/ folder
|
||||||
var serve = serveStatic("public/", {
|
var serve = serveStatic("public/", {
|
||||||
"setHeaders": function(res, path) {
|
"setHeaders": function(res, path) {
|
||||||
res.setHeader("Cache-Control", "public, max-age=0");
|
res.setHeader("Cache-Control", "public, max-age=0");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Create server
|
//Create server
|
||||||
try {
|
try {
|
||||||
http.createServer(function onRequest(req, res) {
|
http.createServer(function onRequest(req, res) {
|
||||||
serve(req, res, finalhandler(req, res));
|
serve(req, res, finalhandler(req, res));
|
||||||
|
@ -99,7 +99,7 @@ function foundProto(func) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function connect() {
|
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);
|
if (!success) setTimeout(connect, 1000);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ Color.prototype.rgbString = function() {
|
|||||||
//https://stackoverflow.com/a/9493060/7344257
|
//https://stackoverflow.com/a/9493060/7344257
|
||||||
function hslToRgb(h, s, l) {
|
function hslToRgb(h, s, l) {
|
||||||
var r, g, b;
|
var r, g, b;
|
||||||
if (s == 0) r = g = b = l; // achromatic
|
if (s == 0) r = g = b = l; //Achromatic
|
||||||
else {
|
else {
|
||||||
var hue2rgb = function hue2rgb(p, q, t) {
|
var hue2rgb = function hue2rgb(p, q, t) {
|
||||||
if (t < 0) t += 1;
|
if (t < 0) t += 1;
|
||||||
|
@ -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
|
//Helper methods
|
||||||
function renderTail(data, ctx) {
|
function renderTail(data, ctx) {
|
||||||
if (data.tail.length === 0) return;
|
if (data.tail.length === 0) return;
|
||||||
@ -377,9 +365,9 @@ function calcCol(data) {
|
|||||||
|
|
||||||
//Instance methods
|
//Instance methods
|
||||||
Player.prototype.render = function(ctx, fade) {
|
Player.prototype.render = function(ctx, fade) {
|
||||||
//Render tail.
|
//Render tail
|
||||||
this.tail.renderTail(ctx);
|
this.tail.renderTail(ctx);
|
||||||
//Render player.
|
//Render player
|
||||||
fade = fade || 1;
|
fade = fade || 1;
|
||||||
ctx.fillStyle = this.shadowColor.deriveAlpha(fade).rgbString();
|
ctx.fillStyle = this.shadowColor.deriveAlpha(fade).rgbString();
|
||||||
ctx.fillRect(this.posX, this.posY, consts.CELL_WIDTH, consts.CELL_WIDTH);
|
ctx.fillRect(this.posX, this.posY, consts.CELL_WIDTH, consts.CELL_WIDTH);
|
||||||
@ -407,7 +395,7 @@ function move(data) {
|
|||||||
data.waitLag++;
|
data.waitLag++;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//Move to new position.
|
//Move to new position
|
||||||
var heading = this.heading;
|
var heading = this.heading;
|
||||||
if (this.posX % consts.CELL_WIDTH !== 0 || this.posY % consts.CELL_WIDTH !== 0) heading = data.currentHeading;
|
if (this.posX % consts.CELL_WIDTH !== 0 || this.posY % consts.CELL_WIDTH !== 0) heading = data.currentHeading;
|
||||||
else data.currentHeading = heading;
|
else data.currentHeading = heading;
|
||||||
@ -417,19 +405,19 @@ function move(data) {
|
|||||||
case 2: data.posY += SPEED; break; //DOWN
|
case 2: data.posY += SPEED; break; //DOWN
|
||||||
case 3: data.posX -= SPEED; break; //LEFT
|
case 3: data.posX -= SPEED; break; //LEFT
|
||||||
}
|
}
|
||||||
//Check for out of bounds.
|
//Check for out of bounds
|
||||||
var row = this.row, col = this.col;
|
var row = this.row, col = this.col;
|
||||||
if (data.grid.isOutOfBounds(row, col)) {
|
if (data.grid.isOutOfBounds(row, col)) {
|
||||||
data.dead = true;
|
data.dead = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//Update tail position.
|
//Update tail position
|
||||||
if (data.grid.get(row, col) === this) {
|
if (data.grid.get(row, col) === this) {
|
||||||
//Safe zone!
|
//Safe zone!
|
||||||
this.tail.fillTail();
|
this.tail.fillTail();
|
||||||
this.tail.reposition(row, col);
|
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);
|
else if (this.posX % consts.CELL_WIDTH === 0 && this.posY % consts.CELL_WIDTH === 0) this.tail.addTail(heading);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ var kills;
|
|||||||
var timeout = undefined;
|
var timeout = undefined;
|
||||||
var dirty = false;
|
var dirty = false;
|
||||||
var deadFrames = 0;
|
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 frameCache = []; //Frames after our request
|
||||||
var allowAnimation = true;
|
var allowAnimation = true;
|
||||||
var grid = new core.Grid(consts.GRID_COUNT, function(row, col, before, after) {
|
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) {
|
socket.on("game", function(data) {
|
||||||
if (timeout != undefined) clearTimeout(timeout);
|
if (timeout != undefined) clearTimeout(timeout);
|
||||||
//Initialize game.
|
//Initialize game
|
||||||
//TODO: display data.gameid --- game id #
|
//TODO: display data.gameid --- game id #
|
||||||
frame = data.frame;
|
frame = data.frame;
|
||||||
reset();
|
reset();
|
||||||
//Load players.
|
//Load players
|
||||||
data.players.forEach(function(p) {
|
data.players.forEach(function(p) {
|
||||||
var pl = new core.Player(grid, p);
|
var pl = new core.Player(grid, p);
|
||||||
addPlayer(pl);
|
addPlayer(pl);
|
||||||
@ -64,7 +64,7 @@ function connectGame(url, name, callback) {
|
|||||||
user = allPlayers[data.num];
|
user = allPlayers[data.num];
|
||||||
if (!user) throw new Error();
|
if (!user) throw new Error();
|
||||||
setUser(user);
|
setUser(user);
|
||||||
//Load grid.
|
//Load grid
|
||||||
var gridData = new Uint8Array(data.grid);
|
var gridData = new Uint8Array(data.grid);
|
||||||
for (var r = 0; r < grid.size; r++) {
|
for (var r = 0; r < grid.size; r++) {
|
||||||
for (var c = 0; c < grid.size; c++) {
|
for (var c = 0; c < grid.size; c++) {
|
||||||
@ -75,7 +75,7 @@ function connectGame(url, name, callback) {
|
|||||||
invokeRenderer("paint", []);
|
invokeRenderer("paint", []);
|
||||||
frame = data.frame;
|
frame = data.frame;
|
||||||
if (requesting !== -1) {
|
if (requesting !== -1) {
|
||||||
//Update those cache frames after we updated game.
|
//Update those cache frames after we updated game
|
||||||
var minFrame = requesting;
|
var minFrame = requesting;
|
||||||
requesting = -1;
|
requesting = -1;
|
||||||
while (frameCache.length > frame - minFrame) processFrame(frameCache[frame - minFrame]);
|
while (frameCache.length > frame - minFrame) processFrame(frameCache[frame - minFrame]);
|
||||||
|
@ -19,7 +19,7 @@ function Game(id) {
|
|||||||
possColors[i++] = new core.Color(HUES[h], SATS[s], .5, 1);
|
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++) {
|
for (var i = 0; i < possColors.length * 50; i++) {
|
||||||
var a = Math.floor(Math.random() * possColors.length);
|
var a = Math.floor(Math.random() * possColors.length);
|
||||||
var b = 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.");
|
if (p.name.indexOf("BOT") == -1) log((p.name || "Unnamed") + " (" + p.num + ") joined.");
|
||||||
client.on("requestFrame", function() {
|
client.on("requestFrame", function() {
|
||||||
if (p.frame === frame) return;
|
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) {
|
var splayers = players.map(function(val) {
|
||||||
return val.serialData();
|
return val.serialData();
|
||||||
});
|
});
|
||||||
@ -75,7 +75,7 @@ function Game(id) {
|
|||||||
"grid": gridSerialData(grid, players)
|
"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) {
|
client.on("verify", function(data, resp) {
|
||||||
if (typeof resp !== "function") return;
|
if (typeof resp !== "function") return;
|
||||||
if (!data.frame) resp(false, false, "No frame supplied");
|
if (!data.frame) resp(false, false, "No frame supplied");
|
||||||
@ -104,7 +104,7 @@ function Game(id) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
client.on("disconnect", function() {
|
client.on("disconnect", function() {
|
||||||
p.die(); //Die immediately if not already.
|
p.die(); //Die immediately if not already
|
||||||
p.disconnected = true;
|
p.disconnected = true;
|
||||||
if (p.name.indexOf("BOT") == -1) log((p.name || "Unnamed") + " (" + p.num + ") left.");
|
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[p.num] = [p.posX, p.posY, p.waitLag];
|
||||||
}
|
}
|
||||||
locs.frame = frame;
|
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);
|
frameLocs.push(locs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,7 +147,7 @@ function Game(id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function tick() {
|
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) {
|
var splayers = players.map(function(val) {
|
||||||
return val.serialData();
|
return val.serialData();
|
||||||
});
|
});
|
||||||
@ -163,7 +163,7 @@ function Game(id) {
|
|||||||
return val.serialData();
|
return val.serialData();
|
||||||
});
|
});
|
||||||
var moves = players.map(function(val) {
|
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;
|
val.heading = val.tmpHeading;
|
||||||
return {
|
return {
|
||||||
num: val.num,
|
num: val.num,
|
||||||
|
@ -24,7 +24,7 @@ function mod(x) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function connect() {
|
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);
|
if (!success) setTimeout(connect, 1000);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user