Skip to content

Commit

Permalink
feat(match server): separate summoner request
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveVanOpstal committed Apr 1, 2016
1 parent a5b5b47 commit 7dc1247
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 69 deletions.
22 changes: 16 additions & 6 deletions src/app/misc/lolapi.service.ts
Expand Up @@ -37,7 +37,7 @@ export class LolApiService {
}

public getItems() {
return this.http.get(this.linkStaticData() + '/item?itemListData=all')
return this.http.get(this.linkStaticData() + '/item?itemListData=gold,hideFromAll,image,maps,requiredChampion,tags,tree')
.map(res => res.json());
}

Expand All @@ -46,25 +46,35 @@ export class LolApiService {
.map(res => res.json());
}

public getSummonerId(summonerName: string) {
return this.http.get(this.linkMatchData() + '/summoner/' + summonerName)
.map(res => res.json());
}

public getSummonerMatchData(summonerName: string, championKey: string, gameTime: number, samples: number) {
return this.http.get(this.linkMatchData() + '/' + summonerName + '/' + championKey + '?gametime=' + gameTime + '&samples=' + samples)
return this.http.get(this.linkMatchData() + '/summoner-match/' + summonerName + '/' + championKey + '?gameTime=' + gameTime + '&samples=' + samples)
.map(res => res.json());
}

public getMatchData(summonerId: number, championKey: string, gameTime: number, samples: number) {
return this.http.get(this.linkMatchData() + '/match/' + summonerId + '/' + championKey + '?gameTime=' + gameTime + '&samples=' + samples)
.map(res => res.json());
}


private linkStaticData() {
return this.linkStaticServer() + 'static-data/' + this.region + '/v1.2';
return this.linkStaticServer() + '/static-data/' + this.region + '/v1.2';
}

private linkMatchData() {
return this.linkMatchServer() + this.region;
return this.linkMatchServer() + '/' + this.region;
}

private linkStaticServer() {
return 'http://' + (this.staticServer.host || 'localhost') + ':' + (this.staticServer.port || 8081) + '/';
return 'http://' + (this.staticServer.host || 'localhost') + ':' + (this.staticServer.port || 8081);
}

private linkMatchServer() {
return 'http://' + (this.matchServer.host || 'localhost') + ':' + (this.matchServer.port || 8082) + '/';
return 'http://' + (this.matchServer.host || 'localhost') + ':' + (this.matchServer.port || 8082);
}
}
183 changes: 120 additions & 63 deletions src/server/matchServer.js
Expand Up @@ -4,7 +4,6 @@ var fs = require('fs');
var async = require('async');
var console = require('./console.js');
var host = require('./host.js');
var tim = require('tinytim').tim;

var Lru = require("lru-cache");
var cache = Lru({
Expand Down Expand Up @@ -33,9 +32,13 @@ config.server.host = config.server.host || "localhost";
config.server.port = config.server.port || 8082;

var errors = {
summoner: {
badRequest: {
code: 400,
text: "Invalid request."
},
invalidSummoner: {
code: 404,
text: "Unable to find summoner {{name}} in {{region}}."
text: "Unable to find summoner."
},
matchlist: {
code: 404,
Expand Down Expand Up @@ -64,11 +67,11 @@ function getSummonerId(region, name, callback) {
}, host.jsonFormatter);
}

function getMatchList(region, championId, summonerId, callback) {
function getMatchList(region, summonerId, championId, callback) {
var path = url.format({ pathname: host.createUrl(region, 'matchlist') + 'by-summoner/' + summonerId, query: { championIds: championId } });
host.sendRequest(region, path, function(data, error, statusCode) {
if (data.totalGames >= config.games.min) {
return callback(null, summonerId, data.matches);
return callback(null, data.matches);
}
else if (error) {
return callback({ code: statusCode, text: error });
Expand All @@ -90,7 +93,7 @@ function getMatches(region, summonerId, matches, callback) {

var path = url.format({ pathname: host.createUrl(region, 'match') + matchId, query: { includeTimeline: true } });
host.sendRequest(region, path, function(data, error, statusCode) {
if (data.timeline.frames) {
if (data && data.timeline && data.timeline.frames) {

if (result.interval > data.timeline.frameInterval) {
result.interval = data.timeline.frameInterval;
Expand Down Expand Up @@ -148,8 +151,8 @@ function fill(games, interval, limit) {
deltaXp += frame.xp - prevFrame.xp;
deltaG += frame.g - prevFrame.g;
}
var avgDeltaXp = Math.round(deltaXp / sampleSize);
var avgDeltaG = Math.round(deltaG / sampleSize);
var avgDeltaXp = deltaXp / sampleSize;
var avgDeltaG = deltaG / sampleSize;

// fill up games using the average trend of the samples
while (games[i][games[i].length - 1].time < limit) {
Expand Down Expand Up @@ -199,13 +202,90 @@ function getSamples(games, sampleSize, factor) {
absG += getRelativeOf(frames, absFactor, function(frame) { return frame.g; });
}

result.xp[i] = absXp / games.length;
result.g[i] = absG / games.length;
result.xp[i] = Math.round(absXp / games.length);
result.g[i] = Math.round(absG / games.length);
}

return result;
}


function handleSummoner(region, pathname, query, callbackSuccess, callbackError) {
var name = pathname[1].toLowerCase();
processSummoner(region, name, callbackSuccess, callbackError);
}

function processSummoner(region, name, callbackSuccess, callbackError) {
getSummonerId(region, name, function(error, result) {
if (error) {
callbackError(error);
return;
}
callbackSuccess(result);
});
}

function handleMatch(region, pathname, query, callbackSuccess, callbackError) {
var summonerId = pathname[0];
var championId = pathname[1];
var sampleSize = isNaN(query.samples) ? config.default.sampleSize : query.samples;
var gameTime = isNaN(query.gameTime) ? config.default.gameTime : query.gameTime;

if (isNaN(summonerId) || isNaN(championId)) {
callbackError(errors.badRequest);
}

processMatch(region, summonerId, championId, gameTime, sampleSize, callbackSuccess, callbackError);
}

function processMatch(region, summonerId, championId, gameTime, sampleSize, callbackSuccess, callbackError) {
var stepSize = gameTime / (sampleSize - 1);

async.waterfall(
[
function(callback) {
getMatchList(region, summonerId, championId, callback);
},
function(matches, callback) {
getMatches(region, summonerId, matches, callback);
}
],
function(error, result) {
if (error) {
callbackError(error);
return;
}

var matches = fill(result.matches, result.interval, gameTime);

var samples = getSamples(matches, sampleSize, stepSize);
result = JSON.stringify(samples);

callbackSuccess(result);
}
);
}

function handleError(response, error) {
response.writeHead(error.code, host.headers);
response.write(error.text);
response.end();

if (error.code >= 500) {
console.error('Response: ' + error.code);
}
else {
console.warn('Response: ' + error.code);
}
}

function handleSuccess(response, cache, result) {
response.writeHead(200, host.headers);
response.write(result);
response.end();
console.log('Response: 200');
}

var server = http.createServer(function(request, response) {
console.start();

Expand All @@ -221,66 +301,43 @@ var server = http.createServer(function(request, response) {
return;
}

var defaultSuccess = function(result) {
console.log(result);
handleSuccess(response, cache, result);
cache.set(request.url, result);
};
var defaultError = function(error) {
handleError(response, error);
};

var requestUrl = url.parse(request.url, true);
host.transformUrl(requestUrl);

var pathname = requestUrl.pathname.split('/');
var region = pathname[1];
var name = pathname[2].toLowerCase();
var championId = pathname[3];
var sampleSize = config.default.sampleSize;
var gameTime = config.default.gameTime;

if (!isNaN(requestUrl.query.samples)) {
sampleSize = requestUrl.query.samples;
}
if (!isNaN(requestUrl.query.gametime)) {
gameTime = requestUrl.query.gametime;
}

var stepSize = gameTime / sampleSize;
var type = pathname[2];

async.waterfall(
[
function(callback) {
getSummonerId(region, name, callback);
},
function(summonerId, callback) {
getMatchList(region, championId, summonerId, callback);
},
function(summonerId, matches, callback) {
getMatches(region, summonerId, matches, callback);
}
],
function(error, result) {
if (error) {
response.writeHead(error.code, host.headers);
error.text = tim(error.text, { name: name, region: region });
response.write(error.text);
response.end();
pathname = pathname.splice(2);
var query = requestUrl.query;

if (error.code >= 500) {
console.error('Response: ' + error.code);
}
else {
console.warn('Response: ' + error.code);
}
}
else {
var matches = fill(result.matches, result.interval, gameTime);

var samples = getSamples(matches, sampleSize, stepSize);
result = JSON.stringify(samples);
console.log(samples);

response.writeHead(200, host.headers);
response.write(result);
response.end();
cache.set(request.url, result);
console.log('Response: 200');
}
}
);
switch (type) {
case 'summoner':
handleSummoner(region, pathname, query, defaultSuccess, defaultError);
break;
case 'summoner-match':
handleSummoner(region, pathname, query,
function(result) {
handleMatch(region, [result, pathname[2]], query, defaultSuccess, defaultError);
},
defaultError);
break;
case 'match':
handleMatch(region, pathname, query, defaultSuccess, defaultError);
break;
default:
handleError(response, errors.badRequest);
break;
}
})
.listen(config.server.port, config.server.host);

Expand Down

0 comments on commit 7dc1247

Please sign in to comment.