From b981b694d5bad40dff7873b40a983d7dc52f273f Mon Sep 17 00:00:00 2001 From: Mike Rosack Date: Sun, 19 Nov 2017 09:04:23 -0600 Subject: [PATCH] remove shared api proxy, we're using swagger proxies now --- index.ts | 2 - package.json | 3 +- src/api.service.ts | 194 ------------------------------------ src/busy/busy.component.ts | 19 ++-- src/busy/busy.service.ts | 8 +- src/civdefs.service.ts | 24 ++--- src/entity.ts | 111 --------------------- src/pipes.ts | 8 +- src/profileCache.service.ts | 49 ++++++--- tslint.json | 122 +++++++++++++++++++++++ 10 files changed, 189 insertions(+), 351 deletions(-) delete mode 100644 src/api.service.ts delete mode 100644 src/entity.ts create mode 100644 tslint.json diff --git a/index.ts b/index.ts index 5afb6ad..02a6af9 100644 --- a/index.ts +++ b/index.ts @@ -1,6 +1,4 @@ -export * from './src/api.service'; export * from './src/civdefs.service'; -export * from './src/entity'; export * from './src/profileCache.service'; export * from './src/busy/busy.component'; export * from './src/busy/busy.service'; diff --git a/package.json b/package.json index ed4076f..444f6a4 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "main": "index.ts", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "prepublish": "tsc", + "lint": "tslint --type-check --project tsconfig.json ./src/**/*.ts", + "prepublish": "npm run clean && npm run lint && tsc", "clean": "rm -rf dist" }, "author": "", diff --git a/src/api.service.ts b/src/api.service.ts deleted file mode 100644 index 3c5195e..0000000 --- a/src/api.service.ts +++ /dev/null @@ -1,194 +0,0 @@ -import { Inject, Injectable, OpaqueToken } from '@angular/core'; -import { Http, Headers } from '@angular/http'; -import { ApiUrlProvider, ApiCredentialsProvider, SteamProfile, Game, JoinGameRequestBody, ChangeCivRequestBody, StartTurnSubmitResponse, UserGames, OpenGamesResponse, CreateGameRequestBody, EditGameRequestBody, User } from "./entity"; -import 'rxjs/add/operator/map'; -import 'rxjs/add/operator/toPromise'; - -export let API_URL_PROVIDER_TOKEN = new OpaqueToken('ApiUrlProvider'); -export let API_CREDENTIALS_PROVIDER_TOKEN = new OpaqueToken('ApiCredentialsProvider'); - -@Injectable() -export class ApiService { - constructor ( - private http: Http, - @Inject(API_URL_PROVIDER_TOKEN) private aup: ApiUrlProvider, - @Inject(API_CREDENTIALS_PROVIDER_TOKEN) private credentials: ApiCredentialsProvider - ) {} - - getPublicJson(url: string): Promise { - return this.http.get(url) - .map(res => { - return res.json(); - }).toPromise(); - } - - getLoginUrl(): Promise { - return this.get(this.aup.url + '/auth/steam', true).then(data => { - return data.redirectURL; - }); - } - - setToken(token: string): Promise { - return this.credentials.store(token, null) - .then(() => { - return this.get(this.aup.url + '/user/steamProfile') as Promise; - }) - .then(profile => { - return this.credentials.store(token, profile); - }); - } - - validateSteamCredentials(queryString: string): Promise { - return this.get(this.aup.url + '/auth/steam/validate' + queryString, true).then(data => { - if (data.token) { - return this.credentials.store(data.token, data.steamProfile); - } - - throw data; - }); - } - - getGame(id: string): Promise { - return this.get(this.aup.url + '/game/' + id, true); - } - - joinGame(data: JoinGameRequestBody): Promise { - return this.post(this.aup.url + '/game/' + data.gameId + '/join', data); - } - - leaveGame(gameId: string): Promise { - return this.post(this.aup.url + '/game/' + gameId + '/leave', {}); - } - - changeCiv(data: ChangeCivRequestBody): Promise { - return this.post(this.aup.url + '/game/' + data.gameId + '/changeCiv', data); - } - - startGame(id: string): Promise { - return this.post(this.aup.url + '/game/' + id + '/start', {}); - } - - getTurnUrl(gameId: string, compressed = false): Promise { - let url = this.aup.url + '/game/' + gameId + '/turn'; - - if (compressed) { - url += "?compressed=true"; - } - - return this.get(url).then(data => { - return data.downloadUrl; - }); - } - - revertTurn(gameId: string): Promise { - return this.post(this.aup.url + '/game/' + gameId + '/turn/revert', {}); - } - - kickUser(gameId: string, userId:string): Promise { - return this.post(this.aup.url + '/game/' + gameId + '/surrender', { - kickUserId: userId - }); - } - - surrender(gameId: string): Promise { - return this.post(this.aup.url + '/game/' + gameId + '/surrender', {}); - } - - startTurnSubmit(gameId: string): Promise { - return this.post(this.aup.url + '/game/' + gameId + '/turn/startSubmit', {}); - } - - finishTurnSubmit(gameId: string): Promise { - return this.post(this.aup.url + '/game/' + gameId + '/turn/finishSubmit', {}); - } - - getUserGames(): Promise { - return this.get(this.aup.url + '/user/games'); - } - - listOpenGames(): Promise { - return this.get(this.aup.url + '/game/listOpen', true); - } - - createGame(data: CreateGameRequestBody): Promise { - return this.post(this.aup.url + '/game/create', data); - } - - deleteGame(gameId: string): Promise { - return this.post(this.aup.url + '/game/' + gameId + '/delete', {}); - } - - editGame(data: EditGameRequestBody): Promise { - return this.post(this.aup.url + '/game/' + data.gameId + '/edit', data); - } - - getUser(): Promise { - return this.get(this.aup.url + '/user/getCurrent'); - } - - getUsers(): Promise { - return this.get(this.aup.url + '/users', true); - } - - getUserById(userId: string): Promise { - return this.get(this.aup.url + '/user/' + userId, true); - } - - setNotificationEmailAddress(emailAddress: string): Promise { - return this.post(this.aup.url + '/user/setNotificationEmail', {'emailAddress': emailAddress}); - } - - getSteamProfiles(steamIds: string[]): Promise { - return this.get(this.aup.url + '/user/steamProfiles?steamIds=' + steamIds.join(), true); - } - - getSteamProfile(): Promise { - return this.credentials.getSteamProfile(); - } - - isLoggedIn(): Promise { - return this.credentials.getToken().then(token => { - return !!token; - }); - } - - getToken(): Promise { - return this.credentials.getToken(); - } - - private getAuthHeaders(disableAuth?: boolean): Promise { - return this.credentials.getToken().then(token => { - let headers = new Headers(); - - if (!disableAuth) { - if (!token) { - throw new Error('Not Logged In!'); - } - - headers.append('Authorization', token); - } - - return headers; - }); - } - - private get(url: string, disableAuth?: boolean): Promise { - return this.getAuthHeaders(disableAuth).then(headers => { - return this.http.get(url, { - headers: headers - }).map(res => { - return res.json(); - }).toPromise(); - }); - } - - private post(url: string, data: any): Promise { - return this.getAuthHeaders().then(headers => { - return this.http.post(url, data, { - headers: headers - }).map(res => { - return res.json(); - }).toPromise(); - }); - } -} diff --git a/src/busy/busy.component.ts b/src/busy/busy.component.ts index 408e7ac..236e3f1 100644 --- a/src/busy/busy.component.ts +++ b/src/busy/busy.component.ts @@ -1,14 +1,13 @@ -import { ChangeDetectorRef, Component, trigger, state, style, transition, animate } from "@angular/core"; -import { Observable } from "rxjs/Observable"; -import { BusyService } from "./busy.service"; +import { ChangeDetectorRef, Component, trigger, state, style, transition, animate } from '@angular/core'; +import { BusyService } from './busy.service'; @Component({ - selector: "pydt-busy", + selector: 'pydt-busy', animations: [ - trigger("visibilityChanged", [ - state("shown", style({ opacity: 1 })), - state("hidden", style({ opacity: 0 })), - transition("* => *", animate(".2s")) + trigger('visibilityChanged', [ + state('shown', style({ opacity: 1 })), + state('hidden', style({ opacity: 0 })), + transition('* => *', animate('.2s')) ]) ], template: ` @@ -34,7 +33,7 @@ export class BusyComponent { } public animationStarted(event: any) { - if (event.toState === "shown") { + if (event.toState === 'shown') { this.busyHidden = false; } @@ -42,7 +41,7 @@ export class BusyComponent { } public animationDone(event: any) { - if (event.toState === "hidden") { + if (event.toState === 'hidden') { this.busyHidden = true; } diff --git a/src/busy/busy.service.ts b/src/busy/busy.service.ts index cd7c85a..91724d1 100644 --- a/src/busy/busy.service.ts +++ b/src/busy/busy.service.ts @@ -1,6 +1,6 @@ -import { Injectable } from "@angular/core"; -import { Subject } from "rxjs/Subject"; -import { Subscription } from "rxjs/Subscription"; +import { Injectable } from '@angular/core'; +import { Subject } from 'rxjs/Subject'; +import { Subscription } from 'rxjs/Subscription'; @Injectable() export class BusyService { @@ -15,4 +15,4 @@ export class BusyService { public subscribeBusy(callback: (value: boolean) => void) { return this.busyStream.subscribe(callback); } -} \ No newline at end of file +} diff --git a/src/civdefs.service.ts b/src/civdefs.service.ts index 77e2149..d2ad6c1 100644 --- a/src/civdefs.service.ts +++ b/src/civdefs.service.ts @@ -29,7 +29,7 @@ export class DLC { } } -export const Civ6DLCs: DLC[] = [ +export const CIV6_DLCS: DLC[] = [ new DLC('02A8BDDE-67EA-4D38-9540-26E685E3156E', 'Aztec Civilization Pack'), new DLC('3809975F-263F-40A2-A747-8BFB171D821A', 'Poland Civilization & Scenario Pack'), new DLC('2F6E858A-28EF-46B3-BEAC-B985E52E9BC1', 'Vikings Scenario Pack'), @@ -52,10 +52,10 @@ export function filterCivsByDlc(leaders: CivDef[], dlcIds: string[]) { return result; } -export const RandomCiv = new CivDef('CIVILIZATION_RANDOM', 'LEADER_RANDOM', 'Random Leader'); +export const RANDOM_CIV = new CivDef('CIVILIZATION_RANDOM', 'LEADER_RANDOM', 'Random Leader'); -export const Civ6Leaders = [ - RandomCiv, +export const CIV6_LEADERS = [ + RANDOM_CIV, new CivDef('CIVILIZATION_MACEDON', 'LEADER_ALEXANDER', null, 'E2749E9A-8056-45CD-901B-C368C8E83DEB'), new CivDef('CIVILIZATION_NUBIA', 'LEADER_AMANITORE', null, '643EA320-8E1A-4CF1-A01C-00D88DDD131A'), new CivDef('CIVILIZATION_FRANCE', 'LEADER_CATHERINE_DE_MEDICI'), @@ -90,7 +90,7 @@ export class GameSpeed { } } -export const Civ6GameSpeeds = new Array( +export const CIV6_GAME_SPEEDS = new Array( new GameSpeed('GAMESPEED_ONLINE', 'Online'), new GameSpeed('GAMESPEED_QUICK', 'Quick'), new GameSpeed('GAMESPEED_STANDARD', 'Standard'), @@ -103,7 +103,7 @@ export class MapSize { } } -export const Civ6MapSizes = new Array( +export const CIV6_MAP_SIZES = new Array( new MapSize('MAPSIZE_DUEL', 'Duel', 2), new MapSize('MAPSIZE_TINY', 'Tiny', 4), new MapSize('MAPSIZE_SMALL', 'Small', 6), @@ -113,18 +113,18 @@ export const Civ6MapSizes = new Array( ); export class Map { - constructor(public file:string, public displayName: string, public mapSize?: MapSize) { + constructor(public file: string, public displayName: string, public mapSize?: MapSize) { } } -export const Civ6Maps = new Array( +export const CIV6_MAPS = new Array( new Map('Continents.lua', 'Continents'), new Map('Fractal.lua', 'Fractal'), new Map('InlandSea.lua', 'Inland Sea'), new Map('Island_Plates.lua', 'Island Plates'), new Map('Pangaea.lua', 'Pangaea'), new Map('Shuffle.lua', 'Shuffle'), - new Map('Balanced4.Civ6Map', '4-Leaf Clover', Civ6MapSizes[1]), - new Map('Balanced6.Civ6Map', '6-Armed Snowflake', Civ6MapSizes[2]), - new Map('EarthStandard.Civ6Map', 'Earth Map', Civ6MapSizes[3]) -); \ No newline at end of file + new Map('Balanced4.Civ6Map', '4-Leaf Clover', CIV6_MAP_SIZES[1]), + new Map('Balanced6.Civ6Map', '6-Armed Snowflake', CIV6_MAP_SIZES[2]), + new Map('EarthStandard.Civ6Map', 'Earth Map', CIV6_MAP_SIZES[3]) +); diff --git a/src/entity.ts b/src/entity.ts deleted file mode 100644 index c2e944a..0000000 --- a/src/entity.ts +++ /dev/null @@ -1,111 +0,0 @@ -export interface Entity { - createdAt: string; - updatedAt: string; - version: number; - } - - export interface SteamProfile { - steamid: string; - personaname: string; - profileurl: string; - avatar: string; - avatarmedium: string; - avatarfull: string; - } - - export interface GamePlayer { - steamId: string; - civType: string; - hasSurrendered?: boolean; - turnsPlayed: number; - turnsSkipped: number; - timeTaken: number; - fastTurns: number; - slowTurns: number; - } - - export interface BaseGame { - displayName: string; - description: string; - dlc: string[]; - slots: number; - humans: number; - gameSpeed: string; - mapFile: string; - mapSize: string; - allowJoinAfterStart: boolean; - } - - export interface Game extends Entity, BaseGame { - gameId: string; - createdBySteamId: string; - inProgress: boolean; - hashedPassword: string; - players: GamePlayer[]; - discourseTopicId: number; - currentPlayerSteamId: string; - turnTimerMinutes: number; - round: number; - gameTurnRangeKey: number; - } - - export interface UserGames { - data: Game[]; - pollUrl: string; - } - - export interface OpenGamesResponse { - notStarted: Game[]; - openSlots: Game[]; - } - - export interface User extends Entity { - steamId: string; - displayName: string; - avatarSmall: string; - avatarMedium: string; - avatarFull: string; - emailAddress: string; - activeGameIds: string[]; - inactiveGameIds: string[]; - turnsPlayed: number; - turnsSkipped: number; - timeTaken: number; - fastTurns: number; - slowTurns: number; - } - - export interface StartTurnSubmitResponse { - putUrl: string; - } - - export interface ApiUrlProvider { - url: string; - } - - export interface ApiCredentialsProvider { - store(token: string, profile: SteamProfile): Promise; - getToken(): Promise; - getSteamProfile(): Promise; - } - - export interface GameRequestBody extends BaseGame { - password: string; - } - - export interface EditGameRequestBody extends GameRequestBody { - gameId: string; - } - - export interface CreateGameRequestBody extends GameRequestBody { - player1Civ: string; - } - - export interface ChangeCivRequestBody { - gameId: string; - playerCiv: string; - } - - export interface JoinGameRequestBody extends ChangeCivRequestBody { - password: string; - } \ No newline at end of file diff --git a/src/pipes.ts b/src/pipes.ts index 8c211e4..6a2a78f 100644 --- a/src/pipes.ts +++ b/src/pipes.ts @@ -1,10 +1,10 @@ import { Pipe, PipeTransform } from '@angular/core'; -import { Civ6GameSpeeds, Civ6Maps, Civ6MapSizes } from './civdefs.service'; +import { CIV6_GAME_SPEEDS, CIV6_MAPS, CIV6_MAP_SIZES } from './civdefs.service'; @Pipe({name: 'civ6gamespeed'}) export class Civ6GameSpeedPipe implements PipeTransform { transform(value: string): string { - for (let gs of Civ6GameSpeeds) { + for (const gs of CIV6_GAME_SPEEDS) { if (gs.key === value) { return gs.displayName; } @@ -17,7 +17,7 @@ export class Civ6GameSpeedPipe implements PipeTransform { @Pipe({name: 'civ6map'}) export class Civ6MapPipe implements PipeTransform { transform(value: string): string { - for (let map of Civ6Maps) { + for (const map of CIV6_MAPS) { if (map.file === value) { return map.displayName; } @@ -30,7 +30,7 @@ export class Civ6MapPipe implements PipeTransform { @Pipe({name: 'civ6mapsize'}) export class Civ6MapSizePipe implements PipeTransform { transform(value: string): string { - for (let ms of Civ6MapSizes) { + for (const ms of CIV6_MAP_SIZES) { if (ms.key === value) { return ms.displayName; } diff --git a/src/profileCache.service.ts b/src/profileCache.service.ts index 36b201d..c92ef03 100644 --- a/src/profileCache.service.ts +++ b/src/profileCache.service.ts @@ -1,17 +1,17 @@ import { Injectable } from '@angular/core'; -import { ApiService } from './api.service'; -import { SteamProfile, Game } from "./entity"; +import { Observable } from 'rxjs/Observable'; import * as _ from 'lodash'; +import 'rxjs/add/operator/toPromise'; @Injectable() export class ProfileCacheService { - private cache = new Map(); - private lastRequest = Promise.resolve(new Map()); + private cache: PcsProfileMap = {}; + private lastRequest: Promise = Promise.resolve({}); - constructor (private api: ApiService) { + constructor (private api: PcsApi) { } - getProfilesForGames(games: Game[]) { + getProfilesForGames(games: PcsGame[]) { const steamIds = _.chain(games) .map('players') .flatten() @@ -22,16 +22,16 @@ export class ProfileCacheService { return this.getProfiles(steamIds); } - getProfilesForGame(game: Game) { + getProfilesForGame(game: PcsGame) { return this.getProfilesForGames([game]); } - getProfiles(steamIds: string[]): Promise> { + getProfiles(steamIds: string[]): Promise { return this.lastRequest = this.lastRequest.then(() => { - let result = new Map(); - let toDownload: string[] = []; + const result: PcsProfileMap = {}; + const toDownload: string[] = []; - for (let steamId of steamIds) { + for (const steamId of steamIds) { if (steamId) { if (this.cache[steamId]) { result[steamId] = this.cache[steamId]; @@ -42,8 +42,8 @@ export class ProfileCacheService { } if (toDownload.length) { - return this.api.getSteamProfiles(toDownload).then(profiles => { - for (let profile of profiles) { + return this.api.userSteamProfiles(toDownload.join(',')).toPromise().then(profiles => { + for (const profile of profiles) { this.cache[profile.steamid] = profile; result[profile.steamid] = profile; } @@ -56,3 +56,26 @@ export class ProfileCacheService { }); } } + +export interface PcsGame { + players: { + steamId: string; + }[]; +} + +export interface PcsSteamProfile { + steamid: string; + personaname: string; + profileurl: string; + avatar: string; + avatarmedium: string; + avatarfull: string; +} + +export interface PcsProfileMap { + [index: string]: PcsSteamProfile; +}; + +export interface PcsApi { + userSteamProfiles(steamIds: string): Observable; +} diff --git a/tslint.json b/tslint.json new file mode 100644 index 0000000..d793f0f --- /dev/null +++ b/tslint.json @@ -0,0 +1,122 @@ +{ + "rules": { + "callable-types": true, + "class-name": true, + "comment-format": [ + true, + "check-space" + ], + "curly": true, + "eofline": true, + "forin": true, + "import-spacing": true, + "indent": [true, 2, "spaces"], + "interface-over-type-literal": true, + "interface-name": [ + false + ], + "jsdoc-format": true, + "label-position": true, + "max-line-length": [ + true, + 140 + ], + "member-access": false, + "member-ordering": [ + "public-before-private", + "static-before-instance", + "variables-before-functions" + ], + "no-angle-bracket-type-assertion": false, + "no-any": false, + "no-arg": true, + "no-bitwise": false, + "no-conditional-assignment": true, + "no-consecutive-blank-lines": true, + "no-console": [ + true, + "log", + "debug", + "info", + "time", + "timeEnd", + "trace" + ], + "no-construct": true, + "no-debugger": true, + "no-duplicate-variable": true, + "no-empty": true, + "no-eval": true, + "no-inferrable-types": true, + "no-internal-module": true, + "no-reference": false, + "no-require-imports": false, + "no-shadowed-variable": true, + "no-string-literal": false, + "no-switch-case-fall-through": true, + "no-trailing-whitespace": true, + "no-unused-expression": true, + "no-unused-variable": true, + "no-use-before-declare": true, + "no-var-keyword": true, + "object-literal-sort-keys": false, + "one-line": [ + true, + "check-open-brace", + "check-catch", + "check-else", + "check-finally", + "check-whitespace" + ], + "prefer-const": true, + "quotemark": [ + true, + "single" + ], + "radix": true, + "semicolon": [ + "always" + ], + "triple-equals": [ + true, + "allow-null-check" + ], + "typedef-whitespace": [ + true, + { + "call-signature": "nospace", + "index-signature": "nospace", + "parameter": "nospace", + "property-declaration": "nospace", + "variable-declaration": "nospace" + }, + { + "call-signature": "space", + "index-signature": "space", + "parameter": "space", + "property-declaration": "space", + "variable-declaration": "space" + } + ], + "typeof-compare": true, + "unified-signatures": true, + "use-isnan": true, + "variable-name": [ + true, + "check-format", + "ban-keywords" + ], + "whitespace": [ + true, + "check-branch", + "check-decl", + "check-operator", + "check-module", + "check-separator", + "check-type", + "check-typecast", + "check-preblock" + ] + } + } + \ No newline at end of file