spell-table.component.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 newSpellName: string = '';
  27. public showInput: boolean = false;
  28. public attributes: any = {
  29. strength: 'STR',
  30. dexterity: 'DEX',
  31. constitution: 'CON',
  32. intelligence: 'INT',
  33. wisdom: 'WIS',
  34. charisma: 'CHA',
  35. };
  36. public areas: any = {
  37. cone: 'Kegel',
  38. sphere: 'Kugel',
  39. circle: 'Kreis',
  40. line: 'Linie',
  41. square: 'Quadrat',
  42. cube: 'Würfel',
  43. };
  44. public constructor(
  45. public dataAccessor: DataService,
  46. private modalAccessor: ModalService,
  47. public detailsAccessor: DetailsService
  48. ) {}
  49. public ngOnInit(): void {
  50. this.spells = this.dataAccessor.favoriteSpells;
  51. this.preparedSpells = this.dataAccessor.getAllPreparedSpells();
  52. this.preparedSpellsNames = this.preparedSpells.map((spell) => spell.name);
  53. console.log(this.preparedSpellsNames);
  54. this.subscribeToData();
  55. }
  56. public showFullSpellcard(spellIndex: number): void {
  57. this.modalAccessor.openModal(FullSpellcardComponent, {
  58. spell: this.spells[spellIndex],
  59. isFromDashboard: true,
  60. });
  61. const resultSubscription = this.modalAccessor.result$.subscribe(
  62. (result) => {
  63. resultSubscription.unsubscribe();
  64. if (result.state === 'delete') {
  65. this.spells.splice(spellIndex, 1);
  66. } else if (result.state !== 'cancel') {
  67. console.log(result.state);
  68. throw new Error('Unexpected result state, please send a bug report.');
  69. }
  70. }
  71. );
  72. }
  73. // LEGACY CODE
  74. public openDetailsPanel(index: number): void {
  75. this.detailsAccessor.openPanel(SpellDetailsComponent, {
  76. spell: this.spells[index],
  77. modifiers: {
  78. attackBonus: this.spellAttackBonus,
  79. saveDC: this.spellSaveDC,
  80. },
  81. });
  82. const resultSubscription = this.detailsAccessor.result$.subscribe(
  83. (result) => {
  84. console.log(result);
  85. if (result.state === 'delete') {
  86. this.deleteSpell(index);
  87. } else if (result.state === 'update') {
  88. this.openModal(true, index);
  89. }
  90. resultSubscription.unsubscribe();
  91. }
  92. );
  93. }
  94. //
  95. public openModal(isUpdate: boolean, index?: number): void {
  96. this.modalAccessor.openModal(SpellModalComponent, {
  97. spell:
  98. index !== undefined
  99. ? JSON.parse(JSON.stringify(this.spells[index]))
  100. : undefined,
  101. isUpdate: isUpdate,
  102. });
  103. const resultSubscription = this.modalAccessor.result$.subscribe(
  104. (result) => {
  105. if (result.state === 'update') {
  106. console.log(result.data);
  107. this.updateSpell(result.data, index!);
  108. } else if (result.state === 'add') {
  109. this.addSpell(result.data);
  110. }
  111. resultSubscription.unsubscribe();
  112. }
  113. );
  114. }
  115. public toggleInput(): void {
  116. this.showInput = !this.showInput;
  117. this.newSpellName = '';
  118. }
  119. public onSpellSelect(spellname: any): void {
  120. const newSpell = this.preparedSpells.filter(
  121. (spell) => spell.name === spellname
  122. );
  123. if (newSpell.length !== 1) {
  124. throw new Error('Spell not found.');
  125. } else {
  126. this.addSpell(newSpell[0]);
  127. }
  128. this.newSpellName = '';
  129. }
  130. public addSpell(spell: Spell) {
  131. this.spells.push(spell);
  132. this.updateSpellsInDatabase();
  133. }
  134. public updateSpell(spell: Spell, index: number): void {
  135. this.spells[index] = spell;
  136. this.updateSpellsInDatabase();
  137. }
  138. public deleteSpell(index: number): void {
  139. this.spells.splice(index, 1);
  140. this.updateSpellsInDatabase();
  141. }
  142. // utils
  143. public dropSpells(event: CdkDragDrop<string[]>): void {
  144. moveItemInArray(this.spells, event.previousIndex, event.currentIndex);
  145. this.updateSpellsInDatabase();
  146. }
  147. public updateSpellsInDatabase(): void {
  148. this.dataAccessor.favoriteSpells = this.spells;
  149. }
  150. private computeSpellAttackBonusAndSaveDC(): void {
  151. let attackBonus =
  152. this.spellcastingAttributeModifier + this.proficiencyBonus;
  153. this.spellSaveDC = 8 + attackBonus;
  154. if (attackBonus >= 0) {
  155. this.spellAttackBonus = '+' + attackBonus;
  156. } else {
  157. this.spellAttackBonus = attackBonus.toString();
  158. }
  159. }
  160. private subscribeToData(): void {
  161. // TODO: this.dataAccessor.getSpellcastingAttribute() oder so
  162. this.spellcastingAttribute = 'intelligence';
  163. this.dataAccessor.proficiency$.subscribe((value) => {
  164. this.proficiencyBonus = value.value;
  165. if (this.spellcastingAttribute) {
  166. this.computeSpellAttackBonusAndSaveDC();
  167. }
  168. });
  169. // TODO: Modify depending on the actual spellcasting attribute
  170. this.dataAccessor.intelligence$.subscribe((value) => {
  171. this.spellcastingAttributeModifier = Math.floor((value.value - 10) / 2);
  172. this.computeSpellAttackBonusAndSaveDC();
  173. });
  174. }
  175. public search: OperatorFunction<string, readonly string[]> = (
  176. text$: Observable<string>
  177. ) =>
  178. text$.pipe(
  179. debounceTime(200),
  180. distinctUntilChanged(),
  181. map((term) =>
  182. term.length < 2
  183. ? []
  184. : this.preparedSpellsNames
  185. .filter((v) => v.toLowerCase().indexOf(term.toLowerCase()) > -1)
  186. .filter(
  187. (v) =>
  188. !this.spells.some(
  189. (spell) => spell.name.toLowerCase() === v.toLowerCase()
  190. )
  191. )
  192. .slice(0, 5)
  193. )
  194. );
  195. }