Skip to content

Commit 373f740

Browse files
committed
Improve creature scores methods & add tests
1 parent 308d562 commit 373f740

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

src/lib/MUser.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { percentChance } from '@oldschoolgg/rng';
22
import { calcWhatPercent, cleanUsername, Emoji, isObject, sumArr, UserError, uniqueArr } from '@oldschoolgg/toolkit';
33
import { escapeMarkdown, userMention } from 'discord.js';
44
import {
5-
addItemToBank,
65
Bank,
76
convertXPtoLVL,
87
EMonster,
@@ -534,11 +533,27 @@ RETURNING (monster_scores->>'${monsterID}')::int AS new_kc;
534533
return ActivityManager.minionIsBusy(this.id);
535534
}
536535

537-
async incrementCreatureScore(creatureID: number, amountToAdd = 1) {
538-
const stats = await this.fetchStats();
539-
await this.statsUpdate({
540-
creature_scores: addItemToBank(stats.creature_scores as ItemBank, creatureID, amountToAdd)
541-
});
536+
async getCreatureScore(creatureID: number): Promise<number> {
537+
if (!Number.isInteger(creatureID)) throw new Error('Invalid monsterID');
538+
const query = `
539+
SELECT COALESCE((creature_scores->>'${creatureID}')::int, 0) AS creature_kc
540+
FROM user_stats
541+
WHERE user_id = ${this.id};`;
542+
const stats = await prisma.$queryRawUnsafe<{ creature_kc: number }[]>(query);
543+
return stats[0]?.creature_kc ?? 0;
544+
}
545+
546+
async incrementCreatureScore(creatureID: number, quantityToAdd = 1): Promise<{ newKC: number }> {
547+
if (!Number.isInteger(creatureID)) throw new Error('Invalid creatureID');
548+
if (!Number.isInteger(quantityToAdd) || quantityToAdd < 1) throw new Error('Invalid quantityToAdd');
549+
const query = `
550+
UPDATE user_stats
551+
SET creature_scores = add_item_to_bank(creature_scores, '${creatureID}', ${quantityToAdd})
552+
WHERE user_id = ${this.id}
553+
RETURNING (creature_scores->>'${creatureID}')::int AS new_kc;
554+
`;
555+
const res = await prisma.$queryRawUnsafe<{ new_kc: number }[]>(query);
556+
return { newKC: res[0]?.new_kc ?? 0 };
542557
}
543558

544559
get blowpipe() {
@@ -714,11 +729,6 @@ Charge your items using ${mentionCommand('minion', 'charge')}.`
714729
};
715730
}
716731

717-
async getCreatureScore(creatureID: number) {
718-
const stats = await this.fetchStats();
719-
return (stats.creature_scores as ItemBank)[creatureID] ?? 0;
720-
}
721-
722732
calculateAddItemsToCLUpdates({
723733
items,
724734
dontAddToTempCL = false

src/tasks/minions/HunterActivity/hunterActivity.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ export const hunterTask: MinionTask = {
156156
}
157157
}
158158

159-
await user.incrementCreatureScore(creature.id, Math.floor(successfulQuantity));
159+
if (successfulQuantity > 0) {
160+
await user.incrementCreatureScore(creature.id, Math.floor(successfulQuantity));
161+
}
160162
await user.transactItems({
161163
collectionLog: true,
162164
itemsToAdd: loot
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { expect, test } from 'vitest';
2+
3+
import Hunter from '@/lib/skilling/skills/hunter/hunter.js';
4+
import { minionCommand } from '@/mahoji/commands/minion.js';
5+
import { createTestUser } from '../util.js';
6+
7+
test('incrementCreatureScore', async () => {
8+
const user = await createTestUser();
9+
10+
await user.incrementCreatureScore(1);
11+
expect(await user.getCreatureScore(1)).toEqual(1);
12+
13+
await user.incrementCreatureScore(1, 10);
14+
expect(await user.getCreatureScore(1)).toEqual(11);
15+
16+
const creatures = Hunter.Creatures.slice(10, 20).map(c => c.id);
17+
await Promise.all([
18+
...creatures.map(id => user.incrementCreatureScore(id, 3)),
19+
...creatures.map(id => user.incrementCreatureScore(id, 3))
20+
]);
21+
22+
for (const id of creatures) {
23+
expect(await user.getCreatureScore(id)).toEqual(6);
24+
}
25+
26+
expect(await user.getCreatureScore(1)).toEqual(11);
27+
const res = await user.runCommand(minionCommand, { kc: { name: 'Sapphire glacialis' } });
28+
expect(res).toEqual('Your Sapphire glacialis KC is: 6.');
29+
});

0 commit comments

Comments
 (0)