123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- import { Component } from '@angular/core';
- import { DataService } from 'src/services/data/data.service';
- import { ModalService } from 'src/services/modal/modal.service';
- import { SpellslotsModalComponent } from './spellslots-modal/spellslots-modal.component';
- @Component({
- selector: 'spellslots',
- templateUrl: './spellslots.component.html',
- styleUrls: ['./spellslots.component.scss'],
- })
- export class SpellslotsComponent {
- public spellslots: any[] = [];
- public showSpellslots: boolean = false;
- public kiPoints: any;
- public spellcastingAttribute: string | undefined = undefined;
- public proficiencyBonus: number = 2;
- public attributeValue: number = 0;
- public spellAttackModifier: number = 0;
- public spellSaveDC: number = 0;
- public slotNumber: number = 1;
- public ngOnInit(): void {
- const spells = this.dataAccessor.spellslots;
- const kiPoints = this.dataAccessor.kiPoints;
- this.spellslots = spells.spellslots;
- this.showSpellslots = spells.showSpellslots;
- this.spellcastingAttribute = spells.spellcastingAttribute;
- this.kiPoints = kiPoints;
- // this.calculateModifiers();
- this.subscribeToProficiency();
- this.subscribeToAttribute();
- }
- public ngAfterViewInit(): void {
- setTimeout(() => {
- this.spellslots.forEach((_, levelIndex) => {
- this.correctSpellslotsView(levelIndex);
- });
- this.correctKiPointsView();
- }, 10);
- }
- public constructor(
- public dataAccessor: DataService,
- public modalAccessor: ModalService
- ) {}
- // FUNCTIONS
- public openModal(): void {
- this.modalAccessor.openModal(SpellslotsModalComponent, {
- kiPoints: JSON.parse(JSON.stringify(this.kiPoints)),
- spellslots: JSON.parse(JSON.stringify(this.spellslots)),
- showSpellslots: this.showSpellslots,
- });
- const resultSubscription = this.modalAccessor.result$.subscribe(
- (result) => {
- if (result.state === 'update') {
- this.updateSlotsAndPoints(result.data);
- } else {
- throw new Error('DND-ERROR: Invalid state');
- }
- resultSubscription.unsubscribe();
- }
- );
- }
- private calculateModifiers(): void {
- const spellcastingAttribute = this.spellcastingAttribute;
- console.log('calculateModifiers wurde aufgerufen');
- console.log('Proficiency: ', this.proficiencyBonus);
- console.log('Attribute: ', this.spellcastingAttribute);
- console.log('Value: ', this.attributeValue);
- if (spellcastingAttribute !== undefined) {
- const modifier = (this.attributeValue - 10) / 2;
- this.spellAttackModifier = modifier + this.proficiencyBonus;
- this.spellSaveDC = 8 + modifier + this.proficiencyBonus;
- console.log('Attack: ', this.spellAttackModifier);
- console.log('Save: ', this.spellSaveDC);
- }
- }
- private subscribeToAttribute(): void {
- if (this.spellcastingAttribute !== undefined) {
- const command =
- 'this.dataAccessor.' +
- this.spellcastingAttribute.toLowerCase() +
- '$.subscribe((attribute) => {this.attributeValue = attribute.value; this.calculateModifiers();});';
- console.log(command);
- eval(command);
- }
- }
- private subscribeToProficiency(): void {
- this.dataAccessor.proficiency$.subscribe((value) => {
- this.proficiencyBonus = value;
- this.calculateModifiers();
- });
- }
- // ki points
- public handleUsedKiPoints(pointIndex: number, eventTarget: any): void {
- if (eventTarget.checked) {
- this.kiPoints.usedPoints++;
- if (pointIndex + 1 !== this.kiPoints.usedPoints) {
- this.correctKiPointsView();
- }
- } else {
- this.kiPoints.usedPoints--;
- if (pointIndex !== this.kiPoints.usedPoints) {
- this.correctKiPointsView();
- }
- }
- this.updateKiPointsDatabase();
- }
- private correctKiPointsView(): void {
- const totalKiPoints = this.kiPoints.totalPoints;
- const usedKiPoints = this.kiPoints.usedPoints;
- for (let kiIndex = 0; kiIndex < usedKiPoints; kiIndex++) {
- setTimeout(() => {
- (
- document.getElementById('checkbox' + kiIndex) as HTMLInputElement
- ).checked = true;
- });
- }
- for (let kiIndex = usedKiPoints; kiIndex < totalKiPoints; kiIndex++) {
- setTimeout(() => {
- (
- document.getElementById('checkbox' + kiIndex) as HTMLInputElement
- ).checked = false;
- });
- }
- }
- // spellslots
- public handleUsedSlots(
- levelIndex: number,
- slotIndex: number,
- eventTarget: any
- ) {
- // if now checked, it means the slot was just used.
- if (eventTarget.checked) {
- this.spellslots[levelIndex].usedSlots++;
- if (slotIndex + 1 !== this.spellslots[levelIndex].usedSlots) {
- this.correctSpellslotsView(levelIndex);
- }
- } else {
- this.spellslots[levelIndex].usedSlots--;
- if (slotIndex !== this.spellslots[levelIndex].usedSlots) {
- this.correctSpellslotsView(levelIndex);
- }
- }
- this.updateSpellslotDatabase();
- }
- public correctSpellslotsView(levelIndex: number): void {
- const totalSlots = this.spellslots[levelIndex].totalSlots;
- const usedSlots = this.spellslots[levelIndex].usedSlots;
- for (let slotIndex = 0; slotIndex < usedSlots; slotIndex++) {
- setTimeout(() => {
- (
- document.getElementById(
- 'checkbox' + levelIndex + '-' + slotIndex
- ) as HTMLInputElement
- ).checked = true;
- });
- }
- for (let slotIndex = usedSlots; slotIndex < totalSlots; slotIndex++) {
- setTimeout(() => {
- (
- document.getElementById(
- 'checkbox' + levelIndex + '-' + slotIndex
- ) as HTMLInputElement
- ).checked = false;
- });
- }
- }
- // general
- public getArray(length: number): any[] {
- return Array.from({ length: length });
- }
- public isNotEmptyObject(obj: object): boolean {
- return Object.keys(obj).length !== 0;
- }
- public updateSpellslotDatabase(): void {
- this.dataAccessor.spellslots = {
- spellslots: this.spellslots,
- showSpellslots: this.showSpellslots,
- spellcastingAttribute: this.spellcastingAttribute,
- };
- }
- public updateKiPointsDatabase(): void {
- this.dataAccessor.kiPoints = this.kiPoints;
- }
- public updateSlotsAndPoints(data: any): void {
- this.spellslots = data.spellslots.spellslots;
- this.showSpellslots = data.spellslots.showSpellslots;
- this.kiPoints = data.kiPoints;
- setTimeout(() => {
- this.spellslots.forEach((_, levelIndex) => {
- this.correctSpellslotsView(levelIndex);
- });
- this.correctKiPointsView();
- }, 200);
- this.updateSpellslotDatabase();
- this.updateKiPointsDatabase();
- }
- }
|