Browse Source

foundation kind of made

master
mort 8 years ago
parent
commit
0dc6e75e87
3 changed files with 90 additions and 4 deletions
  1. 21
    0
      js/game.js
  2. 63
    0
      sections/game.js
  3. 6
    4
      server.js

+ 21
- 0
js/game.js View File

module.exports = function(players) {
this._players = players;

var ps = players.map(function(p) {
return {
type: p.data.type,
}
});

players.forEach(function(p, i) {
p.data.index = i;
p.data.req.reply({
players: ps,
index: i
});
});
}

module.exports.prototype.message = function(args, req, sock) {
}

+ 63
- 0
sections/game.js View File

var Game = require("../js/game.js");

var queues = {
runners: [],
gods: []
}

var games = [];

function cleanQueue(queue) {
queue.forEach(function(elem, i) {
if (elem.stale)
queue.splice(i, 1);
});
}

function tryStartGame() {
cleanQueue(queues.runners);
cleanQueue(queues.gods);

if (queues.runners.length === 0 || queues.gods.length === 0)
return;

var players = [queues.runners.shift(), queues.gods.shift()];

var game = new Game(players);
players.forEach(function(p) {
p.data.game = game;
});
games.push(game);
}

module.exports = function(args, req, sock) {
if (args[1] === "start") {
sock.data = {
req: req,
type: args[2],
game: null,
stale: false
}

if (args[2] === "runner") {
queues.runners.push(sock);
} else if (args[2] === "god") {
queues.gods.push(sock);
} else {
return req.fail();
}

sock.on("close", function() {
sock.data.stale = true;
});

tryStartGame();

return;
} else {
if (!sock.data.game)
return req.error("Not in a game.");

sock.data.game.message(args, req, sock);
}
}

+ 6
- 4
server.js View File

var SockSugar = require("socksugar"); var SockSugar = require("socksugar");


var server = new SockSugar({ var server = new SockSugar({
port: 8081
port: 8083
}); });


var sections = {};
var sections = {
game: require("./sections/game.js")
};


server.on("connection", function(sock) { server.on("connection", function(sock) {
console.log("connection"); console.log("connection");
var s = sections[parts[0]]; var s = sections[parts[0]];


if (s === undefined) if (s === undefined)
return req.error("Not found");
return req.error("Section not found.");


s(parts, req);
s(parts, req, sock);
}); });
}); });

Loading…
Cancel
Save