save-throw-field.component.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { Component, Input } from '@angular/core';
  2. import { DataService } from 'src/services/data/data.service';
  3. import { Observable } from 'rxjs';
  4. import { Attribute } from 'src/interfaces/attribute';
  5. import { DetailsService } from 'src/services/details/details.service';
  6. import { SaveThrowDetailsComponent } from '../save-throw-details/save-throw-details.component';
  7. import { TranslateService } from '@ngx-translate/core';
  8. @Component({
  9. selector: 'save-throw-field',
  10. templateUrl: './save-throw-field.component.html',
  11. styleUrls: ['./save-throw-field.component.scss'],
  12. })
  13. export class SaveThrowFieldComponent {
  14. @Input() attributeName: string = '';
  15. public attribute: Attribute = {
  16. name: '',
  17. value: 0,
  18. proficiency: false,
  19. advantage: 'none',
  20. };
  21. private proficiencyBonus: number = 0;
  22. private attributeModifier: number = 0;
  23. public saveModifier: string = '0';
  24. constructor(
  25. public dataAccessor: DataService,
  26. public detailsAccessor: DetailsService,
  27. public translate: TranslateService,
  28. ) {}
  29. public ngOnInit(): void {
  30. this.initAttributeSubscription();
  31. this.initProficiencySubscription();
  32. }
  33. private initAttributeSubscription(): void {
  34. const observable: Observable<Attribute> = eval(
  35. `this.dataAccessor.${this.attributeName}$`,
  36. );
  37. observable.subscribe((newValue: Attribute) => {
  38. this.attribute = newValue;
  39. // MIGRATION: Add default value for advantage
  40. if (this.attribute.advantage === undefined) {
  41. this.attribute.advantage = 'none';
  42. }
  43. this.attributeModifier = this.calculateAttributeModifier();
  44. this.saveModifier = this.calculateSaveModifier();
  45. });
  46. }
  47. private initProficiencySubscription(): void {
  48. this.dataAccessor.proficiency$.subscribe((newValue: any) => {
  49. this.proficiencyBonus = newValue;
  50. this.attributeModifier = this.calculateAttributeModifier();
  51. this.saveModifier = this.calculateSaveModifier();
  52. });
  53. }
  54. private calculateAttributeModifier(): number {
  55. return Math.floor((this.attribute.value - 10) / 2);
  56. }
  57. public calculateSaveModifier(): string {
  58. let mod = this.attributeModifier;
  59. if (this.attribute.proficiency) {
  60. mod += this.proficiencyBonus;
  61. }
  62. if (mod > 0) {
  63. return '+' + mod;
  64. } else {
  65. return mod.toString();
  66. }
  67. }
  68. public openDetails(): void {
  69. this.detailsAccessor.openPanel(SaveThrowDetailsComponent, {
  70. attributeName: this.attributeName,
  71. saveModifier: this.saveModifier,
  72. advantage: this.attribute.advantage,
  73. });
  74. const resultSubscription = this.detailsAccessor.result$.subscribe(
  75. (result) => {
  76. if (result.state === 'update') {
  77. this.attribute.advantage = result.data.advantage;
  78. this.updateAttribute();
  79. }
  80. resultSubscription.unsubscribe();
  81. },
  82. );
  83. }
  84. public updateAttribute(): void {
  85. setTimeout(() => {
  86. this.dataAccessor.updateAttribute(this.attribute);
  87. });
  88. }
  89. }