Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const models = require('../../models/index');
- const _config = require('../../config/_config');
- let Client = require('ssh2-sftp-client');
- var fs = require('fs');
- const sftp = new Client();
- const RemoteServer = {
- getStatistic: () => {
- return models.Tournament.findAll({
- where: {
- gameId: 2,//TODO:: времянка
- startTime: {
- $lt: Date.now()
- },
- status: {
- $ne: 'canceled'
- }
- },
- include: [
- {model: models.Server.scope('fullInfoForStat'), as: 'server'},
- {model: models.User, as: 'users'}
- ]
- })
- .then((tournaments) => {
- console.log("Турніри", tournaments);
- if(tournaments.length == 0) return;
- return tournaments.map(tournament => {
- const usersInTournaments = tournament.users;
- return sftp.connect({
- host: tournament.server.ip,
- port: 22,
- username: tournament.server.user,
- password: tournament.server.password,
- })
- .then(() => {
- return sftp.list('serverfiles/csgo');
- })
- .then(logs => {
- console.log("Логи", logs);
- const filterLogs = logs.filter((log) => {
- // console.log('Час турніру', tournament.startTime.getTime());
- // console.log('Час логу', log.modifyTime);
- return getExtension(log.name) === '.txt' && log.modifyTime > tournament.startTime.getTime() && log.size != 0;
- });
- return filterLogs.reduce((prev, current) => (prev.modifyTime > current.modifyTime) ? prev : current);
- })
- .then(log => {
- //console.log('Фильтровані логи', log);
- return sftp.get('serverfiles/csgo/' + log.name);
- })
- .then(readStream => {
- return new Promise((resolve, reject) => {
- let logData = '';
- readStream.on("data", (data) => {
- logData += data;
- });
- readStream.on("end", () => {
- console.log("<===== END ЛОГ файл=====>");
- resolve(logData);
- });
- readStream.on('error', function(err){
- reject(err);
- });
- })
- })
- .then(content => {
- console.log('Зміст файлу', content);
- const linesContent = content.split("\n");
- usersInTournaments.map(user => {
- const statisticToUserTournament = {
- side: '',
- killed: 0,
- //deaths: 0,
- killedHeadshot: 0
- };
- //statisticToUserTournament.side = '';
- //console.info('user.steamId64', user.steamId64);
- const statisticToTournament = {
- teamScoreCT: 0,
- teamScoreTERRORIST: 0,
- currentRound: 0,
- isGameOver: 0
- };
- return Promise.all([
- models.UserToTournament.update({
- statistic: statisticToUserTournament,
- }, {
- where: {
- userId: user.id,
- tournamentId: tournament.id
- },
- returning: true
- }),
- models.Tournament.update({
- statistic: statisticToTournament,
- status: (statisticToTournament.isGameOver) ? 'finished' : 'inProgress'
- }, {
- where: {
- id: tournament.id
- },
- returning: true
- })
- ]);
- });
- })
- });
- })
- .catch((err) => {
- console.log('err->RemoteServer-getStatistic', err);
- sftp.end();
- })
- }
- }
- function getExtension(filename) {
- var i = filename.lastIndexOf('.');
- return (i < 0) ? '' : filename.substr(i);
- }
- function getStatusGame(string){
- if(string.indexOf("Game Over") !== -1) return true;
- }
- function currentRound(string){
- if(string.indexOf("World triggered \"Round_Start\"") !== -1) return true;
- }
- function isKilled(string){
- if(string.indexOf("killed") !== -1) return true;
- }
- function isKilledHeadshot(string){
- if(string.indexOf("headshot") !== -1) return true;
- }
- function isDisconnected(string){
- if(string.indexOf("disconnected") !== -1) return true;
- }
- function getCTscore(string){
- //string.match(/Team "CT" scored "/i)
- return res = string.match(/\(([a-z]+) \"([0-9]+)\"\) \(([a-z]+) \"([0-9]+)\"\)/i);
- }
- module.exports = RemoteServer;
Add Comment
Please, Sign In to add comment