data.service.ts 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. import { Injectable } from '@angular/core';
  2. import Localbase from 'localbase';
  3. import { BehaviorSubject } from 'rxjs';
  4. import { Attribute } from 'src/interfaces/attribute';
  5. import { Skill } from 'src/interfaces/skill';
  6. import { Weapon } from 'src/interfaces/weapon';
  7. import { Spell } from 'src/interfaces/spell';
  8. import { Ability } from 'src/interfaces/ability';
  9. import { Trait } from 'src/interfaces/traits';
  10. import { SimpleItem } from 'src/interfaces/simple-item';
  11. import { Food } from 'src/interfaces/food';
  12. import { SpellsService } from '../spells/spells.service';
  13. @Injectable({
  14. providedIn: 'root',
  15. })
  16. export class DataService {
  17. private db: any;
  18. public dataLoaded = false;
  19. public characterName: string = '';
  20. constructor(private spellsService: SpellsService) {
  21. this.db = new Localbase('DNDTools');
  22. this.db.config.debug = false;
  23. }
  24. async loadData(): Promise<any> {
  25. this.characterName = sessionStorage.getItem('characterName')!;
  26. if (this.dataLoaded) {
  27. return Promise.resolve();
  28. }
  29. return this.getCollectionWithKeys(this.characterName).then((data: any) => {
  30. this.buildCurrentCharacter(data);
  31. this.dataLoaded = true;
  32. });
  33. }
  34. // #region character selection and construction
  35. private buildCurrentCharacter(currentCharacterData: any): void {
  36. const [
  37. abilitiesData,
  38. attributesData,
  39. characterData,
  40. combatStatsData,
  41. companionData,
  42. consumablesData,
  43. customSpellsData,
  44. favoriteSpellsData,
  45. favoriteWeaponsData,
  46. foodData,
  47. hitPointsData,
  48. imageData,
  49. kiPointsData,
  50. locationsData,
  51. mapsData,
  52. miscellaneousData,
  53. moneyData,
  54. notesData,
  55. npcsData,
  56. proficienciesData,
  57. questsData,
  58. resistancesData,
  59. skillsData,
  60. spellLevel0,
  61. spellLevel1,
  62. spellLevel2,
  63. spellLevel3,
  64. spellLevel4,
  65. spellLevel5,
  66. spellLevel6,
  67. spellLevel7,
  68. spellLevel8,
  69. spellLevel9,
  70. spellslotsData,
  71. traitsData,
  72. weaponsAndArmorData,
  73. ] = currentCharacterData.map((entry: any) => entry.data);
  74. // Character Data
  75. this.characterData = characterData;
  76. //Character Image
  77. this.image = imageData;
  78. // Attributes
  79. Object.keys(attributesData).forEach((key: string) => {
  80. this.updateAttribute(attributesData[key]);
  81. });
  82. // Skills
  83. Object.keys(skillsData).forEach((key: string) => {
  84. this.updateSkill(skillsData[key]);
  85. });
  86. // Hit Points
  87. this.hitPointsSubject.next(hitPointsData.hitPoints);
  88. this.hitDice = hitPointsData.hitDice;
  89. // Combat Stats
  90. this.armorClass = combatStatsData.armorClass;
  91. this.initiative = combatStatsData.initiative;
  92. this.movement = combatStatsData.movement;
  93. this.updateProficiencyBonus(combatStatsData.proficiencyBonus);
  94. this.deathSaves = combatStatsData.deathSaves;
  95. this.conditions = combatStatsData.conditions;
  96. this.exhaustion = combatStatsData.exhaustion;
  97. this.inspiration = combatStatsData.inspiration;
  98. // Spells
  99. this.customSpellId = customSpellsData.id;
  100. this.customSpells = customSpellsData.spells;
  101. this.spellslots = spellslotsData;
  102. this.favoriteSpells = favoriteSpellsData.spells;
  103. this.spellLevel0 = spellLevel0.spells;
  104. this.spellLevel1 = spellLevel1.spells;
  105. this.spellLevel2 = spellLevel2.spells;
  106. this.spellLevel3 = spellLevel3.spells;
  107. this.spellLevel4 = spellLevel4.spells;
  108. this.spellLevel5 = spellLevel5.spells;
  109. this.spellLevel6 = spellLevel6.spells;
  110. this.spellLevel7 = spellLevel7.spells;
  111. this.spellLevel8 = spellLevel8.spells;
  112. this.spellLevel9 = spellLevel9.spells;
  113. // Upon initialization of the data, the custom spells need to be added to the spellsService
  114. this.spellsService.customSpells = this.customSpells;
  115. // Items
  116. this.favoriteWeapons = favoriteWeaponsData.data;
  117. this.weaponsAndArmor = weaponsAndArmorData.data;
  118. this.consumables = consumablesData.data;
  119. this.miscellaneous = miscellaneousData.data;
  120. this.food = foodData.data;
  121. this.money = moneyData;
  122. // Abilities and stuff
  123. this.kiPoints = kiPointsData;
  124. this.traits = traitsData.data;
  125. this.abilities = abilitiesData.data;
  126. this.proficiencies = proficienciesData;
  127. }
  128. // #endregion
  129. //////////////////////
  130. // FUTURE DATA //////
  131. //////////////////////
  132. private _companions: any = {};
  133. private _notes: any = {};
  134. private _quests: any = {};
  135. private _npcs: any = {};
  136. private _locations: any = {};
  137. private _maps: any = {};
  138. private _resistances: any = {};
  139. public get resistances(): any {
  140. return this._resistances;
  141. }
  142. public set resistances(resistances: any) {
  143. this._resistances = resistances;
  144. this.setData('resistances', resistances);
  145. }
  146. // #region # Character and Image DATA
  147. private _image: any = {
  148. value: '',
  149. };
  150. public get image(): any {
  151. return this._image;
  152. }
  153. public set image(newValue: any) {
  154. this._image = newValue;
  155. this.setData('image', newValue);
  156. }
  157. private _characterData: any = {
  158. class: '',
  159. subclass: '',
  160. race: '',
  161. background: '',
  162. backgroundStory: '',
  163. level: 1,
  164. experience: 0,
  165. };
  166. public get characterData(): any {
  167. return this._characterData;
  168. }
  169. public set characterData(newValue: any) {
  170. this._characterData = newValue;
  171. this.setData('characterData', newValue);
  172. }
  173. // #endregion
  174. // #region # SPELLS
  175. private _favoriteSpells: Spell[] = [];
  176. public get favoriteSpells(): Spell[] {
  177. return this._favoriteSpells;
  178. }
  179. public set favoriteSpells(spells: Spell[]) {
  180. this._favoriteSpells = spells;
  181. this.setData('favoriteSpells', { spells: spells });
  182. }
  183. public addFavoriteSpell(spell: Spell): void {
  184. this._favoriteSpells.push(spell);
  185. this.setData('favoriteSpells', { spells: this._favoriteSpells });
  186. }
  187. /**
  188. * Checks if the given spell is in the favorite spells array.
  189. * If so, it is matched by the id and updated.
  190. * @param spell The spell to be updated.
  191. */
  192. public updateFavoriteSpell(spell: Spell): void {
  193. const index = this._favoriteSpells.findIndex(
  194. (favorite) => favorite.id === spell.id,
  195. );
  196. if (index > -1) {
  197. this._favoriteSpells[index] = spell;
  198. this.setData('favoriteSpells', { spells: this._favoriteSpells });
  199. }
  200. }
  201. public removeFavoriteSpell(spell: Spell): void {
  202. const index = this._favoriteSpells.findIndex(
  203. (favorite) => favorite.id === spell.id,
  204. );
  205. if (index > -1) {
  206. this._favoriteSpells.splice(index, 1);
  207. this.setData('favoriteSpells', { spells: this._favoriteSpells });
  208. }
  209. }
  210. private _customSpells: Spell[] = [];
  211. public get customSpells(): Spell[] {
  212. return this._customSpells;
  213. }
  214. public set customSpells(spells: Spell[]) {
  215. this._customSpells = spells;
  216. this.setData('customSpells', { spells: spells, id: this.customSpellId });
  217. }
  218. /**
  219. * Adds a custom spell to the data service and uploads the resulting array to the database.
  220. * Additionally, the custom spell ID is incremented by one.
  221. * The custom spell array in the spells service is also updated.
  222. * @param spell The spell to be added.
  223. */
  224. public addCustomSpell(spell: Spell): void {
  225. this._customSpells.push(spell);
  226. this._customSpellId++;
  227. this.spellsService.customSpells = this._customSpells;
  228. this.setData('customSpells', {
  229. spells: this._customSpells,
  230. id: this.customSpellId,
  231. });
  232. }
  233. /**
  234. * Updates a custom spell in the data service and uploads the resulting array to the database.
  235. * Moreover, the custom spell array in the spells service is also updated.
  236. * @param spell The spell to be updated.
  237. */
  238. public updateCustomSpell(spell: Spell): void {
  239. const index = this._customSpells.findIndex(
  240. (customSpell) => customSpell.id === spell.id,
  241. );
  242. this._customSpells[index] = spell;
  243. this.spellsService.customSpells = this._customSpells;
  244. this.setData('customSpells', {
  245. spells: this._customSpells,
  246. id: this.customSpellId,
  247. });
  248. }
  249. /**
  250. * Deletes a custom spell from the data service and uploads the resulting array to the database.
  251. * @param spell The spell to be deleted.
  252. */
  253. public deleteCustomSpell(spell: Spell): void {
  254. const index = this._customSpells.indexOf(spell);
  255. this._customSpells.splice(index, 1);
  256. this.setData('customSpells', {
  257. spells: this._customSpells,
  258. id: this.customSpellId,
  259. });
  260. }
  261. private _customSpellId: number = 10000;
  262. public get customSpellId(): number {
  263. return this._customSpellId;
  264. }
  265. private set customSpellId(id: number) {
  266. this._customSpellId = id;
  267. }
  268. // LEVEL 0
  269. private _spellLevel0: Spell[] = [];
  270. public get spellLevel0(): Spell[] {
  271. return this._spellLevel0;
  272. }
  273. public set spellLevel0(spells: Spell[]) {
  274. this._spellLevel0 = spells;
  275. this.setData('spellLevel0', { spells: spells });
  276. }
  277. public addSpellToLevel0(spell: Spell): void {
  278. this._spellLevel0.push(spell);
  279. this.setData('spellLevel0', { spells: this._spellLevel0 });
  280. }
  281. public removeSpellFromLevel0(spell: Spell): void {
  282. const index = this._spellLevel0.findIndex((obj) => obj.id === spell.id);
  283. this._spellLevel0.splice(index, 1);
  284. this.setData('spellLevel0', { spells: this._spellLevel0 });
  285. }
  286. // LEVEL 1
  287. private _spellLevel1: Spell[] = [];
  288. public get spellLevel1(): Spell[] {
  289. return this._spellLevel1;
  290. }
  291. public set spellLevel1(spells: Spell[]) {
  292. this._spellLevel1 = spells;
  293. this.setData('spellLevel1', { spells: spells });
  294. }
  295. public addSpellToLevel1(spell: Spell): void {
  296. this._spellLevel1.push(spell);
  297. this.setData('spellLevel1', { spells: this._spellLevel1 });
  298. }
  299. public removeSpellFromLevel1(spell: Spell): void {
  300. const index = this._spellLevel1.findIndex((obj) => obj.id === spell.id);
  301. this._spellLevel1.splice(index, 1);
  302. this.setData('spellLevel1', { spells: this._spellLevel1 });
  303. }
  304. // LEVEL 2
  305. private _spellLevel2: Spell[] = [];
  306. public get spellLevel2(): Spell[] {
  307. return this._spellLevel2;
  308. }
  309. public set spellLevel2(spells: Spell[]) {
  310. this._spellLevel2 = spells;
  311. this.setData('spellLevel2', { spells: spells });
  312. }
  313. public addSpellToLevel2(spell: Spell): void {
  314. this._spellLevel2.push(spell);
  315. this.setData('spellLevel2', { spells: this._spellLevel2 });
  316. }
  317. public removeSpellFromLevel2(spell: Spell): void {
  318. const index = this._spellLevel2.findIndex((obj) => obj.id === spell.id);
  319. this._spellLevel2.splice(index, 1);
  320. this.setData('spellLevel2', { spells: this._spellLevel2 });
  321. }
  322. // LEVEL 3
  323. private _spellLevel3: Spell[] = [];
  324. public get spellLevel3(): Spell[] {
  325. return this._spellLevel3;
  326. }
  327. public set spellLevel3(spells: Spell[]) {
  328. this._spellLevel3 = spells;
  329. this.setData('spellLevel3', { spells: spells });
  330. }
  331. public addSpellToLevel3(spell: Spell): void {
  332. this._spellLevel3.push(spell);
  333. this.setData('spellLevel3', { spells: this._spellLevel3 });
  334. }
  335. public removeSpellFromLevel3(spell: Spell): void {
  336. const index = this._spellLevel3.findIndex((obj) => obj.id === spell.id);
  337. this._spellLevel3.splice(index, 1);
  338. this.setData('spellLevel3', { spells: this._spellLevel3 });
  339. }
  340. // LEVEL 4
  341. private _spellLevel4: Spell[] = [];
  342. public get spellLevel4(): Spell[] {
  343. return this._spellLevel4;
  344. }
  345. public set spellLevel4(spells: Spell[]) {
  346. this._spellLevel4 = spells;
  347. this.setData('spellLevel4', { spells: spells });
  348. }
  349. public addSpellToLevel4(spell: Spell): void {
  350. this._spellLevel4.push(spell);
  351. this.setData('spellLevel4', { spells: this._spellLevel4 });
  352. }
  353. public removeSpellFromLevel4(spell: Spell): void {
  354. const index = this._spellLevel4.findIndex((obj) => obj.id === spell.id);
  355. this._spellLevel4.splice(index, 1);
  356. this.setData('spellLevel4', { spells: this._spellLevel4 });
  357. }
  358. // LEVEL 5
  359. private _spellLevel5: Spell[] = [];
  360. public get spellLevel5(): Spell[] {
  361. return this._spellLevel5;
  362. }
  363. public set spellLevel5(spells: Spell[]) {
  364. this._spellLevel5 = spells;
  365. this.setData('spellLevel5', { spells: spells });
  366. }
  367. public addSpellToLevel5(spell: Spell): void {
  368. this._spellLevel5.push(spell);
  369. this.setData('spellLevel5', { spells: this._spellLevel5 });
  370. }
  371. public removeSpellFromLevel5(spell: Spell): void {
  372. const index = this._spellLevel5.findIndex((obj) => obj.id === spell.id);
  373. this._spellLevel5.splice(index, 1);
  374. this.setData('spellLevel5', { spells: this._spellLevel5 });
  375. }
  376. // LEVEL 6
  377. private _spellLevel6: Spell[] = [];
  378. public get spellLevel6(): Spell[] {
  379. return this._spellLevel6;
  380. }
  381. public set spellLevel6(spells: Spell[]) {
  382. this._spellLevel6 = spells;
  383. this.setData('spellLevel6', { spells: spells });
  384. }
  385. public addSpellToLevel6(spell: Spell): void {
  386. this._spellLevel6.push(spell);
  387. this.setData('spellLevel6', { spells: this._spellLevel6 });
  388. }
  389. public removeSpellFromLevel6(spell: Spell): void {
  390. const index = this._spellLevel6.findIndex((obj) => obj.id === spell.id);
  391. this._spellLevel6.splice(index, 1);
  392. this.setData('spellLevel6', { spells: this._spellLevel6 });
  393. }
  394. // LEVEL 7
  395. private _spellLevel7: Spell[] = [];
  396. public get spellLevel7(): Spell[] {
  397. return this._spellLevel7;
  398. }
  399. public set spellLevel7(spells: Spell[]) {
  400. this._spellLevel7 = spells;
  401. this.setData('spellLevel7', { spells: spells });
  402. }
  403. public addSpellToLevel7(spell: Spell): void {
  404. this._spellLevel7.push(spell);
  405. this.setData('spellLevel7', { spells: this._spellLevel7 });
  406. }
  407. public removeSpellFromLevel7(spell: Spell): void {
  408. const index = this._spellLevel7.findIndex((obj) => obj.id === spell.id);
  409. this._spellLevel7.splice(index, 1);
  410. this.setData('spellLevel7', { spells: this._spellLevel7 });
  411. }
  412. // LEVEL 8
  413. private _spellLevel8: Spell[] = [];
  414. public get spellLevel8(): Spell[] {
  415. return this._spellLevel8;
  416. }
  417. public set spellLevel8(spells: Spell[]) {
  418. this._spellLevel8 = spells;
  419. this.setData('spellLevel8', { spells: spells });
  420. }
  421. public addSpellToLevel8(spell: Spell): void {
  422. this._spellLevel8.push(spell);
  423. this.setData('spellLevel8', { spells: this._spellLevel8 });
  424. }
  425. public removeSpellFromLevel8(spell: Spell): void {
  426. const index = this._spellLevel8.findIndex((obj) => obj.id === spell.id);
  427. this._spellLevel8.splice(index, 1);
  428. this.setData('spellLevel8', { spells: this._spellLevel8 });
  429. }
  430. // LEVEL 9
  431. private _spellLevel9: Spell[] = [];
  432. public get spellLevel9(): Spell[] {
  433. return this._spellLevel9;
  434. }
  435. public set spellLevel9(spells: Spell[]) {
  436. this._spellLevel9 = spells;
  437. this.setData('spellLevel9', { spells: spells });
  438. }
  439. public addSpellToLevel9(spell: Spell): void {
  440. this._spellLevel9.push(spell);
  441. this.setData('spellLevel9', { spells: this._spellLevel9 });
  442. }
  443. public removeSpellFromLevel9(spell: Spell): void {
  444. const index = this._spellLevel9.findIndex((obj) => obj.id === spell.id);
  445. this._spellLevel9.splice(index, 1);
  446. this.setData('spellLevel9', { spells: this._spellLevel9 });
  447. }
  448. public getAllPreparedSpells(): Spell[] {
  449. return [
  450. ...this._spellLevel0,
  451. ...this._spellLevel1,
  452. ...this._spellLevel2,
  453. ...this._spellLevel3,
  454. ...this._spellLevel4,
  455. ...this._spellLevel5,
  456. ...this._spellLevel6,
  457. ...this._spellLevel7,
  458. ...this._spellLevel8,
  459. ...this._spellLevel9,
  460. ];
  461. }
  462. // #endregion
  463. // #region # ABILITIES
  464. private _abilities: Ability[] = [];
  465. public get abilities(): Ability[] {
  466. return this._abilities;
  467. }
  468. public set abilities(abilities: Ability[]) {
  469. this._abilities = abilities;
  470. this.setData('abilities', { data: abilities });
  471. }
  472. private _traits: Trait[] = [];
  473. public get traits(): Trait[] {
  474. return this._traits;
  475. }
  476. public set traits(traits: Trait[]) {
  477. this._traits = traits;
  478. this.setData('traits', { data: traits });
  479. }
  480. private _spellslots: any = {
  481. spellslots: [],
  482. showSpellslots: true,
  483. spellcastingAttribute: undefined,
  484. };
  485. public get spellslots(): any {
  486. return this._spellslots;
  487. }
  488. public set spellslots(spellslots: any) {
  489. this._spellslots = spellslots;
  490. this.setData('spellslots', spellslots);
  491. }
  492. private _kiPoints: any = {
  493. totalPoints: 4,
  494. usedPoints: 2,
  495. showKiPoints: false,
  496. };
  497. public get kiPoints(): any {
  498. return this._kiPoints;
  499. }
  500. public set kiPoints(kiPoints: any) {
  501. this._kiPoints = kiPoints;
  502. this.setData('kiPoints', kiPoints);
  503. }
  504. private _proficiencies: any = {};
  505. public get proficiencies(): any {
  506. return this._proficiencies;
  507. }
  508. public set proficiencies(proficiencies: any) {
  509. this._proficiencies = proficiencies;
  510. this.setData('proficiencies', proficiencies);
  511. }
  512. // #endregion
  513. // #region # COMBAT STATS
  514. private proficiencySubject = new BehaviorSubject<number>(2);
  515. public proficiency$ = this.proficiencySubject.asObservable();
  516. public updateProficiencyBonus(newValue: number) {
  517. this.proficiencySubject.next(newValue);
  518. this.writeCombatStatsToDatabase();
  519. }
  520. private _armorClass: number = 10;
  521. public get armorClass(): number {
  522. return this._armorClass;
  523. }
  524. public set armorClass(newValue: number) {
  525. this._armorClass = newValue;
  526. this.writeCombatStatsToDatabase();
  527. }
  528. private _initiative: number = 0;
  529. public get initiative(): number {
  530. return this._initiative;
  531. }
  532. public set initiative(newValue: number) {
  533. this._initiative = newValue;
  534. this.writeCombatStatsToDatabase();
  535. }
  536. private _movement: number = 30;
  537. public get movement(): number {
  538. return this._movement;
  539. }
  540. public set movement(newValue: number) {
  541. this._movement = newValue;
  542. this.writeCombatStatsToDatabase();
  543. }
  544. private _deathSaves: number[] = [0, 0];
  545. public get deathSaves(): number[] {
  546. return this._deathSaves;
  547. }
  548. public set deathSaves(newValue: number[]) {
  549. this._deathSaves = newValue;
  550. this.writeCombatStatsToDatabase();
  551. }
  552. private _inspiration: boolean = false;
  553. public get inspiration(): boolean {
  554. return this._inspiration;
  555. }
  556. public set inspiration(newValue: boolean) {
  557. this._inspiration = newValue;
  558. this.writeCombatStatsToDatabase();
  559. }
  560. private _experience: number = 0;
  561. public get experience(): number {
  562. return this._experience;
  563. }
  564. public set experience(newValue: number) {
  565. this._experience = newValue;
  566. this.writeCombatStatsToDatabase();
  567. }
  568. private _exhaustion: number = 0;
  569. public get exhaustion(): number {
  570. return this._exhaustion;
  571. }
  572. public set exhaustion(newValue: number) {
  573. this._exhaustion = newValue;
  574. this.writeCombatStatsToDatabase();
  575. }
  576. private _conditions: string[] = [];
  577. public get conditions(): string[] {
  578. return this._conditions;
  579. }
  580. public set conditions(newValue: string[]) {
  581. this._conditions = newValue;
  582. this.writeCombatStatsToDatabase();
  583. }
  584. private writeCombatStatsToDatabase() {
  585. const combatStatsData = {
  586. armorClass: this._armorClass,
  587. initiative: this._initiative,
  588. movement: this._movement,
  589. deathSaves: this._deathSaves,
  590. proficiencyBonus: this.proficiencySubject.getValue(),
  591. inspiration: this._inspiration,
  592. experience: this._experience,
  593. exhaustion: this._exhaustion,
  594. conditions: this._conditions,
  595. };
  596. this.setData('combatStats', combatStatsData);
  597. }
  598. // #endregion
  599. // #region # HIT POINTS
  600. private hitPointsSubject = new BehaviorSubject<any>({
  601. maxHitPoints: 6,
  602. currentHitPoints: 6,
  603. temporaryHitPoints: 1,
  604. });
  605. public hitPoints$ = this.hitPointsSubject.asObservable();
  606. public updateHitPoints(newValue: any) {
  607. this.hitPointsSubject.next(newValue);
  608. this.updateLifeData();
  609. }
  610. private _hitDice = {
  611. diceNumber: 4,
  612. diceType: 10,
  613. diceUsed: 0,
  614. };
  615. public get hitDice(): any {
  616. return this._hitDice;
  617. }
  618. public set hitDice(newValue: any) {
  619. this._hitDice = newValue;
  620. this.updateLifeData();
  621. }
  622. private updateLifeData() {
  623. const hitPointsData = {
  624. hitPoints: this.hitPointsSubject.getValue(),
  625. hitDice: this._hitDice,
  626. };
  627. this.setData('hitPoints', hitPointsData);
  628. }
  629. // #endregion
  630. // #region # ATTRIBUTES
  631. updateAttribute(newValue: Attribute) {
  632. const functionCall: string = newValue.name + 'Subject.next(newValue)';
  633. eval(`this.${functionCall}`);
  634. const attributesData = {
  635. strength: this.strengthSubject.getValue(),
  636. dexterity: this.dexteritySubject.getValue(),
  637. constitution: this.constitutionSubject.getValue(),
  638. intelligence: this.intelligenceSubject.getValue(),
  639. wisdom: this.wisdomSubject.getValue(),
  640. charisma: this.charismaSubject.getValue(),
  641. };
  642. this.setData('attributes', attributesData);
  643. }
  644. private strengthSubject = new BehaviorSubject<Attribute>({
  645. name: 'strength',
  646. value: 10,
  647. proficiency: true,
  648. });
  649. public strength$ = this.strengthSubject.asObservable();
  650. private dexteritySubject = new BehaviorSubject<Attribute>({
  651. name: 'dexterity',
  652. value: 10,
  653. proficiency: false,
  654. });
  655. public dexterity$ = this.dexteritySubject.asObservable();
  656. private constitutionSubject = new BehaviorSubject<Attribute>({
  657. name: 'constitution',
  658. value: 10,
  659. proficiency: false,
  660. });
  661. public constitution$ = this.constitutionSubject.asObservable();
  662. private intelligenceSubject = new BehaviorSubject<Attribute>({
  663. name: 'intelligence',
  664. value: 10,
  665. proficiency: false,
  666. });
  667. public intelligence$ = this.intelligenceSubject.asObservable();
  668. private wisdomSubject = new BehaviorSubject<Attribute>({
  669. name: 'wisdom',
  670. value: 10,
  671. proficiency: true,
  672. });
  673. public wisdom$ = this.wisdomSubject.asObservable();
  674. private charismaSubject = new BehaviorSubject<Attribute>({
  675. name: 'charisma',
  676. value: 10,
  677. proficiency: false,
  678. });
  679. public charisma$ = this.charismaSubject.asObservable();
  680. // #endregion
  681. // #region # SKILLS
  682. updateSkill(newValue: Skill) {
  683. const functionCall: string = newValue.name + 'Subject.next(newValue)';
  684. eval(`this.${functionCall}`);
  685. const skillsData = {
  686. acrobatics: this.acrobaticsSubject.getValue(),
  687. animalHandling: this.animalHandlingSubject.getValue(),
  688. arcana: this.arcanaSubject.getValue(),
  689. athletics: this.athleticsSubject.getValue(),
  690. deception: this.deceptionSubject.getValue(),
  691. history: this.historySubject.getValue(),
  692. insight: this.insightSubject.getValue(),
  693. intimidation: this.intimidationSubject.getValue(),
  694. investigation: this.investigationSubject.getValue(),
  695. medicine: this.medicineSubject.getValue(),
  696. nature: this.natureSubject.getValue(),
  697. perception: this.perceptionSubject.getValue(),
  698. performance: this.performanceSubject.getValue(),
  699. persuasion: this.persuasionSubject.getValue(),
  700. religion: this.religionSubject.getValue(),
  701. sleightOfHand: this.sleightOfHandSubject.getValue(),
  702. stealth: this.stealthSubject.getValue(),
  703. survival: this.survivalSubject.getValue(),
  704. };
  705. this.setData('skills', skillsData);
  706. }
  707. private acrobaticsSubject = new BehaviorSubject<Skill>({
  708. name: 'acrobatics',
  709. proficiency: true,
  710. });
  711. public acrobatics$ = this.acrobaticsSubject.asObservable();
  712. private animalHandlingSubject = new BehaviorSubject<Skill>({
  713. name: 'animalHandling',
  714. proficiency: false,
  715. });
  716. public animalHandling$ = this.animalHandlingSubject.asObservable();
  717. private arcanaSubject = new BehaviorSubject<Skill>({
  718. name: 'arcana',
  719. proficiency: false,
  720. });
  721. public arcana$ = this.arcanaSubject.asObservable();
  722. private athleticsSubject = new BehaviorSubject<Skill>({
  723. name: 'athletics',
  724. proficiency: true,
  725. });
  726. public athletics$ = this.athleticsSubject.asObservable();
  727. private deceptionSubject = new BehaviorSubject<Skill>({
  728. name: 'deception',
  729. proficiency: false,
  730. });
  731. public deception$ = this.deceptionSubject.asObservable();
  732. private historySubject = new BehaviorSubject<Skill>({
  733. name: 'history',
  734. proficiency: false,
  735. });
  736. public history$ = this.historySubject.asObservable();
  737. private insightSubject = new BehaviorSubject<Skill>({
  738. name: 'insight',
  739. proficiency: false,
  740. });
  741. public insight$ = this.insightSubject.asObservable();
  742. private intimidationSubject = new BehaviorSubject<Skill>({
  743. name: 'intimidation',
  744. proficiency: false,
  745. });
  746. public intimidation$ = this.intimidationSubject.asObservable();
  747. private investigationSubject = new BehaviorSubject<Skill>({
  748. name: 'investigation',
  749. proficiency: false,
  750. });
  751. public investigation$ = this.investigationSubject.asObservable();
  752. private medicineSubject = new BehaviorSubject<Skill>({
  753. name: 'medicine',
  754. proficiency: false,
  755. });
  756. public medicine$ = this.medicineSubject.asObservable();
  757. private natureSubject = new BehaviorSubject<Skill>({
  758. name: 'nature',
  759. proficiency: false,
  760. });
  761. public nature$ = this.natureSubject.asObservable();
  762. private perceptionSubject = new BehaviorSubject<Skill>({
  763. name: 'perception',
  764. proficiency: false,
  765. });
  766. public perception$ = this.perceptionSubject.asObservable();
  767. private performanceSubject = new BehaviorSubject<Skill>({
  768. name: 'performance',
  769. proficiency: false,
  770. });
  771. public performance$ = this.performanceSubject.asObservable();
  772. private persuasionSubject = new BehaviorSubject<Skill>({
  773. name: 'persuasion',
  774. proficiency: false,
  775. });
  776. public persuasion$ = this.persuasionSubject.asObservable();
  777. private religionSubject = new BehaviorSubject<Skill>({
  778. name: 'religion',
  779. proficiency: false,
  780. });
  781. public religion$ = this.religionSubject.asObservable();
  782. private sleightOfHandSubject = new BehaviorSubject<Skill>({
  783. name: 'sleightOfHand',
  784. proficiency: false,
  785. });
  786. public sleightOfHand$ = this.sleightOfHandSubject.asObservable();
  787. private stealthSubject = new BehaviorSubject<Skill>({
  788. name: 'stealth',
  789. proficiency: false,
  790. });
  791. public stealth$ = this.stealthSubject.asObservable();
  792. private survivalSubject = new BehaviorSubject<Skill>({
  793. name: 'survival',
  794. proficiency: false,
  795. });
  796. public survival$ = this.survivalSubject.asObservable();
  797. // #endregion
  798. // #region # inventory
  799. private _favoriteWeapons: Weapon[] = [];
  800. public get favoriteWeapons(): Weapon[] {
  801. return this._favoriteWeapons;
  802. }
  803. public set favoriteWeapons(weapons: Weapon[]) {
  804. this._favoriteWeapons = weapons;
  805. this.setData('favoriteWeapons', { data: this._favoriteWeapons });
  806. }
  807. private _weaponsAndArmor: SimpleItem[] = [];
  808. public get weaponsAndArmor(): SimpleItem[] {
  809. return this._weaponsAndArmor;
  810. }
  811. public set weaponsAndArmor(newValue: SimpleItem[]) {
  812. this._weaponsAndArmor = newValue;
  813. this.setData('weaponsAndArmor', { data: this._weaponsAndArmor });
  814. }
  815. private _food: Food[] = [];
  816. public get food(): Food[] {
  817. return this._food;
  818. }
  819. public set food(newValue: Food[]) {
  820. this._food = newValue;
  821. this.setData('food', { data: this._food });
  822. }
  823. private _money: any = {
  824. copper: 100,
  825. silver: 100,
  826. electrum: 100,
  827. gold: 100,
  828. platinum: 100,
  829. };
  830. public get money(): any {
  831. return this._money;
  832. }
  833. public set money(newValue: any) {
  834. this._money = newValue;
  835. this.setData('money', this._money);
  836. }
  837. private _consumbales: SimpleItem[] = [];
  838. public get consumables(): SimpleItem[] {
  839. return this._consumbales;
  840. }
  841. public set consumables(newValue: SimpleItem[]) {
  842. this._consumbales = newValue;
  843. this.setData('consumables', { data: this._consumbales });
  844. }
  845. private _miscellaneous: SimpleItem[] = [];
  846. public get miscellaneous(): SimpleItem[] {
  847. return this._miscellaneous;
  848. }
  849. public set miscellaneous(newValue: SimpleItem[]) {
  850. this._miscellaneous = newValue;
  851. this.setData('miscellaneous', { data: this._miscellaneous });
  852. }
  853. // #endregion
  854. // #region database calls
  855. public async addData(
  856. collection: string,
  857. data: any,
  858. key?: string,
  859. ): Promise<void> {
  860. if (key) {
  861. return this.db
  862. .collection(collection)
  863. .add(data, key)
  864. .then(() => {});
  865. } else {
  866. return this.db
  867. .collection(collection)
  868. .add(data)
  869. .then(() => {});
  870. }
  871. }
  872. public getData(collection: string, key: string) {
  873. return this.db.collection(collection).doc(key).get();
  874. }
  875. public getCollection(collection: string) {
  876. return this.db.collection(collection).get();
  877. }
  878. public getCollectionWithKeys(collection: string) {
  879. return this.db.collection(collection).get({ keys: true });
  880. }
  881. public setCollection(collection: string, data: any) {
  882. return this.db.collection(collection).set(data);
  883. }
  884. public setData(key: string, data: any) {
  885. return this.db
  886. .collection(this.characterName)
  887. .doc(key)
  888. .set(data)
  889. .then(() => {});
  890. }
  891. public deleteCollection(collection: string) {
  892. console.log('delete collection', collection);
  893. this.db.collection(collection).delete();
  894. }
  895. // #endregion
  896. }