SES-165 created weapon teble for player, connected with backend

This commit is contained in:
Natalia Gawron 2021-01-20 20:57:59 +01:00
parent 5259a2ec57
commit 96ff8ae45f
9 changed files with 258 additions and 5 deletions

View File

@ -50,6 +50,8 @@ import { GameMasterMonstersTableComponent } from './components/game-master-monst
import { MonsterService } from '../services/monster.service';
import { SpellDetailsDialogComponent } from './components/spell-details-dialog/spell-details-dialog.component';
import { GameMasterShopkeepersTableComponent } from './components/game-master-shopkeepers-table/game-master-shopkeepers-table.component';
import { PlayerWeaponsTableComponent } from './components/player-weapons-table/player-weapons-table.component';
import {EquipmentService} from "../services/equipment.service";
@NgModule({
declarations: [
@ -70,6 +72,7 @@ import { GameMasterShopkeepersTableComponent } from './components/game-master-sh
GameMasterMonstersTableComponent,
SpellDetailsDialogComponent,
GameMasterShopkeepersTableComponent,
PlayerWeaponsTableComponent,
],
imports: [
BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
@ -107,6 +110,7 @@ import { GameMasterShopkeepersTableComponent } from './components/game-master-sh
ArmorService,
OtherEquipmentService,
MonsterService,
EquipmentService,
],
bootstrap: [AppComponent],
entryComponents: [
@ -119,6 +123,7 @@ import { GameMasterShopkeepersTableComponent } from './components/game-master-sh
ThrowPrimaryAbilityComponent,
SpellDetailsDialogComponent,
GameMasterShopkeepersTableComponent,
PlayerWeaponsTableComponent,
],
})
export class AppModule {}

View File

@ -15,12 +15,12 @@
<mat-sidenav #sidenav class="sidenav">
<mat-nav-list>
<mat-list-item>
<mat-list-item (click)="SwitchMiddleComponent('AbilitiesComponent')">
<mat-icon [class.active]="selected" matListIcon>query_stats</mat-icon>
<a matLine>Statistic and throws</a>
</mat-list-item>
<mat-list-item>
<mat-list-item (click)="SwitchMiddleComponent('PlayerWeaponsTableComponent')">
<mat-icon [class.active]="selected" matListIcon>local_mall</mat-icon>
<a matLine>Equipment</a>
</mat-list-item>

View File

@ -4,9 +4,10 @@ import { first } from 'rxjs/operators';
import { Store } from '@ngrx/store';
import { AppState } from 'src/app/store/models/app-state.model';
import { AbilitiesComponent } from '../abilities/abilities.component';
import {ClearStore, ClearUserId} from '../../store/actions/app.actions';
import { ClearStore } from '../../store/actions/app.actions';
import { Router } from '@angular/router';
import {ClearCharacterId} from "../../store/actions/player.action";
import { ClearCharacterId } from '../../store/actions/player.action';
import { PlayerWeaponsTableComponent } from '../player-weapons-table/player-weapons-table.component';
@Component({
selector: 'app-player-dashboard',
@ -35,6 +36,10 @@ export class PlayerDashboardComponent implements OnInit {
switch (componentName) {
case 'AbilitiesComponent':
this.middleComponent = AbilitiesComponent;
break;
case 'PlayerWeaponsTableComponent':
this.middleComponent = PlayerWeaponsTableComponent;
break;
}
}

View File

@ -0,0 +1,43 @@
.text-cut {
max-width: 60px; /* feel free to include any other value */
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
table {
background-color: initial;
}
mat-paginator {
background-color: initial;
color: white;
}
::ng-deep .mat-select-arrow {
color: whitesmoke;
}
::ng-deep .mat-select-value {
color: white;
}
.mat-sort-header-container {
color: whitesmoke !important;
}
.mat-form-field {
font-size: 14px;
width: 100%;
}
td,
th {
color: whitesmoke;
}
.text-centre {
padding: 1%;
text-align: center;
align-items: center
}

View File

@ -0,0 +1,39 @@
<div class="text-centre">Your weapons:</div>
<mat-form-field>
<mat-label>Filter</mat-label>
<input matInput (keyup)="applyFilter($event)" placeholder="Ex. Sword" #input>
</mat-form-field>
<div class="mat-elevation-z8">
<table mat-table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Name </th>
<td mat-cell *matCellDef="let row"> {{row.name}} </td>
</ng-container>
<ng-container matColumnDef="weaponType">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Weapon Type </th>
<td mat-cell *matCellDef="let row"> {{row.weaponType}} </td>
</ng-container>
<ng-container matColumnDef="weight">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Weight </th>
<td mat-cell *matCellDef="let row"> {{row.weight}} </td>
</ng-container>
<ng-container matColumnDef="cost">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Cost </th>
<td mat-cell *matCellDef="let row"> {{row.cost}} $ </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
<tr class="mat-row" *matNoDataRow>
<td class="mat-cell" colspan="4">No data matching the filter "{{input.value}}"</td>
</tr>
</table>
<mat-paginator [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator>
</div>

View File

@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { PlayerWeaponsTableComponent } from './player-weapons-table.component';
describe('PlayerWeaponsTableComponent', () => {
let component: PlayerWeaponsTableComponent;
let fixture: ComponentFixture<PlayerWeaponsTableComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ PlayerWeaponsTableComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(PlayerWeaponsTableComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@ -0,0 +1,69 @@
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { WeaponViewModel } from '../../../types/viewmodels/weapon-viewmodels/WeaponViewModel';
import { first } from 'rxjs/operators';
import { ErrorResponse } from '../../../types/ErrorResponse';
import { HttpErrorResponse } from '@angular/common/http';
import { EquipmentService } from '../../../services/equipment.service';
import { Store } from '@ngrx/store';
import { AppState } from '../../store/models/app-state.model';
@Component({
selector: 'app-player-weapons-table',
templateUrl: './player-weapons-table.component.html',
styleUrls: ['./player-weapons-table.component.css']
})
export class PlayerWeaponsTableComponent implements OnInit {
weapons: WeaponViewModel[];
displayedColumns: string[] = [
'name',
'weaponType',
'weight',
'cost',
];
dataSource: MatTableDataSource<WeaponViewModel>;
@ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
@ViewChild(MatSort, { static: true }) sort: MatSort;
constructor(private store: Store<AppState>, private equipmentService: EquipmentService) {}
ngOnInit() {
this.getCharacterWeapons();
}
getCharacterWeapons() {
this.store
.select((s) => s.playerStore.characterId)
.pipe(first())
.subscribe((characterId) => {
this.equipmentService
.getCharacterWeapons(characterId)
.pipe(first())
.subscribe(
(result) => {
this.dataSource = new MatTableDataSource(result);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
},
(error: ErrorResponse | HttpErrorResponse) => {
if (error instanceof HttpErrorResponse) {
error = error.error as ErrorResponse;
}
console.error(error.message);
}
);
});
}
applyFilter(event: Event) {
const filterValue = (event.target as HTMLInputElement).value;
this.dataSource.filter = filterValue.trim().toLowerCase();
if (this.dataSource.paginator) {
this.dataSource.paginator.firstPage();
}
}
}

View File

@ -0,0 +1,60 @@
import { Inject, Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { WeaponViewModel } from '../types/viewmodels/weapon-viewmodels/WeaponViewModel';
import { Observable, of, throwError } from 'rxjs';
import { Either } from '../types/Either';
import { ErrorResponse } from '../types/ErrorResponse';
import { switchMap } from 'rxjs/operators';
import { ArmorViewModel } from '../types/viewmodels/armor-viewmodels/ArmorViewModel';
import { OtherEquipmentViewModel } from '../types/viewmodels/otherEquipment-viewmodels/OtherEquipmentViewModel';
Injectable({
providedIn: 'root',
});
export class EquipmentService {
private baseUrl = 'api/character/equipment/';
constructor(private http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
this.baseUrl = baseUrl + this.baseUrl;
}
getCharacterWeapons(characterId: number): Observable<WeaponViewModel[]> {
const params = new HttpParams().set('characterId', characterId.toString());
return this.http.get<Either<WeaponViewModel[], ErrorResponse>>(this.baseUrl + 'getWeapons', {params}).pipe(
switchMap((response) => {
if (response.isLeft) {
return of(response.left);
} else {
return throwError(response.right);
}
})
);
}
getCharacterArmors(characterId: number): Observable<ArmorViewModel[]> {
const params = new HttpParams().set('characterId', characterId.toString());
return this.http.get<Either<ArmorViewModel[], ErrorResponse>>(this.baseUrl + 'getArmors', {params}).pipe(
switchMap((response) => {
if (response.isLeft) {
return of(response.left);
} else {
return throwError(response.right);
}
})
);
}
getCharacterOtherEquipment(characterId: number): Observable<OtherEquipmentViewModel[]> {
const params = new HttpParams().set('characterId', characterId.toString());
return this.http.get<Either<OtherEquipmentViewModel[], ErrorResponse>>(this.baseUrl + 'getOtherEquipment', {params}).pipe(
switchMap((response) => {
if (response.isLeft) {
return of(response.left);
} else {
return throwError(response.right);
}
})
);
}
}

View File

@ -0,0 +1,7 @@
export interface OtherEquipmentViewModel {
id: number;
name: string;
description: string;
const: number;
currencyType: number;
}