Skip to content

Commit

Permalink
fix(match-server): error in request
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveVanOpstal committed Jun 11, 2016
1 parent abf2caf commit 67c92de
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 32 deletions.
63 changes: 41 additions & 22 deletions src/server/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,33 @@ export class Match {
[
(cb) => {
this.summoner.getData(region, summonerName, (res) => {
if (res.success) {
cb(res.json[summonerName].id);
if (res.success && res.json[summonerName.toLowerCase()]) {
cb(undefined, res.json[summonerName.toLowerCase()].id);
} else {
cb(undefined, res.data);
callback(res);
}
});
},
(summonerId, cb) => {
this.getMatchList(region, summonerId, championKey, cb);
this.getMatchList(region, summonerId, championKey, (err: HttpError, results?: any) => {
if (err) {
cb(err);
} else {
cb(undefined, summonerId, results);
}
});
},
(summonerId, matches, cb) => {
this.getMatches(region, summonerId, matches, cb);
}
],
(error, results) => {
if (error) {
let response: HostResponse;
response.data = error.message;
// response.status = error.status;
response.success = false;
callback(response);
callback({
data: error.message,
status: 500,
success: false
});
return;
}

Expand All @@ -108,22 +114,21 @@ export class Match {
let samples = this.getSamples(matches, sampleSize, stepSize);
results = JSON.stringify(samples);

let response: HostResponse;
response.data = samples;
response.json = results;
response.status = 200;
response.success = true;

callback(response);
callback({
data: samples,
json: results,
status: 200,
success: true
});
}
);
}

private getMatchList(region, summonerId, championKey, callback: CallBack) {
let path = this.server.getBaseUrl(region) + '/' + settings.apiVersions.matchlist + 'matchlist/by-summoner/' + summonerId;
let path = this.server.getBaseUrl(region) + '/' + settings.apiVersions.matchlist + '/matchlist/by-summoner/' + summonerId;
this.server.sendRequest(path, region, (res: HostResponse) => {
if (res.success && res.json.totalGames >= config.games.min) {
return callback(undefined, res.json);
return callback(undefined, res.json.matches);
} else if (res.success) {
return callback(Errors.matchlist);
} else {
Expand All @@ -134,17 +139,27 @@ export class Match {

private getMatches(region: string, summonerId: number, matches, callback: CallBack) {
let matchRequests = [];
let i = 0;
for (let index in matches) {
i++;
matchRequests.push((cb) => {
let match = matches[index];
this.getMatch(region, summonerId, match.matchId, cb);
});
if (i >= config.games.max) {
break;
}
}

parallel(matchRequests, (err, results: Array<any>) => {
let data = { interval: 120000, matches: [] };
for (let index in results) {
let result = results[index];
if (!result || !result.timeline || !result.timeline.frameInterval) {
callback(Errors.matches);
return;
}

if (data.interval > result.timeline.frameInterval) {
data.interval = result.timeline.frameInterval;
}
Expand All @@ -158,6 +173,7 @@ export class Match {

if (participantId <= -1) {
callback(Errors.participant);
return;
}

data.matches[index] = new Array();
Expand All @@ -169,11 +185,13 @@ export class Match {
};
});
}

callback(undefined, data);
});
}

private getMatch(region: string, summonerId: number, matchId: number, callback: CallBack) {
let path = this.server.getBaseUrl(region) + '/' + settings.apiVersions.match + 'match' + matchId + '?includeTimeline=true';
let path = this.server.getBaseUrl(region) + '/' + settings.apiVersions.match + '/match/' + matchId + '?includeTimeline=true';
this.server.sendRequest(path, region, (res: HostResponse) => {
if (res.success) {
callback(undefined, res.json);
Expand Down Expand Up @@ -221,9 +239,10 @@ export class Match {
absG += this.getRelativeOf(frames, absFactor, (frame) => { return frame.g; });
}

let sample: Sample;
sample.xp[i] = Math.round(absXp / matches.length);
sample.g[i] = Math.round(absG / matches.length);
let sample: Sample = {
xp: Math.round(absXp / matches.length),
g: Math.round(absG / matches.length)
};
samples.push(sample);
}

Expand Down
35 changes: 27 additions & 8 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ export function getPathname(path: string): Array<string> {
}

export function getQuery(path: string): any {
return url.parse(path).query;
return url.parse(path, true).query;
}

export interface HttpError {
status: number;
message: string;
}


Expand Down Expand Up @@ -81,6 +86,7 @@ export class Server {

let options: https.RequestOptions = { path: url };
this.merge(this.options, options);
options.hostname = url.indexOf('global') > 0 ? this.getHostname() : this.getHostname(region);

this.sendHttpsRequest(options, callback);
}
Expand All @@ -104,14 +110,14 @@ export class Server {
private sendHttpsRequest(options: https.RequestOptions, callback: (response: HostResponse) => void) {
let console = new ColorConsole();
let req = https.request(options, (res: IncomingMessage) => this.handleResponse(console, options, res, callback));
req.on('error', (e) => this.handleResponseError(console, options, e, callback));
req.on('error', (e) => this.handleResponseError(console, options, { status: 500, message: e }, callback));
req.end();
}

private sendHttpRequest(options: any, callback: (response: HostResponse) => void) {
let console = new ColorConsole();
let req = http.request(options, (res: IncomingMessage) => this.handleResponse(console, options, res, callback));
req.on('error', (e) => this.handleResponseError(console, options, e, callback));
req.on('error', (e) => this.handleResponseError(console, options, { status: 500, message: e }, callback));
req.end();
}

Expand All @@ -126,11 +132,24 @@ export class Server {
}

private handleResponseSuccess(console: ColorConsole, options: https.RequestOptions, res: IncomingMessage, data: any, callback: (response: HostResponse) => void) {
let json = {};
let json;
try {
json = JSON.parse(data);
} catch (e) {
this.handleResponseError(console, options, e, callback);
let error: HttpError = {
status: 500,
message: e
};
this.handleResponseError(console, options, error, callback);
return;
}

if (json.status) {
let error: HttpError = {
status: json.status.status_code,
message: json.status.message
};
this.handleResponseError(console, options, error, callback);
return;
}

Expand All @@ -144,13 +163,13 @@ export class Server {
callback(response);
}

private handleResponseError(console: ColorConsole, options: any, e: any, callback: (response: HostResponse) => void) {
private handleResponseError(console: ColorConsole, options: any, e: HttpError, callback: (response: HostResponse) => void) {
let response: HostResponse = {
data: e,
status: e.statusCode,
status: e.status,
success: false
};
console.logHttp(options.method, this.maskApiKey(options.path), e.statusCode, e);
console.logHttp(options.method, this.maskApiKey(options.path), e.status, e.message);
callback(response);
}

Expand Down
4 changes: 2 additions & 2 deletions src/server/summoner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export class Summoner {
public get(region: string, name: string, request: IncomingMessage, response: ServerResponse) {
this.getData(region, name, (res) => {
response.writeHead(res.status, this.server.headers);
if (res.success && res.json[name]) {
response.write(res.json[name].id);
if (res.success && res.json[name.toLowerCase()]) {
response.write(res.json[name.toLowerCase()].id);
this.server.setCache(request.url, res.data);
} else {
response.write(res.data + '\n');
Expand Down

0 comments on commit 67c92de

Please sign in to comment.