Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/events/interactionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,16 @@ export default {
await handleTwoPlayerMatchVoting(interaction, {
participants: matchUsersArray,
onComplete: async (interaction, winner) => {
console.log(
`Starting finish vote from vote from ${interaction.user.id} with winner ${winner}`,
)
try {
const customSelId = interaction.values[0]
const matchDataParts: string[] = customSelId.split('_')
const matchId = parseInt(matchDataParts[1])
console.log(
`Finishing vote for match ${matchId}, winner ${winner}`,
)

// Check if this match is Best of 3 or 5
const matchDataObj = await getMatchData(matchId)
Expand Down Expand Up @@ -279,6 +285,10 @@ export default {
winningTeam = 2
}

console.log(
`Winning team variable for match ${matchId}: ${winningTeam}`,
)

if (winningTeam) {
await setWinningTeam(matchId, winningTeam)
await endMatch(matchId)
Expand Down
86 changes: 54 additions & 32 deletions src/utils/matchHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,43 +566,15 @@ export async function endMatch(
matchId: number,
cancelled = false,
): Promise<boolean> {
try {
// close match in DB
await closeMatch(matchId)

// get log file using glob library
// const pattern = path
// .join(__dirname, '..', 'logs', `match-${matchId}_*.log`)
// .replace(/\\/g, '/')
// const files = await glob(pattern)
// const file: string | null = files[0] ?? null
console.log(`Ending match ${matchId}, cancelled: ${cancelled}`)

// TODO: Re-add this and send it to the website
// if (file) {
// // format and send transcript
// const logContent = fs.readFileSync(file, 'utf8')
// const logLines = logContent
// .split('\n')
// .filter((line) => line.trim() !== '')
// const parsedLogLines = await parseLogLines(logLines)
// console.log(parsedLogLines) // json body
//
// // delete the log file after transcript is sent
// fs.unlinkSync(file)
// }

// delete match channel
if (cancelled) {
console.log(`Match ${matchId} cancelled.`)
const wasSuccessfullyDeleted = await deleteMatchChannel(matchId)
if (!wasSuccessfullyDeleted) {
console.log(`Channel id not found / failed to delete match ${matchId}`)
}

if (cancelled) return true
} catch (err) {
console.error(
`Error in file formatting or channel deletion for match ${matchId}:`,
err,
)
return true
}

// build results button row
Expand All @@ -628,8 +600,11 @@ export async function endMatch(
}

const queueId = await getQueueIdFromMatch(matchId)
console.log(`Queue ID for match ${matchId}: ${queueId}`)
const queueSettings = await getQueueSettings(queueId, ['queue_name', 'color'])
console.log(`Queue settings for match ${matchId}:`, queueSettings)
const matchData = await getMatchData(matchId)
console.log(`Match data for match ${matchId}:`, matchData)

let teamResults: teamResults | null
// create our teamResults object here
Expand All @@ -643,9 +618,12 @@ export async function endMatch(

teamResults = await calculateNewMMR(queueId, matchId, teamResultsData)

console.log(`match ${matchId} results: ${teamResults.teams}`)

// Save elo_change and winstreak to database
const updatePromises = teamResults.teams.flatMap((team) =>
team.players.map(async (player) => {
console.log(`Team ${team} player ${player} in match ${matchId}`)
// Update win streak
await updatePlayerWinStreak(player.user_id, queueId, team.score == 1)

Expand All @@ -661,6 +639,48 @@ export async function endMatch(

await Promise.all(updatePromises)

console.log(`Updated elo_change and win_streak for match ${matchId}`)

try {
// close match in DB
console.log(`Ending match ${matchId}, cancelled: ${cancelled}`)
await closeMatch(matchId)

// get log file using glob library
// const pattern = path
// .join(__dirname, '..', 'logs', `match-${matchId}_*.log`)
// .replace(/\\/g, '/')
// const files = await glob(pattern)
// const file: string | null = files[0] ?? null

// TODO: Re-add this and send it to the website
// if (file) {
// // format and send transcript
// const logContent = fs.readFileSync(file, 'utf8')
// const logLines = logContent
// .split('\n')
// .filter((line) => line.trim() !== '')
// const parsedLogLines = await parseLogLines(logLines)
// console.log(parsedLogLines) // json body
//
// // delete the log file after transcript is sent
// fs.unlinkSync(file)
// }

// delete match channel
const wasSuccessfullyDeleted = await deleteMatchChannel(matchId)
if (!wasSuccessfullyDeleted) {
console.log(`Channel id not found / failed to delete match ${matchId}`)
}

if (cancelled) return true
} catch (err) {
console.error(
`Error in file formatting or channel deletion for match ${matchId}:`,
err,
)
}

// build results embed
const resultsEmbed = new EmbedBuilder()
.setTitle(`🏆 ${queueSettings.queue_name} Match #${matchId} 🏆`)
Expand Down Expand Up @@ -771,6 +791,8 @@ export async function endMatch(
return false
}

console.log(`Sending results to ${resultsChannel.id} on match ${matchId}`)

await resultsChannel.send({
embeds: [resultsEmbed],
components: [resultsButtonRow],
Expand Down