Skip to content

Commit

Permalink
better results message
Browse files Browse the repository at this point in the history
  • Loading branch information
WhatCats committed Dec 9, 2024
1 parent 7128aa5 commit 1eeacd4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
18 changes: 10 additions & 8 deletions src/database/models/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ class Template {
return mcCache.get(id)
}

static updateElo(id: string, elo: number) {
eloCache.set(id, elo)
}

static async setMcUuid(id: string, uuid: string) {
await Promise.all([
Player.updateMany({ _id: { $ne: id }, mcUUID: uuid }, { $unset: { mcUUID: "" } }),
Expand All @@ -51,6 +47,10 @@ class Template {
return eloCache.get(id) ?? DEFAULT_ELO
}

static updateElo(id: string, elo: number) {
eloCache.set(id, elo)
}

@LongProp({ required: true })
_id!: string

Expand Down Expand Up @@ -85,8 +85,10 @@ Player.find({}, { mcUUID: 1, [elo]: 1 }).then((players) => {
})
})

Player.watcher().on("update", (v, id) => {
const updated = v.updatedFields as any
if (updated[elo] !== undefined) eloCache.set(id.toString(), updated[elo])
if (updated?.mcUUID !== undefined) mcCache.set(id.toString(), updated.mcUUID.toString())
Player.watcher().on("update", (_v, _id, doc) => {
if (doc) {
eloCache.set(doc.id, doc.getRankedStats().elo)
if (doc.mcUUID) mcCache.set(doc.id, doc.mcUUID)
else mcCache.delete(doc.id)
}
})
3 changes: 1 addition & 2 deletions src/discord/commands/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ export default {
.setDescription("The channel to queue.")
.setRequired(false),
)
.setContexts(InteractionContextType.Guild)
.setDefaultMemberPermissions("0"),
.setContexts(InteractionContextType.Guild),

async execute(interaction: ChatInputCommandInteraction) {
await interaction.deferReply({ ephemeral: true })
Expand Down
3 changes: 2 additions & 1 deletion src/lib/discord/InteractionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export class InteractionHandler {
embeds: [new EmbedBuilder().setColor(0x5ca3f5).setDescription(response)],
})
} catch (error) {
if (error instanceof DiscordAPIError && [10062, 10008].includes(error.code as number)) return
if (error instanceof DiscordAPIError && ["10062", "10008", "10003"].includes(`${error.code}`))
return

let userError: UserError
if (!(error instanceof UserError)) {
Expand Down
7 changes: 4 additions & 3 deletions src/lib/game/closeChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export async function closeChannel(game: Game, score1: number, score2: number, i
const embed = new EmbedBuilder()
.setTitle(`Game ${game.sequence} Finished`)
.setDescription(
`The game has finished.\nPlease confirm ${bold(stringifyScore(game, score1, score2))}.`,
`The game has finished.\nPlease confirm ${bold(stringifyScore(game, score1, score2))} ` +
`or overwrite it with the /score-game command.`,
)
.setColor(colors.baseColor)
.setImage(image ?? null)
Expand All @@ -36,8 +37,8 @@ export async function closeChannel(game: Game, score1: number, score2: number, i
)

const button = new ButtonBuilder()
.setLabel("Confirm")
.setStyle(ButtonStyle.Success)
.setLabel("Score Game")
.setStyle(ButtonStyle.Primary)
.setCustomId(`confirmScore:${score1}:${score2}`)

Promise.allSettled(game.channels!.map((id) => client.rest.delete(Routes.channel(id))))
Expand Down
15 changes: 9 additions & 6 deletions src/lib/game/scoreGame.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { EmbedBuilder, userMention } from "discord.js"
import { EmbedBuilder, spoiler, userMention } from "discord.js"

import { Stats } from "@/Constants"
import { Player } from "@/database"
import type { Game } from "@/database/models/Game"
import { colors } from "@/discord"
import { gameLog } from "@/lib/game/gameLog"
import { Elo, Result } from "@/util/elo"
import { stringifyScore } from "@/util/scores"
import { archiveGame } from "."

export async function scoreGame(game: Game, team1Score: number, team2Score: number) {
Expand Down Expand Up @@ -43,23 +44,24 @@ export async function scoreGame(game: Game, team1Score: number, team2Score: numb
})
}

Player.updateElo(id, newElo[id])
await Player.updateOne(
{ _id: id },
updates.map((v) => ({ $set: v })),
{ upsert: true },
)
Player.updateElo(id, newElo[id])
})
}),
)

const teams = winner === 1 ? [game.teams[1], game.teams[0]] : game.teams
const embed = new EmbedBuilder()
.setColor(colors.baseColor)
.setTitle(`Game #${game.sequence} Results`)
.setDescription(`${team1Score} - ${team2Score}`)
.setDescription(stringifyScore(game, team1Score, team2Score) + ".")
.setFields(
game.teams.map((team, i) => ({
name: `Team ${i + 1}`,
teams.map((team, i) => ({
name: winner === -1 ? `Team ${i + 1}` : i === 0 ? "Winner" : "Loser",
value: team.players
.map((id) => `• ${userMention(id)} \`${oldElo[id]}\` **->** \`${newElo[id]}\``)
.join("\n"),
Expand All @@ -68,6 +70,7 @@ export async function scoreGame(game: Game, team1Score: number, team2Score: numb
)
.setTimestamp()

gameLog(game, { embeds: [embed] }).catch(console.error)
const content = spoiler(players.map(userMention).join(" "))
gameLog(game, { content, embeds: [embed] }).catch(console.error)
return true
}

0 comments on commit 1eeacd4

Please sign in to comment.