skill-field.component.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { Component, Input } from '@angular/core';
  2. import { Skill } from 'src/interfaces/skill';
  3. import { DataService } from 'src/services/data/data.service';
  4. import { DetailsService } from 'src/services/details/details.service';
  5. import { Observable } from 'rxjs';
  6. import { Attribute } from 'src/interfaces/attribute';
  7. import { SkillDetailsComponent } from '../skill-details/skill-details.component';
  8. @Component({
  9. selector: 'app-skill-field',
  10. templateUrl: './skill-field.component.html',
  11. styleUrls: ['./skill-field.component.scss'],
  12. })
  13. export class SkillFieldComponent {
  14. @Input() skillName: string = '';
  15. public skill: Skill = { name: '', proficiency: false };
  16. private attribute: Attribute = { name: '', value: 0, proficiency: false };
  17. public skillModifier: string = '0';
  18. public skillProficiency: boolean = false;
  19. private proficiencyBonus: number = 0;
  20. public constructor(
  21. public dataAccessor: DataService,
  22. public detailsAccessor: DetailsService,
  23. ) {}
  24. public skillNames: any = {
  25. acrobatics: { skill: 'Akrobatik', attribute: 'GES', long: 'dexterity' },
  26. animalHandling: { skill: 'Tierkunde', attribute: 'WIS', long: 'wisdom' },
  27. arcana: { skill: 'Arkana', attribute: 'INT', long: 'intelligence' },
  28. athletics: { skill: 'Athletik', attribute: 'STR', long: 'strength' },
  29. deception: { skill: 'Täuschen', attribute: 'CHA', long: 'charisma' },
  30. history: { skill: 'Geschichte', attribute: 'INT', long: 'intelligence' },
  31. insight: { skill: 'Motiv erkennen', attribute: 'WIS', long: 'wisdom' },
  32. intimidation: {
  33. skill: 'Einschüchtern',
  34. attribute: 'CHA',
  35. long: 'charisma',
  36. },
  37. investigation: {
  38. skill: 'Nachforschung',
  39. attribute: 'INT',
  40. long: 'intelligence',
  41. },
  42. medicine: { skill: 'Heilkunde', attribute: 'WIS', long: 'wisdom' },
  43. nature: { skill: 'Naturkunde', attribute: 'INT', long: 'intelligence' },
  44. perception: { skill: 'Wahrnehmung', attribute: 'WIS', long: 'wisdom' },
  45. performance: { skill: 'Auftreten', attribute: 'CHA', long: 'charisma' },
  46. persuasion: { skill: 'Überzeugen', attribute: 'CHA', long: 'charisma' },
  47. religion: { skill: 'Religion', attribute: 'INT', long: 'intelligence' },
  48. sleightOfHand: {
  49. skill: 'Fingerfertigkeit',
  50. attribute: 'GES',
  51. long: 'dexterity',
  52. },
  53. stealth: { skill: 'Heimlichkeit', attribute: 'GES', long: 'dexterity' },
  54. survival: { skill: 'Überlebenskunst', attribute: 'WIS', long: 'wisdom' },
  55. };
  56. ngOnInit(): void {
  57. this.initProficiencySubscription();
  58. this.initAttributeSubscription();
  59. this.initSkillSubscription();
  60. }
  61. private initAttributeSubscription(): void {
  62. const observable: Observable<Attribute> = eval(
  63. `this.dataAccessor.${this.skillNames[this.skillName].long}$`,
  64. );
  65. observable.subscribe((newValue: Attribute) => {
  66. this.attribute = newValue;
  67. this.skillModifier = this.calculateModifier();
  68. });
  69. }
  70. private initSkillSubscription(): void {
  71. const observable: Observable<Skill> = eval(
  72. `this.dataAccessor.${this.skillName}$`,
  73. );
  74. observable.subscribe((newValue: Skill) => {
  75. this.skill = newValue;
  76. this.skillProficiency = this.skill.proficiency;
  77. this.skillModifier = this.calculateModifier();
  78. });
  79. }
  80. private initProficiencySubscription(): void {
  81. this.dataAccessor.proficiency$.subscribe((newValue: any) => {
  82. this.proficiencyBonus = newValue;
  83. this.skillModifier = this.calculateModifier();
  84. });
  85. }
  86. public updateValue(): void {
  87. this.dataAccessor.updateSkill({
  88. name: this.skillName,
  89. proficiency: this.skillProficiency,
  90. });
  91. }
  92. public calculateModifier(): string {
  93. let mod: number = Math.floor((this.attribute.value - 10) / 2);
  94. if (this.skillProficiency) {
  95. mod += this.proficiencyBonus;
  96. }
  97. if (mod > 0) {
  98. return '+' + mod;
  99. } else {
  100. return mod.toString();
  101. }
  102. }
  103. public openDetails(): void {
  104. this.detailsAccessor.openPanel(SkillDetailsComponent, {
  105. skillName: this.skillName,
  106. skillModifier: this.skillModifier,
  107. });
  108. }
  109. }