spell-table.component.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { Component } from '@angular/core';
  2. import { DataService } from 'src/services/data/data.service';
  3. import { ModalService } from 'src/services/modal/modal.service';
  4. import { DetailsService } from 'src/services/details/details.service';
  5. import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';
  6. import { Spell } from 'src/interfaces/spell';
  7. import { SpellDetailsComponent } from './spell-details/spell-details.component';
  8. import { SpellModalComponent } from 'src/app/journal/spell-modal/spell-modal.component';
  9. import { FullSpellcardComponent } from 'src/app/shared-components/full-spellcard/full-spellcard.component';
  10. import { Observable, OperatorFunction } from 'rxjs';
  11. import { debounceTime, distinctUntilChanged, map } from 'rxjs/operators';
  12. @Component({
  13. selector: 'spell-table',
  14. templateUrl: './spell-table.component.html',
  15. styleUrls: ['./spell-table.component.scss'],
  16. })
  17. export class SpellTableComponent {
  18. public spellAttackBonus: string = '0';
  19. public spellSaveDC: number = 0;
  20. private spellcastingAttribute: string | undefined;
  21. private spellcastingAttributeModifier: number = 0;
  22. private proficiencyBonus: number = 2;
  23. public spells!: Spell[];
  24. private preparedSpells!: Spell[];
  25. private preparedSpellsNames: string[] = [];
  26. public attributes: any = {
  27. strength: 'STR',
  28. dexterity: 'DEX',
  29. constitution: 'CON',
  30. intelligence: 'INT',
  31. wisdom: 'WIS',
  32. charisma: 'CHA',
  33. };
  34. public areas: any = {
  35. cone: 'Kegel',
  36. sphere: 'Kugel',
  37. circle: 'Kreis',
  38. line: 'Linie',
  39. square: 'Quadrat',
  40. cube: 'Würfel',
  41. };
  42. public constructor(
  43. public dataAccessor: DataService,
  44. private modalAccessor: ModalService,
  45. public detailsAccessor: DetailsService
  46. ) {}
  47. public ngOnInit(): void {
  48. this.spells = this.dataAccessor.favoriteSpells;
  49. this.preparedSpells = this.dataAccessor.getAllPreparedSpells();
  50. this.preparedSpellsNames = this.preparedSpells.map((spell) => spell.name);
  51. console.log(this.preparedSpellsNames);
  52. this.subscribeToData();
  53. }
  54. public showFullSpellcard(spellIndex: number): void {
  55. this.modalAccessor.openModal(FullSpellcardComponent, {
  56. spell: this.spells[spellIndex],
  57. isEditable: false,
  58. });
  59. const resultSubscription = this.modalAccessor.result$.subscribe(
  60. (result) => {
  61. resultSubscription.unsubscribe();
  62. if (result.state === 'delete') {
  63. this.spells.splice(spellIndex, 1);
  64. } else if (result.state !== 'cancel') {
  65. console.log(result.state);
  66. throw new Error('Unexpected result state, please send a bug report.');
  67. }
  68. }
  69. );
  70. }
  71. // LEGACY CODE
  72. public openDetailsPanel(index: number): void {
  73. this.detailsAccessor.openPanel(SpellDetailsComponent, {
  74. spell: this.spells[index],
  75. modifiers: {
  76. attackBonus: this.spellAttackBonus,
  77. saveDC: this.spellSaveDC,
  78. },
  79. });
  80. const resultSubscription = this.detailsAccessor.result$.subscribe(
  81. (result) => {
  82. console.log(result);
  83. if (result.state === 'delete') {
  84. this.deleteSpell(index);
  85. } else if (result.state === 'update') {
  86. this.openModal(true, index);
  87. }
  88. resultSubscription.unsubscribe();
  89. }
  90. );
  91. }
  92. //
  93. public openModal(isUpdate: boolean, index?: number): void {
  94. this.modalAccessor.openModal(SpellModalComponent, {
  95. spell:
  96. index !== undefined
  97. ? JSON.parse(JSON.stringify(this.spells[index]))
  98. : undefined,
  99. isUpdate: isUpdate,
  100. });
  101. const resultSubscription = this.modalAccessor.result$.subscribe(
  102. (result) => {
  103. if (result.state === 'update') {
  104. console.log(result.data);
  105. this.updateSpell(result.data, index!);
  106. } else if (result.state === 'add') {
  107. this.addSpell(result.data);
  108. }
  109. resultSubscription.unsubscribe();
  110. }
  111. );
  112. }
  113. public addSpell(spell: Spell) {
  114. this.spells.push(spell);
  115. this.updateSpellsInDatabase();
  116. }
  117. public updateSpell(spell: Spell, index: number): void {
  118. this.spells[index] = spell;
  119. this.updateSpellsInDatabase();
  120. }
  121. public deleteSpell(index: number): void {
  122. this.spells.splice(index, 1);
  123. this.updateSpellsInDatabase();
  124. }
  125. // utils
  126. public dropSpells(event: CdkDragDrop<string[]>): void {
  127. moveItemInArray(this.spells, event.previousIndex, event.currentIndex);
  128. this.updateSpellsInDatabase();
  129. }
  130. public updateSpellsInDatabase(): void {
  131. this.dataAccessor.favoriteSpells = this.spells;
  132. }
  133. private computeSpellAttackBonusAndSaveDC(): void {
  134. let attackBonus =
  135. this.spellcastingAttributeModifier + this.proficiencyBonus;
  136. this.spellSaveDC = 8 + attackBonus;
  137. if (attackBonus >= 0) {
  138. this.spellAttackBonus = '+' + attackBonus;
  139. } else {
  140. this.spellAttackBonus = attackBonus.toString();
  141. }
  142. }
  143. private subscribeToData(): void {
  144. // TODO: this.dataAccessor.getSpellcastingAttribute() oder so
  145. this.spellcastingAttribute = 'intelligence';
  146. this.dataAccessor.proficiency$.subscribe((value) => {
  147. this.proficiencyBonus = value.value;
  148. if (this.spellcastingAttribute) {
  149. this.computeSpellAttackBonusAndSaveDC();
  150. }
  151. });
  152. // TODO: Modify depending on the actual spellcasting attribute
  153. this.dataAccessor.intelligence$.subscribe((value) => {
  154. this.spellcastingAttributeModifier = Math.floor((value.value - 10) / 2);
  155. this.computeSpellAttackBonusAndSaveDC();
  156. });
  157. }
  158. }