Skip to content

Commit

Permalink
feat(lolapi): implement v3
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveVanOpstal committed Jun 26, 2017
1 parent 84073eb commit 51e5373
Show file tree
Hide file tree
Showing 12 changed files with 254 additions and 211 deletions.
13 changes: 8 additions & 5 deletions DEVELOPER.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ These are the default values that can be altered via an override:
port: 8082,
sampleSize: 32
},
apiVersions: {
'summoner': 'v1.4',
'matchlist': 'v2.2',
'match': 'v2.2',
'static-data': 'v1.2'
api: {
regions: ['ru', 'kr', 'pbe1', 'br1', 'oc1', 'jp1', 'na1', 'eun1', 'euw1', 'tr1', 'la1'],
versions: {
'summoner': 'v3',
'match': 'v3',
'static-data': 'v3',
'status': 'v3'
}
},
gameTime: 45 * 60 * 1000
}
Expand Down
12 changes: 7 additions & 5 deletions config/settings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ export declare module settings {
let static: {port: number};
let match: {port: number, sampleSize: number};
let sampleSize: number;
let apiVersions: {
summoner: string,
matchlist: string,
match: string,
'static-data': string,
let api: {
regions: Array<string>,
versions: {
summoner: string,
match: string,
'static-data': string,
}
};
let gameTime: number;
}
5 changes: 4 additions & 1 deletion config/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ var exportSettings = helpers.merge(settings, {
port: null,
static: {port: 8081},
match: {port: 8082, sampleSize: 32},
apiVersions: {'summoner': 'v1.4', 'matchlist': 'v2.2', 'match': 'v2.2', 'static-data': 'v1.2'},
api: {
regions: ['ru', 'kr', 'pbe1', 'br1', 'oc1', 'jp1', 'na1', 'eun1', 'euw1', 'tr1', 'la1'],
versions: {'summoner': 'v3', 'match': 'v3', 'static-data': 'v3', 'status': 'v3'},
},
gameTime: 45 * 60 * 1000
});

Expand Down
12 changes: 6 additions & 6 deletions src/client/services/lolapi.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ describe('LolApiService', () => {
});
})));

it('should get summonerId', async(inject([LolApiService], (service) => {
service.getSummonerId('', '').subscribe(
it('should get accountId', async(inject([LolApiService], (service) => {
service.getAccountId('', '').subscribe(
res => {
expect(res).toBeDefined();
},
Expand Down Expand Up @@ -137,14 +137,14 @@ describe('LolApiService', () => {

it('should get the correct resolved link to the static-server',
inject([LolApiService], (service) => {
expect(service.getEndpoint(Endpoint.static, 'static-data', 'region'))
.toBe('https://' + settings.domain + '/staticapi/static-data/region/v1.2');
expect(service.getEndpoint(Endpoint.static))
.toBe('https://' + settings.domain + '/staticapi/');
}));

it('should get the correct resolved link to the match-server',
inject([LolApiService], (service) => {
expect(service.getEndpoint(Endpoint.match, 'static-data', 'region'))
.toBe('https://' + settings.domain + '/matchapi/region');
expect(service.getEndpoint(Endpoint.match))
.toBe('https://' + settings.domain + '/matchapi/');
}));

it('should not get incorrect params', inject([LolApiService], (service) => {
Expand Down
60 changes: 31 additions & 29 deletions src/client/services/lolapi.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ export class LolApiService {
constructor(private http: Http, private router: Router) {}

public getRegions(): Observable<any> {
return this.cache('https://status.leagueoflegends.com/shards').map(res => {
res.push({name: 'Public Beta Environment', slug: 'pbe'});
return res;
});
let observables: Array<Observable<any>> = [];
for (let region of settings.api.regions) {
observables.push(
this.cache(this.getEndpoint(Endpoint.static) + region + '/status/shard-data'));
}
return Observable.forkJoin(observables);
}

public getRealm(): Observable<any> {
return this.get(Endpoint.static, 'static-data', '/realm').map(res => {
return this.get(Endpoint.static, 'static-data/realms').map(res => {
// both http and https are supported, realm data will return http
if (res.cdn) {
res.cdn = res.cdn.replace('http://', 'https://');
Expand All @@ -34,36 +36,38 @@ export class LolApiService {
}

public getLanguageStrings(): Observable<any> {
return this.get(Endpoint.static, 'static-data', '/language-strings');
return this.get(Endpoint.static, 'static-data/language-strings');
}

public getChampions(): Observable<any> {
return this.get(Endpoint.static, 'static-data', '/champion?champData=info,tags,image');
return this.get(Endpoint.static, 'static-data/champions?tags=image&tags=info&tags=tags');
}

public getChampion(championKey: string): Observable<any> {
return this.get(
Endpoint.static, 'static-data',
'/champion/' + championKey + '?champData=allytips,image,passive,spells,stats,tags');
Endpoint.static,
'static-data/champions/' + championKey +
'?tags=allytips&tags=image&tags=passive&tags=spells&tags=stats&tags=tags');
}

public getItems(): Observable<any> {
return this.get(Endpoint.static, 'static-data', '/item?itemListData=all');
return this.get(Endpoint.static, 'static-data/items?tags=all');
}

public getMasteries(): Observable<any> {
return this.get(Endpoint.static, 'static-data', '/mastery?masteryListData=all');
return this.get(Endpoint.static, 'static-data/mastery?masteryListData=all');
}

public getSummonerId(summonerName: string): Observable<any> {
return this.get(Endpoint.match, null, '/summoner/' + summonerName);
public getAccountId(summonerName: string): Observable<any> {
return this.get(Endpoint.match, 'summoner/' + summonerName);
}

public getMatchData(summonerName: string, championKey: string, gameTime: number, samples: number):
Observable<any> {
return this.get(
Endpoint.match, null, '/match/' + summonerName + '/' + championKey + '?gameTime=' +
gameTime + '&samples=' + samples);
Endpoint.match,
'match/' + summonerName + '/' + championKey + '?gameTime=' + gameTime +
'&samples=' + samples);
}

public getCurrentRegion(): Observable<string> {
Expand All @@ -82,8 +86,8 @@ export class LolApiService {
}


private get(endpoint: Endpoint, api: string, url: string): Observable<any> {
return this.getUrl(endpoint, api, url).mergeMap((urlResolved) => this.cache(urlResolved));
private get(endpoint: Endpoint, url: string): Observable<any> {
return this.getUrl(endpoint, url).mergeMap((urlResolved) => this.cache(urlResolved));
}

private cache(url: string): Observable<any> {
Expand All @@ -94,17 +98,16 @@ export class LolApiService {
return this.cachedObservables[url].take(1).map(res => res.json());
}

private getUrl(endpoint: Endpoint, api: string, url: string): Observable<string> {
return this.getCurrentRegion().map(region => this.getEndpoint(endpoint, api, region) + url);
private getUrl(endpoint: Endpoint, url: string): Observable<string> {
return this.getCurrentRegion().map(region => this.getEndpoint(endpoint) + region + '/' + url);
}

private getEndpoint(endpoint: Endpoint, api: string, region: string): string {
private getEndpoint(endpoint: Endpoint): string {
switch (endpoint) {
case Endpoint.static:
return 'https://' + settings.domain + '/staticapi/' + api + '/' + region + '/' +
settings.apiVersions[api];
return 'https://' + settings.domain + '/staticapi/';
default:
return 'https://' + settings.domain + '/matchapi/' + region;
return 'https://' + settings.domain + '/matchapi/';
}
}

Expand All @@ -120,12 +123,11 @@ export class LolApiService {

private checkRegion(region: string): Observable<string> {
return this.getRegions().map((regions: Array<string>) => {
if (regions.find((r: any) => {
return r.slug === region;
})) {
return region;
} else {
throw Error('Region does not exist');
let foundRegion = regions.find((r: any) => {
return r.slug === region;
});
if (foundRegion) {
return foundRegion['hostname'].split('.')[1];
}
});
}
Expand Down
6 changes: 3 additions & 3 deletions src/client/summoner/summoner.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {LolApiService} from '../services';
<p>
Enter your summoner name:
<input type="text" name="name" #summoner>
<button (click)="getSummonerId(summoner)">Go</button>
<button (click)="getAccountId(summoner)">Go</button>
</p>
<p *ngIf="error">Error summoner does not exist</p>
</div>`
Expand All @@ -24,8 +24,8 @@ export class SummonerComponent {
constructor(
private route: ActivatedRoute, private router: Router, private lolApi: LolApiService) {}

getSummonerId(event: HTMLInputElement) {
this.lolApi.getSummonerId(event.value)
getAccountId(event: HTMLInputElement) {
this.lolApi.getAccountId(event.value)
.subscribe(
res => {
if (!isNaN(res)) {
Expand Down
65 changes: 41 additions & 24 deletions src/server/match/match.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,54 @@ describe('Match', () => {
let server: MockServer;
let match: Match;

let responseSummoner = {
url: 'summoner',
message: new MockHostResponseSuccess(JSON.stringify({'DinosHaveNoLife': {'id': 42457671}}))
let responseSummoners = {
url: 'summoners',
message: new MockHostResponseSuccess(JSON.stringify({'accountId': 123}))
};

let responseMatchList = {
url: 'matchlist',
message: new MockHostResponseSuccess(JSON.stringify(
{'matches': [{'matchId': 2701428538}, {'matchId': 2698839638}, {'matchId': 2695882481}]}))
let responseMatchLists = {
url: 'matchlists/by-account',
message: new MockHostResponseSuccess(JSON.stringify({
'matches': [{'gameId': 2701428538}, {'gameId': 2698839638}, {'gameId': 2695882481}],
'totalGames': 3
}))
};

let responseMatch = {
url: 'match',
url: 'matches',
message: new MockHostResponseSuccess(JSON.stringify({
'timeline': {
'frameInterval': 60000,
'frames': [
{'timestamp': 0, 'participantFrames': {'1': {'totalGold': 500, 'xp': 0}}},
{'timestamp': 60047, 'participantFrames': {'1': {'totalGold': 500, 'xp': 0}}},
{'timestamp': 120058, 'participantFrames': {'1': {'totalGold': 538, 'xp': 206}}},
{'timestamp': 180094, 'participantFrames': {'1': {'totalGold': 808, 'xp': 706}}}
]
},
'participantIdentities': [{
'player': {'summonerName': 'DinosHaveNoLife', 'summonerId': 42457671},
'player': {'summonerName': 'DinosHaveNoLife', 'currentAccountId': 123},
'participantId': 1
}],
'gameDuration': 2250,
'mapId': 11
}))
};

let responseTimelines = {
url: 'timelines',
message: new MockHostResponseSuccess(JSON.stringify({
'frameInterval': 60000,
'frames': [
{'timestamp': 0, 'participantFrames': {'1': {'totalGold': 500, 'xp': 0}}},
{'timestamp': 60047, 'participantFrames': {'1': {'totalGold': 500, 'xp': 0}}},
{'timestamp': 120058, 'participantFrames': {'1': {'totalGold': 538, 'xp': 206}}},
{'timestamp': 180094, 'participantFrames': {'1': {'totalGold': 808, 'xp': 706}}}
]
}))
};



beforeEach(() => {
server = new MockServer();
match = new Match(server);
server.headers = {test: 'test'};
});

it('should get the summoner id', () => {
server.responses = [responseSummoner, responseMatchList, responseMatch];
it('should get frames', () => {
server.responses = [responseSummoners, responseMatchLists, responseMatch, responseTimelines];
let incomingMessage: any = {url: 'test'};
let serverResponse: any = new MockServerResponse();

Expand All @@ -54,10 +63,18 @@ describe('Match', () => {
incomingMessage, serverResponse);

expect(serverResponse.getHeader('test')).toBe('test');
expect(serverResponse.buffer).toBe(responseSummoner.message.data);
expect(serverResponse.buffer).toBe(JSON.stringify({
'xp': [
0, 93, 657, 802, 904, 1007, 1109, 1211, 1314, 1416, 1519,
1621, 1724, 1826, 1929, 2031, 2134, 2236, 2339, 2441, 2544, 2646,
2749, 2851, 2954, 3056, 3159, 3261, 3364, 3466, 3569, 3671
],
'gold': [
500, 517, 781, 850, 894, 939, 984, 1029, 1073, 1118, 1163,
1207, 1252, 1297, 1341, 1386, 1431, 1476, 1520, 1565, 1610, 1654,
1699, 1744, 1789, 1833, 1878, 1923, 1967, 2012, 2057, 2102
]
}));
expect(server.mockCache.url).toBe(incomingMessage.url);
// expect(server.mockCache.data).toBe(server.response.data);
// expect(server.response.success).toBeTruthy();
// expect(server.response.data).toBe(server.response.data);
});
});

0 comments on commit 51e5373

Please sign in to comment.