spellslots.component.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { Component } from '@angular/core';
  2. import { DataService } from 'src/services/data/data.service';
  3. import { NgxSmartModalService } from 'ngx-smart-modal';
  4. @Component({
  5. selector: 'spellslots',
  6. templateUrl: './spellslots.component.html',
  7. styleUrls: ['./spellslots.component.scss'],
  8. })
  9. export class SpellslotsComponent {
  10. public constructor(
  11. public dataAccessor: DataService,
  12. public ngxSmartModalService: NgxSmartModalService
  13. ) {}
  14. public spellslots: any[] = [];
  15. public showSpellslots: boolean = false;
  16. public kiPoints: any;
  17. public slotNumber: number = 1;
  18. public ngOnInit(): void {
  19. const spells = this.dataAccessor.getSpellslots();
  20. const kiPoints = this.dataAccessor.getKiPoints();
  21. this.spellslots = spells.spellslots;
  22. this.showSpellslots = spells.showSpellslots;
  23. this.kiPoints = kiPoints;
  24. }
  25. public ngAfterViewInit(): void {
  26. setTimeout(() => {
  27. this.spellslots.forEach((_, levelIndex) => {
  28. this.correctSpellslotsView(levelIndex);
  29. });
  30. this.correctKiPointsView();
  31. }, 200);
  32. }
  33. //////////////
  34. // ki points
  35. public handleUsedKiPoints(pointIndex: number, eventTarget: any): void {
  36. if (eventTarget.checked) {
  37. this.kiPoints.usedPoints++;
  38. if (pointIndex + 1 !== this.kiPoints.usedPoints) {
  39. this.correctKiPointsView();
  40. }
  41. } else {
  42. this.kiPoints.usedPoints--;
  43. if (pointIndex !== this.kiPoints.usedPoints) {
  44. this.correctKiPointsView();
  45. }
  46. }
  47. this.updateKiPointsDatabase();
  48. }
  49. private correctKiPointsView(): void {
  50. const totalKiPoints = this.kiPoints.totalPoints;
  51. const usedKiPoints = this.kiPoints.usedPoints;
  52. for (let kiIndex = 0; kiIndex < usedKiPoints; kiIndex++) {
  53. setTimeout(() => {
  54. (
  55. document.getElementById('checkbox' + kiIndex) as HTMLInputElement
  56. ).checked = true;
  57. });
  58. }
  59. for (let kiIndex = usedKiPoints; kiIndex < totalKiPoints; kiIndex++) {
  60. setTimeout(() => {
  61. (
  62. document.getElementById('checkbox' + kiIndex) as HTMLInputElement
  63. ).checked = false;
  64. });
  65. }
  66. }
  67. // spellslots
  68. public handleUsedSlots(
  69. levelIndex: number,
  70. slotIndex: number,
  71. eventTarget: any
  72. ) {
  73. // if now checked, it means the slot was just used.
  74. if (eventTarget.checked) {
  75. this.spellslots[levelIndex].usedSlots++;
  76. if (slotIndex + 1 !== this.spellslots[levelIndex].usedSlots) {
  77. this.correctSpellslotsView(levelIndex);
  78. }
  79. } else {
  80. this.spellslots[levelIndex].usedSlots--;
  81. if (slotIndex !== this.spellslots[levelIndex].usedSlots) {
  82. this.correctSpellslotsView(levelIndex);
  83. }
  84. }
  85. this.updateSpellslotDatabase();
  86. }
  87. public correctSpellslotsView(levelIndex: number): void {
  88. const totalSlots = this.spellslots[levelIndex].totalSlots;
  89. const usedSlots = this.spellslots[levelIndex].usedSlots;
  90. for (let slotIndex = 0; slotIndex < usedSlots; slotIndex++) {
  91. setTimeout(() => {
  92. (
  93. document.getElementById(
  94. 'checkbox' + levelIndex + '-' + slotIndex
  95. ) as HTMLInputElement
  96. ).checked = true;
  97. });
  98. }
  99. for (let slotIndex = usedSlots; slotIndex < totalSlots; slotIndex++) {
  100. setTimeout(() => {
  101. (
  102. document.getElementById(
  103. 'checkbox' + levelIndex + '-' + slotIndex
  104. ) as HTMLInputElement
  105. ).checked = false;
  106. });
  107. }
  108. }
  109. // general
  110. public getArray(length: number): any[] {
  111. return Array.from({ length: length });
  112. }
  113. public isNotEmptyObject(obj: object): boolean {
  114. return Object.keys(obj).length !== 0;
  115. }
  116. public updateSpellslotDatabase(): void {
  117. this.dataAccessor.setSpellslots({
  118. spellslots: this.spellslots,
  119. showSpellslots: this.showSpellslots,
  120. });
  121. }
  122. public updateKiPointsDatabase(): void {
  123. this.dataAccessor.setKiPoints(this.kiPoints);
  124. }
  125. public openSpellslotModal(): void {
  126. this.ngxSmartModalService.getModal('spellslotsModal').open();
  127. }
  128. public updateSlotsAndPoints(data: any): void {
  129. console.log(data.spellslots.spellslots);
  130. this.spellslots = data.spellslots.spellslots;
  131. this.showSpellslots = data.spellslots.showSpellslots;
  132. this.kiPoints = data.kiPoints.kiPoints;
  133. setTimeout(() => {
  134. this.spellslots.forEach((_, levelIndex) => {
  135. this.correctSpellslotsView(levelIndex);
  136. });
  137. this.correctKiPointsView();
  138. }, 200);
  139. this.updateSpellslotDatabase();
  140. this.updateKiPointsDatabase();
  141. }
  142. }