Skip to content

Commit 107bdce

Browse files
committed
2 parents 5c6f5b2 + 2d7a3a6 commit 107bdce

File tree

9 files changed

+563
-342
lines changed

9 files changed

+563
-342
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @type {import('node-pg-migrate').ColumnDefinitions | undefined}
3+
*/
4+
export const shorthands = undefined;
5+
6+
/**
7+
* @param pgm {import('node-pg-migrate').MigrationBuilder}
8+
* @param run {() => void | undefined}
9+
* @returns {Promise<void> | void}
10+
*/
11+
export const up = (pgm) => {
12+
pgm.createTable('votes', {
13+
id: { type: 'serial', primaryKey: true },
14+
match_id: { type: 'integer', notNull: true, references: '"matches"', onDelete: 'CASCADE' },
15+
user_id: { type: 'varchar(255)', notNull: true, references: '"users"(user_id)', onDelete: 'CASCADE', onUpdate: 'CASCADE' },
16+
vote_type: { type: 'varchar(50)', notNull: true }, // 'win', 'cancel', 'rematch', 'bo3', 'bo5'
17+
vote_value: { type: 'integer' }, // For win votes: team number (1, 2, etc), for others: null (presence = yes vote)
18+
created_at: { type: 'timestamp with time zone', notNull: true, default: pgm.func('NOW()') },
19+
});
20+
21+
// Index for faster lookups
22+
pgm.addIndex('votes', ['match_id', 'user_id']);
23+
pgm.addIndex('votes', ['match_id', 'vote_type']);
24+
25+
// Constraint: A user can only have one active vote per match (they can change their vote)
26+
pgm.addConstraint('votes', 'unique_user_vote_per_match', {
27+
unique: ['match_id', 'user_id']
28+
});
29+
};
30+
31+
/**
32+
* @param pgm {import('node-pg-migrate').MigrationBuilder}
33+
* @param run {() => void | undefined}
34+
* @returns {Promise<void> | void}
35+
*/
36+
export const down = (pgm) => {
37+
pgm.dropTable('votes', { ifExists: true, cascade: true });
38+
};

src/events/interactionCreate.ts

Lines changed: 74 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import {
3737
getQueueSettings,
3838
getStake,
3939
getStakeByName,
40-
getUserPriorityQueueId,
40+
// getUserPriorityQueueId,
4141
getUserQueues,
4242
setMatchStakeVoteTeam,
4343
setPickedMatchStake,
@@ -194,73 +194,80 @@ export default {
194194
}
195195
}
196196

197-
await handleTwoPlayerMatchVoting(interaction, {
198-
participants: matchUsersArray,
199-
onComplete: async (interaction, winner) => {
200-
const customSelId = interaction.values[0]
201-
const matchDataParts: string[] = customSelId.split('_')
202-
const matchId = parseInt(matchDataParts[1])
203-
204-
// Check if this match is Best of 3 or 5
205-
const matchDataObj = await getMatchData(matchId)
206-
const isBo3 = matchDataObj.best_of_3
207-
const isBo5 = matchDataObj.best_of_5
208-
209-
if (!isBo3 && !isBo5) {
210-
await setWinningTeam(matchId, winner)
211-
await endMatch(matchId)
212-
return
213-
}
214-
215-
const embed = interaction.message.embeds[0]
216-
const fields = embed.data.fields || []
197+
try {
198+
await handleTwoPlayerMatchVoting(interaction, {
199+
participants: matchUsersArray,
200+
onComplete: async (interaction, winner) => {
201+
const customSelId = interaction.values[0]
202+
const matchDataParts: string[] = customSelId.split('_')
203+
const matchId = parseInt(matchDataParts[1])
204+
205+
// Check if this match is Best of 3 or 5
206+
const matchDataObj = await getMatchData(matchId)
207+
const isBo3 = matchDataObj.best_of_3
208+
const isBo5 = matchDataObj.best_of_5
209+
210+
if (!isBo3 && !isBo5) {
211+
await setWinningTeam(matchId, winner)
212+
await endMatch(matchId)
213+
return
214+
} else {
215+
const embed = interaction.message.embeds[0]
216+
const fields = embed.data.fields || []
217217

218-
// Update Best of scores and check for match winner
219-
const winnerIndex = winner === 1 ? 0 : 1
220-
for (let i = 0; i < Math.min(2, fields.length); i++) {
221-
const val = fields[i].value || ''
222-
const lines = val.split('\n')
218+
// Update Best of scores in the embed (for display only)
219+
const winnerIndex = winner === 1 ? 0 : 1
220+
for (let i = 0; i < Math.min(2, fields.length); i++) {
221+
const val = fields[i].value || ''
222+
const lines = val.split('\n')
223223

224-
const cleaned = lines.filter(
225-
(l) => !l.includes('Win Votes') && !l.includes('<@'),
226-
)
224+
const cleaned = lines.filter(
225+
(l) => !l.includes('Win Votes') && !l.includes('<@'),
226+
)
227227

228-
const mmrIdx = cleaned.findIndex((l) => l.includes('MMR'))
229-
if (mmrIdx !== -1) {
230-
const mmrLine = cleaned[mmrIdx]
231-
const m = mmrLine.match(/Score:\s*(\d+)/i)
232-
let score = m ? parseInt(m[1], 10) || 0 : 0
228+
const mmrIdx = cleaned.findIndex((l) => l.includes('MMR'))
229+
if (mmrIdx !== -1) {
230+
const mmrLine = cleaned[mmrIdx]
231+
const m = mmrLine.match(/Score:\s*(\d+)/i)
232+
let score = m ? parseInt(m[1], 10) || 0 : 0
233233

234-
if (i === winnerIndex) score += 1
234+
if (i === winnerIndex) score += 1
235235

236-
cleaned[mmrIdx] =
237-
mmrLine.replace(/\s*-?\s*Score:\s*\d+/i, '').trimEnd() +
238-
` - Score: ${score}`
239-
}
236+
cleaned[mmrIdx] =
237+
mmrLine.replace(/\s*-?\s*Score:\s*\d+/i, '').trimEnd() +
238+
` - Score: ${score}`
239+
}
240240

241-
fields[i].value = cleaned.join('\n')
242-
}
243-
244-
const scores = getBestOfMatchScores(fields)
245-
const requiredWins = isBo5 ? 3 : isBo3 ? 2 : 1
246-
const [team1Wins, team2Wins] = scores
241+
fields[i].value = cleaned.join('\n')
242+
}
247243

248-
let winningTeam = 0
249-
if (team1Wins >= requiredWins) {
250-
winningTeam = 1
251-
} else if (team2Wins >= requiredWins) {
252-
winningTeam = 2
253-
}
244+
// Get updated scores from the embed (after incrementing)
245+
let scores = getBestOfMatchScores(fields)
246+
let requiredWins = isBo5 ? 3 : isBo3 ? 2 : 1
247+
const [team1Wins, team2Wins] = scores
248+
249+
// Check if a team has won the Best of series
250+
let winningTeam = 0
251+
if (team1Wins >= requiredWins) {
252+
winningTeam = 1
253+
} else if (team2Wins >= requiredWins) {
254+
winningTeam = 2
255+
}
254256

255-
if (winningTeam) {
256-
await endMatch(matchId)
257-
return
258-
}
257+
if (winningTeam) {
258+
await setWinningTeam(matchId, winningTeam)
259+
await endMatch(matchId)
260+
return
261+
}
259262

260-
interaction.message.embeds[0] = embed
261-
await interaction.update({ embeds: interaction.message.embeds })
262-
},
263-
})
263+
interaction.message.embeds[0] = embed
264+
await interaction.update({ embeds: interaction.message.embeds })
265+
}
266+
},
267+
})
268+
} catch (err) {
269+
console.error(err)
270+
}
264271
}
265272

266273
if (interaction.customId.startsWith('deck-bans-')) {
@@ -434,9 +441,9 @@ export default {
434441

435442
if (interaction.customId === 'check-queued') {
436443
const userQueueList = await getUserQueues(interaction.user.id)
437-
const priorityQueueId = await getUserPriorityQueueId(
438-
interaction.user.id,
439-
)
444+
// const priorityQueueId = await getUserPriorityQueueId(
445+
// interaction.user.id,
446+
// )
440447

441448
if (userQueueList.length > 0) {
442449
const timeSpent = await timeSpentInQueue(
@@ -447,7 +454,7 @@ export default {
447454
content:
448455
`
449456
You are in queue for **${userQueueList.map((queue) => `${queue.queue_name}`).join(', ')}**!` +
450-
`${priorityQueueId ? `\nYour priority queue is **${(await getQueueSettings(priorityQueueId, ['queue_name'])).queue_name}**!` : ``}` +
457+
// `${priorityQueueId ? `\nYour priority queue is **${(await getQueueSettings(priorityQueueId, ['queue_name'])).queue_name}**!` : ``}` +
451458
`\nJoined queue ${timeSpent}.`,
452459
flags: MessageFlags.Ephemeral,
453460
})
@@ -542,6 +549,7 @@ export default {
542549
voteType: 'Cancel Match Votes',
543550
embedFieldIndex: 2,
544551
participants: matchUsersArray,
552+
matchId: matchId,
545553
onComplete: async (interaction) => {
546554
await cancel(interaction, matchId)
547555
},
@@ -724,6 +732,8 @@ export default {
724732
voteType: 'Rematch Votes',
725733
embedFieldIndex: 2,
726734
participants: matchUsersArray,
735+
matchId: matchId,
736+
resendMessage: false,
727737
onComplete: async (interaction, { embed }) => {
728738
await interaction.update({
729739
content: 'A Rematch for this matchup has begun!',
@@ -796,6 +806,7 @@ export default {
796806
voteType: voteFieldName,
797807
embedFieldIndex: 3,
798808
participants: matchUsersArray,
809+
matchId: matchId,
799810
onComplete: async (interaction, { embed }) => {
800811
const rows = interaction.message.components.map((row) =>
801812
ActionRowBuilder.from(row as any),

src/utils/Autocompletions.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ export async function strikeAutocomplete(interaction: AutocompleteInteraction) {
3333
const uniqueIds = [...new Set(userIds)]
3434
const users = await Promise.all(uniqueIds.map((id) => fetchUserSafe(id)))
3535

36+
// why in the world is this here - jeff
3637
// logging
37-
console.group('ids')
38-
console.log(uniqueIds)
39-
console.groupEnd()
40-
console.group('users')
41-
for (const user of users) console.log(user?.username)
42-
console.groupEnd()
38+
// console.group('ids')
39+
// console.log(uniqueIds)
40+
// console.groupEnd()
41+
// console.group('users')
42+
// for (const user of users) console.log(user?.username)
43+
// console.groupEnd()
4344
//
4445

4546
const q = value.toLowerCase()

0 commit comments

Comments
 (0)