mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-24 20:19:49 +02:00
feat: migrated to new db
This commit is contained in:
1 parent
0885e7e68a
commit
4062e56b4c
19 files changed
+79
-60
No files matched your search
@@ -53,7 +53,7 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
$env:GH_TOKEN = "${{ secrets.GH_TOKEN }}"
|
$env:GH_TOKEN = "${{ secrets.GH_TOKEN }}"
|
||||||
$env:DISCORD_TOKEN = "${{ secrets.DISCORD_TOKEN }}"
|
$env:DISCORD_TOKEN = "${{ secrets.DISCORD_TOKEN }}"
|
||||||
$env:DISCORD_BLITZ_DB = "${{ secrets.DISCORD_BLITZ_DB }}"
|
$env:DATABASE_URL = "${{ secrets.DATABASE_URL }}"
|
||||||
$env:WOTB_DLC_CDN = "${{ secrets.WOTB_DLC_CDN }}"
|
$env:WOTB_DLC_CDN = "${{ secrets.WOTB_DLC_CDN }}"
|
||||||
yarn setup:patches --depot
|
yarn setup:patches --depot
|
||||||
|
|
||||||
@@ -61,5 +61,5 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
$env:GH_TOKEN = "${{ secrets.GH_TOKEN }}"
|
$env:GH_TOKEN = "${{ secrets.GH_TOKEN }}"
|
||||||
$env:DISCORD_TOKEN = "${{ secrets.DISCORD_TOKEN }}"
|
$env:DISCORD_TOKEN = "${{ secrets.DISCORD_TOKEN }}"
|
||||||
$env:DISCORD_BLITZ_DB = "${{ secrets.DISCORD_BLITZ_DB }}"
|
$env:DATABASE_URL = "${{ secrets.DATABASE_URL }}"
|
||||||
yarn build:assets --all-targets --depot ${{ github.ref == 'refs/heads/main' && '--production' || '' }}
|
yarn build:assets --all-targets --depot ${{ github.ref == 'refs/heads/main' && '--production' || '' }}
|
||||||
@@ -33,4 +33,4 @@ jobs:
|
|||||||
unzip bot.zip
|
unzip bot.zip
|
||||||
|
|
||||||
- name: Run bot
|
- name: Run bot
|
||||||
run: NODE_ENV=production GH_TOKEN=${{ secrets.GH_TOKEN }} DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }} DISCORD_BLITZ_DB=${{ secrets.DISCORD_BLITZ_DB }} node --enable-source-maps index.cjs
|
run: NODE_ENV=production GH_TOKEN=${{ secrets.GH_TOKEN }} DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }} DATABASE_URL=${{ secrets.DATABASE_URL }} node --enable-source-maps index.cjs
|
||||||
@@ -31,4 +31,4 @@ jobs:
|
|||||||
run: yarn
|
run: yarn
|
||||||
|
|
||||||
- name: Generate leaderboard
|
- name: Generate leaderboard
|
||||||
run: GH_TOKEN=${{ secrets.GH_TOKEN }} DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }} DISCORD_BLITZ_DB=${{ secrets.DISCORD_BLITZ_DB }} yarn publish:ratingleaderboard --region=asia --target=midnight --production
|
run: GH_TOKEN=${{ secrets.GH_TOKEN }} DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }} DATABASE_URL=${{ secrets.DATABASE_URL }} yarn publish:ratingleaderboard --region=asia --target=midnight --production
|
||||||
@@ -31,4 +31,4 @@ jobs:
|
|||||||
run: yarn
|
run: yarn
|
||||||
|
|
||||||
- name: Generate leaderboard
|
- name: Generate leaderboard
|
||||||
run: GH_TOKEN=${{ secrets.GH_TOKEN }} DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }} DISCORD_BLITZ_DB=${{ secrets.DISCORD_BLITZ_DB }} yarn publish:ratingleaderboard --region=eu --target=midnight --production
|
run: GH_TOKEN=${{ secrets.GH_TOKEN }} DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }} DATABASE_URL=${{ secrets.DATABASE_URL }} yarn publish:ratingleaderboard --region=eu --target=midnight --production
|
||||||
@@ -31,4 +31,4 @@ jobs:
|
|||||||
run: yarn
|
run: yarn
|
||||||
|
|
||||||
- name: Generate all leaderboards
|
- name: Generate all leaderboards
|
||||||
run: GH_TOKEN=${{ secrets.GH_TOKEN }} DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }} DISCORD_BLITZ_DB=${{ secrets.DISCORD_BLITZ_DB }} yarn publish:ratingleaderboard --region=na,asia,eu --target=latest --production
|
run: GH_TOKEN=${{ secrets.GH_TOKEN }} DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }} DATABASE_URL=${{ secrets.DATABASE_URL }} yarn publish:ratingleaderboard --region=na,asia,eu --target=latest --production
|
||||||
@@ -31,4 +31,4 @@ jobs:
|
|||||||
run: yarn
|
run: yarn
|
||||||
|
|
||||||
- name: Generate leaderboard
|
- name: Generate leaderboard
|
||||||
run: GH_TOKEN=${{ secrets.GH_TOKEN }} DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }} DISCORD_BLITZ_DB=${{ secrets.DISCORD_BLITZ_DB }} yarn publish:ratingleaderboard --region=na --target=midnight --production
|
run: GH_TOKEN=${{ secrets.GH_TOKEN }} DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }} DATABASE_URL=${{ secrets.DATABASE_URL }} yarn publish:ratingleaderboard --region=na --target=midnight --production
|
||||||
+2
-2
@@ -5,13 +5,13 @@
|
|||||||
"main": "src/server.ts",
|
"main": "src/server.ts",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build:bot": "vite-node --script scripts/build",
|
"build:bot": "vite-node --script scripts/build --production",
|
||||||
"build:website": "next build && next-sitemap --config next-sitemap.config.cjs",
|
"build:website": "next build && next-sitemap --config next-sitemap.config.cjs",
|
||||||
"build:docs": "vitepress build docs",
|
"build:docs": "vitepress build docs",
|
||||||
"build:ratingleaderboard": "vite-node --script scripts/buildRatingLeaderboard",
|
"build:ratingleaderboard": "vite-node --script scripts/buildRatingLeaderboard",
|
||||||
"build:assets": "vite-node --max_old_space_size=16384 --script scripts/buildAssets/index",
|
"build:assets": "vite-node --max_old_space_size=16384 --script scripts/buildAssets/index",
|
||||||
"build:test": "vite-node --script scripts/buildTest",
|
"build:test": "vite-node --script scripts/buildTest",
|
||||||
"dev:bot": "vite-node --script scripts/build --production && yarn start:bot",
|
"dev:bot": "vite-node --script scripts/build && yarn start:bot",
|
||||||
"dev:docs": "vitepress dev docs",
|
"dev:docs": "vitepress dev docs",
|
||||||
"dev:website": "next dev",
|
"dev:website": "next dev",
|
||||||
"start:bot": "node --enable-source-maps dist/bot/index.cjs",
|
"start:bot": "node --enable-source-maps dist/bot/index.cjs",
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
-- CreateTable
|
|
||||||
CREATE TABLE "discord_blitz" (
|
|
||||||
"discord" INT8 NOT NULL,
|
|
||||||
"region" STRING NOT NULL,
|
|
||||||
"blitz" INT4 NOT NULL,
|
|
||||||
|
|
||||||
CONSTRAINT "discord_blitz_pkey" PRIMARY KEY ("discord")
|
|
||||||
);
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
# Please do not edit this file manually
|
|
||||||
# It should be added in your version-control system (i.e. Git)
|
|
||||||
provider = "cockroachdb"
|
|
||||||
@@ -4,11 +4,15 @@ generator client {
|
|||||||
|
|
||||||
datasource db {
|
datasource db {
|
||||||
provider = "cockroachdb"
|
provider = "cockroachdb"
|
||||||
url = env("DISCORD_BLITZ_DB")
|
url = env("DATABASE_URL")
|
||||||
}
|
}
|
||||||
|
|
||||||
model discord_blitz {
|
model Users {
|
||||||
|
blitz Int @id
|
||||||
|
last_used DateTime? @default(now())
|
||||||
|
}
|
||||||
|
|
||||||
|
model DiscordUsers {
|
||||||
discord BigInt @id
|
discord BigInt @id
|
||||||
region String
|
|
||||||
blitz Int
|
blitz Int
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,7 @@ import { Glow } from '../components/AllStatsOverview/components/HeroStat/compone
|
|||||||
import CommandWrapper from '../components/CommandWrapper';
|
import CommandWrapper from '../components/CommandWrapper';
|
||||||
import { getAccountInfo } from '../core/blitz/getAccountInfo';
|
import { getAccountInfo } from '../core/blitz/getAccountInfo';
|
||||||
import { getClanAccountInfo } from '../core/blitz/getClanAccountInfo';
|
import { getClanAccountInfo } from '../core/blitz/getClanAccountInfo';
|
||||||
import { linkBlitzAndDiscord } from '../core/blitzkrieg/discordBlitz';
|
import { linkBlitzAndDiscord } from '../core/blitzkrieg/discordBlitzLink';
|
||||||
import addUsernameChoices from '../core/discord/addUsernameChoices';
|
import addUsernameChoices from '../core/discord/addUsernameChoices';
|
||||||
import autocompleteUsername from '../core/discord/autocompleteUsername';
|
import autocompleteUsername from '../core/discord/autocompleteUsername';
|
||||||
import { createLocalizedCommand } from '../core/discord/createLocalizedCommand';
|
import { createLocalizedCommand } from '../core/discord/createLocalizedCommand';
|
||||||
@@ -29,11 +29,11 @@ export const verifyCommand = new Promise<CommandRegistry>((resolve) => {
|
|||||||
async handler(interaction) {
|
async handler(interaction) {
|
||||||
const { t, translate } = translator(interaction.locale);
|
const { t, translate } = translator(interaction.locale);
|
||||||
const { id, region } = await resolvePlayerFromCommand(interaction);
|
const { id, region } = await resolvePlayerFromCommand(interaction);
|
||||||
const discordId = parseInt(interaction.user.id);
|
const discordId = BigInt(interaction.user.id);
|
||||||
const accountInfo = await getAccountInfo(region, id);
|
const accountInfo = await getAccountInfo(region, id);
|
||||||
const clanAccountInfo = await getClanAccountInfo(region, id, ['clan']);
|
const clanAccountInfo = await getClanAccountInfo(region, id, ['clan']);
|
||||||
|
|
||||||
await linkBlitzAndDiscord(discordId, region, id);
|
await linkBlitzAndDiscord(discordId, id);
|
||||||
|
|
||||||
if (interaction.guildId === discord.sklld_guild_id) {
|
if (interaction.guildId === discord.sklld_guild_id) {
|
||||||
if (!interaction.guild?.members.me?.permissions.has('ManageRoles')) {
|
if (!interaction.guild?.members.me?.permissions.has('ManageRoles')) {
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import { Region } from '../../constants/regions';
|
||||||
|
|
||||||
|
export function idToRange(id: number): Region {
|
||||||
|
if (id >= 42e8) {
|
||||||
|
throw new Error('bot id');
|
||||||
|
} else if (id >= 31e8) {
|
||||||
|
throw new Error('china id');
|
||||||
|
} else if (id >= 20e8) {
|
||||||
|
return 'asia';
|
||||||
|
} else if (id >= 10e8) {
|
||||||
|
return 'com';
|
||||||
|
} else if (id >= 5e8) {
|
||||||
|
return 'eu';
|
||||||
|
} else if (id >= 0) {
|
||||||
|
throw new Error('ru id');
|
||||||
|
} else {
|
||||||
|
throw new Error(`id ${id} is out of range`);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
import { Region } from '../../constants/regions';
|
|
||||||
import { discordBlitzDB } from './discordBlitzDB';
|
|
||||||
|
|
||||||
export function linkBlitzAndDiscord(
|
|
||||||
discord: number,
|
|
||||||
region: Region,
|
|
||||||
blitz: number,
|
|
||||||
) {
|
|
||||||
return discordBlitzDB.discord_blitz.upsert({
|
|
||||||
where: { discord },
|
|
||||||
update: { region, blitz },
|
|
||||||
create: { discord, region, blitz },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getBlitzFromDiscord(discord: number) {
|
|
||||||
return discordBlitzDB.discord_blitz.findUnique({
|
|
||||||
where: { discord },
|
|
||||||
select: { region: true, blitz: true },
|
|
||||||
}) as Promise<null | { region: Region; blitz: number }>;
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
import { PrismaClient } from '@prisma/client';
|
|
||||||
|
|
||||||
export const discordBlitzDB = new PrismaClient();
|
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import { usersDatabase } from '../../databases/users';
|
||||||
|
import { idToRange } from '../blitz/idToRegion';
|
||||||
|
|
||||||
|
export async function flagUserActivity(blitz: number) {
|
||||||
|
await usersDatabase.users.upsert({
|
||||||
|
where: { blitz },
|
||||||
|
update: {},
|
||||||
|
create: { blitz },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function linkBlitzAndDiscord(discord: bigint, blitz: number) {
|
||||||
|
await usersDatabase.discordUsers.upsert({
|
||||||
|
where: { discord },
|
||||||
|
update: { blitz },
|
||||||
|
create: { discord, blitz },
|
||||||
|
});
|
||||||
|
await flagUserActivity(blitz);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getBlitzFromDiscord(discord: bigint) {
|
||||||
|
const unique = await usersDatabase.discordUsers.findUnique({
|
||||||
|
where: { discord },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (unique === null) return null;
|
||||||
|
|
||||||
|
await flagUserActivity(unique.blitz);
|
||||||
|
return { id: unique.blitz, region: idToRange(unique.blitz) };
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
|
import { usersDatabase } from '../../databases/users';
|
||||||
import { client } from '../discord/client';
|
import { client } from '../discord/client';
|
||||||
import { discordBlitzDB } from './discordBlitzDB';
|
|
||||||
|
|
||||||
const EXIT_EVENTS = [
|
const EXIT_EVENTS = [
|
||||||
'beforeExit',
|
'beforeExit',
|
||||||
@@ -13,7 +13,7 @@ const EXIT_EVENTS = [
|
|||||||
async function cleanup() {
|
async function cleanup() {
|
||||||
console.log('Cleaning up...');
|
console.log('Cleaning up...');
|
||||||
|
|
||||||
await Promise.all([discordBlitzDB.$disconnect(), client.destroy()]);
|
await Promise.all([usersDatabase.$disconnect(), client.destroy()]);
|
||||||
|
|
||||||
console.log('Gracefully exiting...');
|
console.log('Gracefully exiting...');
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ config();
|
|||||||
const SECRET_KEYS = [
|
const SECRET_KEYS = [
|
||||||
'DISCORD_TOKEN',
|
'DISCORD_TOKEN',
|
||||||
'GH_TOKEN',
|
'GH_TOKEN',
|
||||||
'DISCORD_BLITZ_DB',
|
'DATABASE_URL',
|
||||||
'WOTB_DLC_CDN',
|
'WOTB_DLC_CDN',
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import markdownEscape from 'markdown-escape';
|
|||||||
import { Region } from '../../../constants/regions';
|
import { Region } from '../../../constants/regions';
|
||||||
import { UserError } from '../../../hooks/userError';
|
import { UserError } from '../../../hooks/userError';
|
||||||
import searchPlayersAcrossRegions from '../../blitz/searchPlayersAcrossRegions';
|
import searchPlayersAcrossRegions from '../../blitz/searchPlayersAcrossRegions';
|
||||||
import { getBlitzFromDiscord } from '../../blitzkrieg/discordBlitz';
|
import { getBlitzFromDiscord } from '../../blitzkrieg/discordBlitzLink';
|
||||||
import { translator } from '../../localization/translator';
|
import { translator } from '../../localization/translator';
|
||||||
import { serverAndIdPattern } from './constants';
|
import { serverAndIdPattern } from './constants';
|
||||||
|
|
||||||
@@ -43,12 +43,10 @@ export default async function resolvePlayerFromCommand(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const account = await getBlitzFromDiscord(parseInt(interaction.user.id));
|
const account = await getBlitzFromDiscord(BigInt(interaction.user.id));
|
||||||
|
|
||||||
if (account) {
|
if (account) return account;
|
||||||
return { region: account.region, id: account.blitz };
|
|
||||||
} else {
|
throw new UserError(t`bot.common.errors.player_not_linked`);
|
||||||
throw new UserError(t`bot.common.errors.player_not_linked`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
import { PrismaClient } from '@prisma/client';
|
||||||
|
|
||||||
|
export const usersDatabase = new PrismaClient();
|
||||||
Reference in new issue
Block a user