You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

game.js 1.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. var Game = require("../js/game.js");
  2. var queues = {
  3. runners: [],
  4. gods: []
  5. }
  6. var games = [];
  7. function cleanQueue(queue) {
  8. queue.forEach(function(elem, i) {
  9. if (elem.stale)
  10. queue.splice(i, 1);
  11. });
  12. }
  13. function tryStartGame() {
  14. cleanQueue(queues.runners);
  15. cleanQueue(queues.gods);
  16. if (queues.runners.length === 0 || queues.gods.length === 0)
  17. return;
  18. var players = [queues.runners.shift(), queues.gods.shift()];
  19. var game = new Game(players);
  20. players.forEach(function(p) {
  21. p.data.game = game;
  22. });
  23. games.push(game);
  24. }
  25. module.exports = function(args, req, sock) {
  26. if (args[1] === "start") {
  27. sock.data = {
  28. req: req,
  29. type: args[2],
  30. game: null,
  31. stale: false
  32. }
  33. if (args[2] === "runner") {
  34. queues.runners.push(sock);
  35. } else if (args[2] === "god") {
  36. queues.gods.push(sock);
  37. } else {
  38. return req.fail();
  39. }
  40. sock.on("close", function() {
  41. sock.data.stale = true;
  42. });
  43. tryStartGame();
  44. return;
  45. } else {
  46. if (!sock.data.game)
  47. return req.error("Not in a game.");
  48. sock.data.game.message(args, req, sock);
  49. }
  50. }