Skip to content

Commit

Permalink
Add MatchMaking match parsing
Browse files Browse the repository at this point in the history
Fixes #28
  • Loading branch information
niekcandaele committed Jul 25, 2020
1 parent 09e8c14 commit 25d53ab
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 39 deletions.
146 changes: 121 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@
"@nestjs/platform-express": "^6.7.2",
"@nestjs/typeorm": "^7.0.0",
"@sentry/node": "^5.15.5",
"@types/concat-stream": "^1.6.0",
"@typescript-eslint/eslint-plugin": "^2.29.0",
"@typescript-eslint/parser": "^2.29.0",
"bull": "^3.13.0",
"class-transformer": "^0.2.3",
"class-validator": "^0.12.2",
"concat-stream": "^2.0.0",
"csgo": "^1.5.14",
"demofile": "^1.3.1",
"discord.js": "^12.2.0",
Expand All @@ -76,6 +78,7 @@
"@nestjs/testing": "^6.7.1",
"@types/bull": "^3.12.2",
"@types/bytebuffer": "^5.0.40",
"@types/decompress": "^4.2.3",
"@types/express": "^4.17.1",
"@types/faker": "^4.1.11",
"@types/jest": "^24.0.18",
Expand Down
61 changes: 47 additions & 14 deletions src/match/match.service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import {
OnQueueCompleted,
OnQueueError,
OnQueueFailed,
OnQueueProgress,
Process,
Processor
} from '@nestjs/bull';
import { OnQueueCompleted, OnQueueError, OnQueueFailed, OnQueueProgress, Process, Processor } from '@nestjs/bull';
import { HttpService, Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { InjectRepository } from '@nestjs/typeorm';
import * as Sentry from '@sentry/node';
import { Job, Queue } from 'bull';
import concat = require('concat-stream');
import { Connection } from 'typeorm';
import bz2 = require('unbzip2-stream');
import { promisify } from 'util';
import { unzip } from 'zlib';

Expand Down Expand Up @@ -153,16 +148,54 @@ export class MatchService {
}

private async downloadDemo(match: CsgoMatchDto): Promise<Buffer> {
// Different sources return the data in different ways
if (match.demoUrl.endsWith('.dem.gz')) {
return await this.handleFaceitDemoDownload(match);
}

if (match.demoUrl.endsWith('.bz2')) {
return await this.handleMatchmakingDemoDownload(match);
}

throw new Error(
`Unknown file type, not sure how to handle demo... ${match.demoUrl}`
);
}

private handleMatchmakingDemoDownload(match: CsgoMatchDto): Promise<Buffer> {
return new Promise((resolve, reject) => {
this.httpService
.get(match.demoUrl, { responseType: 'arraybuffer' })
.get(match.demoUrl, { responseType: 'stream' })
.toPromise()
.then(async response => {
const doUnzip = promisify(unzip);

doUnzip(response.data).then(buffer => {
resolve(buffer as Buffer);
.then(response => {
const writer = concat(buffer => {
resolve(buffer);
});
response.data.pipe(bz2()).pipe(writer);
writer.on('error', reject);
})

.catch(e => {
this.logger.error(e);
reject(e);
});
});
}

private handleFaceitDemoDownload(match: CsgoMatchDto): Promise<Buffer> {
return new Promise((resolve, reject) => {
this.httpService
.get(match.demoUrl, { responseType: 'arraybuffer' })
.toPromise()
.then(response => {
promisify(unzip)(response.data)
.then(buffer => {
resolve(buffer as Buffer);
})
.catch(e => {
this.logger.error(e);
reject(e);
});
})
.catch(e => {
this.logger.error(e);
Expand Down

0 comments on commit 25d53ab

Please sign in to comment.