spellslots.component.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 | undefined = undefined;
  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. console.log('calculateModifiers wurde aufgerufen');
  66. console.log('Proficiency: ', this.proficiencyBonus);
  67. console.log('Attribute: ', this.spellcastingAttribute);
  68. console.log('Value: ', this.attributeValue);
  69. if (spellcastingAttribute !== undefined) {
  70. const modifier = (this.attributeValue - 10) / 2;
  71. this.spellAttackModifier = modifier + this.proficiencyBonus;
  72. this.spellSaveDC = 8 + modifier + this.proficiencyBonus;
  73. console.log('Attack: ', this.spellAttackModifier);
  74. console.log('Save: ', this.spellSaveDC);
  75. }
  76. }
  77. private subscribeToAttribute(): void {
  78. if (this.spellcastingAttribute !== undefined) {
  79. const command =
  80. 'this.dataAccessor.' +
  81. this.spellcastingAttribute.toLowerCase() +
  82. '$.subscribe((attribute) => {this.attributeValue = attribute.value; this.calculateModifiers();});';
  83. console.log(command);
  84. eval(command);
  85. }
  86. }
  87. private subscribeToProficiency(): void {
  88. this.dataAccessor.proficiency$.subscribe((value) => {
  89. this.proficiencyBonus = value;
  90. this.calculateModifiers();
  91. });
  92. }
  93. // ki points
  94. public handleUsedKiPoints(pointIndex: number, eventTarget: any): void {
  95. if (eventTarget.checked) {
  96. this.kiPoints.usedPoints++;
  97. if (pointIndex + 1 !== this.kiPoints.usedPoints) {
  98. this.correctKiPointsView();
  99. }
  100. } else {
  101. this.kiPoints.usedPoints--;
  102. if (pointIndex !== this.kiPoints.usedPoints) {
  103. this.correctKiPointsView();
  104. }
  105. }
  106. this.updateKiPointsDatabase();
  107. }
  108. private correctKiPointsView(): void {
  109. const totalKiPoints = this.kiPoints.totalPoints;
  110. const usedKiPoints = this.kiPoints.usedPoints;
  111. for (let kiIndex = 0; kiIndex < usedKiPoints; kiIndex++) {
  112. setTimeout(() => {
  113. (
  114. document.getElementById('checkbox' + kiIndex) as HTMLInputElement
  115. ).checked = true;
  116. });
  117. }
  118. for (let kiIndex = usedKiPoints; kiIndex < totalKiPoints; kiIndex++) {
  119. setTimeout(() => {
  120. (
  121. document.getElementById('checkbox' + kiIndex) as HTMLInputElement
  122. ).checked = false;
  123. });
  124. }
  125. }
  126. // spellslots
  127. public handleUsedSlots(
  128. levelIndex: number,
  129. slotIndex: number,
  130. eventTarget: any
  131. ) {
  132. // if now checked, it means the slot was just used.
  133. if (eventTarget.checked) {
  134. this.spellslots[levelIndex].usedSlots++;
  135. if (slotIndex + 1 !== this.spellslots[levelIndex].usedSlots) {
  136. this.correctSpellslotsView(levelIndex);
  137. }
  138. } else {
  139. this.spellslots[levelIndex].usedSlots--;
  140. if (slotIndex !== this.spellslots[levelIndex].usedSlots) {
  141. this.correctSpellslotsView(levelIndex);
  142. }
  143. }
  144. this.updateSpellslotDatabase();
  145. }
  146. public correctSpellslotsView(levelIndex: number): void {
  147. const totalSlots = this.spellslots[levelIndex].totalSlots;
  148. const usedSlots = this.spellslots[levelIndex].usedSlots;
  149. for (let slotIndex = 0; slotIndex < usedSlots; slotIndex++) {
  150. setTimeout(() => {
  151. (
  152. document.getElementById(
  153. 'checkbox' + levelIndex + '-' + slotIndex
  154. ) as HTMLInputElement
  155. ).checked = true;
  156. });
  157. }
  158. for (let slotIndex = usedSlots; slotIndex < totalSlots; slotIndex++) {
  159. setTimeout(() => {
  160. (
  161. document.getElementById(
  162. 'checkbox' + levelIndex + '-' + slotIndex
  163. ) as HTMLInputElement
  164. ).checked = false;
  165. });
  166. }
  167. }
  168. // general
  169. public getArray(length: number): any[] {
  170. return Array.from({ length: length });
  171. }
  172. public isNotEmptyObject(obj: object): boolean {
  173. return Object.keys(obj).length !== 0;
  174. }
  175. public updateSpellslotDatabase(): void {
  176. this.dataAccessor.spellslots = {
  177. spellslots: this.spellslots,
  178. showSpellslots: this.showSpellslots,
  179. spellcastingAttribute: this.spellcastingAttribute,
  180. };
  181. }
  182. public updateKiPointsDatabase(): void {
  183. this.dataAccessor.kiPoints = this.kiPoints;
  184. }
  185. public updateSlotsAndPoints(data: any): void {
  186. this.spellslots = data.spellslots.spellslots;
  187. this.showSpellslots = data.spellslots.showSpellslots;
  188. this.kiPoints = data.kiPoints;
  189. setTimeout(() => {
  190. this.spellslots.forEach((_, levelIndex) => {
  191. this.correctSpellslotsView(levelIndex);
  192. });
  193. this.correctKiPointsView();
  194. }, 200);
  195. this.updateSpellslotDatabase();
  196. this.updateKiPointsDatabase();
  197. }
  198. }