journal-inventory.component.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import { Component, inject } from '@angular/core';
  2. import {
  3. CdkDragDrop,
  4. CdkDropList,
  5. CdkDrag,
  6. moveItemInArray,
  7. } from '@angular/cdk/drag-drop';
  8. import { DetailsService } from 'src/services/details/details.service';
  9. import { DataService } from 'src/services/data/data.service';
  10. import { SimpleItem, Food } from 'src/interfaces/interfaces';
  11. import { SimpleItemDetailsComponent } from './simple-item-details/simple-item-details.component';
  12. import { ModalService } from 'src/services/modal/modal.service';
  13. import { SimpleItemModalComponent } from './simple-item-modal/simple-item-modal.component';
  14. @Component({
  15. selector: 'app-journal-inventory',
  16. templateUrl: './journal-inventory.component.html',
  17. styleUrls: ['./journal-inventory.component.scss'],
  18. })
  19. export class JournalInventoryComponent {
  20. public active: number = 1;
  21. public foodActive: number = 1;
  22. public weaponsAndArmor: SimpleItem[] = [];
  23. public consumables: SimpleItem[] = [];
  24. public miscellaneous: SimpleItem[] = [];
  25. public money: any = {};
  26. public food: Food[] = [];
  27. public totalWeight: number = 0;
  28. public maximumWeight: number = 0;
  29. private strength: number = 0;
  30. private weightModifier: number = 15;
  31. private speciesWeightModifierTable: any = {
  32. dwarf: 15,
  33. elf: 15,
  34. eladrin: 15,
  35. halfElf: 15,
  36. halfElfDetection: 15,
  37. halfOrc: 15,
  38. human: 15,
  39. Halfling: 7.5,
  40. gnome: 7.5,
  41. orc: 15,
  42. tiefling: 15,
  43. };
  44. private details = inject(DetailsService);
  45. private data = inject(DataService);
  46. private modal = inject(ModalService);
  47. public ngOnInit(): void {
  48. this.weaponsAndArmor = this.data.weaponsAndArmor;
  49. this.consumables = this.data.consumables;
  50. this.miscellaneous = this.data.miscellaneous;
  51. this.food = this.data.food;
  52. this.money = this.data.money;
  53. this.updateWeight();
  54. this.weightModifier =
  55. this.speciesWeightModifierTable[this.data.characterData.race];
  56. this.data.strength$.subscribe((strength) => {
  57. this.strength = strength.value;
  58. this.maximumWeight = this.strength * this.weightModifier;
  59. });
  60. }
  61. drop(event: CdkDragDrop<string[]>, list: any[], listName: string) {
  62. moveItemInArray(list, event.previousIndex, event.currentIndex);
  63. if (listName === 'weaponsAndArmor') {
  64. this.data.weaponsAndArmor = list;
  65. } else if (listName === 'food') {
  66. this.data.food = list;
  67. } else if (listName === 'consumables') {
  68. this.data.consumables = list;
  69. } else if (listName === 'miscellaneous') {
  70. this.data.miscellaneous = list;
  71. }
  72. }
  73. public openItemsDetails(list: any, index: number, listName: string): void {
  74. this.details.openPanel(SimpleItemDetailsComponent, {
  75. item: list[index],
  76. });
  77. const resultSubscription = this.details.result$.subscribe((result) => {
  78. if (result.state === 'delete') {
  79. list.splice(index, 1);
  80. this.updateDatabase(listName);
  81. } else if (result.state === 'update') {
  82. this.openItemModal(true, listName, list, index);
  83. } else if (result.state === 'cancel') {
  84. // Do nothing
  85. }
  86. resultSubscription.unsubscribe();
  87. });
  88. }
  89. public openFoodModal(isUpdate: boolean, index?: number): void {
  90. this.modal.openModal(SimpleItemModalComponent, {
  91. item:
  92. index !== undefined
  93. ? JSON.parse(JSON.stringify(this.food[index]))
  94. : undefined,
  95. isUpdate: isUpdate,
  96. isFood: true,
  97. });
  98. const resultSubscription = this.modal.result$.subscribe((result) => {
  99. if (result.state === 'update') {
  100. this.food[index!] = result.data;
  101. this.updateFood();
  102. } else if (result.state === 'add') {
  103. this.food.push(result.data);
  104. this.updateFood();
  105. } else if (result.state === 'cancel') {
  106. // Do nothing
  107. }
  108. resultSubscription.unsubscribe();
  109. });
  110. }
  111. public openItemModal(
  112. isUpdate: boolean,
  113. listname: string,
  114. list?: SimpleItem[],
  115. index?: number,
  116. ): void {
  117. this.modal.openModal(SimpleItemModalComponent, {
  118. item:
  119. list && index !== undefined
  120. ? JSON.parse(JSON.stringify(list[index]))
  121. : undefined,
  122. isUpdate: isUpdate,
  123. });
  124. const resultSubscription = this.modal.result$.subscribe((result) => {
  125. if (result.state === 'update') {
  126. list![index!] = result.data;
  127. this.updateDatabase(listname);
  128. } else if (result.state === 'add') {
  129. list!.push(result.data);
  130. this.updateDatabase(listname);
  131. } else if (result.state === 'cancel') {
  132. // Do nothing
  133. }
  134. resultSubscription.unsubscribe();
  135. });
  136. }
  137. public addItem(table: string): void {
  138. if (table === 'items') {
  139. if (this.active === 1) {
  140. this.openItemModal(false, 'weaponsAndArmor', this.weaponsAndArmor);
  141. } else {
  142. this.openItemModal(false, 'miscellaneous', this.miscellaneous);
  143. }
  144. } else if (table === 'consumables') {
  145. if (this.foodActive === 1) {
  146. this.openFoodModal(false);
  147. } else if (this.foodActive === 2) {
  148. this.openItemModal(false, 'consumables', this.consumables);
  149. }
  150. }
  151. }
  152. /**
  153. * Consumes one unit of food from the inventory.
  154. * It always uses the first entry in the list.
  155. * If the food item is empty, it will be removed from the list.
  156. */
  157. public consumeFood(): void {
  158. if (this.food.length > 0) {
  159. this.food[0].quantity--;
  160. if (this.food[0].quantity === 0) {
  161. this.food.splice(0, 1);
  162. }
  163. this.updateFood();
  164. }
  165. }
  166. // Updates
  167. public updateDatabase(listname: string): void {
  168. if (listname === 'weaponsAndArmor') {
  169. this.data.weaponsAndArmor = this.weaponsAndArmor;
  170. } else if (listname === 'consumables') {
  171. this.data.consumables = this.consumables;
  172. } else if (listname === 'miscellaneous') {
  173. this.data.miscellaneous = this.miscellaneous;
  174. } else if (listname === 'food') {
  175. this.data.food = this.food;
  176. }
  177. this.updateWeight();
  178. }
  179. public updateMoney(): void {
  180. this.data.money = this.money;
  181. this.updateWeight();
  182. }
  183. public updateFood(): void {
  184. this.data.food = this.food;
  185. this.updateWeight();
  186. }
  187. private updateWeight(): void {
  188. let totalWeight = 0;
  189. this.weaponsAndArmor.forEach((item) => {
  190. totalWeight += item.weight * item.quantity;
  191. });
  192. this.consumables.forEach((item) => {
  193. totalWeight += item.weight * item.quantity;
  194. });
  195. this.miscellaneous.forEach((item) => {
  196. totalWeight += item.weight * item.quantity;
  197. });
  198. this.food.forEach((item) => {
  199. totalWeight += item.weight * item.quantity;
  200. });
  201. const numberOfCoins =
  202. this.money.copper +
  203. this.money.silver +
  204. this.money.gold +
  205. this.money.platinum;
  206. totalWeight += Math.floor(numberOfCoins / 50);
  207. this.totalWeight = totalWeight;
  208. }
  209. }