Skip to content

Commit

Permalink
Fix gacha getting stuck (#423)
Browse files Browse the repository at this point in the history
* fix(gacha): miscalculation of characters rating in pool

* chore(workflows): update to deno v2

* fix: lint typing issues

* fix: fmt issues
  • Loading branch information
ker0olos authored Nov 13, 2024
1 parent 00eac27 commit 33a53bc
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 31 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
deno-version: v2.x
- run: deno fmt --check

lint:
Expand All @@ -25,7 +25,7 @@ jobs:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
deno-version: v2.x
- run: deno lint

test:
Expand All @@ -34,7 +34,7 @@ jobs:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
deno-version: v2.x
- run: deno test -A --coverage=cov_profile --no-check
- run: deno coverage cov_profile --lcov --output=lcov.info
- uses: codecov/codecov-action@v4
Expand All @@ -53,7 +53,7 @@ jobs:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
deno-version: v2.x

- run: deno run -A db/_ensureIndexes.ts
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/discord.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
deno-version: v2.x

- if: github.ref != 'refs/heads/main'
name: Set environment to dummy
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/search-index.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
workflow_dispatch:
schedule:
# Once a month "At 00:00 on day-of-month 2" (see https://crontab.guru/once-a-month)
- cron: "0 0 2 * *"
- cron: '0 0 2 * *'

jobs:
index:
Expand All @@ -14,7 +14,7 @@ jobs:

- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
deno-version: v2.x

- run: deno run -A search-index/index.ts
env:
Expand All @@ -24,9 +24,9 @@ jobs:
uses: peter-evans/create-pull-request@v5
with:
token: ${{ secrets.PAT }}
title: "[bot] Updated Search Indexes"
title: '[bot] Updated Search Indexes'
author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
commit-message: "[bot] Updated `search-index/*.bin`"
commit-message: '[bot] Updated `search-index/*.bin`'
branch: update-search-index
delete-branch: true
body: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/topgg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
workflow_dispatch:
schedule:
# Everyday "At 00:00" (see https://crontab.guru/daily)
- cron: "0 0 * * *"
- cron: '0 0 * * *'

jobs:
update-stats:
Expand All @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
deno-version: v2.x

- run: deno run -A update_topgg_stats.ts
env:
Expand Down
6 changes: 3 additions & 3 deletions db/addCharacter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import type * as Schema from '~/db/schema.ts';

import type { SkillKey } from '~/src/types.ts';

import type { AnyBulkWriteOperation } from 'mongodb';

const newSkills = (rating: number): number => {
switch (rating) {
case 5:
Expand Down Expand Up @@ -160,9 +162,7 @@ export async function addCharacter(
lastPull: new Date(),
};

const deleteSacrifices: Parameters<
ReturnType<typeof mongo.characters>['bulkWrite']
>[0] = [];
const deleteSacrifices: AnyBulkWriteOperation<Schema.Character>[] = [];

// if sacrifices (merge)
if (sacrifices?.length) {
Expand Down
6 changes: 3 additions & 3 deletions db/gainExp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { getFloorExp } from '~/src/tower.ts';

import type * as Schema from './schema.ts';

import type { AnyBulkWriteOperation } from 'mongodb';

export const MAX_LEVEL = 10;

type Status = {
Expand Down Expand Up @@ -102,9 +104,7 @@ export async function gainExp(

const session = db.startSession();

const bulk: Parameters<
ReturnType<typeof db.characters>['bulkWrite']
>[0] = [];
const bulk: AnyBulkWriteOperation<Schema.Character>[] = [];

let status: Status[];

Expand Down
6 changes: 3 additions & 3 deletions db/tradeCharacters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { NonFetalError } from '~/src/errors.ts';

import type * as Schema from '~/db/schema.ts';

import type { AnyBulkWriteOperation } from 'mongodb';

export const STEAL_COOLDOWN_HOURS = 3 * 24;

export async function giveCharacters(
Expand Down Expand Up @@ -210,9 +212,7 @@ export async function tradeCharacters(
throw new NonFetalError('CHARACTER_IN_PARTY');
}

const bulk: Parameters<
ReturnType<typeof db.characters>['bulkWrite']
>[0] = [];
const bulk: AnyBulkWriteOperation<Schema.Character>[] = [];

bulk.push(
{
Expand Down
2 changes: 1 addition & 1 deletion src/communityAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export async function publish(req: Request): Promise<Response> {
statusText: 'Created',
});
} catch (err) {
switch (err.message) {
switch ((err as Error).message) {
case 'PERMISSION_DENIED':
return utils.json({ error: 'No permission to edit this pack' }, {
status: 403,
Expand Down
2 changes: 1 addition & 1 deletion src/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ export class Message {
throw err;
}

utils.captureException(err, {
utils.captureException(err as Error, {
extra: {
url,
payload: JSON.stringify(this.json()),
Expand Down
7 changes: 6 additions & 1 deletion src/gacha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ async function rngPool(
if (edge) {
const popularity = character.popularity || edge.node.popularity || lowest;

if (new Rating({ popularity, role: edge.role }).stars !== rating) {
if (
new Rating({
popularity,
role: character.popularity ? undefined : edge.role,
}).stars !== rating
) {
return false;
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1381,8 +1381,10 @@ export const handler = async (r: Request) => {
}
} catch (err) {
if (
err.response?.status === 404 || err?.message === '404' ||
err.message?.toLowerCase?.() === 'not found'
// deno-lint-ignore no-explicit-any
(err as any).response?.status === 404 ||
(err as Error)?.message === '404' ||
(err as Error).message?.toLowerCase?.() === 'not found'
) {
return new discord.Message()
.setContent('')
Expand Down Expand Up @@ -1418,7 +1420,7 @@ export const handler = async (r: Request) => {
throw err;
}

const refId = utils.captureException(err, {
const refId = utils.captureException(err as Error, {
extra: { ...interaction },
});

Expand Down
2 changes: 1 addition & 1 deletion src/steal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ function attempt({
])
.followup(token);
} catch (err) {
switch (err.message) {
switch ((err as Error).message) {
case 'CHARACTER_NOT_FOUND':
throw new NonFetalError(
i18n.get('character-hasnt-been-found', locale, characterName),
Expand Down
6 changes: 5 additions & 1 deletion src/tower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,11 @@ function reclear({ token, guildId, userId }: {

return await message.patch(token);
} catch (err) {
if (err.message.includes('Write conflict during plan execution')) {
if (
(err as Error).message.includes(
'Write conflict during plan execution',
)
) {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ function nick({
} catch (err) {
const names = packs.aliasToArray(results[0].name);

switch (err.message) {
switch ((err as Error).message) {
case 'CHARACTER_NOT_FOUND': {
return message
.addEmbed(
Expand Down Expand Up @@ -433,7 +433,7 @@ function image({
} catch (err) {
const names = packs.aliasToArray(results[0].name);

switch (err.message) {
switch ((err as Error).message) {
case 'CHARACTER_NOT_FOUND': {
return message
.addEmbed(
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ async function readJson<T>(filePath: string): Promise<T> {
const jsonString = await Deno.readTextFile(filePath);
return JSON.parse(jsonString);
} catch (err) {
err.message = `${filePath}: ${err.message}`;
(err as Error).message = `${filePath}: ${(err as Error).message}`;
throw err;
}
}
Expand Down

0 comments on commit 33a53bc

Please sign in to comment.