123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- import { Component } from '@angular/core';
- import { DataService } from 'src/services/data/data.service';
- import { ModalService } from 'src/services/modal/modal.service';
- import { DetailsService } from 'src/services/details/details.service';
- import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';
- import { Spell } from 'src/interfaces/spell';
- import { SpellDetailsComponent } from './spell-details/spell-details.component';
- import { SpellModalComponent } from 'src/app/journal/spell-modal/spell-modal.component';
- import { FullSpellcardComponent } from 'src/app/shared-components/full-spellcard/full-spellcard.component';
- import { Observable, OperatorFunction } from 'rxjs';
- import { debounceTime, distinctUntilChanged, map } from 'rxjs/operators';
- @Component({
- selector: 'spell-table',
- templateUrl: './spell-table.component.html',
- styleUrls: ['./spell-table.component.scss'],
- })
- export class SpellTableComponent {
- public spellAttackBonus: string = '0';
- public spellSaveDC: number = 0;
- private spellcastingAttribute: string | undefined;
- private spellcastingAttributeModifier: number = 0;
- private proficiencyBonus: number = 2;
- public spells!: Spell[];
- private preparedSpells!: Spell[];
- private preparedSpellsNames: string[] = [];
- public attributes: any = {
- strength: 'STR',
- dexterity: 'DEX',
- constitution: 'CON',
- intelligence: 'INT',
- wisdom: 'WIS',
- charisma: 'CHA',
- };
- public areas: any = {
- cone: 'Kegel',
- sphere: 'Kugel',
- circle: 'Kreis',
- line: 'Linie',
- square: 'Quadrat',
- cube: 'Würfel',
- };
- public constructor(
- public dataAccessor: DataService,
- private modalAccessor: ModalService,
- public detailsAccessor: DetailsService
- ) {}
- public ngOnInit(): void {
- this.spells = this.dataAccessor.favoriteSpells;
- this.preparedSpells = this.dataAccessor.getAllPreparedSpells();
- this.preparedSpellsNames = this.preparedSpells.map((spell) => spell.name);
- console.log(this.preparedSpellsNames);
- this.subscribeToData();
- }
- public showFullSpellcard(spellIndex: number): void {
- this.modalAccessor.openModal(FullSpellcardComponent, {
- spell: this.spells[spellIndex],
- isEditable: false,
- });
- const resultSubscription = this.modalAccessor.result$.subscribe(
- (result) => {
- resultSubscription.unsubscribe();
- if (result.state === 'delete') {
- this.spells.splice(spellIndex, 1);
- } else if (result.state !== 'cancel') {
- console.log(result.state);
- throw new Error('Unexpected result state, please send a bug report.');
- }
- }
- );
- }
- // LEGACY CODE
- public openDetailsPanel(index: number): void {
- this.detailsAccessor.openPanel(SpellDetailsComponent, {
- spell: this.spells[index],
- modifiers: {
- attackBonus: this.spellAttackBonus,
- saveDC: this.spellSaveDC,
- },
- });
- const resultSubscription = this.detailsAccessor.result$.subscribe(
- (result) => {
- console.log(result);
- if (result.state === 'delete') {
- this.deleteSpell(index);
- } else if (result.state === 'update') {
- this.openModal(true, index);
- }
- resultSubscription.unsubscribe();
- }
- );
- }
- //
- public openModal(isUpdate: boolean, index?: number): void {
- this.modalAccessor.openModal(SpellModalComponent, {
- spell:
- index !== undefined
- ? JSON.parse(JSON.stringify(this.spells[index]))
- : undefined,
- isUpdate: isUpdate,
- });
- const resultSubscription = this.modalAccessor.result$.subscribe(
- (result) => {
- if (result.state === 'update') {
- console.log(result.data);
- this.updateSpell(result.data, index!);
- } else if (result.state === 'add') {
- this.addSpell(result.data);
- }
- resultSubscription.unsubscribe();
- }
- );
- }
- public addSpell(spell: Spell) {
- this.spells.push(spell);
- this.updateSpellsInDatabase();
- }
- public updateSpell(spell: Spell, index: number): void {
- this.spells[index] = spell;
- this.updateSpellsInDatabase();
- }
- public deleteSpell(index: number): void {
- this.spells.splice(index, 1);
- this.updateSpellsInDatabase();
- }
- // utils
- public dropSpells(event: CdkDragDrop<string[]>): void {
- moveItemInArray(this.spells, event.previousIndex, event.currentIndex);
- this.updateSpellsInDatabase();
- }
- public updateSpellsInDatabase(): void {
- this.dataAccessor.favoriteSpells = this.spells;
- }
- private computeSpellAttackBonusAndSaveDC(): void {
- let attackBonus =
- this.spellcastingAttributeModifier + this.proficiencyBonus;
- this.spellSaveDC = 8 + attackBonus;
- if (attackBonus >= 0) {
- this.spellAttackBonus = '+' + attackBonus;
- } else {
- this.spellAttackBonus = attackBonus.toString();
- }
- }
- private subscribeToData(): void {
- // TODO: this.dataAccessor.getSpellcastingAttribute() oder so
- this.spellcastingAttribute = 'intelligence';
- this.dataAccessor.proficiency$.subscribe((value) => {
- this.proficiencyBonus = value.value;
- if (this.spellcastingAttribute) {
- this.computeSpellAttackBonusAndSaveDC();
- }
- });
- // TODO: Modify depending on the actual spellcasting attribute
- this.dataAccessor.intelligence$.subscribe((value) => {
- this.spellcastingAttributeModifier = Math.floor((value.value - 10) / 2);
- this.computeSpellAttackBonusAndSaveDC();
- });
- }
- }
|