spellslots.component.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 { SpellslotsModalComponent } from './spellslots-modal/spellslots-modal.component';
  5. @Component({
  6. selector: 'spellslots-table',
  7. templateUrl: './spellslots.component.html',
  8. styleUrls: ['./spellslots.component.scss'],
  9. })
  10. export class SpellslotsComponent {
  11. public spellslots: any[] = [];
  12. public showSpellslots: boolean = false;
  13. public kiPoints: any;
  14. public spellcastingAttribute: string = '';
  15. public proficiencyBonus: number = 2;
  16. public attributeValue: number = 0;
  17. public isMonk: boolean = true;
  18. public spellAttackModifier: number = 0;
  19. public spellSaveDC: number = 0;
  20. public slotNumber: number = 1;
  21. public constructor(
  22. public dataAccessor: DataService,
  23. public modalAccessor: ModalService,
  24. ) {
  25. this.isMonk = this.dataAccessor.characterData.class === 'Monk';
  26. }
  27. public ngOnInit(): void {
  28. const spells = this.dataAccessor.spellslots;
  29. const kiPoints = this.dataAccessor.kiPoints;
  30. this.spellslots = spells.spellslots;
  31. this.showSpellslots = spells.showSpellslots;
  32. this.spellcastingAttribute = spells.spellcastingAttribute;
  33. this.kiPoints = kiPoints;
  34. // this.calculateModifiers();
  35. this.subscribeToProficiency();
  36. this.subscribeToAttribute();
  37. }
  38. public ngAfterViewInit(): void {
  39. setTimeout(() => {
  40. this.spellslots.forEach((_, levelIndex) => {
  41. this.correctSpellslotsView(levelIndex);
  42. });
  43. this.correctKiPointsView();
  44. }, 10);
  45. }
  46. // FUNCTIONS
  47. public openModal(): void {
  48. this.modalAccessor.openModal(SpellslotsModalComponent, {
  49. kiPoints: JSON.parse(JSON.stringify(this.kiPoints)),
  50. spellslots: JSON.parse(JSON.stringify(this.spellslots)),
  51. showSpellslots: this.showSpellslots,
  52. isMonk: this.isMonk,
  53. });
  54. const resultSubscription = this.modalAccessor.result$.subscribe(
  55. (result) => {
  56. if (result.state === 'update') {
  57. this.updateSlotsAndPoints(result.data);
  58. }
  59. resultSubscription.unsubscribe();
  60. },
  61. );
  62. }
  63. private calculateModifiers(): void {
  64. const spellcastingAttribute = this.spellcastingAttribute;
  65. if (spellcastingAttribute !== undefined) {
  66. const modifier = Math.floor((this.attributeValue - 10) / 2);
  67. this.spellAttackModifier = modifier + this.proficiencyBonus;
  68. this.spellSaveDC = 8 + modifier + this.proficiencyBonus;
  69. }
  70. }
  71. private subscribeToAttribute(): void {
  72. if (
  73. this.spellcastingAttribute !== undefined &&
  74. this.spellcastingAttribute !== null
  75. ) {
  76. console.log(this.spellcastingAttribute);
  77. const command =
  78. 'this.dataAccessor.' +
  79. this.spellcastingAttribute.toLowerCase() +
  80. '$.subscribe((attribute) => {this.attributeValue = attribute.value; this.calculateModifiers();});';
  81. eval(command);
  82. }
  83. }
  84. private subscribeToProficiency(): void {
  85. this.dataAccessor.proficiency$.subscribe((value) => {
  86. this.proficiencyBonus = value;
  87. this.calculateModifiers();
  88. });
  89. }
  90. // ki points
  91. public handleUsedKiPoints(pointIndex: number, eventTarget: any): void {
  92. if (eventTarget.checked) {
  93. this.kiPoints.usedPoints++;
  94. if (pointIndex + 1 !== this.kiPoints.usedPoints) {
  95. this.correctKiPointsView();
  96. }
  97. } else {
  98. this.kiPoints.usedPoints--;
  99. if (pointIndex !== this.kiPoints.usedPoints) {
  100. this.correctKiPointsView();
  101. }
  102. }
  103. this.updateKiPointsDatabase();
  104. }
  105. private correctKiPointsView(): void {
  106. const totalKiPoints = this.kiPoints.totalPoints;
  107. const usedKiPoints = this.kiPoints.usedPoints;
  108. for (let kiIndex = 0; kiIndex < usedKiPoints; kiIndex++) {
  109. setTimeout(() => {
  110. (
  111. document.getElementById('checkbox' + kiIndex) as HTMLInputElement
  112. ).checked = true;
  113. });
  114. }
  115. for (let kiIndex = usedKiPoints; kiIndex < totalKiPoints; kiIndex++) {
  116. setTimeout(() => {
  117. (
  118. document.getElementById('checkbox' + kiIndex) as HTMLInputElement
  119. ).checked = false;
  120. });
  121. }
  122. }
  123. // spellslots
  124. public handleUsedSlots(
  125. levelIndex: number,
  126. slotIndex: number,
  127. eventTarget: any,
  128. ) {
  129. // if now checked, it means the slot was just used.
  130. if (eventTarget.checked) {
  131. this.spellslots[levelIndex].usedSlots++;
  132. if (slotIndex + 1 !== this.spellslots[levelIndex].usedSlots) {
  133. this.correctSpellslotsView(levelIndex);
  134. }
  135. } else {
  136. this.spellslots[levelIndex].usedSlots--;
  137. if (slotIndex !== this.spellslots[levelIndex].usedSlots) {
  138. this.correctSpellslotsView(levelIndex);
  139. }
  140. }
  141. this.updateSpellslotDatabase();
  142. }
  143. public correctSpellslotsView(levelIndex: number): void {
  144. const totalSlots = this.spellslots[levelIndex].totalSlots;
  145. const usedSlots = this.spellslots[levelIndex].usedSlots;
  146. for (let slotIndex = 0; slotIndex < usedSlots; slotIndex++) {
  147. setTimeout(() => {
  148. (
  149. document.getElementById(
  150. 'checkbox' + levelIndex + '-' + slotIndex,
  151. ) as HTMLInputElement
  152. ).checked = true;
  153. });
  154. }
  155. for (let slotIndex = usedSlots; slotIndex < totalSlots; slotIndex++) {
  156. setTimeout(() => {
  157. (
  158. document.getElementById(
  159. 'checkbox' + levelIndex + '-' + slotIndex,
  160. ) as HTMLInputElement
  161. ).checked = false;
  162. });
  163. }
  164. }
  165. // general
  166. public getArray(length: number): any[] {
  167. return Array.from({ length: length });
  168. }
  169. public isNotEmptyObject(obj: object): boolean {
  170. return Object.keys(obj).length !== 0;
  171. }
  172. public updateSpellslotDatabase(): void {
  173. this.dataAccessor.spellslots = {
  174. spellslots: this.spellslots,
  175. showSpellslots: this.showSpellslots,
  176. spellcastingAttribute: this.spellcastingAttribute,
  177. };
  178. }
  179. public updateKiPointsDatabase(): void {
  180. this.dataAccessor.kiPoints = this.kiPoints;
  181. }
  182. public updateSlotsAndPoints(data: any): void {
  183. this.spellslots = data.spellslots.spellslots;
  184. this.showSpellslots = data.spellslots.showSpellslots;
  185. this.kiPoints = data.kiPoints;
  186. setTimeout(() => {
  187. this.spellslots.forEach((_, levelIndex) => {
  188. this.correctSpellslotsView(levelIndex);
  189. });
  190. this.correctKiPointsView();
  191. }, 200);
  192. this.updateSpellslotDatabase();
  193. this.updateKiPointsDatabase();
  194. }
  195. }