Skip to content

Commit

Permalink
feat(match): improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveVanOpstal committed May 30, 2016
1 parent 1204479 commit 670b352
Showing 1 changed file with 44 additions and 34 deletions.
78 changes: 44 additions & 34 deletions src/server/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module Errors {
};

interface CallBack { (err: HttpError, results?: any): void; }
interface Sample { xp: number; g: number; }

export class Match {
private summoner: Summoner;
Expand All @@ -69,7 +70,7 @@ export class Match {
});
}

public getData(region: string, summonerName: string, championKey: string, gameTime: number, sampleSize: number, callback: (response: Host.Response) => void) {
private getData(region: string, summonerName: string, championKey: string, gameTime: number, sampleSize: number, callback: (response: Host.Response) => void) {
gameTime = isNaN(gameTime) ? config.default.gameTime : gameTime;
sampleSize = isNaN(sampleSize) ? config.default.sampleSize : sampleSize;
var stepSize = gameTime / (sampleSize - 1);
Expand Down Expand Up @@ -210,49 +211,58 @@ export class Match {
return games;
}

private getRelativeOf(frames, time, frameCb) {
if (!frames) {
return false;
}
private getSamples(matches: Array<Array<any>>, sampleSize: number, factor: number): Array<Sample> {
var samples = Array<Sample>();
for (var i = 0; i < sampleSize; i++) {
var absFactor = i * factor;
var absXp = 0;
var absG = 0;

var index = -1;
for (var j = 0; j < frames.length; j++) {
if (frames[j].time > time) {
index = j;
break;
for (let index in matches) {
var frames = matches[index];
absXp += this.getRelativeOf(frames, absFactor, (frame) => { return frame.xp; });
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);
samples.push(sample);
}

if (index > 0) {
var lowerFrame = frames[index - 1];
var upperFrame = frames[index];
return samples;
}

var ratio = (time - lowerFrame.time) / (upperFrame.time - lowerFrame.time);
var rel = (frameCb(upperFrame) - frameCb(lowerFrame)) * ratio;
private getRelativeOf(frames: Array<any>, time: number, callback: (frame: any) => number): number {
if (!frames) {
return 0;
}

return frameCb(lowerFrame) + rel;
} else {
return false;
var index = this.getFrameIndex(frames, time);
if (index < 0) {
return 0;
}
}

private getSamples(games, sampleSize, factor) {
var result = { xp: [], g: [] };
for (var i = 0; i < sampleSize; i++) {
var absFactor = i * factor;
var absXp = 0;
var absG = 0;
var lowerFrame = frames[index - 1];
var upperFrame = frames[index];

for (var j = 0; j < games.length; j++) {
var frames = games[j];
absXp += this.getRelativeOf(frames, absFactor, function (frame) { return frame.xp; });
absG += this.getRelativeOf(frames, absFactor, function (frame) { return frame.g; });
}
var ratio = (time - lowerFrame.time) / (upperFrame.time - lowerFrame.time);

result.xp[i] = Math.round(absXp / games.length);
result.g[i] = Math.round(absG / games.length);
}
let lowerValue = (callback(lowerFrame));
let upperValue = (callback(upperFrame));
var rel = upperValue - lowerValue * ratio;

return lowerValue + rel;
}

return result;
private getFrameIndex(frames: Array<any>, time: number) {
var index = -1;
for (var j = 0; j < frames.length; j++) {
if (frames[j].time > time) {
index = j;
break;
}
}
return index;
}
}

0 comments on commit 670b352

Please sign in to comment.