Skip to content

Commit

Permalink
fix(api): rework lots of internal getters to guarantee existence rath…
Browse files Browse the repository at this point in the history
…er than check in every subfunction
  • Loading branch information
seiyria committed Sep 20, 2023
1 parent 82e6a17 commit f468476
Show file tree
Hide file tree
Showing 21 changed files with 158 additions and 273 deletions.
11 changes: 10 additions & 1 deletion server/src/modules/achievements/achievements.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ export class AchievementsService {
private readonly achievements: EntityRepository<Achievements>,
) {}

async getAchievementsForUser(
async getAchievementsForUser(userId: string): Promise<Achievements> {
const achievements = await this.getOrCreateAchievementsForUser(userId);
if (!achievements) {
throw new BadRequestException(`achievements id ${userId} not found.`);
}

return achievements;
}

private async getOrCreateAchievementsForUser(
userId: string,
): Promise<Achievements | undefined> {
const dbAchievements = await this.achievements.findOne({ userId });
Expand Down
2 changes: 0 additions & 2 deletions server/src/modules/aggregator/aggregator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ export class AggregatorService {
}): Promise<void> {
const { userId, amount } = event;
const player = await this.playerService.getPlayerForUser(userId);
if (!player) return;

const playerPatches = await getPatchesAfterPropChanges(
player,
Expand All @@ -140,7 +139,6 @@ export class AggregatorService {
const { userId, amount } = event;

const player = await this.playerService.getPlayerForUser(userId);
if (!player) return;

const playerPatches = await getPatchesAfterPropChanges(
player,
Expand Down
97 changes: 67 additions & 30 deletions server/src/modules/content/content.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Injectable, NotFoundException } from '@nestjs/common';

import {
ICollectible,
Expand Down Expand Up @@ -112,78 +112,115 @@ export class ContentService {
}

public allLocations(): ILocation[] {
return Object.values(this.locations);
return Object.values(this.locations ?? {});
}

public getLocation(location: string): ILocation | undefined {
return this.locations[location];
public getLocation(location: string): ILocation {
const locationRef = this.locations[location];
if (!location)
throw new NotFoundException(`Location ${location} not found!`);

return locationRef;
}

public allJobs(): IJob[] {
return Object.values(this.jobs);
return Object.values(this.jobs ?? {});
}

public getJob(job: string): IJob | undefined {
return this.jobs[job];
public getJob(job: string): IJob {
const jobRef = this.jobs[job];
if (!jobRef) throw new NotFoundException(`Job ${job} not found!`);

return jobRef;
}

public allCollectibles(): ICollectible[] {
return Object.values(this.collectibles);
return Object.values(this.collectibles ?? {});
}

public allResources(): IResource[] {
return Object.values(this.resources);
return Object.values(this.resources ?? {});
}

public getResource(resourceId: string): IResource | undefined {
return this.resources[resourceId];
public getResource(resourceId: string): IResource {
const resourceRef = this.resources[resourceId];
if (!resourceRef)
throw new NotFoundException(`Resource ${resourceId} not found!`);

return resourceRef;
}

public getCollectible(collectibleId: string): ICollectible | undefined {
return this.collectibles[collectibleId];
public getCollectible(collectibleId: string): ICollectible {
const collectibleRef = this.collectibles[collectibleId];
if (!collectibleRef)
throw new NotFoundException(`Collectible ${collectibleId} not found!`);

return collectibleRef;
}

public allEquipment(): IEquipment[] {
return Object.values(this.equipment);
return Object.values(this.equipment ?? {});
}

public getEquipment(equipmentId: string): IEquipment | undefined {
return this.equipment[equipmentId];
public getEquipment(equipmentId: string): IEquipment {
const equipmentRef = this.equipment[equipmentId];
if (!equipmentRef)
throw new NotFoundException(`Equipment ${equipmentId} not found!`);

return equipmentRef;
}

public getItem(item: string): IItem | undefined {
public getItem(item: string): IItem {
return this.equipment[item] || this.collectibles[item];
}

public getRecipe(item: string): IRecipe | undefined {
return this.recipes[item];
public getRecipe(item: string): IRecipe {
const recipeRef = this.recipes[item];
if (!recipeRef) throw new NotFoundException(`Recipe ${item} not found!`);

return recipeRef;
}

public allFormations(): IMonsterFormation[] {
return Object.values(this.formations);
return Object.values(this.formations ?? {});
}

public getFormation(formation: string): IMonsterFormation | undefined {
return this.formations[formation];
public getFormation(formation: string): IMonsterFormation {
const formationRef = this.formations[formation];
if (!formationRef)
throw new NotFoundException(`Formation ${formation} not found!`);

return formationRef;
}

public getMonster(monster: string): IMonster | undefined {
return this.monsters[monster];
public getMonster(monster: string): IMonster {
const monsterRef = this.monsters[monster];
if (!monsterRef)
throw new NotFoundException(`Monster ${monster} not found!`);

return monsterRef;
}

public allAbilities(): ICombatAbility[] {
return Object.values(this.abilities);
return Object.values(this.abilities ?? {});
}

public getAbility(ability: string): ICombatAbility | undefined {
return this.abilities[ability];
public getAbility(ability: string): ICombatAbility {
const abilityRef = this.abilities[ability];
if (!abilityRef)
throw new NotFoundException(`Ability ${ability} not found!`);

return abilityRef;
}

public allNPCs(): ILocationNPC[] {
return Object.values(this.npcs);
return Object.values(this.npcs ?? {});
}

public getNPC(npc: string): ILocationNPC | undefined {
return this.npcs[npc];
public getNPC(npc: string): ILocationNPC {
const npcRef = this.npcs[npc];
if (!npcRef) throw new NotFoundException(`NPC ${npc} not found!`);

return npcRef;
}
}
13 changes: 12 additions & 1 deletion server/src/modules/crafting/crafting.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,18 @@ export class CraftingService {
private readonly crafting: EntityRepository<Crafting>,
) {}

async getCraftingForUser(userId: string): Promise<Crafting | undefined> {
async getCraftingForUser(userId: string): Promise<Crafting> {
const crafting = await this.getOrCreateCraftingForUser(userId);
if (!crafting) {
throw new BadRequestException(`crafting id ${userId} not found.`);
}

return crafting;
}

private async getOrCreateCraftingForUser(
userId: string,
): Promise<Crafting | undefined> {
const dbCrafting = await this.crafting.findOne({ userId });
if (!dbCrafting) {
return await this.createCraftingForUser(userId);
Expand Down
11 changes: 1 addition & 10 deletions server/src/modules/discoveries/discoveries.controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { UserResponse } from '@interfaces';
import { DiscoveriesService } from '@modules/discoveries/discoveries.service';
import {
Body,
Controller,
Get,
NotFoundException,
Post,
UseGuards,
} from '@nestjs/common';
import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
import { User } from '../../utils/user.decorator';
import { JwtAuthGuard } from '../auth/jwt.guard';
Expand All @@ -24,8 +17,6 @@ export class DiscoveriesController {
const discoveries = await this.discoveriesService.getDiscoveriesForUser(
user.userId,
);
if (!discoveries)
throw new NotFoundException(`User ${user.userId} not found`);

return { discoveries };
}
Expand Down
41 changes: 13 additions & 28 deletions server/src/modules/discoveries/discoveries.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ export class DiscoveriesService {
private readonly events: EventEmitter2,
) {}

async getDiscoveriesForUser(
async getDiscoveriesForUser(userId: string): Promise<Discoveries> {
const discoveries = await this.getOrCreateDiscoveriesForUser(userId);
if (!discoveries) {
throw new BadRequestException(`discoveries id ${userId} not found.`);
}

return discoveries;
}

private async getOrCreateDiscoveriesForUser(
userId: string,
): Promise<Discoveries | undefined> {
const dbDiscoveries = await this.discoveries.findOne({ userId });
Expand Down Expand Up @@ -117,20 +126,13 @@ export class DiscoveriesService {
instanceId: string,
): Promise<UserResponse> {
const discoveries = await this.getDiscoveriesForUser(userId);
if (!discoveries) throw new NotFoundException(`User ${userId} not found`);

const item = await this.inventoryService.getInventoryItemForUser(
userId,
instanceId,
);
if (!item)
throw new NotFoundException(`Collectible item ${instanceId} not found`);

const itemDefinition = await this.contentService.getCollectible(
item.itemId,
);
if (!itemDefinition)
throw new NotFoundException(`Item definition ${item.itemId} not found`);
const itemDefinition = this.contentService.getCollectible(item.itemId);

await this.inventoryService.removeInventoryItemForUser(userId, instanceId);

Expand Down Expand Up @@ -168,16 +170,13 @@ export class DiscoveriesService {
instanceId: string,
): Promise<UserResponse> {
const discoveries = await this.getDiscoveriesForUser(userId);
if (!discoveries) throw new NotFoundException(`User ${userId} not found`);

const item = await this.inventoryService.getInventoryItemForUser(
userId,
instanceId,
);
if (!item)
throw new NotFoundException(`Equipment item ${instanceId} not found`);

const itemDefinition = await this.contentService.getEquipment(item.itemId);
const itemDefinition = this.contentService.getEquipment(item.itemId);
if (!itemDefinition) {
await this.inventoryService.removeInventoryItemForUser(
userId,
Expand Down Expand Up @@ -221,10 +220,8 @@ export class DiscoveriesService {

async discoverMonster(userId: string, monsterId: string): Promise<void> {
const discoveries = await this.getDiscoveriesForUser(userId);
if (!discoveries) throw new NotFoundException(`User ${userId} not found`);

const monster = await this.contentService.getMonster(monsterId);
if (!monster) throw new NotFoundException(`Monster ${monsterId} not found`);
const monster = this.contentService.getMonster(monsterId);

this.logger.verbose(`Discovered monster ${monster.name} for ${userId}.`);

Expand All @@ -238,8 +235,6 @@ export class DiscoveriesService {

async claimUniqueCollectibleReward(userId: string): Promise<UserResponse> {
const discoveries = await this.getDiscoveriesForUser(userId);
if (!discoveries)
throw new NotFoundException(`Discoveries ${userId} not found`);

const totalTimesClaimed = discoveries.uniqueCollectibleClaims ?? 0;
const totalCollectiblesFound = sum(Object.keys(discoveries.collectibles));
Expand Down Expand Up @@ -279,8 +274,6 @@ export class DiscoveriesService {

async claimTotalCollectibleReward(userId: string): Promise<UserResponse> {
const discoveries = await this.getDiscoveriesForUser(userId);
if (!discoveries)
throw new NotFoundException(`Discoveries ${userId} not found`);

const totalTimesClaimed = discoveries.totalCollectibleClaims ?? 0;
const totalCollectiblesFound = sum(Object.values(discoveries.collectibles));
Expand Down Expand Up @@ -320,8 +313,6 @@ export class DiscoveriesService {

async claimUniqueEquipmentReward(userId: string): Promise<UserResponse> {
const discoveries = await this.getDiscoveriesForUser(userId);
if (!discoveries)
throw new NotFoundException(`Discoveries ${userId} not found`);

const totalTimesClaimed = discoveries.uniqueEquipmentClaims ?? 0;
const totalItemsFound = sum(Object.keys(discoveries.items));
Expand Down Expand Up @@ -361,8 +352,6 @@ export class DiscoveriesService {

async claimTotalEquipmentReward(userId: string): Promise<UserResponse> {
const discoveries = await this.getDiscoveriesForUser(userId);
if (!discoveries)
throw new NotFoundException(`Discoveries ${userId} not found`);

const totalTimesClaimed = discoveries.totalEquipmentClaims ?? 0;
const totalItemsFound = sum(Object.values(discoveries.items));
Expand Down Expand Up @@ -402,8 +391,6 @@ export class DiscoveriesService {

async claimUniqueMonsterReward(userId: string): Promise<UserResponse> {
const discoveries = await this.getDiscoveriesForUser(userId);
if (!discoveries)
throw new NotFoundException(`Discoveries ${userId} not found`);

const totalTimesClaimed = discoveries.uniqueMonsterClaims ?? 0;
const totalItemsFound = sum(Object.keys(discoveries.monsters));
Expand Down Expand Up @@ -443,8 +430,6 @@ export class DiscoveriesService {

async claimTotalMonsterReward(userId: string): Promise<UserResponse> {
const discoveries = await this.getDiscoveriesForUser(userId);
if (!discoveries)
throw new NotFoundException(`Discoveries ${userId} not found`);

const totalTimesClaimed = discoveries.totalMonsterClaims ?? 0;
const totalItemsFound = sum(Object.values(discoveries.monsters));
Expand Down
Loading

0 comments on commit f468476

Please sign in to comment.