Guest User

Untitled

a guest
Dec 10th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. const models = require('../../models/index');
  2. const _config = require('../../config/_config');
  3. let Client = require('ssh2-sftp-client');
  4. var fs = require('fs');
  5. const sftp = new Client();
  6. const RemoteServer = {
  7. getStatistic: () => {
  8. return models.Tournament.findAll({
  9. where: {
  10. gameId: 2,//TODO:: времянка
  11. startTime: {
  12. $lt: Date.now()
  13. },
  14. status: {
  15. $ne: 'canceled'
  16. }
  17. },
  18. include: [
  19. {model: models.Server.scope('fullInfoForStat'), as: 'server'},
  20. {model: models.User, as: 'users'}
  21.  
  22. ]
  23. })
  24. .then((tournaments) => {
  25. console.log("Турніри", tournaments);
  26. if(tournaments.length == 0) return;
  27. return tournaments.map(tournament => {
  28. const usersInTournaments = tournament.users;
  29. return sftp.connect({
  30. host: tournament.server.ip,
  31. port: 22,
  32. username: tournament.server.user,
  33. password: tournament.server.password,
  34. })
  35. .then(() => {
  36. return sftp.list('serverfiles/csgo');
  37. })
  38. .then(logs => {
  39. console.log("Логи", logs);
  40. const filterLogs = logs.filter((log) => {
  41. // console.log('Час турніру', tournament.startTime.getTime());
  42. // console.log('Час логу', log.modifyTime);
  43. return getExtension(log.name) === '.txt' && log.modifyTime > tournament.startTime.getTime() && log.size != 0;
  44. });
  45. return filterLogs.reduce((prev, current) => (prev.modifyTime > current.modifyTime) ? prev : current);
  46. })
  47. .then(log => {
  48. //console.log('Фильтровані логи', log);
  49. return sftp.get('serverfiles/csgo/' + log.name);
  50. })
  51. .then(readStream => {
  52. return new Promise((resolve, reject) => {
  53. let logData = '';
  54. readStream.on("data", (data) => {
  55. logData += data;
  56. });
  57. readStream.on("end", () => {
  58. console.log("<===== END ЛОГ файл=====>");
  59. resolve(logData);
  60. });
  61. readStream.on('error', function(err){
  62. reject(err);
  63. });
  64. })
  65.  
  66. })
  67. .then(content => {
  68. console.log('Зміст файлу', content);
  69. const linesContent = content.split("\n");
  70. usersInTournaments.map(user => {
  71. const statisticToUserTournament = {
  72. side: '',
  73. killed: 0,
  74. //deaths: 0,
  75. killedHeadshot: 0
  76. };
  77. //statisticToUserTournament.side = '';
  78. //console.info('user.steamId64', user.steamId64);
  79. const statisticToTournament = {
  80. teamScoreCT: 0,
  81. teamScoreTERRORIST: 0,
  82. currentRound: 0,
  83. isGameOver: 0
  84.  
  85. };
  86.  
  87.  
  88. return Promise.all([
  89. models.UserToTournament.update({
  90. statistic: statisticToUserTournament,
  91. }, {
  92. where: {
  93. userId: user.id,
  94. tournamentId: tournament.id
  95.  
  96. },
  97. returning: true
  98. }),
  99. models.Tournament.update({
  100. statistic: statisticToTournament,
  101. status: (statisticToTournament.isGameOver) ? 'finished' : 'inProgress'
  102. }, {
  103. where: {
  104. id: tournament.id
  105. },
  106. returning: true
  107. })
  108.  
  109. ]);
  110. });
  111.  
  112. })
  113.  
  114. });
  115. })
  116. .catch((err) => {
  117. console.log('err->RemoteServer-getStatistic', err);
  118. sftp.end();
  119. })
  120. }
  121. }
  122. function getExtension(filename) {
  123. var i = filename.lastIndexOf('.');
  124. return (i < 0) ? '' : filename.substr(i);
  125. }
  126.  
  127. function getStatusGame(string){
  128. if(string.indexOf("Game Over") !== -1) return true;
  129. }
  130. function currentRound(string){
  131. if(string.indexOf("World triggered \"Round_Start\"") !== -1) return true;
  132. }
  133. function isKilled(string){
  134. if(string.indexOf("killed") !== -1) return true;
  135. }
  136. function isKilledHeadshot(string){
  137. if(string.indexOf("headshot") !== -1) return true;
  138. }
  139. function isDisconnected(string){
  140. if(string.indexOf("disconnected") !== -1) return true;
  141. }
  142. function getCTscore(string){
  143. //string.match(/Team "CT" scored "/i)
  144. return res = string.match(/\(([a-z]+) \"([0-9]+)\"\) \(([a-z]+) \"([0-9]+)\"\)/i);
  145.  
  146. }
  147. module.exports = RemoteServer;
Add Comment
Please, Sign In to add comment