/config/lib/server_state.js

https://gitlab.com/dogcalas/solaci · JavaScript · 366 lines · 301 code · 48 blank · 17 comment · 48 complexity · 09ba4daddde84b793378b711da655666 MD5 · raw file

  1. 'use strict';
  2. var moment = require('moment');
  3. require('moment-range');
  4. var universe = global || window;
  5. // Define the Socket.io configuration method
  6. exports = module.exports = ServerState;
  7. function ServerState() {
  8. if (!(this instanceof ServerState)) {
  9. var singleton = universe.server_state;
  10. return singleton ? singleton : new ServerState();
  11. }
  12. if (universe.server_state) {
  13. throw 'El server state no puede crearse dos veces';
  14. }
  15. universe.server_state = this;
  16. // persistent data
  17. this.fecha_concurso = null;
  18. this.concurso_activo = false;
  19. this.concurso_pregunta = false;
  20. this.concurso_moderacion = false;
  21. this.concurso_fin_preguntas = false;
  22. this.concurso_ganador = false;
  23. this.concurso_finalizado = false;
  24. this.users_count = 0;
  25. this.start_time = moment();
  26. this.pregunta_activa = 0;
  27. // transient data
  28. this.reg_preguntas = [];
  29. this.reg_options = [];
  30. this.lista_preguntas = [];
  31. }
  32. ServerState.prototype.getServerStatus = function () {
  33. var mfactual = moment();
  34. var mfconcurso = moment(this.fecha_concurso);
  35. var dr = moment.range(mfactual, mfconcurso);
  36. var diff = dr.diff();
  37. if (diff > 0) {
  38. // En espera de concurso...
  39. return 1;
  40. } else if (this.concurso_activo) {
  41. // Concurso activo...
  42. if (this.concurso_pregunta) {
  43. // Servidor inicializa en realizar pregunta...
  44. return 3;
  45. } else if (this.concurso_moderacion) {
  46. // Servidor inicializa en moderación de pregunta...
  47. return 4;
  48. } else if (this.concurso_fin_preguntas) {
  49. // Servidor inicializa en finalización de preguntas...
  50. return 5;
  51. } else if (this.concurso_ganador) {
  52. // Servidor inicializa en presentación del ganador...
  53. return 6;
  54. } else {
  55. //Servidor inicializa en presentacion del concurso...
  56. return 2;
  57. }
  58. } else if (this.concurso_finalizado) {
  59. //Servidor no realiza más acciones, fin del concurso...
  60. return 7;
  61. }
  62. };
  63. ServerState.prototype.setListaPreguntas = function (lista_preguntas) {
  64. this.lista_preguntas = lista_preguntas;
  65. };
  66. // start_time
  67. ServerState.prototype.setStartTime = function (startTime) {
  68. this.start_time = startTime;
  69. };
  70. ServerState.prototype.getStartTime = function () {
  71. return this.start_time;
  72. };
  73. // Acciones de la máquina de estado
  74. ServerState.prototype.esperarConcurso = function () {
  75. this.concurso_activo = false;
  76. this.concurso_pregunta = false;
  77. this.concurso_moderacion = false;
  78. this.concurso_fin_preguntas = false;
  79. this.concurso_ganador = false;
  80. this.concurso_finalizado = false;
  81. this.users_count = 0;
  82. this.reset();
  83. };
  84. // Acciones de la máquina de estado
  85. ServerState.prototype.restartServer = function () {
  86. this.concurso_activo = true;
  87. this.concurso_pregunta = false;
  88. this.concurso_moderacion = false;
  89. this.concurso_fin_preguntas = false;
  90. this.concurso_ganador = false;
  91. this.concurso_finalizado = false;
  92. this.users_count = 0;
  93. this.reset();
  94. this.saveData();
  95. };
  96. ServerState.prototype.iniciarConcurso = function () {
  97. this.concurso_activo = true;
  98. this.concurso_pregunta = false;
  99. this.concurso_moderacion = false;
  100. this.concurso_fin_preguntas = false;
  101. this.concurso_ganador = false;
  102. this.concurso_finalizado = false;
  103. this.users_count = 0;
  104. this.reset();
  105. };
  106. ServerState.prototype.realizarPregunta = function () {
  107. this.concurso_activo = true;
  108. this.concurso_pregunta = true;
  109. this.concurso_moderacion = false;
  110. this.concurso_fin_preguntas = false;
  111. this.concurso_ganador = false;
  112. this.concurso_finalizado = false;
  113. this.reg_preguntas = [];
  114. this.reg_options = [];
  115. };
  116. ServerState.prototype.moderarPregunta = function () {
  117. this.concurso_activo = true;
  118. this.concurso_pregunta = false;
  119. this.concurso_moderacion = true;
  120. this.concurso_fin_preguntas = false;
  121. this.concurso_ganador = false;
  122. this.concurso_finalizado = false;
  123. };
  124. ServerState.prototype.finalizarPreguntas = function () {
  125. this.concurso_activo = true;
  126. this.concurso_pregunta = false;
  127. this.concurso_moderacion = false;
  128. this.concurso_fin_preguntas = true;
  129. this.concurso_ganador = false;
  130. this.concurso_finalizado = false;
  131. };
  132. ServerState.prototype.presentarGanador = function () {
  133. this.concurso_activo = true;
  134. this.concurso_pregunta = false;
  135. this.concurso_moderacion = false;
  136. this.concurso_fin_preguntas = false;
  137. this.concurso_ganador = true;
  138. this.concurso_finalizado = false;
  139. };
  140. ServerState.prototype.finalizarConcurso = function () {
  141. this.concurso_activo = false;
  142. this.concurso_pregunta = false;
  143. this.concurso_moderacion = false;
  144. this.concurso_fin_preguntas = false;
  145. this.concurso_ganador = false;
  146. this.concurso_finalizado = true;
  147. };
  148. ServerState.prototype.changeDate = function (date) {
  149. this.fecha_concurso = moment(date, 'DD/MM/YYYY HH:mm');
  150. this.saveData();
  151. };
  152. ServerState.prototype.getDate = function () {
  153. return this.fecha_concurso;
  154. };
  155. // Register Clients Answers
  156. ServerState.prototype.registerAnswer = function (uid, qid, oid, time) {
  157. if (!this.reg_preguntas[uid.toString()]) {
  158. this.reg_preguntas[uid.toString()] = {
  159. 'uid': uid,
  160. 'qid': qid,
  161. 'oid': oid,
  162. 'time': time
  163. };
  164. if (this.reg_options[oid.toString()]) {
  165. this.reg_options[oid.toString()].count++;
  166. } else {
  167. let option = this.getOptionQuestion(qid, oid);
  168. if (option) {
  169. this.reg_options[oid.toString()] = {
  170. 'name': option.name,
  171. 'correct': option.correct,
  172. 'count': 1
  173. };
  174. }
  175. }
  176. }
  177. };
  178. ServerState.prototype.getOptionStatistic = function () {
  179. var result = [];
  180. for (var key in this.reg_options){
  181. result.push(this.reg_options[key]);
  182. }
  183. return result;
  184. };
  185. ServerState.prototype.getOptionQuestion = function (qid, oid) {
  186. let pregunta = this.previus();
  187. let voption = null;
  188. if (pregunta && pregunta._id.toString() === qid.toString()) {
  189. pregunta.options.forEach(function (value) {
  190. if (oid.toString() === value._id.toString()) {
  191. voption = value;
  192. return value;
  193. }
  194. });
  195. }
  196. return voption;
  197. };
  198. ServerState.prototype.getRegisterAnswer = function (uid) {
  199. if (this.reg_preguntas[uid.toString()]) {
  200. return this.reg_preguntas[uid.toString()];
  201. } else {
  202. return false;
  203. }
  204. };
  205. ServerState.prototype.existUserAnswer = function (uid) {
  206. if (this.reg_preguntas[uid.toString()]) {
  207. return true;
  208. }
  209. return false;
  210. };
  211. // Iterate Questions
  212. ServerState.prototype.reset = function () {
  213. this.pregunta_activa = 0;
  214. };
  215. ServerState.prototype.revert = function () {
  216. if (this.pregunta_activa > 0) {
  217. this.pregunta_activa--;
  218. }
  219. };
  220. ServerState.prototype.previus = function () {
  221. if (this.lista_preguntas !== null) {
  222. var idx = this.pregunta_activa - 1;
  223. if (idx >= 0) {
  224. return this.lista_preguntas[idx];
  225. }
  226. }
  227. return null;
  228. };
  229. ServerState.prototype.next = function () {
  230. if (this.lista_preguntas !== null) {
  231. if (this.pregunta_activa < this.lista_preguntas.length) {
  232. return this.lista_preguntas[this.pregunta_activa++];
  233. }
  234. }
  235. return null;
  236. };
  237. ServerState.prototype.current = function () {
  238. if (this.lista_preguntas !== null) {
  239. if (this.pregunta_activa < this.lista_preguntas.length) {
  240. return this.lista_preguntas[this.pregunta_activa];
  241. }
  242. }
  243. return null;
  244. };
  245. // Persist JSON Config
  246. var jsonfile = require('jsonfile');
  247. var fs = require('fs');
  248. var path = require('path');
  249. var mkdirSync = function (path) {
  250. try {
  251. fs.mkdirSync(path);
  252. } catch (e) {
  253. if (e.code !== 'EEXIST') throw e;
  254. }
  255. };
  256. function fileExists(filePath) {
  257. try {
  258. var result = fs.statSync(filePath).isFile();
  259. return result;
  260. }
  261. catch (err) {
  262. return false;
  263. }
  264. }
  265. ServerState.prototype.loadData = function () {
  266. if (!fileExists('data/data_config.json')) {
  267. mkdirSync('data');
  268. this.fecha_concurso = moment('06/10/2016 00:55', 'DD/MM/YYYY HH:mm');
  269. this.pregunta_activa = 0;
  270. this.concurso_activo = false;
  271. this.concurso_pregunta = false;
  272. this.concurso_moderacion = false;
  273. this.concurso_fin_preguntas = false;
  274. this.concurso_ganador = false;
  275. this.concurso_finalizado = false;
  276. this.users_count = 0;
  277. this.saveData();
  278. } else {
  279. var jsonData = jsonfile.readFileSync('data/data_config.json');
  280. if (jsonData) {
  281. this.fecha_concurso = moment(jsonData.fecha_concurso, 'DD/MM/YYYY HH:mm');
  282. this.pregunta_activa = jsonData.pregunta_activa || 0;
  283. this.concurso_activo = jsonData.concurso_activo || false;
  284. this.concurso_pregunta = jsonData.concurso_pregunta || false;
  285. this.concurso_moderacion = jsonData.concurso_moderacion || false;
  286. this.concurso_fin_preguntas = jsonData.concurso_fin_preguntas || false;
  287. this.concurso_ganador = jsonData.concurso_ganador || false;
  288. this.concurso_finalizado = jsonData.concurso_finalizado || false;
  289. this.users_count = jsonData.concurso_usuarios || 0;
  290. var server_state = this;
  291. setInterval(function () {
  292. server_state.saveData();
  293. }, 5000);
  294. console.log('Server state loaded ...');
  295. } else {
  296. console.log('Error reading server configuration');
  297. }
  298. }
  299. };
  300. ServerState.prototype.saveData = function () {
  301. var server_state = this;
  302. var jsonData = {
  303. 'fecha_concurso': moment(server_state.fecha_concurso).format('DD/MM/YYYY HH:mm'),
  304. 'pregunta_activa': server_state.pregunta_activa,
  305. 'concurso_activo': server_state.concurso_activo,
  306. 'concurso_pregunta': server_state.concurso_pregunta,
  307. 'concurso_moderacion': server_state.concurso_moderacion,
  308. 'concurso_fin_preguntas': server_state.concurso_fin_preguntas,
  309. 'concurso_ganador': server_state.concurso_ganador,
  310. 'concurso_finalizado': server_state.concurso_finalizado,
  311. 'concurso_usuarios': server_state.users_count
  312. };
  313. jsonfile.writeFile('data/data_config.json', jsonData, function (err) {
  314. if (err) {
  315. console.log(err.message);
  316. }
  317. });
  318. };