Skip to content

Commit

Permalink
Merge branch 'master' into bso
Browse files Browse the repository at this point in the history
  • Loading branch information
gc committed Mar 5, 2024
2 parents ac2d549 + 95ebdc3 commit 1fd207b
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 37 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@typescript-eslint/no-base-to-string": "error",
"eqeqeq": "error",
"no-shadow": "warn",
"@typescript-eslint/no-floating-promises": "warn"
"@typescript-eslint/no-floating-promises": "warn",
"@typescript-eslint/ban-ts-comment": "off"
}
}
21 changes: 12 additions & 9 deletions src/lib/grandExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Item, ItemBank } from 'oldschooljs/dist/meta/types';
import PQueue from 'p-queue';

import { ADMIN_IDS, OWNER_IDS, production } from '../config';
import { BLACKLISTED_USERS } from './blacklists';
import { BitField, globalConfig, ONE_TRILLION, PerkTier } from './constants';
import { isCustomItem } from './customItems/util';
import { marketPricemap } from './marketPrices';
Expand Down Expand Up @@ -615,13 +616,15 @@ ${type} ${toKMB(quantity)} ${item.name} for ${toKMB(price)} each, for a total of
items: buyerLoot,
collectionLog: false,
dontAddToTempCL: true,
filterLoot: false
filterLoot: false,
neverUpdateHistory: true
});
await sellerUser.addItemsToBank({
items: sellerLoot,
collectionLog: false,
dontAddToTempCL: true,
filterLoot: false
filterLoot: false,
neverUpdateHistory: true
});

const itemName = itemNameFromID(buyerListing.item_id)!;
Expand Down Expand Up @@ -695,9 +698,7 @@ ${type} ${toKMB(quantity)} ${item.name} for ${toKMB(price)} each, for a total of
type: GEListingType.Buy,
fulfilled_at: null,
cancelled_at: null,
user_id: {
not: null
}
user_id: { not: null }
},
orderBy: [
{
Expand All @@ -713,9 +714,7 @@ ${type} ${toKMB(quantity)} ${item.name} for ${toKMB(price)} each, for a total of
type: GEListingType.Sell,
fulfilled_at: null,
cancelled_at: null,
user_id: {
not: null
}
user_id: { not: null }
},
orderBy: [
{
Expand Down Expand Up @@ -849,7 +848,11 @@ Difference: ${shouldHave.difference(currentBank)}`);
if (this.locked) return;
const stopwatch = new Stopwatch();
stopwatch.start();
const { buyListings, sellListings } = await this.fetchActiveListings();
const { buyListings: _buyListings, sellListings: _sellListings } = await this.fetchActiveListings();

// Filter out listings from Blacklisted users:
const buyListings = _buyListings.filter(l => !BLACKLISTED_USERS.has(l.user_id!));
const sellListings = _sellListings.filter(l => !BLACKLISTED_USERS.has(l.user_id!));

for (const buyListing of buyListings) {
// These are all valid, matching sell listings we can match with this buy listing.
Expand Down
21 changes: 21 additions & 0 deletions src/mahoji/commands/rp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { makeBankImage } from '../../lib/util/makeBankImage';
import { migrateUser } from '../../lib/util/migrateUser';
import { parseBank } from '../../lib/util/parseStringBank';
import { sendToChannelID } from '../../lib/util/webhook';
import { cancelUsersListings } from '../lib/abstracted_commands/cancelGEListingCommand';
import { gearSetupOption } from '../lib/mahojiCommandOptions';
import { OSBMahojiCommand } from '../lib/util';
import { mahojiUsersSettingsFetch } from '../mahojiSettings';
Expand Down Expand Up @@ -323,6 +324,19 @@ export const rpCommand: OSBMahojiCommand = {
required: false
}
]
},
{
type: ApplicationCommandOptionType.Subcommand,
name: 'ge_cancel',
description: 'Cancel GE Listings',
options: [
{
type: ApplicationCommandOptionType.User,
name: 'user',
description: 'The user',
required: true
}
]
}
]
}
Expand Down Expand Up @@ -369,6 +383,7 @@ export const rpCommand: OSBMahojiCommand = {
partner?: MahojiUserOption;
guild_id?: string;
};
ge_cancel?: { user: MahojiUserOption };
};
}>) => {
await deferInteraction(interaction);
Expand Down Expand Up @@ -777,6 +792,12 @@ ORDER BY item_id ASC;`);
return { files: [{ attachment: Buffer.from(report), name: 'trade_report.txt' }] };
}

if (options.player?.ge_cancel) {
const targetUser = await mUserFetch(options.player.ge_cancel.user.user.id);
await cancelUsersListings(targetUser);
return `Cancelled listings for ${targetUser}`;
}

return 'Invalid command.';
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { makeTransactFromTableBankQueries } from '../../../lib/tableBank';
import { logError } from '../../../lib/util/logError';

export async function cancelUsersListings(user: MUser) {
await user.sync();
const activeListings = await prisma.gEListing.findMany({
where: {
user_id: user.id,
Expand Down
2 changes: 2 additions & 0 deletions src/scripts/no-prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @ts-ignore
global.prisma = { $on: () => {} };
1 change: 1 addition & 0 deletions src/scripts/render-creatables-file.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './no-prisma';
import '../lib/data/itemAliases';

import { isFunction } from 'e';
Expand Down
36 changes: 9 additions & 27 deletions tests/integration/grandExchange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { GrandExchange } from '../../src/lib/grandExchange';
import { assert } from '../../src/lib/util';
import resolveItems from '../../src/lib/util/resolveItems';
import { geCommand } from '../../src/mahoji/commands/ge';
import { cancelUsersListings } from '../../src/mahoji/lib/abstracted_commands/cancelGEListingCommand';
import { createTestUser, mockClient, TestUser } from './util';

const quantities = [-1, 0, 100_000_000_000_000_000, 1, 2, 38, 1_000_000_000_000, 500, '5*5'];
Expand Down Expand Up @@ -37,28 +38,6 @@ const sampleBank = new Bank()
.add('Trout', 1000)
.freeze();

async function cancelAllListings(user: TestUser) {
const activeListings = await global.prisma!.gEListing.findMany({
where: {
user_id: user.id
}
});
for (const listing of activeListings) {
const result = (await user.runCommand(geCommand, {
cancel: {
listing: listing.userfacing_id
}
})) as string;

if (
result !== 'You cannot cancel a listing that has already been fulfilled.' &&
!result.startsWith('Successfully cancelled your listing,')
) {
throw new Error(`Unexpected result from cancelling listing: ${result}`);
}
}
}

describe('Grand Exchange', async () => {
const itemPool = resolveItems(['Egg', 'Trout', 'Coal']);
GrandExchange.calculateSlotsOfUser = async () => ({ slots: 500 } as any);
Expand Down Expand Up @@ -128,7 +107,7 @@ describe('Grand Exchange', async () => {
// Cancel all remaining listings
const cancelPromises = [];
for (const user of users) {
cancelPromises.push(cancelAllListings(user));
cancelPromises.push(cancelUsersListings(user));
}
await Promise.all(cancelPromises);
await waitForGEToBeEmpty();
Expand Down Expand Up @@ -175,7 +154,6 @@ Based on G.E data, we should have received ${data.totalTax} tax`;

await GrandExchange.queue.onEmpty();
assert(GrandExchange.queue.size === 0, 'Queue should be empty');
const geBank = await GrandExchange.fetchOwnedBank();
},
{
repeats: 1,
Expand Down Expand Up @@ -220,18 +198,22 @@ Based on G.E data, we should have received ${data.totalTax} tax`;
await GrandExchange.tick();
await GrandExchange.tick();

// The user object isn't updated by the GE tick, since that uses completely separate user objects.
await wes.sync();
await magnaboy.sync();

const amountSold = 50;
const priceSoldAt = 100;
const totalGPBeforeTax = amountSold * priceSoldAt;
const taxPerItem = calcPercentOfNum(1, priceSoldAt);
expect(taxPerItem).toEqual(1);
const totalTax = taxPerItem * amountSold;
expect(taxPerItem).toEqual(1);
expect(totalTax).toEqual(50);
const gpShouldBeReceivedAfterTax = totalGPBeforeTax - totalTax;
expect(gpShouldBeReceivedAfterTax).toEqual(4950);

await cancelAllListings(wes);
await cancelAllListings(magnaboy);
await cancelUsersListings(wes);
await cancelUsersListings(magnaboy);

expect(wes.bankWithGP.toString()).toEqual(
new Bank()
Expand Down

0 comments on commit 1fd207b

Please sign in to comment.