diff --git a/resources/server/marketplace/marketplace.controller.ts b/resources/server/marketplace/marketplace.controller.ts new file mode 100644 index 0000000..2e6caf6 --- /dev/null +++ b/resources/server/marketplace/marketplace.controller.ts @@ -0,0 +1,43 @@ +import { MarketplaceEvents, MarketplaceListing } from '../../../typings/marketplace'; +import { getSource } from '../utils/miscUtils'; +import PlayerService from '../players/player.service'; +import { reportListingToDiscord } from '../discord'; +import { selloutLogger } from './marketplace.utils'; +import MarketplaceService from './marketplace.service'; + +onNet(MarketplaceEvents.FETCH_LISTING, async () => { + const src = getSource(); + MarketplaceService.handleFetchListings(src).catch((e) => + selloutLogger.error(`Error occurred in fetch listing event (${src}), Error: ${e.message}`), + ); +}); + +onNet(MarketplaceEvents.ADD_LISTING, async (listing: MarketplaceListing) => { + const src = getSource(); + const Player = PlayerService.getPlayer(src); + + MarketplaceService.handleAddListing( + src, + Player.getFirstName(), + Player.username, + Player.getName(), + Player.getPhoneNumber(), + listing, + ).catch((e) => + selloutLogger.error(`Error occurred in add listing event (${src}), Error: ${e.message}`), + ); +}); + +onNet(MarketplaceEvents.DELETE_LISTING, async (listingId: number) => { + const src = getSource(); + MarketplaceService.handleDeleteListing(src, listingId).catch((e) => + selloutLogger.error(`Error occurred in delete listing event (${src}), Error: ${e.message}`), + ); +}); + +onNet(MarketplaceEvents.REPORT_LISTING, async (listing: MarketplaceListing) => { + const src = getSource(); + MarketplaceService.handleReportListing(src, listing).catch((e) => + selloutLogger.error(`Error occurred in report listing event (${src}), Error: ${e.message}`), + ); +}); diff --git a/resources/server/marketplace/marketplace.db.ts b/resources/server/marketplace/marketplace.db.ts new file mode 100644 index 0000000..f449fbe --- /dev/null +++ b/resources/server/marketplace/marketplace.db.ts @@ -0,0 +1,62 @@ +import { pool } from '../db'; +import { MarketplaceListing } from '../../../typings/marketplace'; + +export class _MarketplaceDB { + async addListing( + identifier: string, + username: string, + name: string, + number: any, + listing: MarketplaceListing, + ): Promise { + const query = + 'INSERT INTO npwd_marketplace_listings (identifier, username, name, number, title, url, description) VALUES (?, ?, ?, ?, ?, ?, ?)'; + + await pool.query(query, [ + identifier, + username, + name, + number, + listing.title, + listing.url, + listing.description, + ]); + } + + async fetchListings(): Promise { + const query = 'SELECT * FROM npwd_marketplace_listings ORDER BY id DESC'; + + const [results] = await pool.query(query); + return results; + } + + async deleteListing(listingId: number, identifier: string): Promise { + const query = 'DELETE FROM npwd_marketplace_listings WHERE id = ? AND identifier = ?'; + + await pool.query(query, [listingId, identifier]); + } + + async getListing(listingId: number): Promise { + const query = `SELECT * FROM npwd_marketplace_listings WHERE id = ?`; + const [results] = await pool.query(query, [listingId]); + const listings = results; + return listings[0]; + } + + async reportListing(listingId: number, profile: string): Promise { + const query = `INSERT INTO npwd_marketplace_reports (listing_id, profile) VALUES (?, ?)`; + await pool.query(query, [listingId, profile]); + } + + async doesReportExist(listingId: number, profile: string): Promise { + const query = `SELECT * FROM npwd_marketplace_reports WHERE listing_id = ? AND profile = ?`; + const [results] = await pool.query(query, [listingId, profile]); + const result = results; + + return result.length > 0; + } +} + +const MarketplaceDB = new _MarketplaceDB(); + +export default MarketplaceDB; diff --git a/resources/server/marketplace/marketplace.service.ts b/resources/server/marketplace/marketplace.service.ts new file mode 100644 index 0000000..92a726e --- /dev/null +++ b/resources/server/marketplace/marketplace.service.ts @@ -0,0 +1,110 @@ +import PlayerService from '../players/player.service'; +import { selloutLogger } from './marketplace.utils'; +import MarketplaceDB, { _MarketplaceDB } from './marketplace.db'; +import { MarketplaceEvents, MarketplaceListing } from '../../../typings/marketplace'; +import { notesLogger } from '../notes/notes.utils'; +import { listenerCount } from 'cluster'; +import { reportListingToDiscord } from '../discord'; + +class _MarketplaceService { + private readonly marketplaceDB: _MarketplaceDB; + + constructor() { + this.marketplaceDB = MarketplaceDB; + selloutLogger.debug('Marketplace service started'); + } + + async handleAddListing( + src: number, + identifier: string, + username: string, + name: string, + number: any, + listing: MarketplaceListing, + ): Promise { + selloutLogger.debug('Handling add listing, listing:'); + selloutLogger.debug; + + try { + await this.marketplaceDB.addListing(identifier, username, name, number, listing); + + emitNet(MarketplaceEvents.ADD_LISTING_SUCCESS, src); + emitNet(MarketplaceEvents.ACTION_RESULT, src, { + message: 'MARKETPLACE_CREATE_LISTING_SUCCESS', + type: 'success', + }); + } catch (e) { + selloutLogger.error(`Failed to add listing ${e.message}`, { + source: src, + }); + + emitNet(MarketplaceEvents.ACTION_RESULT, src, { + message: 'MARKETPLACE_CREATE_LISTING_FAILED', + type: 'error', + }); + } + } + + async handleFetchListings(src: number) { + try { + const listings = await this.marketplaceDB.fetchListings(); + + emitNet(MarketplaceEvents.SEND_LISTING, src, listings); + } catch (e) { + selloutLogger.error(`Failed to fetch listings, ${e.message}`, { + source: src, + }); + } + } + + async handleDeleteListing(src: number, listingId: number): Promise { + const identifier = PlayerService.getIdentifier(src); + try { + await this.marketplaceDB.deleteListing(listingId, identifier); + + emitNet(MarketplaceEvents.DELETE_LISTING_SUCCESS, src); + emitNet(MarketplaceEvents.ACTION_RESULT, src, { + message: 'MARKETPLACE_DELETE_LISTING_SUCCESS', + type: 'success', + }); + } catch (e) { + notesLogger.error(`Error in handleDeleteListing, ${e.message}`); + + emitNet(MarketplaceEvents.ACTION_RESULT, src, { + message: 'MARKETPLACE_DELETE_LISTING_FAILED', + type: 'error', + }); + } + } + + async handleReportListing(src: number, listing: MarketplaceListing): Promise { + try { + const rListing = await this.marketplaceDB.getListing(listing.id); + const reportExists = await this.marketplaceDB.doesReportExist(listing.id, listing.name); + + const reportingPlayer = GetPlayerName(src.toString()); + if (reportExists) { + selloutLogger.error(`This listing has already been reported`); + return emitNet(MarketplaceEvents.ACTION_RESULT, src, { + message: 'MARKETPLACE_REPORT_LISTING_FAILED', + type: 'info', + }); + } + + await this.marketplaceDB.reportListing(rListing.id, rListing.name); + await reportListingToDiscord(rListing, reportingPlayer); + + emitNet(MarketplaceEvents.ACTION_RESULT, src, { + message: 'MARKETPLACE_REPORT_LISTING_SUCCESS', + type: 'success', + }); + } catch (e) { + selloutLogger.error(`Failed to report listing ${e.message}`, { + source: src, + }); + } + } +} + +const MarketplaceService = new _MarketplaceService(); +export default MarketplaceService; diff --git a/resources/server/marketplace/marketplace.utils.ts b/resources/server/marketplace/marketplace.utils.ts new file mode 100644 index 0000000..54cb221 --- /dev/null +++ b/resources/server/marketplace/marketplace.utils.ts @@ -0,0 +1,3 @@ +import { mainLogger } from '../sv_logger'; + +export const selloutLogger = mainLogger.child({ module: 'sellout' }); diff --git a/resources/server/sv_marketplace.ts b/resources/server/sv_marketplace.ts deleted file mode 100644 index f900132..0000000 --- a/resources/server/sv_marketplace.ts +++ /dev/null @@ -1,163 +0,0 @@ -import { pool } from './db'; -import { mainLogger } from './sv_logger'; -import { MarketplaceListing } from '../../typings/marketplace'; -import { reportListingToDiscord } from './discord'; -import { MarketplaceEvents } from '../../typings/marketplace'; -import { getSource } from './utils/miscUtils'; -import PlayerService from './players/player.service'; - -const selloutLogger = mainLogger.child({ module: 'sellout' }); - -async function fetchAllListings(): Promise { - const query = 'SELECT * FROM npwd_marketplace_listings ORDER BY id DESC'; - - const [results] = await pool.query(query); - return results; -} - -async function addListing( - identifier: string, - username: string, - name: string, - number: any, - listing: MarketplaceListing, -): Promise { - const query = - 'INSERT INTO npwd_marketplace_listings (identifier, username, name, number, title, url, description) VALUES (?, ?, ?, ?, ?, ?, ?)'; - await pool.query(query, [ - identifier, - username, - name, - number, - listing.title, - listing.url, - listing.description, - ]); -} - -async function deleteListing(listingId: number, identifier: string): Promise { - const query = 'DELETE FROM npwd_marketplace_listings WHERE id = ? AND identifier = ?'; - - await pool.query(query, [listingId, identifier]); -} - -async function getListing(listingId: number): Promise { - const query = `SELECT * FROM npwd_marketplace_listings WHERE id = ?`; - const [results] = await pool.query(query, [listingId]); - const listings = results; - return listings[0]; -} - -async function reportListing(listingId: number, profile: string): Promise { - const query = `INSERT INTO npwd_marketplace_reports (listing_id, profile) VALUES (?, ?)`; - - await pool.query(query, [listingId, profile]); -} - -async function doesReportExist(listingId: number, profile: string): Promise { - const query = `SELECT * FROM npwd_marketplace_reports WHERE listing_id = ? AND profile = ?`; - const [results] = await pool.query(query, [listingId, profile]); - const result = results; - - return result.length > 0; -} - -onNet(MarketplaceEvents.FETCH_LISTING, async () => { - const _source = getSource(); - try { - const listings = await fetchAllListings(); - emitNet(MarketplaceEvents.SEND_LISTING, _source, listings); - } catch (e) { - selloutLogger.error(`Failed to fetch listings, ${e.message}`, { - source: _source, - }); - } -}); - -onNet(MarketplaceEvents.ADD_LISTING, async (listing: MarketplaceListing) => { - const _source = getSource(); - try { - const player = PlayerService.getPlayer(_source); - - // This is used as username for the reports - await addListing( - player.getIdentifier(), - player.username, - player.getName(), - player.getPhoneNumber(), - listing, - ); - - emitNet(MarketplaceEvents.ADD_LISTING_SUCCESS, _source); - emitNet(MarketplaceEvents.ACTION_RESULT, _source, { - message: 'MARKETPLACE_CREATE_LISTING_SUCCESS', - type: 'success', - }); - } catch (e) { - selloutLogger.error(`Failed to add listing ${e.message}`, { - source: _source, - }); - - emitNet(MarketplaceEvents.ACTION_RESULT, _source, { - message: 'MARKETPLACE_CREATE_LISTING_FAILED', - type: 'error', - }); - } -}); - -onNet(MarketplaceEvents.DELETE_LISTING, async (listingId: number) => { - const pSource = getSource(); - try { - const identifier = PlayerService.getIdentifier(pSource); - - await deleteListing(listingId, identifier); - - // fetches the listings again - emitNet(MarketplaceEvents.DELETE_LISTING_SUCCESS, pSource); - - emitNet(MarketplaceEvents.ACTION_RESULT, pSource, { - message: 'MARKETPLACE_DELETE_LISTING_SUCCESS', - type: 'success', - }); - } catch (e) { - selloutLogger.error(`Failed to delete listing ${e.message}`, { - source: pSource, - }); - emitNet(MarketplaceEvents.ACTION_RESULT, pSource, { - message: 'MARKETPLACE_DELETE_LISTING_FAILED', - type: 'error', - }); - } -}); - -onNet(MarketplaceEvents.REPORT_LISTING, async (listing: MarketplaceListing) => { - const pSource = getSource(); - - try { - const rListing = await getListing(listing.id); - const reportExists = await doesReportExist(listing.id, listing.name); - - // gets the player name (steam) of the player that is reporting - const reportingPlayer = GetPlayerName(pSource); - if (reportExists) { - // send an info alert - selloutLogger.error(`This listing has already been reported`); - return emitNet(MarketplaceEvents.ACTION_RESULT, pSource, { - message: 'MARKETPLACE_REPORT_LISTING_FAILED', - type: 'info', - }); - } - - await reportListing(rListing.id, rListing.name); - await reportListingToDiscord(rListing, reportingPlayer); - - emitNet(MarketplaceEvents.ACTION_RESULT, pSource, { - message: 'MARKETPLACE_REPORT_LISTING_SUCCESS', - type: 'success', - }); - } catch (e) { - selloutLogger.error(`Failed to report listing ${e.message}`, { - source: pSource, - }); - } -});