core.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. var glob = require('glob'),
  2. fs = require('fs'),
  3. crypto = require('crypto');
  4. commands = require('./commands');
  5. session = {};
  6. class Core {
  7. constructor() {
  8. this.timerId = -1;
  9. this.lastTime = 0;
  10. }
  11. load() {
  12. session.players = [];
  13. session.playerIndex = 1;
  14. if(this.timerId != -1 ) {
  15. clearInterval(this.timerId);
  16. }
  17. this.lastTime = new Date().getTime();
  18. //todo: populate rooms
  19. this.timerId = setInterval(function() {
  20. core.tick(new Date().getTime());
  21. }, 250);
  22. }
  23. handleInput(player, input) {
  24. var action = input.action;
  25. for(var commanderClass in commands) {
  26. var commander = new commands[commanderClass]();
  27. if (typeof commander[action] == 'function') {
  28. try {
  29. if(commander[action](player, input)) {
  30. return true;
  31. }
  32. } catch (error) {
  33. console.error("Exception in command " + commanderClass, error, error.stack);
  34. }
  35. }
  36. }
  37. throw new Error("Unhandled Command");
  38. return true;
  39. }
  40. tick(time) {
  41. var delta = this.getUpdateRate(this.lastTime, time);
  42. this.lastTime = time;
  43. try {
  44. //loop over tick-able items
  45. for(var i in session.players) {
  46. var player = session.players[i];
  47. if(player == null) {
  48. continue;
  49. }
  50. player.update(delta);
  51. }
  52. } catch (error) {
  53. console.error("Tick error", error, error.stack);
  54. }
  55. }
  56. sendUpdateToAll(sender) {
  57. for(var i in session.players) {
  58. var player = session.players[i];
  59. if(player == null) {
  60. continue;
  61. }
  62. player.sendUpdate(sender);
  63. }
  64. }
  65. sendToAll(data) {
  66. for(var i in session.players) {
  67. var player = session.players[i];
  68. if(player == null) {
  69. continue;
  70. }
  71. player.sendMessage(data);
  72. }
  73. }
  74. getUpdateRate(start, stop) {
  75. return (stop - start) / 16;
  76. }
  77. getColor() {
  78. var colors = [
  79. "#DD3333",
  80. "#DDDD33",
  81. "#33DD33",
  82. "#33DDDD",
  83. "#3333DD",
  84. "#333333",
  85. ];
  86. return colors[parseInt(Math.random() * colors.length)];
  87. }
  88. attachPlayer(player) {
  89. player.id = session.playerIndex++;
  90. player.shipColor = this.getColor();
  91. player.playerName = "Player " + player.id;
  92. for(var i in session.players) {
  93. var other = session.players[i];
  94. if(other == null) {
  95. continue;
  96. }
  97. player.sendUpdate(other);
  98. }
  99. session.players[player.id] = player;
  100. core.sendUpdateToAll(player);
  101. }
  102. removePlayer(player) {
  103. session.players[session.players.indexOf(player)] = null;
  104. core.sendToAll({"remove": player.id});
  105. }
  106. rollRandom(probabilitySet) {
  107. var rollValue = parseInt(Math.random() * 100) + 1;
  108. var probabilityTotal = 0;
  109. var sortedSet = probabilitySet.sort(function(a, b) {
  110. return (a.probability < b.probability) ? 1 : ((a.probability > b.probability) ? -1 : 0);
  111. });
  112. for(var index in sortedSet) {
  113. var row = sortedSet[index];
  114. probabilityTotal += row.probability;
  115. if(rollValue <= probabilityTotal) {
  116. return row.value;
  117. }
  118. }
  119. }
  120. }
  121. module.exports = new Core();