123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- import { Component, inject } from '@angular/core';
- import {
- CdkDragDrop,
- CdkDropList,
- CdkDrag,
- moveItemInArray,
- } from '@angular/cdk/drag-drop';
- import { DetailsService } from 'src/services/details/details.service';
- import { DataService } from 'src/services/data/data.service';
- import { SimpleItem, Food } from 'src/interfaces/interfaces';
- import { SimpleItemDetailsComponent } from './simple-item-details/simple-item-details.component';
- import { ModalService } from 'src/services/modal/modal.service';
- import { SimpleItemModalComponent } from './simple-item-modal/simple-item-modal.component';
- @Component({
- selector: 'app-journal-inventory',
- templateUrl: './journal-inventory.component.html',
- styleUrls: ['./journal-inventory.component.scss'],
- })
- export class JournalInventoryComponent {
- public active: number = 1;
- public foodActive: number = 1;
- public weaponsAndArmor: SimpleItem[] = [];
- public consumables: SimpleItem[] = [];
- public miscellaneous: SimpleItem[] = [];
- public money: any = {};
- public food: Food[] = [];
- public totalWeight: number = 0;
- public maximumWeight: number = 0;
- private strength: number = 0;
- private weightModifier: number = 15;
- private speciesWeightModifierTable: any = {
- dwarf: 15,
- elf: 15,
- eladrin: 15,
- halfElf: 15,
- halfElfDetection: 15,
- halfOrc: 15,
- human: 15,
- Halfling: 7.5,
- gnome: 7.5,
- orc: 15,
- tiefling: 15,
- };
- private details = inject(DetailsService);
- private data = inject(DataService);
- private modal = inject(ModalService);
- public ngOnInit(): void {
- this.weaponsAndArmor = this.data.weaponsAndArmor;
- this.consumables = this.data.consumables;
- this.miscellaneous = this.data.miscellaneous;
- this.food = this.data.food;
- this.money = this.data.money;
- this.updateWeight();
- this.weightModifier =
- this.speciesWeightModifierTable[this.data.characterData.race];
- this.data.strength$.subscribe((strength) => {
- this.strength = strength.value;
- this.maximumWeight = this.strength * this.weightModifier;
- });
- }
- drop(event: CdkDragDrop<string[]>, list: any[], listName: string) {
- moveItemInArray(list, event.previousIndex, event.currentIndex);
- if (listName === 'weaponsAndArmor') {
- this.data.weaponsAndArmor = list;
- } else if (listName === 'food') {
- this.data.food = list;
- } else if (listName === 'consumables') {
- this.data.consumables = list;
- } else if (listName === 'miscellaneous') {
- this.data.miscellaneous = list;
- }
- }
- public openItemsDetails(list: any, index: number, listName: string): void {
- this.details.openPanel(SimpleItemDetailsComponent, {
- item: list[index],
- });
- const resultSubscription = this.details.result$.subscribe((result) => {
- if (result.state === 'delete') {
- list.splice(index, 1);
- this.updateDatabase(listName);
- } else if (result.state === 'update') {
- this.openItemModal(true, listName, list, index);
- } else if (result.state === 'cancel') {
- // Do nothing
- }
- resultSubscription.unsubscribe();
- });
- }
- public openFoodModal(isUpdate: boolean, index?: number): void {
- this.modal.openModal(SimpleItemModalComponent, {
- item:
- index !== undefined
- ? JSON.parse(JSON.stringify(this.food[index]))
- : undefined,
- isUpdate: isUpdate,
- isFood: true,
- });
- const resultSubscription = this.modal.result$.subscribe((result) => {
- if (result.state === 'update') {
- this.food[index!] = result.data;
- this.updateFood();
- } else if (result.state === 'add') {
- this.food.push(result.data);
- this.updateFood();
- } else if (result.state === 'cancel') {
- // Do nothing
- }
- resultSubscription.unsubscribe();
- });
- }
- public openItemModal(
- isUpdate: boolean,
- listname: string,
- list?: SimpleItem[],
- index?: number,
- ): void {
- this.modal.openModal(SimpleItemModalComponent, {
- item:
- list && index !== undefined
- ? JSON.parse(JSON.stringify(list[index]))
- : undefined,
- isUpdate: isUpdate,
- });
- const resultSubscription = this.modal.result$.subscribe((result) => {
- if (result.state === 'update') {
- list![index!] = result.data;
- this.updateDatabase(listname);
- } else if (result.state === 'add') {
- list!.push(result.data);
- this.updateDatabase(listname);
- } else if (result.state === 'cancel') {
- // Do nothing
- }
- resultSubscription.unsubscribe();
- });
- }
- public addItem(table: string): void {
- if (table === 'items') {
- if (this.active === 1) {
- this.openItemModal(false, 'weaponsAndArmor', this.weaponsAndArmor);
- } else {
- this.openItemModal(false, 'miscellaneous', this.miscellaneous);
- }
- } else if (table === 'consumables') {
- if (this.foodActive === 1) {
- this.openFoodModal(false);
- } else if (this.foodActive === 2) {
- this.openItemModal(false, 'consumables', this.consumables);
- }
- }
- }
- /**
- * Consumes one unit of food from the inventory.
- * It always uses the first entry in the list.
- * If the food item is empty, it will be removed from the list.
- */
- public consumeFood(): void {
- if (this.food.length > 0) {
- this.food[0].quantity--;
- if (this.food[0].quantity === 0) {
- this.food.splice(0, 1);
- }
- this.updateFood();
- }
- }
- // Updates
- public updateDatabase(listname: string): void {
- if (listname === 'weaponsAndArmor') {
- this.data.weaponsAndArmor = this.weaponsAndArmor;
- } else if (listname === 'consumables') {
- this.data.consumables = this.consumables;
- } else if (listname === 'miscellaneous') {
- this.data.miscellaneous = this.miscellaneous;
- } else if (listname === 'food') {
- this.data.food = this.food;
- }
- this.updateWeight();
- }
- public updateMoney(): void {
- this.data.money = this.money;
- this.updateWeight();
- }
- public updateFood(): void {
- this.data.food = this.food;
- this.updateWeight();
- }
- private updateWeight(): void {
- let totalWeight = 0;
- this.weaponsAndArmor.forEach((item) => {
- totalWeight += item.weight * item.quantity;
- });
- this.consumables.forEach((item) => {
- totalWeight += item.weight * item.quantity;
- });
- this.miscellaneous.forEach((item) => {
- totalWeight += item.weight * item.quantity;
- });
- this.food.forEach((item) => {
- totalWeight += item.weight * item.quantity;
- });
- const numberOfCoins =
- this.money.copper +
- this.money.silver +
- this.money.gold +
- this.money.platinum;
- totalWeight += Math.floor(numberOfCoins / 50);
- this.totalWeight = totalWeight;
- }
- }
|