Files
new-phone-who-dis/resources/server/sv_sellout.ts
T
Taso 0f61088999 refactor(events): Events are now tied to a common enum typings (#167)
* chore(scripts/twitter): Refactored twitter to use event enums

* chore(scripts/marketplace): Refactored marketplace to use event enums

* chore(scripts/messages): Refactored messages to use event enums

* chore(scripts/contacts): Refactored contacts to use event enums

* chore(scripts/calls): Refactored calls to use event enums

* chore(scripts/notes): Refactored notes to use event enums

* chore(scripts/photos): Refactored photos to use event enums

* chore(scripts/bank): Refactored bank to use event enums

* chore(scripts/phone): Refactored phone to use event enums

* chore(events): Removed old events object

* refactor(phone/nui-events): Attached NUI events to respective enum

* tweak(event-enums): Added npwd prefix to all events

* tweak(phone/serve): Added craco to override CRA without eject

Also bumped react-scripts to 4.0.1, with seemingly no issues

* fix(typings/twitter): Fixed method enums for twitter
2021-03-22 13:37:07 -04:00

163 lines
4.9 KiB
TypeScript

import { getIdentifier, getPlayer, getSource } from './functions';
import { pool } from './db';
import { mainLogger } from './sv_logger';
import { MarketplaceListing } from '../../typings/marketplace';
import { reportListingToDiscord } from './discord';
import { MarketplaceEvents } from '../../typings/marketplace';
const selloutLogger = mainLogger.child({ module: 'sellout' });
async function fetchAllListings(): Promise<MarketplaceListing[]> {
const query = 'SELECT * FROM npwd_marketplace_listings ORDER BY id DESC';
const [results] = await pool.query(query);
return <MarketplaceListing[]>results;
}
async function addListing(
identifier: string,
username: string,
name: string,
number: any,
listing: MarketplaceListing,
): Promise<void> {
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<void> {
const query = 'DELETE FROM npwd_marketplace_listings WHERE id = ? AND identifier = ?';
await pool.query(query, [listingId, identifier]);
}
async function getListing(listingId: number): Promise<MarketplaceListing> {
const query = `SELECT * FROM npwd_marketplace_listings WHERE id = ?`;
const [results] = await pool.query(query, [listingId]);
const listings = <MarketplaceListing[]>results;
return listings[0];
}
async function reportListing(listingId: number, profile: string): Promise<void> {
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<boolean> {
const query = `SELECT * FROM npwd_marketplace_reports WHERE listing_id = ? AND profile = ?`;
const [results] = await pool.query(query, [listingId, profile]);
const result = <any>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 = getPlayer(_source);
// This is used as username for the reports
await addListing(
player.identifier,
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 = 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,
});
}
});