Skip to content

Commit

Permalink
fix(api): unite response values of services/controllers to all be the…
Browse files Browse the repository at this point in the history
… same interface
  • Loading branch information
seiyria committed Jun 24, 2023
1 parent 0740d47 commit aeadbda
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 60 deletions.
14 changes: 14 additions & 0 deletions server/src/interfaces/api.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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[];
}
7 changes: 3 additions & 4 deletions server/src/modules/inventory/inventory.controller.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<Partial<IFullUser | IPatchUser>> {
return this.inventoryService.getInventoryItemsForUser(user.userId);
}
}
7 changes: 5 additions & 2 deletions server/src/modules/inventory/inventory.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -43,8 +44,10 @@ export class InventoryService {
return inventory;
}

async getInventoryItemsForUser(userId: string): Promise<InventoryItem[]> {
return this.inventoryItems.find({ userId });
async getInventoryItemsForUser(
userId: string,
): Promise<Partial<IFullUser | IPatchUser>> {
return { items: await this.inventoryItems.find({ userId }) };
}

async getInventoryItemForUser(
Expand Down
51 changes: 25 additions & 26 deletions server/src/modules/player/gameplay.controller.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<Partial<IFullUser | IPatchUser>> {
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<Partial<IFullUser | IPatchUser>> {
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<Partial<IFullUser | IPatchUser>> {
return this.gameplayService.travelToLocation(user.userId, location);
}

@UseGuards(JwtAuthGuard)
Expand All @@ -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<Partial<IFullUser | IPatchUser>> {
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<Partial<IFullUser | IPatchUser>> {
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<Partial<IFullUser | IPatchUser>> {
return this.gameplayService.sellItem(user.userId, instanceId);
}
}
38 changes: 23 additions & 15 deletions server/src/modules/player/gameplay.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand All @@ -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<Partial<IFullUser | IPatchUser>> {
const player = await this.playerService.getPlayerForUser(userId);
if (!player) throw new ForbiddenException('Player not found');

Expand Down Expand Up @@ -191,7 +193,7 @@ export class GameplayService {
async walkToLocation(
userId: string,
locationName: string,
): Promise<jsonpatch.Operation[]> {
): Promise<Partial<IFullUser | IPatchUser>> {
const player = await this.playerService.getPlayerForUser(userId);
if (!player) throw new ForbiddenException('Player not found');

Expand Down Expand Up @@ -231,10 +233,13 @@ export class GameplayService {
},
);

return playerPatches;
return { player: playerPatches };
}

async travelToLocation(userId: string, locationName: string): Promise<any> {
async travelToLocation(
userId: string,
locationName: string,
): Promise<Partial<IFullUser | IPatchUser>> {
const player = await this.playerService.getPlayerForUser(userId);
if (!player) throw new ForbiddenException('Player not found');

Expand Down Expand Up @@ -279,14 +284,14 @@ export class GameplayService {
},
);

return playerPatches;
return { player: playerPatches };
}

async waveToPlayer(
userId: string,
targetUserId: string,
isWaveBack: boolean,
) {
): Promise<Partial<IFullUser | IPatchUser>> {
const player = await this.playerService.getPlayerForUser(userId);
if (!player) throw new ForbiddenException('Player not found');

Expand Down Expand Up @@ -363,10 +368,10 @@ export class GameplayService {
);
}

return playerPatches;
return { player: playerPatches };
}

async takeItem(userId: string) {
async takeItem(userId: string): Promise<Partial<IFullUser | IPatchUser>> {
const player = await this.playerService.getPlayerForUser(userId);
if (!player) throw new ForbiddenException('Player not found');

Expand All @@ -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<Partial<IFullUser | IPatchUser>> {
if (!instanceId) throw new ForbiddenException('Item instance not found!');

const player = await this.playerService.getPlayerForUser(userId);
Expand Down
13 changes: 6 additions & 7 deletions server/src/modules/player/player.controller.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<Partial<IFullUser | IPatchUser>> {
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);
}
}
24 changes: 18 additions & 6 deletions server/src/modules/player/player.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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()
Expand Down Expand Up @@ -55,13 +60,20 @@ export class PlayerService {
async updatePortraitForPlayer(
userId: string,
portrait: number,
): Promise<jsonpatch.Operation[]> {
): Promise<Partial<IFullUser | IPatchUser>> {
const player = await this.getPlayerForUser(userId);
if (!player) throw new ForbiddenException('Player not found');

return getPatchesAfterPropChanges<Player>(player, async (playerRef) => {
playerRef.cosmetics = { ...player.cosmetics, portrait };
});
const playerPatches = await getPatchesAfterPropChanges<Player>(
player,
async (playerRef) => {
playerRef.cosmetics = { ...player.cosmetics, portrait };
},
);

return {
player: playerPatches,
};
}

gainXp(player: Player, xp = 1) {
Expand Down

0 comments on commit aeadbda

Please sign in to comment.