Skip to content

Commit

Permalink
can now get a list of items back
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyria committed Aug 1, 2023
1 parent a8d7e4f commit 4ace12a
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 9 deletions.
24 changes: 22 additions & 2 deletions client/src/app/components/modals/market/market.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component, OnInit } from '@angular/core';
import { IMarketItem, Rarity } from '@interfaces';
import { ModalController } from '@ionic/angular';
import { MarketService } from '@services/market.service';

@Component({
selector: 'app-market',
Expand Down Expand Up @@ -39,7 +40,10 @@ export class MarketModalComponent implements OnInit {
public searchCostMin = 0;
public searchCostMax = 0;

constructor(private modalCtrl: ModalController) {}
constructor(
private modalCtrl: ModalController,
private marketService: MarketService,
) {}

ngOnInit() {
this.search();
Expand All @@ -59,5 +63,21 @@ export class MarketModalComponent implements OnInit {

public myListings() {}

public search() {}
public search() {
this.marketService
.getItems({
name: this.searchName,
levelMin: this.searchLevelMin,
levelMax: this.searchLevelMax,
costMin: this.searchCostMin,
costMax: this.searchCostMax,
rarities: Object.keys(this.searchRarities).filter(
(r) => this.searchRarities[r],
),
types: Object.keys(this.searchTypes).filter((t) => this.searchTypes[t]),
})
.subscribe((items) => {
this.marketItems = items;
});
}
}
14 changes: 13 additions & 1 deletion client/src/app/services/market.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import { HttpClient } from '@angular/common/http';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from '@environment';
import { IMarketItem } from '@interfaces';

@Injectable({
providedIn: 'root',
})
export class MarketService {
constructor(private http: HttpClient) {}

getItems(filters: any) {
const params = Object.keys(filters).reduce((prev, key) => {
if (!filters[key]) return prev;
return prev.set(key, filters[key]);
}, new HttpParams());

return this.http.get<IMarketItem[]>(`${environment.apiUrl}/market/items`, {
params,
});
}

sellItem(instanceId: string, price: number) {
return this.http.put(`${environment.apiUrl}/market/listings`, {
instanceId,
Expand Down
11 changes: 6 additions & 5 deletions server/src/modules/market/market.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IFullUser, IPatchUser } from '@interfaces';
import { IFullUser, IMarketItem, IPatchUser } from '@interfaces';
import { JwtAuthGuard } from '@modules/auth/jwt.guard';
import { MarketService } from '@modules/market/market.service';
import {
Expand All @@ -8,6 +8,7 @@ import {
Patch,
Post,
Put,
Query,
UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
Expand All @@ -21,15 +22,15 @@ export class MarketController {
@UseGuards(JwtAuthGuard)
@ApiOperation({ summary: 'Get a filtered item list from the market' })
@Get('items')
async getItems(@User() user): Promise<Partial<IFullUser | IPatchUser>> {
return {};
async getItems(@Query() query: any): Promise<IMarketItem[]> {
return this.marketService.getItems(query);
}

@UseGuards(JwtAuthGuard)
@ApiOperation({ summary: 'Get the current users listings' })
@Get('listings')
async getMyListings(@User() user): Promise<Partial<IFullUser | IPatchUser>> {
return {};
async getMyListings(@User() user): Promise<IMarketItem[]> {
return [];
}

@UseGuards(JwtAuthGuard)
Expand Down
2 changes: 2 additions & 0 deletions server/src/modules/market/market.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ContentModule } from '@modules/content/content.module';
import { InventoryModule } from '@modules/inventory/inventory.module';
import { MarketItem } from '@modules/market/marketitem.schema';
import { PlayerModule } from '@modules/player/player.module';
import { UserModule } from '@modules/user/user.module';
import { Module } from '@nestjs/common';
import { MarketController } from './market.controller';
import { MarketService } from './market.service';
Expand All @@ -13,6 +14,7 @@ import { MarketService } from './market.service';
PlayerModule,
InventoryModule,
ContentModule,
UserModule,
],
controllers: [MarketController],
providers: [MarketService],
Expand Down
94 changes: 93 additions & 1 deletion server/src/modules/market/market.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { cleanNumber } from '@helpers/input';
import { IEquipment, IFullUser, IPatchUser } from '@interfaces';
import {
allAccessoryTypes,
allArmorTypes,
allWeaponTypes,
} from '@helpers/item';
import { IEquipment, IFullUser, IMarketItem, 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 @@ -8,6 +13,7 @@ import { InventoryService } from '@modules/inventory/inventory.service';
import { MarketItem } from '@modules/market/marketitem.schema';
import { Player } from '@modules/player/player.schema';
import { PlayerService } from '@modules/player/player.service';
import { UserService } from '@modules/user/user.service';
import { BadRequestException, Injectable } from '@nestjs/common';
import { getPatchesAfterPropChanges } from '@utils/patches';

Expand All @@ -17,6 +23,7 @@ export class MarketService {
private readonly em: EntityManager,
@InjectRepository(MarketItem)
private readonly marketItem: EntityRepository<MarketItem>,
private readonly userService: UserService,
private readonly playerService: PlayerService,
private readonly inventoryService: InventoryService,
private readonly contentService: ContentService,
Expand All @@ -31,6 +38,9 @@ export class MarketService {
const player = await this.playerService.getPlayerForUser(userId);
if (!player) throw new BadRequestException('Player not found');

const user = await this.userService.findUserById(userId);
if (!user) throw new BadRequestException('User not found');

const item = await this.inventoryService.getInventoryItemForUser(
userId,
instanceId,
Expand Down Expand Up @@ -70,6 +80,8 @@ export class MarketService {
level: (itemRef as IEquipment).levelRequirement ?? 0,
name: itemRef.name,
type: itemRef.type,
listedBy: user.username,
listedById: user.discriminator,
},
);
await this.marketItem.create(dbItem);
Expand Down Expand Up @@ -100,4 +112,84 @@ export class MarketService {
],
};
}

async getItems(query: any): Promise<IMarketItem[]> {
const { name, levelMin, levelMax, costMin, costMax, types, rarities } =
query;

const filterName = name
? { 'meta.name': { $regex: name, $options: 'i' } }
: {};

const filterLevel: any = {};

const filterCost: any = {};

const filterRarities =
rarities.split(',').filter(Boolean).length > 0
? { 'meta.rarity': { $in: rarities.split(',') } }
: {};
const filterTypes: any = {};

if (levelMin || levelMax) {
const levelMinNum = cleanNumber(levelMin, 0, {
round: true,
abs: true,
min: 0,
});

const levelMaxNum = cleanNumber(levelMax, 0, {
round: true,
abs: true,
min: 0,
});

filterLevel['meta.level'] = {};
if (levelMinNum > 0) filterLevel['meta.level'].$gte = levelMinNum;
if (levelMaxNum > 0) filterLevel['meta.level'].$lte = levelMaxNum;
}

if (costMin || costMax) {
const costMinNum = cleanNumber(costMin, 0, {
round: true,
abs: true,
min: 0,
});

const costMaxNum = cleanNumber(costMax, 0, {
round: true,
abs: true,
min: 0,
});

filterLevel['cost'] = {};
if (costMinNum > 0) filterCost['cost'].$gte = costMinNum;
if (costMaxNum > 0) filterCost['cost'].$lte = costMaxNum;
}

if (types.length > 0) {
filterTypes['meta.type'] = {
$in: [
...(types.includes('Weapons') ? [allWeaponTypes()] : []),
...(types.includes('Armors') ? [allArmorTypes()] : []),
...(types.includes('Accessories') ? [allAccessoryTypes()] : []),
...(types.includes('Collectibles') ? ['collectible'] : []),
...(types.includes('Resources') ? ['resource'] : []),
]
.flat()
.filter(Boolean),
};
}

const items = await this.marketItem.find({
isSold: { $ne: true },
...filterName,
...filterLevel,
...filterCost,
...filterRarities,
...filterTypes,
});

return items;
}
}
10 changes: 10 additions & 0 deletions shared/helpers/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,13 @@ export function itemSlotForItem(

return undefined;
}

export function allArmorTypes() {
return AllArmor;
}
export function allWeaponTypes() {
return AllWeapons;
}
export function allAccessoryTypes() {
return AllAccessories;
}
2 changes: 2 additions & 0 deletions shared/interfaces/market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ export interface IMarketItemMeta {
level: number;
type: string;
rarity: Rarity;
listedBy: string;
listedById: string;
}

0 comments on commit 4ace12a

Please sign in to comment.