add-card.component.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { Component, Input, Output, EventEmitter } from '@angular/core';
  2. import { DataService } from 'src/services/data/data.service';
  3. import { Spell } from 'src/interfaces/spell';
  4. import { SpellsService } from 'src/services/spells/spells.service';
  5. @Component({
  6. selector: 'add-card',
  7. templateUrl: './add-card.component.html',
  8. styleUrl: './add-card.component.scss',
  9. })
  10. export class AddCardComponent {
  11. @Input() level!: number;
  12. @Input() alreadyUsedSpells!: any[];
  13. @Output() createNewSpell = new EventEmitter<any>();
  14. @Output() createNewSpellFromOfficial = new EventEmitter<any>();
  15. @Output() onSpellSelected = new EventEmitter<any>();
  16. public newSpellName: string = '';
  17. private classAvailableSpells: any[] = [];
  18. private allAvailableSpells: any[] = [];
  19. public availableSpells: any[] = [];
  20. public isModification: boolean | undefined;
  21. public state: number = 1;
  22. public showAll: boolean = false;
  23. public constructor(
  24. private spellsAccessor: SpellsService,
  25. private dataAccessor: DataService,
  26. ) {}
  27. public ngOnInit(): void {
  28. this.classAvailableSpells = this.spellsAccessor.getAvailableSpells(
  29. this.level,
  30. this.dataAccessor.characterData.class,
  31. );
  32. this.allAvailableSpells = this.spellsAccessor.getAvailableSpells(
  33. this.level,
  34. );
  35. console.log('all: ', this.allAvailableSpells);
  36. console.log('class: ', this.classAvailableSpells);
  37. this.filterSpellArray();
  38. this.spellsAccessor.closeSubject$.subscribe((level) => {
  39. if (level !== this.level) {
  40. this.resetThis();
  41. }
  42. });
  43. }
  44. public toggleShowAll(): void {
  45. this.showAll = !this.showAll;
  46. if (this.isModification) {
  47. this.filterSpellArrayForModification();
  48. } else {
  49. this.filterSpellArray();
  50. }
  51. }
  52. public emitNewSpell(): void {
  53. this.createNewSpell.emit(this.level);
  54. this.resetThis();
  55. }
  56. public emitNewSpellFromOfficial(spell: Spell): void {
  57. this.createNewSpellFromOfficial.emit(spell);
  58. this.resetThis();
  59. }
  60. public continueToSpellSelection(modify: boolean): void {
  61. this.classAvailableSpells = this.spellsAccessor.getAvailableSpells(
  62. this.level,
  63. this.dataAccessor.characterData.class,
  64. );
  65. // this.filterSpellArray();
  66. this.isModification = modify;
  67. if (modify) {
  68. this.filterSpellArrayForModification();
  69. } else {
  70. this.filterSpellArray();
  71. }
  72. this.closeOthers();
  73. }
  74. public spellSelected(spell: any): void {
  75. this.onSpellSelected.emit(spell);
  76. this.resetThis();
  77. }
  78. public closeOthers(): void {
  79. this.spellsAccessor.closeAllOthers(this.level);
  80. }
  81. public resetThis(): void {
  82. this.newSpellName = '';
  83. this.state = 1;
  84. this.isModification = undefined;
  85. }
  86. public filterSpellArray(): void {
  87. let array = this.showAll
  88. ? this.allAvailableSpells
  89. : this.classAvailableSpells;
  90. if (this.newSpellName.length > 0) {
  91. this.availableSpells = array.filter(
  92. (spell) =>
  93. (spell.german
  94. .toLowerCase()
  95. .includes(this.newSpellName.toLowerCase()) ||
  96. spell.english
  97. .toLowerCase()
  98. .includes(this.newSpellName.toLowerCase())) &&
  99. !this.alreadyUsedSpells.includes(spell.id),
  100. );
  101. } else {
  102. this.availableSpells = array.filter(
  103. (spell) => !this.alreadyUsedSpells.includes(spell.id),
  104. );
  105. }
  106. console.log('available: ', this.availableSpells);
  107. }
  108. public filterSpellArrayForModification(): void {
  109. let array = this.showAll
  110. ? this.allAvailableSpells
  111. : this.classAvailableSpells;
  112. if (this.newSpellName.length > 0) {
  113. this.availableSpells = array.filter(
  114. (spell) =>
  115. spell.german
  116. .toLowerCase()
  117. .includes(this.newSpellName.toLowerCase()) ||
  118. spell.english.toLowerCase().includes(this.newSpellName.toLowerCase()),
  119. );
  120. } else {
  121. this.availableSpells = array;
  122. }
  123. console.log('available: ', this.availableSpells);
  124. }
  125. }