From aeadbda27264eee5b8d7146b91b58c961067c33b Mon Sep 17 00:00:00 2001 From: Kyle Kemp Date: Fri, 23 Jun 2023 19:43:27 -0500 Subject: [PATCH] fix(api): unite response values of services/controllers to all be the same interface --- server/src/interfaces/api.ts | 14 +++++ .../modules/inventory/inventory.controller.ts | 7 ++- .../modules/inventory/inventory.service.ts | 7 ++- .../src/modules/player/gameplay.controller.ts | 51 +++++++++---------- server/src/modules/player/gameplay.service.ts | 38 ++++++++------ .../src/modules/player/player.controller.ts | 13 +++-- server/src/modules/player/player.service.ts | 24 ++++++--- 7 files changed, 94 insertions(+), 60 deletions(-) diff --git a/server/src/interfaces/api.ts b/server/src/interfaces/api.ts index 6a6fe7d..61264c8 100644 --- a/server/src/interfaces/api.ts +++ b/server/src/interfaces/api.ts @@ -1,6 +1,9 @@ +import * as jsonpatch from 'fast-json-patch'; + import { Achievements } from '@modules/achievements/achievements.schema'; import { Discoveries } from '@modules/discoveries/discoveries.schema'; import { Inventory } from '@modules/inventory/inventory.schema'; +import { InventoryItem } from '@modules/inventory/inventoryitem.schema'; import { Player } from '@modules/player/player.schema'; import { Stats } from '@modules/stats/stats.schema'; import { User } from '@modules/user/user.schema'; @@ -16,4 +19,15 @@ export interface IFullUser { discoveries: Discoveries; achievements: Achievements; inventory: Inventory; + items: InventoryItem[]; +} + +export interface IPatchUser { + user: jsonpatch.Operation[]; + player: jsonpatch.Operation[]; + stats: jsonpatch.Operation[]; + discoveries: jsonpatch.Operation[]; + achievements: jsonpatch.Operation[]; + inventory: jsonpatch.Operation[]; + items: jsonpatch.Operation[]; } diff --git a/server/src/modules/inventory/inventory.controller.ts b/server/src/modules/inventory/inventory.controller.ts index 9ec1064..f0adeaa 100644 --- a/server/src/modules/inventory/inventory.controller.ts +++ b/server/src/modules/inventory/inventory.controller.ts @@ -1,3 +1,4 @@ +import { IFullUser, IPatchUser } from '@interfaces'; import { JwtAuthGuard } from '@modules/auth/jwt.guard'; import { InventoryService } from '@modules/inventory/inventory.service'; import { Controller, Get, UseGuards } from '@nestjs/common'; @@ -12,9 +13,7 @@ export class InventoryController { @UseGuards(JwtAuthGuard) @ApiOperation({ summary: 'Get my current item list' }) @Get('items') - async getItems(@User() user) { - return { - items: await this.inventoryService.getInventoryItemsForUser(user.userId), - }; + async getItems(@User() user): Promise> { + return this.inventoryService.getInventoryItemsForUser(user.userId); } } diff --git a/server/src/modules/inventory/inventory.service.ts b/server/src/modules/inventory/inventory.service.ts index 433018f..55fe9d3 100644 --- a/server/src/modules/inventory/inventory.service.ts +++ b/server/src/modules/inventory/inventory.service.ts @@ -1,3 +1,4 @@ +import { IFullUser, IPatchUser } from '@interfaces'; import { EntityManager, EntityRepository } from '@mikro-orm/mongodb'; import { InjectRepository } from '@mikro-orm/nestjs'; import { ConstantsService } from '@modules/content/constants.service'; @@ -43,8 +44,10 @@ export class InventoryService { return inventory; } - async getInventoryItemsForUser(userId: string): Promise { - return this.inventoryItems.find({ userId }); + async getInventoryItemsForUser( + userId: string, + ): Promise> { + return { items: await this.inventoryItems.find({ userId }) }; } async getInventoryItemForUser( diff --git a/server/src/modules/player/gameplay.controller.ts b/server/src/modules/player/gameplay.controller.ts index a79891b..2ff58f4 100644 --- a/server/src/modules/player/gameplay.controller.ts +++ b/server/src/modules/player/gameplay.controller.ts @@ -1,3 +1,4 @@ +import { IFullUser, IPatchUser } from '@interfaces'; import { JwtAuthGuard } from '@modules/auth/jwt.guard'; import { InventoryService } from '@modules/inventory/inventory.service'; import { GameplayService } from '@modules/player/gameplay.service'; @@ -22,29 +23,28 @@ export class GameplayController { @UseGuards(JwtAuthGuard) @ApiOperation({ summary: 'Explore the current location' }) @Post('explore') - async explore(@User() user) { + async explore(@User() user): Promise> { return this.gameplayService.explore(user.userId); } @UseGuards(JwtAuthGuard) @ApiOperation({ summary: 'Set a walking path to a new location' }) @Post('walk') - async walk(@User() user, @Body('location') location: string) { - return { - player: await this.gameplayService.walkToLocation(user.userId, location), - }; + async walk( + @User() user, + @Body('location') location: string, + ): Promise> { + return this.gameplayService.walkToLocation(user.userId, location); } @UseGuards(JwtAuthGuard) @ApiOperation({ summary: 'Travel immediately to a new location' }) @Post('travel') - async travel(@User() user, @Body('location') location: string) { - return { - player: await this.gameplayService.travelToLocation( - user.userId, - location, - ), - }; + async travel( + @User() user, + @Body('location') location: string, + ): Promise> { + return this.gameplayService.travelToLocation(user.userId, location); } @UseGuards(JwtAuthGuard) @@ -54,33 +54,32 @@ export class GameplayController { @User() user, @Body('targetUserId') targetUserId: string, @Body('isWaveBack') isWaveBack: boolean, - ) { - return { - player: await this.gameplayService.waveToPlayer( - user.userId, - targetUserId, - isWaveBack, - ), - }; + ): Promise> { + return this.gameplayService.waveToPlayer( + user.userId, + targetUserId, + isWaveBack, + ); } @UseGuards(JwtAuthGuard) @ApiOperation({ summary: 'Explore Event: Take an item' }) @Post('takeitem') - async takeItem(@User() user) { + async takeItem(@User() user): Promise> { if (await this.inventoryService.isInventoryFull(user.userId)) { throw new BadRequestException('Inventory is full.'); } - return { - player: await this.gameplayService.takeItem(user.userId), - }; + return this.gameplayService.takeItem(user.userId); } @UseGuards(JwtAuthGuard) @ApiOperation({ summary: 'Sell an item' }) @Post('sellitem') - async sellItem(@User() user, @Body('instanceId') instanceId: string) { - return await this.gameplayService.sellItem(user.userId, instanceId); + async sellItem( + @User() user, + @Body('instanceId') instanceId: string, + ): Promise> { + return this.gameplayService.sellItem(user.userId, instanceId); } } diff --git a/server/src/modules/player/gameplay.service.ts b/server/src/modules/player/gameplay.service.ts index 9e50cc1..d2884ff 100644 --- a/server/src/modules/player/gameplay.service.ts +++ b/server/src/modules/player/gameplay.service.ts @@ -1,5 +1,11 @@ import { itemValue } from '@helpers/item'; -import { IItem, ILocation, TrackedStat } from '@interfaces'; +import { + IFullUser, + IItem, + ILocation, + IPatchUser, + TrackedStat, +} from '@interfaces'; import { ConstantsService } from '@modules/content/constants.service'; import { ContentService } from '@modules/content/content.service'; import { Discoveries } from '@modules/discoveries/discoveries.schema'; @@ -11,7 +17,6 @@ import { PlayerService } from '@modules/player/player.service'; import { StatsService } from '@modules/stats/stats.service'; import { ForbiddenException, Injectable } from '@nestjs/common'; import { getPatchesAfterPropChanges } from '@utils/patches'; -import * as jsonpatch from 'fast-json-patch'; import { sample } from 'lodash'; type ExploreResult = 'Nothing' | 'Wave' | 'Item' | 'Discovery' | 'Collectible'; @@ -31,10 +36,7 @@ export class GameplayService { private readonly contentService: ContentService, ) {} - async explore(userId: string): Promise<{ - player: jsonpatch.Operation[]; - discoveries: jsonpatch.Operation[]; - }> { + async explore(userId: string): Promise> { const player = await this.playerService.getPlayerForUser(userId); if (!player) throw new ForbiddenException('Player not found'); @@ -191,7 +193,7 @@ export class GameplayService { async walkToLocation( userId: string, locationName: string, - ): Promise { + ): Promise> { const player = await this.playerService.getPlayerForUser(userId); if (!player) throw new ForbiddenException('Player not found'); @@ -231,10 +233,13 @@ export class GameplayService { }, ); - return playerPatches; + return { player: playerPatches }; } - async travelToLocation(userId: string, locationName: string): Promise { + async travelToLocation( + userId: string, + locationName: string, + ): Promise> { const player = await this.playerService.getPlayerForUser(userId); if (!player) throw new ForbiddenException('Player not found'); @@ -279,14 +284,14 @@ export class GameplayService { }, ); - return playerPatches; + return { player: playerPatches }; } async waveToPlayer( userId: string, targetUserId: string, isWaveBack: boolean, - ) { + ): Promise> { const player = await this.playerService.getPlayerForUser(userId); if (!player) throw new ForbiddenException('Player not found'); @@ -363,10 +368,10 @@ export class GameplayService { ); } - return playerPatches; + return { player: playerPatches }; } - async takeItem(userId: string) { + async takeItem(userId: string): Promise> { const player = await this.playerService.getPlayerForUser(userId); if (!player) throw new ForbiddenException('Player not found'); @@ -389,10 +394,13 @@ export class GameplayService { }, ); - return playerPatches; + return { player: playerPatches }; } - async sellItem(userId: string, instanceId: string) { + async sellItem( + userId: string, + instanceId: string, + ): Promise> { if (!instanceId) throw new ForbiddenException('Item instance not found!'); const player = await this.playerService.getPlayerForUser(userId); diff --git a/server/src/modules/player/player.controller.ts b/server/src/modules/player/player.controller.ts index 4739912..f8491a2 100644 --- a/server/src/modules/player/player.controller.ts +++ b/server/src/modules/player/player.controller.ts @@ -1,3 +1,4 @@ +import { IFullUser, IPatchUser } from '@interfaces'; import { JwtAuthGuard } from '@modules/auth/jwt.guard'; import { PlayerService } from '@modules/player/player.service'; import { Body, Controller, Patch, UseGuards } from '@nestjs/common'; @@ -12,14 +13,12 @@ export class PlayerController { @UseGuards(JwtAuthGuard) @ApiOperation({ summary: 'Get the online users count' }) @Patch('cosmetics/portrait') - async changePortrait(@User() user, @Body('portrait') portrait: number) { + async changePortrait( + @User() user, + @Body('portrait') portrait: number, + ): Promise> { const portraitId = Math.round(Math.min(107, Math.max(0, portrait))); - return { - player: await this.playerService.updatePortraitForPlayer( - user.userId, - portraitId, - ), - }; + return this.playerService.updatePortraitForPlayer(user.userId, portraitId); } } diff --git a/server/src/modules/player/player.service.ts b/server/src/modules/player/player.service.ts index 53fd36f..bffa7ff 100644 --- a/server/src/modules/player/player.service.ts +++ b/server/src/modules/player/player.service.ts @@ -1,5 +1,11 @@ import { xpForLevel } from '@helpers/xp'; -import { Currency, ILocation, INotificationAction } from '@interfaces'; +import { + Currency, + IFullUser, + ILocation, + INotificationAction, + IPatchUser, +} from '@interfaces'; import { EntityManager, EntityRepository } from '@mikro-orm/mongodb'; import { InjectRepository } from '@mikro-orm/nestjs'; import { ContentService } from '@modules/content/content.service'; @@ -13,7 +19,6 @@ import { Injectable, } from '@nestjs/common'; import { getPatchesAfterPropChanges } from '@utils/patches'; -import * as jsonpatch from 'fast-json-patch'; import { sample } from 'lodash'; @Injectable() @@ -55,13 +60,20 @@ export class PlayerService { async updatePortraitForPlayer( userId: string, portrait: number, - ): Promise { + ): Promise> { const player = await this.getPlayerForUser(userId); if (!player) throw new ForbiddenException('Player not found'); - return getPatchesAfterPropChanges(player, async (playerRef) => { - playerRef.cosmetics = { ...player.cosmetics, portrait }; - }); + const playerPatches = await getPatchesAfterPropChanges( + player, + async (playerRef) => { + playerRef.cosmetics = { ...player.cosmetics, portrait }; + }, + ); + + return { + player: playerPatches, + }; } gainXp(player: Player, xp = 1) {