server-tools.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. 'use strict';
  2. import io from 'socket.io-client';
  3. // process.exit(1);
  4. var usage = function() {
  5. var text = `
  6. USAGE : npm run tools <option>
  7. with <option> :
  8. - players : get authorized players
  9. - add-player <username> : Add a new authorized player
  10. - remove-player <username> : Remove an authorized player
  11. - syncdb : Force server to sync with db
  12. - add-game <json>: Add new game into DB
  13. '{"player1":"","player2":"","deck":"","advRules":[],"status":"","data":"{}"}'
  14. deck : faction tournament draft (one of these)
  15. advRules : popularity discard (0 or any of these)
  16. status : CREATED ONGOING FINISHED (one of these)
  17. - remove-game <method> <filter> : Remove game(s) from DB
  18. <method> / <filter>
  19. byId gameId
  20. byPlayer1 username
  21. byPlayerAny username
  22. byStatus CREATED ONGOING FINISHED (one of these)
  23. byDays number of days old is last connection
  24. finishedByDays number of days old is last connection(only finished)
  25. `;
  26. console.log(text);
  27. };
  28. var args = process.argv.slice(2);
  29. if (args.length === 0) {
  30. usage();
  31. process.exit(0);
  32. }
  33. var command = '';
  34. var option = '';
  35. switch (args[0]) {
  36. case 'players':
  37. case 'syncdb':
  38. command = args[0];
  39. break;
  40. case 'add-player':
  41. case 'remove-player':
  42. case 'add-game':
  43. case 'remove-game':
  44. if (args[0] === 'remove-game' && args.length < 3) {
  45. usage();
  46. process.exit(0);
  47. }
  48. if (args[1] && args[1] !== '') {
  49. command = args[0];
  50. if (args[0] === 'add-game') {
  51. try {
  52. option = [JSON.parse(args[1])];
  53. } catch (error) {
  54. console.log(error);
  55. process.exit(0);
  56. }
  57. } else {
  58. args.shift();
  59. option = args;
  60. }
  61. } else {
  62. console.log('error ; specify a username');
  63. usage();
  64. }
  65. break;
  66. default:
  67. usage();
  68. break;
  69. }
  70. if (command !== '') {
  71. console.log('Connecting to server...');
  72. var socket = io('http://localhost:1664');
  73. socket.emit(command, option, function(response) {
  74. console.log('Done, response from server : ', response);
  75. });
  76. }