Skip to content

Commit

Permalink
Add cancel_ge listings RP command & updated GE to use cancelUsersList…
Browse files Browse the repository at this point in the history
…ings fn
  • Loading branch information
themrrobert committed Mar 4, 2024
1 parent f401795 commit cec0de9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
21 changes: 21 additions & 0 deletions src/mahoji/commands/rp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,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 @@ -303,6 +304,19 @@ export const rpCommand: OSBMahojiCommand = {
required: false
}
]
},
{
type: ApplicationCommandOptionType.Subcommand,
name: 'cancel_ge',
description: 'Cancel GE Listings',
options: [
{
type: ApplicationCommandOptionType.User,
name: 'user',
description: 'The user',
required: true
}
]
}
]
}
Expand Down Expand Up @@ -347,6 +361,7 @@ export const rpCommand: OSBMahojiCommand = {
partner?: MahojiUserOption;
guild_id?: string;
};
cancel_ge?: { user: MahojiUserOption };
};
}>) => {
await deferInteraction(interaction);
Expand Down Expand Up @@ -727,6 +742,12 @@ ORDER BY item_id ASC;`);
return { files: [{ attachment: Buffer.from(report), name: 'trade_report.txt' }] };
}

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

return 'Invalid command.';
}
};
30 changes: 4 additions & 26 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 @@ -230,8 +208,8 @@ Based on G.E data, we should have received ${data.totalTax} tax`;
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 cec0de9

Please sign in to comment.