spellslots.component.ts 6.5 KB

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