Skip to content

Commit

Permalink
Fix #2 and move gunfight storage to objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Aug 7, 2020
1 parent df5119f commit f64b0e6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 44 deletions.
53 changes: 27 additions & 26 deletions server/bot/commands/gunfight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,34 @@ export const handleGunfight: Command = {

// Do not challenge someone already in a gunfight.
// Possible gunfights.
const possibleGunfight = tempDB.gunfight.find((gunfight) => gunfight.challenged === user.id)
const possibleGunfight = Object.keys(tempDB.gunfight)
.find((gunfight) => gunfight.split('-').includes(user.id))
if (possibleGunfight) return 'This user is already in a fight!'
// Push to database.
tempDB.gunfight.push({
const timestamp = Date.now()
tempDB.gunfight[message.author.id + '-' + user.id] = {
timestamp,
accepted: false,
challenged: user.id,
challenger: message.author.id,
randomWord: '',
channelID: message.channel.id
})
channelID: message.channel.id,
wordSaid: false
}
client.createMessage(
message.channel.id, `${user.mention}, say /accept to accept the challenge.`
)
// The following will delete the gunfight if unaccepted within 30 seconds.
setTimeout(async () => {
// Find the gunfight we pushed.
const gunfightPushed = tempDB.gunfight.find((gunfight) => gunfight.challenged === user.id)
const gunfightPushed = tempDB.gunfight[message.author.id + '-' + user.id]
// Remove the object and send a response.
if (!gunfightPushed.accepted) {
if (gunfightPushed && !gunfightPushed.accepted && gunfightPushed.timestamp === timestamp) {
const mention = message.author.mention
client.createMessage(
message.channel.id,
`${mention}, your challenge to ${user.mention} has been cancelled.`
)
delete tempDB.gunfight[message.author.id + '-' + user.id]
}
tempDB.gunfight.splice(tempDB.gunfight.indexOf(gunfightPushed), 1)
}, 30000)
}
}
Expand All @@ -72,40 +74,39 @@ export const handleAccept: Command = {
},
generator: (message, args, { client, tempDB }) => {
// Find the gunfight, if exists.
const gunfightToAccept = tempDB.gunfight.find((gunfight) => (
gunfight.challenged === message.author.id && !gunfight.accepted &&
message.channel.id === gunfight.channelID
const gunfightToAccept = Object.keys(tempDB.gunfight).find((gunfight) => (
gunfight.substr(gunfight.indexOf('-') + 1) === message.author.id &&
!tempDB.gunfight[gunfight].accepted && message.channel.id === tempDB.gunfight[gunfight].channelID
))
if (gunfightToAccept === undefined) return // Insert checks.
// Accept only if person is not in another gunfight.
if (tempDB.gunfight.find((gunfight) => (
gunfight.challenged === message.author.id
)) !== gunfightToAccept) {
if (Object.keys(tempDB.gunfight).filter((gunfight) => (
gunfight.split('-').includes(message.author.id)
)).length > 1) {
return 'You are already in a gunfight!'
}
// Accept the challenge.
const indexOfGunfight = tempDB.gunfight.indexOf(gunfightToAccept)
const words = ['fire', 'water', 'gun', 'dot']
tempDB.gunfight[indexOfGunfight].accepted = true
tempDB.gunfight[indexOfGunfight].randomWord = words[Math.floor(Math.random() * words.length)]
tempDB.gunfight[gunfightToAccept].accepted = true
tempDB.gunfight[gunfightToAccept].randomWord = words[Math.floor(Math.random() * words.length)]
client.createMessage(
message.channel.id, `Within 20 seconds, you will be asked to say a random word.`
)
const timestamp = tempDB.gunfight[gunfightToAccept].timestamp
// Let's wait for random amount under 20s and call a random word.
setTimeout(async () => {
try {
await message.channel.createMessage('Say ' + tempDB.gunfight[indexOfGunfight].randomWord + '!')
await message.channel.createMessage('Say ' + tempDB.gunfight[gunfightToAccept].randomWord + '!')
} catch (e) {}
/* TODO
tempDB.gunfight[tempDB.gunfight.indexOf(gunfightToAccept)].wordSaid = true
tempDB.gunfight[gunfightToAccept].wordSaid = true
setTimeout(() => {
if (tempDB.gunfight.find(
i => i.challenged === message.author.id && message.channel.id === i.channelID
)) {
if (tempDB.gunfight[gunfightToAccept] &&
tempDB.gunfight[gunfightToAccept].timestamp === timestamp &&
tempDB.gunfight[gunfightToAccept].channelID === message.channel.id) {
message.channel.createMessage('Neither of you said the word so you both lose..')
tempDB.gunfight.splice(tempDB.gunfight.indexOf(gunfightToAccept), 1)
delete tempDB.gunfight[gunfightToAccept]
}
}, 30000) */
}, 30000)
}, Math.floor(Math.random() * 20000 + 1000))
}
}
16 changes: 9 additions & 7 deletions server/bot/imports/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import CommandParser from '../client'
import { Db } from 'mongodb'

export type DB = {
gunfight: Array<{
challenged: string,
challenger: string,
accepted: boolean,
randomWord: string,
channelID: string
}>,
gunfight: {
[key: string]: {
randomWord: string,
timestamp: number,
channelID: string,
accepted: boolean,
wordSaid: boolean
}
},
say: {
// Channels.
[index: string]: string
Expand Down
20 changes: 10 additions & 10 deletions server/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,17 @@ export default async (message: Message, client: Client, tempDB: DB, db: Db) => {
else if (command.startsWith('iphone x')) sendResponse(`You don't deserve it. 😎`)
else if (command.startsWith('triggered')) sendResponse('Ah, pathetic people again.')
else if (command.startsWith('ayy')) sendResponse('lmao')
// TODO: Handle answers to gunfight.
/* else if (['fire', 'water', 'gun', 'dot'].includes(command)) {
const gunfight = tempDB.gunfight.find(i => i.randomWord === command && (
i.challenged === message.author.id || i.challenger === message.author.id
) && i.wordSaid)
if (!gunfight) return
// Handle answers to gunfight.
else if (['fire', 'water', 'gun', 'dot'].includes(command)) {
const key = Object.keys(tempDB.gunfight).find(i => (
i.split('-')[0] === message.author.id || i.split('-')[1] === message.author.id
))
if (!key) return
const gunfight = tempDB.gunfight[key]
if (!gunfight || !gunfight.wordSaid || gunfight.randomWord !== command) return
sendResponse(`${message.author.mention} won!`)
tempDB.gunfight.splice(tempDB.gunfight.findIndex(i => i.randomWord === command && (
i.challenged === message.author.id || i.challenger === message.author.id
)), 1)
} */
delete tempDB.gunfight[key]
}

// Get settings, server specific from now on.
if (!message.member) return
Expand Down
2 changes: 1 addition & 1 deletion server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ client.on('error', (err: Error, id: string) => {

// Create a database to handle certain stuff.
const tempDB: DB = {
gunfight: [], say: {}, link: {}, leave: [], mute: {}, cooldowns: { request: [] }
gunfight: {}, say: {}, link: {}, leave: [], mute: {}, cooldowns: { request: [] }
}

/* SERVER CODE STARTS HERE */
Expand Down

0 comments on commit f64b0e6

Please sign in to comment.