Skip to content

Commit

Permalink
fix(admin): respect default player skill in player skill edit (#2019)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrappachc authored Nov 14, 2022
1 parent f4ddb7e commit 7614cf6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 44 deletions.
22 changes: 17 additions & 5 deletions src/app/players/player-edit/player-edit.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import { PlayerEditSkillComponent } from '../player-edit-skill/player-edit-skill
import { FeatherComponent } from 'angular-feather';
import { Location } from '@angular/common';
import { Player } from '../models/player';
import { ConfigurationService } from '@app/configuration/configuration.service';
import { DefaultPlayerSkill } from '@app/configuration/models/default-player-skill';
import { ConfigurationEntryKey } from '@app/configuration/configuration-entry-key';

describe(PlayerEditComponent.name, () => {
let fixture: MockedComponentFixture;
Expand All @@ -31,14 +34,14 @@ describe(PlayerEditComponent.name, () => {
let fetchPlayerSkill: Subject<{ [gameClass in Tf2ClassName]?: number }>;
let setPlayerName: Subject<Player>;
let setPlayerSkill: Subject<any>;
let defaultSkill: Subject<{ [gameClass in Tf2ClassName]?: number }>;
let defaultPlayerSkill: Subject<DefaultPlayerSkill>;

beforeEach(() => {
routeParams = new Subject();
fetchPlayerSkill = new Subject();
setPlayerName = new Subject();
setPlayerSkill = new Subject();
defaultSkill = new Subject();
defaultPlayerSkill = new Subject();
});

beforeEach(() =>
Expand All @@ -63,7 +66,12 @@ describe(PlayerEditComponent.name, () => {
.mock(Title)
.mock(Location)
.mock(PlayerEditSkillComponent)
.mock(FeatherComponent),
.mock(FeatherComponent)
.mock(ConfigurationService, {
fetchValue: jasmine
.createSpy('fetchValue')
.and.returnValue(defaultPlayerSkill.pipe(take(1))),
}),
);

beforeEach(() => {
Expand All @@ -83,13 +91,14 @@ describe(PlayerEditComponent.name, () => {
playersService.setPlayerSkill.and.returnValue(
setPlayerSkill.asObservable(),
);
playersService.defaultSkill.and.returnValue(defaultSkill.pipe(take(1)));

fixture.detectChanges();
saveButton = ngMocks.find('button[type=submit]').nativeElement;
nameInput = ngMocks.find('input[type=text]').nativeElement;
});

afterEach(() => defaultPlayerSkill.complete());

it('should create', () => {
expect(component).toBeTruthy();
});
Expand Down Expand Up @@ -147,7 +156,10 @@ describe(PlayerEditComponent.name, () => {
beforeEach(() => {
fetchPlayerSkill.next({});
fetchPlayerSkill.complete();
defaultSkill.next({ scout: -1, soldier: -1 });
defaultPlayerSkill.next({
key: ConfigurationEntryKey.defaultPlayerSkill,
value: { scout: -1, soldier: -1 },
});
fixture.detectChanges();
});

Expand Down
4 changes: 2 additions & 2 deletions src/app/players/player-edit/player-edit.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class PlayerEditComponent implements OnInit, AfterViewInit, OnDestroy {
ngOnInit() {
this.store.player
.pipe(
filter(player => !!player),
filter(player => Boolean(player)),
take(1),
takeUntil(this.destroyed),
)
Expand All @@ -86,7 +86,7 @@ export class PlayerEditComponent implements OnInit, AfterViewInit, OnDestroy {

this.store.skill
.pipe(
filter(skill => !!skill),
filter(skill => Boolean(skill)),
take(1),
takeUntil(this.destroyed),
)
Expand Down
20 changes: 16 additions & 4 deletions src/app/players/player-edit/player-edit.store.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Injectable } from '@angular/core';
import { ConfigurationEntryKey } from '@app/configuration/configuration-entry-key';
import { ConfigurationService } from '@app/configuration/configuration.service';
import { DefaultPlayerSkill } from '@app/configuration/models/default-player-skill';
import { Tf2ClassName } from '@app/shared/models/tf2-class-name';
import { ComponentStore } from '@ngrx/component-store';
import { Store } from '@ngrx/store';
import { isEmpty } from 'lodash-es';
import { Observable, of, zip } from 'rxjs';
import {
filter,
map,
mergeMap,
switchMap,
tap,
Expand Down Expand Up @@ -88,10 +92,14 @@ export class PlayerEditStore extends ComponentStore<PlayerEditState> {
this.playersService.fetchPlayerSkill(id).pipe(
switchMap(skill => {
if (isEmpty(skill)) {
return this.playersService.defaultSkill(id);
} else {
return of(skill);
return this.configurationService
.fetchValue<DefaultPlayerSkill>(
ConfigurationEntryKey.defaultPlayerSkill,
)
.pipe(map(value => value.value));
}

return of(skill);
}),
tap(skill => this.setSkill(skill)),
),
Expand Down Expand Up @@ -120,7 +128,11 @@ export class PlayerEditStore extends ComponentStore<PlayerEditState> {
}),
);

constructor(private store: Store, private playersService: PlayersService) {
constructor(
private readonly store: Store,
private readonly playersService: PlayersService,
private readonly configurationService: ConfigurationService,
) {
super(initialState);
}
}
16 changes: 0 additions & 16 deletions src/app/players/players.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,22 +227,6 @@ describe('PlayersService', () => {
));
});

describe('#defaultSkill()', () => {
it('should init default config', inject(
[PlayersService],
(service: PlayersService) => {
service.defaultSkill('FAKE_PLAYER_ID').subscribe(playerSkill => {
expect(playerSkill).toEqual({
scout: 1,
soldier: 1,
demoman: 1,
medic: 1,
});
});
},
));
});

describe('#forceCreatePlayer()', () => {
it('should post the new player', inject(
[PlayersService],
Expand Down
17 changes: 0 additions & 17 deletions src/app/players/players.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import { Player } from './models/player';
import { API_URL } from '@app/api-url';
import { HttpClient } from '@angular/common/http';
import { Game } from '@app/games/models/game';
import { map, first } from 'rxjs/operators';
import { PlayerStats } from './models/player-stats';
import { PlayerBan } from './models/player-ban';
import { PaginatedList } from '@app/core/models/paginated-list';
import { Store } from '@ngrx/store';
import { queueConfig } from '@app/queue/queue.selectors';
import { PlayerRole } from './models/player-role';
import { Tf2ClassName } from '@app/shared/models/tf2-class-name';
import { LinkedProfiles } from './models/linked-profiles';
Expand Down Expand Up @@ -108,21 +106,6 @@ export class PlayersService {
);
}

defaultSkill(
_playerId: string,
): Observable<{ [gameClass in Tf2ClassName]?: number }> {
return this.store.select(queueConfig).pipe(
first(config => Boolean(config)),
// eslint-disable-next-line ngrx/avoid-mapping-selectors
map(config =>
config.classes.reduce((_skill, curr) => {
_skill[curr.name] = 1;
return _skill;
}, {} as Record<Tf2ClassName, number>),
),
);
}

forceCreatePlayer(player: Partial<Player>) {
return this.http.post<Player>(`${this.apiUrl}/players`, player);
}
Expand Down

0 comments on commit 7614cf6

Please sign in to comment.