server.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. var engine = require('engine.io');
  2. var server = engine.listen(8924);
  3. /*
  4. Client Server
  5. -hello(gameid, pass)->
  6. <-youAre(playerid)-
  7. -or-
  8. -login(gameid, playerid, pass)->
  9. <-youAre(playerid)-
  10. -newGame(gamename)->
  11. <-initgame(gameid)-
  12. <-master(playerid)-
  13. -startGame->
  14. <-startGame-
  15. -joinGame(gameid)->
  16. <-joined(bool:success)-
  17. <-startTurn(playerid)-
  18. <-pm(otherplayerid)->
  19. -move->
  20. -endTurn->
  21. <-endedTurn(playerid)-
  22. =cheated(playerid)=>
  23. <-disqualified(playerid)- // master should decide what happens?
  24. =playerwon(playerid)=>
  25. <-won(playerid)-
  26. <-lost(playerid)-
  27. */
  28. var Games = {};
  29. var SocketStates = {};
  30. function Game(creator) {
  31. this.id = next_game_id++;
  32. this.players = [creator];
  33. this.master = creator;
  34. this.maxPlayers = 0;
  35. this.lockOnStart = true;
  36. this.canChangeSettingsAfterStart = false;
  37. this.skipAwayPlayers = false;
  38. this.turnNumber = 0;
  39. this.state = "init";
  40. this.currentPlayer = null;
  41. this.settingsBlob = "{}";
  42. this.dataBlob = "{}";
  43. Games[this.id] = this;
  44. this.join(creator);
  45. }
  46. Game.prototype.join = function(player) {
  47. if(-1 !== this.players.indexOf(player))
  48. return "You are already playing";
  49. if(this.maxPlayers && this.maxPlayers >= this.players.length)
  50. return "Maximum players reached";
  51. if(this.lockOnStart && this.state != "init")
  52. return "You cannot join after the game has started";
  53. this.players.push(player);
  54. this.broadcast("joined", player);
  55. return true;
  56. }
  57. Game.prototype.start = function() {
  58. this.state = "playing";
  59. this.broadcast("gameStarted");
  60. this.currentPlayer = this.players[this.players.length-1];
  61. this.nextTurn();
  62. }
  63. Game.prototype.nextTurn = function() {
  64. var i = this.players.indexOf(this.currentPlayer);
  65. // move to next player
  66. i++;
  67. if(i == this.players.length-1) i = 0;
  68. var p = this.players[i];
  69. this.currentPlayer = p;
  70. this.broadcast("startTurn", p);
  71. }
  72. Game.prototype.endTurn = function(player) {
  73. if(player != this.currentPlayer)
  74. return "It is not that player's turn";
  75. this.broadcast("turnEnded", player);
  76. this.nextTurn();
  77. }
  78. Game.prototype.broadcast = function(verb, data) {
  79. var len = this.players.length;
  80. for(var i = 0; i < len; i++) {
  81. this.players[i].sendMessage(verb, data);
  82. }
  83. }
  84. Game.prototype.pm = function(to, verb, data) {
  85. if(-1 === this.players.indexOf(to))
  86. return "Recipient is not in the game";
  87. SocketStates[to].sendMessage(verb, data);
  88. }
  89. function SocketState(sock) {
  90. this.id = next_connection_id++;
  91. this.sock = sock;
  92. this.game = null;
  93. SocketStates[this.id] = this;
  94. sock.send(JSON.stringify({
  95. ordinal: next_message_ordinal++,
  96. name: 'youAre',
  97. data: this.id,
  98. }));
  99. sock.on('message', SocketState.prototype.onMessage.bind(this));
  100. sock.on('close', SocketState.prototype.onClose.bind(this));
  101. }
  102. SocketState.prototype.sendMessage = function(verb, data) {
  103. this.sock.send(JSON.stringify({
  104. ordinal: next_message_ordinal++,
  105. name: verb,
  106. data: data,
  107. }));
  108. }
  109. function jsonParse(x) {
  110. try {
  111. var y = JSON.parse(x);
  112. }
  113. catch(e) {
  114. return undefined;
  115. }
  116. return y;
  117. }
  118. var kosherVerbs = {
  119. newGame: true,
  120. joinGame: true,
  121. startGame: true,
  122. endTurn: true,
  123. move: true,
  124. won: true,
  125. roll: true,
  126. shuffle: true,
  127. userBroadcast: true,
  128. userPM: true,
  129. };
  130. var nonPlayingVerbs = {
  131. newGame: true,
  132. joinGame: true,
  133. };
  134. SocketState.prototype.onMessage = function(e) {
  135. console.log("new message:", e);
  136. var msg = jsonParse(e);
  137. if(msg === undefined) {
  138. return this.sendMessage("error", "Invalid request format");
  139. }
  140. if(!kosherVerbs[msg.name]) {
  141. console.log("invalid verb", msg);
  142. return;
  143. }
  144. if(!nonPlayingVerbs[msg.name] && this.game == null) {
  145. return this.sendMessage("error", "You are not currently in a game");
  146. }
  147. // TODO: check ordinal
  148. this["rcv_" + msg.name](msg.data);
  149. }
  150. SocketState.prototype.onClose = function(e) {
  151. console.log("connection closed:", this.id, e);
  152. delete SocketStates[this.id];
  153. }
  154. SocketState.prototype.rcv_joinGame = function(msg) {
  155. var g = Games[msg.data];
  156. if(!g) {
  157. return this.sendMessage("error", "Game not found");
  158. }
  159. var rep = g.join(this.id);
  160. if(true === rep) {
  161. this.game = Games[msg.data]
  162. return;
  163. }
  164. this.sendMessage("error", rep);
  165. }
  166. SocketState.prototype.rcv_newGame = function(msg) {
  167. var g = new Game(this.id);
  168. this.game = g;
  169. this.sendMessage("gameCreated", g.id);
  170. this.sendMessage("promoted", this.id);
  171. }
  172. SocketState.prototype.rcv_startGame = function(msg) {
  173. this.game.start();
  174. }
  175. SocketState.prototype.rcv_endTurn = function(msg) {
  176. var res = this.game.endTurn(this.id);
  177. if(res === true) return;
  178. this.sendMessage("error", res);
  179. }
  180. SocketState.prototype.rcv_move = function(msg) {
  181. // var g = new Game(this.id);
  182. // this.sendMessage("gameCreated", g.id);
  183. }
  184. SocketState.prototype.rcv_won = function(msg) {
  185. // var g = new Game(this.id);
  186. // this.sendMessage("gameCreated", g.id);
  187. }
  188. SocketState.prototype.rcv_shuffle = function(msg) {
  189. var n = msg|0;
  190. var arr = new Array(n);
  191. for(var i = 0; i < n; i++) arr[i] = i;
  192. this.game.broadcast('shuffled', {
  193. who: this.id,
  194. results: _.shuffle(arr),
  195. });
  196. }
  197. SocketState.prototype.rcv_roll = function(msg) {
  198. var dice = msg.split('d');
  199. var n = dice[0]|0;
  200. var sides = dice[1]|0;
  201. var vals = [];
  202. for(var i = 0; i < n; i++) {
  203. vals.push((Math.random() * sides)|0);
  204. }
  205. this.game.broadcast("rolled", {
  206. who: this.id,
  207. dice: msg,
  208. results: vals,
  209. })
  210. }
  211. SocketState.prototype.rcv_userBroadcast = function(msg) {
  212. this.game.broadcast("userBroadcast", {
  213. from: this.id,
  214. data: msg.data,
  215. });
  216. }
  217. SocketState.prototype.rcv_userPM = function(msg) {
  218. this.game.pm(msg.to, "userPM", {
  219. from: this.id,
  220. to: msg.to,
  221. data: msg.data,
  222. });
  223. }
  224. var next_game_id = 1;
  225. var next_message_ordinal = 1;
  226. var next_connection_id = 1;
  227. server.on('connection', function(socket){
  228. console.log('meh');
  229. new SocketState(socket);
  230. // socket.on('message', idleListener(socket, next_connection_id++))
  231. });