Modularize Node.js project

This commit is contained in:
Henry Wang 2017-10-30 21:24:39 -05:00
parent d7f1af47e2
commit 2ddfc7c0a0
17 changed files with 2154 additions and 2130 deletions

View File

@ -3,12 +3,12 @@ if (process.argv.length < 3) {
process.exit(1);
}
var core = require("./game-core");
var client = require("./player-client.js");
var consts = require("./game-consts.js");
var user;
var GRID_SIZE = consts.GRID_SIZE;
var CELL_WIDTH = consts.CELL_WIDTH;
var GRID_SIZE = core.GRID_SIZE;
var CELL_WIDTH = core.CELL_WIDTH;
function connect() {
client.connectGame(process.argv[2], process.argv[3] || '[BOT]', function(success, msg) {

View File

@ -1,14 +1,13 @@
/* global $ */
var Rolling = require("./rolling.js");
var Color = require("./color.js");
var Grid = require("./grid.js");
var consts = require("./game-consts.js");
var client = require("./player-client.js");
var GRID_SIZE = consts.GRID_SIZE;
var CELL_WIDTH = consts.CELL_WIDTH;
var SPEED = consts.SPEED;
var BORDER_WIDTH = consts.BORDER_WIDTH;
var core = require("../game-core");
var client = require("../client");
var Rolling = require("./rolling");
var GRID_SIZE = core.GRID_SIZE;
var CELL_WIDTH = core.CELL_WIDTH;
var SPEED = core.SPEED;
var BORDER_WIDTH = core.BORDER_WIDTH;
var SHADOW_OFFSET = 5;
var ANIMATE_FRAMES = 24;
var BOUNCE_FRAMES = [8, 4];
@ -58,7 +57,7 @@ function updateSize()
}
function reset() {
animateGrid = new Grid(GRID_SIZE);
animateGrid = new core.Grid(GRID_SIZE);
playerPortion = [];
portionsRolling = [];
@ -119,7 +118,7 @@ function paintGrid(ctx)
if (animateSpec.before) //fading animation
{
var frac = (animateSpec.frame / ANIMATE_FRAMES);
var back = new Color(.58, .41, .92, 1);
var back = new core.Color(.58, .41, .92, 1);
baseColor = animateSpec.before.lightBaseColor.interpolateToString(back, frac);
shadowColor = animateSpec.before.shadowColor.interpolateToString(back, frac);
}
@ -449,4 +448,4 @@ module.exports = exports = {
},
paint: paintDoubleBuff,
update: update
};
};

View File

@ -1,11 +1,10 @@
var Player = require('./player.js');
var Grid = require('./grid.js');
var consts = require('./game-consts.js');
var core = require('./game-core.js');
var core = require('./game-core');
var Player = core.Player;
var io = require('socket.io-client');
var GRID_SIZE = consts.GRID_SIZE;
var CELL_WIDTH = consts.CELL_WIDTH;
var GRID_SIZE = core.GRID_SIZE;
var CELL_WIDTH = core.CELL_WIDTH;
var running = false;
var user, socket, frame;
@ -21,7 +20,7 @@ var frameCache = []; //Frames after our request.
var allowAnimation = true;
var grid = new Grid(consts.GRID_SIZE, function(row, col, before, after) {
var grid = new core.Grid(core.GRID_SIZE, function(row, col, before, after) {
invokeRenderer('updateGrid', [row, col, before, after]);
});
@ -366,4 +365,4 @@ Object.defineProperties(exports, {
get: function() { return kills; },
enumerable: true
}
});
});

View File

@ -1,13 +1,13 @@
/* global $ */
var client = require("./player-client.js");
var consts = require("./game-consts.js");
var client = require("./client");
var core = require("./game-core");
var io = require('socket.io-client');
var GRID_SIZE = consts.GRID_SIZE;
var CELL_WIDTH = consts.CELL_WIDTH;
var GRID_SIZE = core.GRID_SIZE;
var CELL_WIDTH = core.CELL_WIDTH;
client.allowAnimation = true;
client.renderer = require("./game-renderer.js");
client.renderer = require("./client-modes/user-mode");
/**
* Provides requestAnimationFrame in a cross browser way. (edited so that this is also compatible with node.)
@ -118,4 +118,4 @@ $(document).keydown(function(e) {
client.changeHeading(newHeading);
e.preventDefault();
});
});

View File

@ -1,5 +1,4 @@
function constant(val)
{
function constant(val) {
return {
value: val,
enumerable: true
@ -14,4 +13,4 @@ var consts = {
MAX_PLAYERS: constant(81)
};
Object.defineProperties(module.exports, consts);
Object.defineProperties(module.exports, consts);

17
game-core/index.js Normal file
View File

@ -0,0 +1,17 @@
var core = require('./game-core');
var consts = require('./game-consts');
exports.Color = require('./color');
exports.Grid = require('./grid');
exports.Player = require('./player');
exports.initPlayer = core.initPlayer;
exports.updateFrame = core.updateFrame;
for (var prop in consts) {
Object.defineProperty(exports, prop, {
enumerable: true,
value: consts[prop]
});
}

View File

@ -1,5 +1,5 @@
var Stack = require("./stack.js");
var Color = require("./color.js");
var Stack = require("./stack");
var Color = require("./color");
var Grid = require("./grid.js");
var consts = require("./game-consts.js");
@ -500,4 +500,4 @@ function move(data)
this.tail.addTail(heading);
}
module.exports = Player;
module.exports = Player;

View File

@ -1,14 +1,9 @@
var Color = require("./color");
var Grid = require("./grid");
var Player = require("./player");
//var Gate = require("./gate");
var core = require("./game-core");
var consts = require("./game-consts");
var GRID_SIZE = consts.GRID_SIZE;
var CELL_WIDTH = consts.CELL_WIDTH;
var MAX_PLAYERS = consts.MAX_PLAYERS;
var GRID_SIZE = core.GRID_SIZE;
var CELL_WIDTH = core.CELL_WIDTH;
var MAX_PLAYERS = core.MAX_PLAYERS;
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(function(val) {return val / 240});
var SATS = [192, 150, 100].map(function(val) {return val / 240});
@ -30,7 +25,7 @@ function Game(id)
i = 0;
for (var s = 0; s < SATS.length; s++)
for (var h = 0; h < HUES.length; h++)
possColors[i++] = new Color(HUES[h], SATS[s], .5, 1);
possColors[i++] = new core.Color(HUES[h], SATS[s], .5, 1);
var nextInd = 0;
var players = [];
@ -39,7 +34,7 @@ function Game(id)
var frame = 0;
var filled = 0;
var grid = new Grid(GRID_SIZE, function(row, col, before, after) {
var grid = new core.Grid(GRID_SIZE, function(row, col, before, after) {
if (!!after ^ !!before)
{
if (after)
@ -70,7 +65,7 @@ function Game(id)
base: possColors.shift()
};
var p = new Player(grid, params);
var p = new core.Player(grid, params);
p.tmpHeading = params.currentHeading;
p.client = client;
players.push(p);
@ -312,4 +307,4 @@ function findEmpty(grid)
return available[Math.floor(available.length * Math.random())];
}
module.exports = Game;
module.exports = Game;

47
gate.js
View File

@ -1,47 +0,0 @@
var inherits = require('util').inherits;
var EventEmitter = require('events').EventEmitter;
module.exports = Gate;
function Gate(awaiting)
{
var _this = this;
if (!(this instanceof Gate))
return new Gate(awaiting);
if (typeof awaiting !== "number")
awaiting = 0;
var currentAwaiting = awaiting;
var readyCount = 0;
var ready = new Array(currentAwaiting);
this.setAwaiting = function(count) {
awaiting = count;
};
this.ready = function(ind) {
if (Math.floor(ind) != ind || ind >= readyCount)
return false;
ready[ind] = true;
readyCount++;
_this.emit("ready", ind);
if (readyCount >= currentAwaiting)
{
_this.emit("allReady");
_this.reset();
}
return true;
};
this.reset = function() {
_this.emit("reset");
ready = new Array(currentAwaiting = awaiting);
readyCount = 0;
};
EventEmitter.call(this);
}
inherits(Gate, EventEmitter);

48
npm-debug.log Normal file
View File

@ -0,0 +1,48 @@
0 info it worked if it ends with ok
1 verbose cli [ '/usr/lib/node-v6.11.3-linux-x64/bin/node',
1 verbose cli '/usr/bin/npm',
1 verbose cli 'start',
1 verbose cli '8080' ]
2 info using npm@3.10.10
3 info using node@v6.11.3
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle blockly-io@0.9.0~prestart: blockly-io@0.9.0
6 silly lifecycle blockly-io@0.9.0~prestart: no script for prestart, continuing
7 info lifecycle blockly-io@0.9.0~start: blockly-io@0.9.0
8 verbose lifecycle blockly-io@0.9.0~start: unsafe-perm in lifecycle true
9 verbose lifecycle blockly-io@0.9.0~start: PATH: /usr/lib/node-v6.11.3-linux-x64/lib/node_modules/npm/bin/node-gyp-bin:/home/henry/git/BlocklyIO/node_modules/.bin:/usr/lib/node-v6.11.3-linux-x64/bin:/home/henry/bin:/home/henry/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
10 verbose lifecycle blockly-io@0.9.0~start: CWD: /home/henry/git/BlocklyIO
11 silly lifecycle blockly-io@0.9.0~start: Args: [ '-c', 'node server.js "8080"' ]
12 silly lifecycle blockly-io@0.9.0~start: Returned: code: 1 signal: null
13 info lifecycle blockly-io@0.9.0~start: Failed to exec start script
14 verbose stack Error: blockly-io@0.9.0 start: `node server.js "8080"`
14 verbose stack Exit status 1
14 verbose stack at EventEmitter.<anonymous> (/usr/lib/node-v6.11.3-linux-x64/lib/node_modules/npm/lib/utils/lifecycle.js:255:16)
14 verbose stack at emitTwo (events.js:106:13)
14 verbose stack at EventEmitter.emit (events.js:191:7)
14 verbose stack at ChildProcess.<anonymous> (/usr/lib/node-v6.11.3-linux-x64/lib/node_modules/npm/lib/utils/spawn.js:40:14)
14 verbose stack at emitTwo (events.js:106:13)
14 verbose stack at ChildProcess.emit (events.js:191:7)
14 verbose stack at maybeClose (internal/child_process.js:920:16)
14 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:230:5)
15 verbose pkgid blockly-io@0.9.0
16 verbose cwd /home/henry/git/BlocklyIO
17 error Linux 4.10.0-37-generic
18 error argv "/usr/lib/node-v6.11.3-linux-x64/bin/node" "/usr/bin/npm" "start" "8080"
19 error node v6.11.3
20 error npm v3.10.10
21 error code ELIFECYCLE
22 error blockly-io@0.9.0 start: `node server.js "8080"`
22 error Exit status 1
23 error Failed at the blockly-io@0.9.0 start script 'node server.js "8080"'.
23 error Make sure you have the latest version of node.js and npm installed.
23 error If you do, this is most likely a problem with the blockly-io package,
23 error not with npm itself.
23 error Tell the author that this fails on your system:
23 error node server.js "8080"
23 error You can get information on how to open an issue for this project with:
23 error npm bugs blockly-io
23 error Or if that isn't available, you can get their info via:
23 error npm owner ls blockly-io
23 error There is likely additional logging output above.
24 verbose exit [ 1, true ]

File diff suppressed because it is too large Load Diff

View File

@ -1,13 +0,0 @@
var ws = require("nodejs-websocket")
// Scream server example: "hi" -> "HI!!!"
var server = ws.createServer(function (conn) {
console.log("New connection")
conn.on("text", function (str) {
console.log("Received "+str)
conn.sendText(str.toUpperCase()+"!!!")
})
conn.on("close", function (code, reason) {
console.log("Connection closed")
})
}).listen(8081);