Switch to new command() routing.
This commit is contained in:
parent
a3c838438e
commit
b4ce41eb6a
|
@ -5,9 +5,31 @@ const buyableItems = require('./buyableItems')
|
||||||
const upgrades = require('./upgrades')
|
const upgrades = require('./upgrades')
|
||||||
const achievements = require('./achievements')
|
const achievements = require('./achievements')
|
||||||
|
|
||||||
const saveFile = './hvacoins.json'
|
const saveFile = 'hvacoins.json'
|
||||||
|
|
||||||
const logError = msg => msg ? console.error(msg) : undefined
|
const parseOr = (parseable, orFunc) => {
|
||||||
|
try {
|
||||||
|
return JSON.parse(parseable)
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
|
return orFunc()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const game = parseOr(fs.readFileSync('./' + saveFile, 'utf-8'),
|
||||||
|
() => ({ users: {}, nfts: [] }))
|
||||||
|
const { users, nfts } = game
|
||||||
|
|
||||||
|
const logError = msg => msg ? console.error(msg) : () => {}
|
||||||
|
|
||||||
|
let saves = 0
|
||||||
|
const saveGame = () => {
|
||||||
|
if (saves % 100 === 0) {
|
||||||
|
fs.writeFileSync('./backups/' + saveFile + new Date().toLocaleString().replace(/[^a-z0-9]/gi, '_'), JSON.stringify(game))
|
||||||
|
}
|
||||||
|
saves += 1
|
||||||
|
fs.writeFileSync('./' + saveFile, JSON.stringify(game))
|
||||||
|
}
|
||||||
|
|
||||||
const maybeNews = say => {
|
const maybeNews = say => {
|
||||||
const random = Math.random()
|
const random = Math.random()
|
||||||
|
@ -35,15 +57,27 @@ const helpText = (highestCoins, user) => Object.entries(buyableItems)
|
||||||
'\n\nNote: Listed prices are _base costs_ and will increase as you buy more.'
|
'\n\nNote: Listed prices are _base costs_ and will increase as you buy more.'
|
||||||
|
|
||||||
const getUpgradeEmoji = upgrade => upgrade.emoji || buyableItems[upgrade.type].emoji
|
const getUpgradeEmoji = upgrade => upgrade.emoji || buyableItems[upgrade.type].emoji
|
||||||
const upgradeText = user =>
|
const upgradeText = user => {
|
||||||
(!user ? '' : '\n\n' + Object.entries(upgrades).filter(([upgradeName, upgrade]) => !hasUpgrade(user, upgrade, upgradeName)).filter(([, upgrade]) => upgrade.condition(user)).map(([key, value]) => `:${getUpgradeEmoji(value)}: *${key}* - ${commas(value.cost)}\n_${value.description}_`).join('\n\n')) +
|
return (!user ? '' : '\n\n' + Object.entries(upgrades).filter(([upgradeName, upgrade]) => !hasUpgrade(user, upgrade, upgradeName)).filter(([, upgrade]) => upgrade.condition(user)).map(([key, value]) => `:${getUpgradeEmoji(value)}: *${key}* - ${commas(value.cost)}\n_${value.description}_`).join('\n\n')) +
|
||||||
'\n\n:grey_question::grey_question::grey_question:' +
|
'\n\n:grey_question::grey_question::grey_question:' +
|
||||||
'\n\nJust type \'!upgrade upgrade_name\' to purchase'
|
'\n\nJust type \'!upgrade upgrade_name\' to purchase'
|
||||||
|
}
|
||||||
|
|
||||||
const game = JSON.parse(fs.readFileSync(saveFile, 'utf-8'))
|
const getUser = userId => {
|
||||||
const { users, nfts } = game
|
if (!users[userId]) {
|
||||||
|
users[userId] = {
|
||||||
const saveGame = () => fs.writeFile(saveFile, JSON.stringify(game), logError)
|
coins: 0,
|
||||||
|
items: {},
|
||||||
|
upgrades: {},
|
||||||
|
achievements: {}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
users[userId].items ??= {}
|
||||||
|
users[userId].upgrades ??= {}
|
||||||
|
users[userId].achievements ??= {}
|
||||||
|
}
|
||||||
|
return users[userId]
|
||||||
|
}
|
||||||
|
|
||||||
const getSeconds = () => new Date().getTime() / 1000
|
const getSeconds = () => new Date().getTime() / 1000
|
||||||
|
|
||||||
|
@ -65,33 +99,33 @@ const addAchievement = async (user, achievementName, say) => {
|
||||||
await say(`You earned the achievement ${achievements[achievementName].name}!`)
|
await say(`You earned the achievement ${achievements[achievementName].name}!`)
|
||||||
}
|
}
|
||||||
|
|
||||||
// I'm super not confident this will stay
|
const alwaysAccessible = () => true
|
||||||
// What's nice is that it centralizes
|
const adminOnly = userId => userId === slack.sageUserId
|
||||||
// * calling with event,say,words
|
|
||||||
// * checking for words[1]==='help'
|
|
||||||
const commands = new Map()
|
const commands = new Map()
|
||||||
const command = (commandNames, helpText, action, condition) => {
|
let commandHelpText = ''
|
||||||
|
const command = (commandNames, helpText, action, hidden = false, condition = alwaysAccessible) => {
|
||||||
|
if (!hidden) {
|
||||||
|
commandHelpText += `\n${commandNames.toString().replace(/,/g, ', ')} - ${helpText}\n`
|
||||||
|
}
|
||||||
commandNames.forEach(name => commands.set(name, {
|
commandNames.forEach(name => commands.set(name, {
|
||||||
|
commandNames,
|
||||||
helpText,
|
helpText,
|
||||||
action,
|
action,
|
||||||
condition: condition || (() => true)
|
condition,
|
||||||
|
hidden
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
command( // I don't like that command() is at the highest indentation level instead of the route name
|
command(
|
||||||
['!t', '!test'],
|
['!help', '!h'],
|
||||||
'HelpText: Testing out new routing functionality',
|
'List available commands',
|
||||||
async ({ event, say, words }) => {// This extra indendation sucks
|
async ({ say }) => await say('```' + commandHelpText + '```')
|
||||||
await say('This is the route acton') // Also the function can't go after command() - not initialized
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
slack.onMessage(async ({ event, say }) => {
|
slack.onMessage(async ({ event, say }) => {
|
||||||
const words = event?.text?.split(/\s+/) || []
|
const words = event?.text?.split(/\s+/) || []
|
||||||
const c = commands.get(words[0])
|
const c = commands.get(words[0])
|
||||||
if (event.user !== slack.sageUserId) {
|
|
||||||
return // Don't do anything while this is incubating
|
|
||||||
}
|
|
||||||
if (!c?.condition(event.user)) {
|
if (!c?.condition(event.user)) {
|
||||||
await say(`Command '${words[0]} not found'`)
|
await say(`Command '${words[0]} not found'`)
|
||||||
return
|
return
|
||||||
|
@ -102,21 +136,24 @@ slack.onMessage(async ({ event, say }) => {
|
||||||
}
|
}
|
||||||
await c.action({ event, say, words })
|
await c.action({ event, say, words })
|
||||||
})
|
})
|
||||||
// End of new command action
|
|
||||||
|
|
||||||
const listAchievements = async ({ event, say }) => {
|
command(
|
||||||
const user = getUser(event.user)
|
['!a', '!ach', '!achievements'],
|
||||||
|
'List your glorious achievements',
|
||||||
|
async ({ event, say }) => {
|
||||||
|
const user = getUser(event.user)
|
||||||
|
|
||||||
const achievementCount = Object.keys(user.achievements).length
|
const achievementCount = Object.keys(user.achievements).length
|
||||||
const prefix = `You have ${achievementCount} achievements!\n\n`
|
const prefix = `You have ${achievementCount} achievements!\n\n`
|
||||||
const mult = (Math.pow(1.01, achievementCount) - 1) * 100
|
const mult = (Math.pow(1.01, achievementCount) - 1) * 100
|
||||||
const list = Object.keys(user.achievements)
|
const list = Object.keys(user.achievements)
|
||||||
.map(name => achievements[name])
|
.map(name => achievements[name])
|
||||||
.map(({description, emoji, name}) => `:${emoji}: *${name}* - ${description}`)
|
.map(({description, emoji, name}) => `:${emoji}: *${name}* - ${description}`)
|
||||||
.join('\n')
|
.join('\n')
|
||||||
const postfix = achievementCount ? `\n\n_Achievements are boosting your CPS by ${mult.toPrecision(3)}%_` : ''
|
const postfix = achievementCount ? `\n\n_Achievements are boosting your CPS by ${mult.toPrecision(3)}%_` : ''
|
||||||
await say(prefix + list + postfix)
|
await say(prefix + list + postfix)
|
||||||
}
|
}
|
||||||
|
)
|
||||||
|
|
||||||
const getItemCps = (user, itemName) => {
|
const getItemCps = (user, itemName) => {
|
||||||
const achievements = Object.keys(user.achievements || {}).length
|
const achievements = Object.keys(user.achievements || {}).length
|
||||||
|
@ -143,7 +180,7 @@ const getCPS = userId => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const getCoins = userId => {
|
const getCoins = userId => {
|
||||||
const user = users[userId]
|
const user = getUser(userId)
|
||||||
const currentTime = getSeconds()
|
const currentTime = getSeconds()
|
||||||
const lastCheck = user.lastCheck || currentTime
|
const lastCheck = user.lastCheck || currentTime
|
||||||
const secondsPassed = currentTime - lastCheck
|
const secondsPassed = currentTime - lastCheck
|
||||||
|
@ -156,85 +193,84 @@ const getCoins = userId => {
|
||||||
return user.coins
|
return user.coins
|
||||||
}
|
}
|
||||||
|
|
||||||
const mineCoinHelp = 'Mine HVAC coins: `!coin` or `!c`'
|
command(['!cps'], 'Display your current Coins Per Second', async ({ event, say }) => {
|
||||||
const mineCoin = async ({ event, say, words }) => {
|
await say(`You are currently earning \`${commas(getCPS(event.user))}\` HVAC Coin per second.`)
|
||||||
const user = getUser(event.user)
|
})
|
||||||
if (words[1] === 'help') {
|
|
||||||
await say(mineCoinHelp)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
maybeNews(say)
|
|
||||||
const random = Math.random()
|
|
||||||
const c = getCoins(event.user)
|
|
||||||
const secondsOfCps = seconds => Math.floor(getCPS(event.user) * seconds)
|
|
||||||
let diff
|
|
||||||
let prefix
|
|
||||||
if (random > 0.9967) {
|
|
||||||
diff = 500 + Math.floor(c * 0.10) + secondsOfCps(60 * 30)
|
|
||||||
prefix = `:gem: You found a lucky gem worth ${commas(diff)} HVAC!\n`
|
|
||||||
await addAchievement(user, 'luckyGem', say)
|
|
||||||
await slack.messageSage(`${slack.ourUsers[event.user]} FOUND A LUCKY GEM COIN WORTH ${commas(diff)} HVAC!`)
|
|
||||||
} else if (random > 0.986) {
|
|
||||||
diff = 50 + Math.floor(c * 0.025) + secondsOfCps(60)
|
|
||||||
prefix = `:goldbrick: You found a lucky gold coin worth ${commas(diff)} HVAC!\n`
|
|
||||||
await slack.messageSage(`${slack.ourUsers[event.user]} found a lucky gold coin worth ${commas(diff)} HVAC!`)
|
|
||||||
await addAchievement(user, 'goldBrick', say)
|
|
||||||
} else if (random > 0.96) {
|
|
||||||
diff = 10 + Math.floor(c * 0.01) + secondsOfCps(10)
|
|
||||||
prefix = `:money_with_wings: You found a lucky green coin worth ${commas(diff)} HVAC!\n`
|
|
||||||
await addAchievement(user, 'greenCoin', say)
|
|
||||||
} else {
|
|
||||||
prefix = `You mined one HVAC.\n`
|
|
||||||
diff = 1
|
|
||||||
}
|
|
||||||
user.coins += diff
|
|
||||||
await say(`${prefix}You now have ${commas(user.coins)} HVAC coin` + (c !== 0 ? 's' : '') + '. Spend wisely.')
|
|
||||||
saveGame()
|
|
||||||
}
|
|
||||||
|
|
||||||
const gambleCoinHelp = 'Gamble away your HVAC: `!gamble` or `!g`\n'+
|
command(
|
||||||
'To use, say `!gamble coin_amount` or `!gamble all`'
|
['!c', '!coin', '!mine'],
|
||||||
const gambleCoin = async ({ event, say, words }) => {
|
'Mine HVAC coins',
|
||||||
if (words[1] === 'help') {
|
async ({ event, say }) => {
|
||||||
await say(gambleCoinHelp)
|
const user = getUser(event.user)
|
||||||
return
|
maybeNews(say)
|
||||||
}
|
const random = Math.random()
|
||||||
if (!users[event.user]) {
|
const c = getCoins(event.user)
|
||||||
users[event.user] = {
|
const secondsOfCps = seconds => Math.floor(getCPS(event.user) * seconds)
|
||||||
coins: 0
|
let diff
|
||||||
|
let prefix
|
||||||
|
if (random > 0.9967) {
|
||||||
|
diff = 500 + Math.floor(c * 0.10) + secondsOfCps(60 * 30)
|
||||||
|
prefix = `:gem: You found a lucky gem worth ${commas(diff)} HVAC!\n`
|
||||||
|
await addAchievement(user, 'luckyGem', say)
|
||||||
|
await slack.messageSage(`${slack.ourUsers[event.user]} FOUND A LUCKY GEM COIN WORTH ${commas(diff)} HVAC!`)
|
||||||
|
} else if (random > 0.986) {
|
||||||
|
diff = 50 + Math.floor(c * 0.025) + secondsOfCps(60)
|
||||||
|
prefix = `:goldbrick: You found a lucky gold coin worth ${commas(diff)} HVAC!\n`
|
||||||
|
await slack.messageSage(`${slack.ourUsers[event.user]} found a lucky gold coin worth ${commas(diff)} HVAC!`)
|
||||||
|
await addAchievement(user, 'goldBrick', say)
|
||||||
|
} else if (random > 0.96) {
|
||||||
|
diff = 10 + Math.floor(c * 0.01) + secondsOfCps(10)
|
||||||
|
prefix = `:money_with_wings: You found a lucky green coin worth ${commas(diff)} HVAC!\n`
|
||||||
|
await addAchievement(user, 'greenCoin', say)
|
||||||
|
} else {
|
||||||
|
prefix = `You mined one HVAC.\n`
|
||||||
|
diff = 1
|
||||||
|
}
|
||||||
|
user.coins += diff
|
||||||
|
await say(`${prefix}You now have ${commas(user.coins)} HVAC coin` + (c !== 0 ? 's' : '') + '. Spend wisely.')
|
||||||
|
saveGame()
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
let n
|
|
||||||
let currentCoins = getCoins(event.user)
|
command(
|
||||||
if (words[1].toLowerCase() === 'all') {
|
['!g', '!gamble'],
|
||||||
if (currentCoins === 0) {
|
'Gamble away your HVAC\n' +
|
||||||
await say('You don\'t have any coins!')
|
' To use, say \'gamble coin_amount\' or \'!gamble all\'',
|
||||||
return
|
async ({ event, say, words }) => {
|
||||||
|
getCoins(event.user) // Update coin counts
|
||||||
|
const user = getUser(event.user)
|
||||||
|
|
||||||
|
let n
|
||||||
|
if (words[1].toLowerCase() === 'all') {
|
||||||
|
if (user.coins === 0) {
|
||||||
|
await say('You don\'t have any coins!')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
n = user.coins
|
||||||
|
} else {
|
||||||
|
n = parseInt(words[1])
|
||||||
|
}
|
||||||
|
if (!n || n < 0) {
|
||||||
|
await say(`Invalid number '${n}'`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (user.coins < n) {
|
||||||
|
await say(`You don\'t have that many coins! You have ${commas(user.coins)}.`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
user.coins -= n
|
||||||
|
let outcome
|
||||||
|
if (Math.random() > 0.5) {
|
||||||
|
user.coins += (2 * n)
|
||||||
|
outcome = 'won'
|
||||||
|
} else {
|
||||||
|
outcome = 'lost'
|
||||||
|
}
|
||||||
|
console.log(`They ${outcome}`)
|
||||||
|
await say(`You bet ${commas(n)} coins and ${outcome}! You now have ${commas(user.coins)}.`)
|
||||||
|
saveGame()
|
||||||
}
|
}
|
||||||
n = currentCoins
|
)
|
||||||
} else {
|
|
||||||
n = parseInt(event.text.substring(7))
|
|
||||||
}
|
|
||||||
if (!n || n < 0) {
|
|
||||||
await say(`Invalid number '${n}'`)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (currentCoins < n) {
|
|
||||||
await say(`You don\'t have that many coins! You have ${commas(currentCoins)}.`)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
users[event.user].coins -= n
|
|
||||||
let outcome
|
|
||||||
if (Math.random() > 0.5) {
|
|
||||||
users[event.user].coins += (2 * n)
|
|
||||||
outcome = 'won'
|
|
||||||
} else {
|
|
||||||
outcome = 'lost'
|
|
||||||
}
|
|
||||||
console.log(`They ${outcome}`)
|
|
||||||
await say(`You bet ${commas(n)} coins and ${outcome}! You now have ${commas(users[event.user].coins)}.`)
|
|
||||||
saveGame()
|
|
||||||
}
|
|
||||||
|
|
||||||
const calculateCost = ({ itemName, user, quantity = 1 }) => {
|
const calculateCost = ({ itemName, user, quantity = 1 }) => {
|
||||||
let currentlyOwned = user.items[itemName] || 0
|
let currentlyOwned = user.items[itemName] || 0
|
||||||
|
@ -246,242 +282,228 @@ const calculateCost = ({ itemName, user, quantity = 1 }) => {
|
||||||
return realCost
|
return realCost
|
||||||
}
|
}
|
||||||
|
|
||||||
const buyNftHelp = 'Acquire high-quality art: `!buynft`\n'+
|
command(
|
||||||
'To use, say `!buynft nft_name`'
|
['!buynft', '!bn'],
|
||||||
const buyNft = async ({ event, say, words }) => {
|
'Acquire high-quality art\n' +
|
||||||
if (words[1] === 'help') {
|
' To use, say \'!buynft nft_name\'',
|
||||||
await say(buyNftHelp)
|
async ({ event, say, words }) => {
|
||||||
return
|
const nft = nfts.find(n => n.name.toLowerCase() === words[1])
|
||||||
}
|
if (!nft) {
|
||||||
const nft = nfts.find(n => n.name.toLowerCase() === words[1])
|
const suffix = words[1]?.match(/[^a-z0-9]/i) ? '. And I\'m unhackable, so cut it out.' : ''
|
||||||
if (!nft) {
|
await say('No NFT with that name found' + suffix)
|
||||||
const suffix = words[1].match(/[^a-z0-9]/i) ? '. And I\'m unhackable, so cut it out.' : ''
|
return
|
||||||
await say('No NFT with that name found' + suffix)
|
}
|
||||||
return
|
if (nft.owner) {
|
||||||
}
|
await say('Someone already owns that NFT!')
|
||||||
if (nft.owner) {
|
return
|
||||||
await say('Someone already owns that NFT!')
|
}
|
||||||
return
|
const c = getCoins(event.user)
|
||||||
}
|
if (c < nft.price) {
|
||||||
const c = getCoins(event.user)
|
await say('You don\'t have enough coin for this nft')
|
||||||
if (c < nft.price) {
|
return
|
||||||
await say('You don\'t have enough coin for this nft')
|
}
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
users[event.user].coins -= nft.price
|
users[event.user].coins -= nft.price
|
||||||
nft.owner = event.user
|
nft.owner = event.user
|
||||||
saveGame()
|
saveGame()
|
||||||
await say('You bought ' + nft.name + '!')
|
await say('You bought ' + nft.name + '!')
|
||||||
}
|
|
||||||
|
|
||||||
const getUser = userId => {
|
|
||||||
if (!users[userId]) {
|
|
||||||
users[userId] = {
|
|
||||||
coins: 0,
|
|
||||||
items: {},
|
|
||||||
upgrades: {},
|
|
||||||
achievements: {}
|
|
||||||
}
|
}
|
||||||
} else {
|
)
|
||||||
users[userId].items ??= {}
|
|
||||||
users[userId].upgrades ??= {}
|
|
||||||
users[userId].achievements ??= {}
|
|
||||||
}
|
|
||||||
return users[userId]
|
|
||||||
}
|
|
||||||
|
|
||||||
const buyUpgradeHelp = 'Improve the performance of your HVAC-generators. `!upgrade` or `!u`\n'+
|
command(
|
||||||
'Say `!upgrade` to list available upgrades, or `!upgrade upgrade_name` to purchase.'
|
['!upgrade', '!u'],
|
||||||
const buyUpgrade = async ({ userId, say, words }) => {
|
'Improve the performance of your HVAC-generators.\n' +
|
||||||
if (words[1] === 'help') {
|
' Say \'!upgrade\' to list available upgrades, or \'!upgrade upgrade_name\' to purchase.',
|
||||||
await say(buyUpgradeHelp)
|
async ({ userId, say, words }) => {
|
||||||
return
|
const user = getUser(userId)
|
||||||
}
|
const upgradeName = words[1]
|
||||||
const user = getUser(userId)
|
if (!upgradeName) {
|
||||||
const upgradeName = words[1]
|
await say(upgradeText(user))
|
||||||
if (!upgradeName) {
|
return
|
||||||
await say(upgradeText(user))
|
}
|
||||||
return
|
const upgrade = upgrades[upgradeName]
|
||||||
}
|
if (!upgrade) {
|
||||||
const upgrade = upgrades[upgradeName]
|
await say('An upgrade with that name does not exist!')
|
||||||
if (!upgrade) {
|
return
|
||||||
await say('An upgrade with that name does not exist!')
|
}
|
||||||
return
|
if (!user.upgrades[upgrade.type]) {
|
||||||
}
|
user.upgrades[upgrade.type] = []
|
||||||
if (!user.upgrades[upgrade.type]) {
|
}
|
||||||
user.upgrades[upgrade.type] = []
|
if (hasUpgrade(user, upgrade, upgradeName)) {
|
||||||
}
|
await say('You already have that upgrade!')
|
||||||
if (hasUpgrade(user, upgrade, upgradeName)) {
|
return
|
||||||
await say('You already have that upgrade!')
|
}
|
||||||
return
|
if (!upgrade.condition(user)) {
|
||||||
}
|
await say('That item does not exist!')
|
||||||
if (!upgrade.condition(user)) {
|
return
|
||||||
await say('That item does not exist!')
|
}
|
||||||
return
|
const c = getCoins(userId)
|
||||||
}
|
if (c < upgrade.cost) {
|
||||||
const c = getCoins(userId)
|
await say(`You don't have enough coins! You have ${commas(c)}, but you need ${commas(upgrade.cost)}`)
|
||||||
if (c < upgrade.cost) {
|
return
|
||||||
await say(`You don't have enough coins! You have ${commas(c)}, but you need ${commas(upgrade.cost)}`)
|
}
|
||||||
return
|
user.coins -= upgrade.cost
|
||||||
}
|
user.upgrades[upgrade.type].push(upgradeName)
|
||||||
user.coins -= upgrade.cost
|
await saveGame()
|
||||||
user.upgrades[upgrade.type].push(upgradeName)
|
await say(`You bought ${upgradeName}!`)
|
||||||
await saveGame()
|
|
||||||
await say(`You bought ${upgradeName}!`)
|
|
||||||
}
|
|
||||||
|
|
||||||
const buyItemHelp = 'Buy new items to earn HVAC with: `!buy` or `!b`\n'+
|
|
||||||
'Use `!buy` to list available items, and `!buy item_name optional_quantity` to get \'em.'
|
|
||||||
const buyItem = async ({ event, say, words }) => {
|
|
||||||
if (words[1] === 'help') {
|
|
||||||
await say(buyItemHelp)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
const user = getUser(event.user)
|
|
||||||
const buying = words[1]
|
|
||||||
const quantity = parseInt(words[2] || '1')
|
|
||||||
if (!quantity || quantity < 1) {
|
|
||||||
await say('Quantity must be a positive integer')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
setHighestCoins(event.user)
|
|
||||||
if (!buying) {
|
|
||||||
const highestCoins = user.highestEver || user.coins || 1
|
|
||||||
if (buyableItems.quade.baseCost < highestCoins * 100) {
|
|
||||||
await addAchievement(user, 'seeTheQuade', say)
|
|
||||||
}
|
}
|
||||||
await say(helpText(highestCoins, user))
|
)
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const buyable = buyableItems[buying]
|
command(
|
||||||
if (!buyable) {
|
['!buy', '!b'],
|
||||||
await say('That item does not exist!')
|
'Buy new items to earn HVAC with\n' +
|
||||||
return
|
' Use without arguments to list all available items.\n' +
|
||||||
}
|
' Say \'!buy item_name optional_quantity\' to make your purchase.',
|
||||||
const realCost = calculateCost({itemName: buying, user, quantity})
|
async ({ event, say, words }) => {
|
||||||
const currentCoins = getCoins(event.user)
|
const user = getUser(event.user)
|
||||||
if (currentCoins < realCost) {
|
const buying = words[1]
|
||||||
await say(`You don't have enough coins! You have ${commas(currentCoins)}, but you need ${commas(realCost)}`)
|
const quantity = parseInt(words[2] || '1')
|
||||||
return
|
if (!quantity || quantity < 1) {
|
||||||
}
|
await say('Quantity must be a positive integer')
|
||||||
user.coins -= realCost
|
return
|
||||||
user.items[buying] = user.items[buying] || 0
|
}
|
||||||
user.items[buying] += quantity
|
setHighestCoins(event.user)
|
||||||
if (quantity === 1) {
|
if (!buying) {
|
||||||
await say(`You bought one ${buying}`)
|
const highestCoins = user.highestEver || user.coins || 1
|
||||||
} else {
|
if (buyableItems.quade.baseCost < highestCoins * 100) {
|
||||||
await say(`You bought ${quantity} ${buying}`)
|
await addAchievement(user, 'seeTheQuade', say)
|
||||||
}
|
}
|
||||||
saveGame()
|
await say(helpText(highestCoins, user))
|
||||||
}
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const getCoinCommand = async ({ target, say }) => {
|
const buyable = buyableItems[buying]
|
||||||
if (!target?.startsWith('<@') || !target.endsWith('>')) {
|
if (!buyable) {
|
||||||
await say('Target must be a valid @')
|
await say('That item does not exist!')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const targetId = target.substring(2, target.length - 1)
|
const realCost = calculateCost({itemName: buying, user, quantity})
|
||||||
if (targetId !== 'U0344TFA7HQ') {
|
const currentCoins = getCoins(event.user)
|
||||||
const user = getUser(targetId)
|
if (currentCoins < realCost) {
|
||||||
await say(`<@${targetId}> has ${commas(user.coins)} HVAC.`)
|
await say(`You don't have enough coins! You have ${commas(currentCoins)}, but you need ${commas(realCost)}`)
|
||||||
} else {
|
return
|
||||||
const members = (await slack.app.client.conversations.members({channel: slack.temperatureChannelId})).members
|
}
|
||||||
const humanMembers = members.filter(name => name.length === 11)
|
user.coins -= realCost
|
||||||
await say(`Hvacker owns ${humanMembers.length} souls.`)
|
user.items[buying] = user.items[buying] || 0
|
||||||
}
|
user.items[buying] += quantity
|
||||||
}
|
if (quantity === 1) {
|
||||||
|
await say(`You bought one ${buying}`)
|
||||||
const gift = async ({ userId, words, say }) => {
|
} else {
|
||||||
const user = getUser(userId)
|
await say(`You bought ${quantity} ${buying}`)
|
||||||
const [, target, amountText] = words
|
}
|
||||||
const amount = amountText === 'all' ? (getCoins(userId)) : parseInt(amountText)
|
saveGame()
|
||||||
if (!amount || amount < 0) {
|
|
||||||
await say('Amount must be a positive integer!')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (!target?.startsWith('<@') || !target.endsWith('>')) {
|
|
||||||
await say('Target must be a valid @')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (user.coins < amount) {
|
|
||||||
await say(`You don't have that many coins! You have ${commas(user.coins)} HVAC.`)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
const targetId = target.substring(2, target.length - 1)
|
|
||||||
const targetUser = getUser(targetId)
|
|
||||||
user.coins -= amount
|
|
||||||
targetUser.coins += amount
|
|
||||||
await say(`Gifted ${commas(amount)} HVAC to <@${targetId}>`)
|
|
||||||
}
|
|
||||||
|
|
||||||
slack.onMessage(async ({ event, say }) => {
|
|
||||||
// if (event?.text?.startsWith('!') && event.user !== slack.sageUserId) {
|
|
||||||
// await say('Hvacker is taking a quick nap.')
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
const words = event?.text?.split(/\s+/) || []
|
|
||||||
console.log(event?.text, 'by', slack?.ourUsers[event?.user], 'at', new Date().toLocaleTimeString())
|
|
||||||
switch (words[0]) {
|
|
||||||
case '!c':
|
|
||||||
case '!coin':
|
|
||||||
return mineCoin({ event, say, words })
|
|
||||||
case '!g':
|
|
||||||
case '!gamble':
|
|
||||||
return gambleCoin({ event, say, words })
|
|
||||||
case '!b':
|
|
||||||
case '!buy': {
|
|
||||||
return buyItem({event, say, words})
|
|
||||||
}
|
}
|
||||||
case '!u':
|
)
|
||||||
case '!upgrade':
|
|
||||||
return buyUpgrade({userId: event.user, say, words})
|
|
||||||
case '!cps':
|
command(
|
||||||
return say(`You are currently earning \`${commas(getCPS(event.user))}\` HVAC Coin per second.`)
|
['!check', '!ch'],
|
||||||
case '!gift':
|
'Check how many coins another player has',
|
||||||
case '!give':
|
async ({ target, say }) => {
|
||||||
return gift({userId: event.user, words, say})
|
if (!target?.startsWith('<@') || !target.endsWith('>')) {
|
||||||
case '!check':
|
await say('Target must be a valid @')
|
||||||
return getCoinCommand({target: words[1], say})
|
return
|
||||||
case '!s':
|
}
|
||||||
case '!status':
|
const targetId = target.substring(2, target.length - 1)
|
||||||
return say(
|
if (targetId !== 'U0344TFA7HQ') {
|
||||||
|
const user = getUser(targetId)
|
||||||
|
await say(`<@${targetId}> has ${commas(user.coins)} HVAC.`)
|
||||||
|
} else {
|
||||||
|
const members = (await slack.app.client.conversations.members({channel: slack.temperatureChannelId})).members
|
||||||
|
const humanMembers = members.filter(name => name.length === 11)
|
||||||
|
await say(`Hvacker owns ${humanMembers.length} souls.`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
command(
|
||||||
|
['!gift', '!give', '!gi'],
|
||||||
|
'Donate coins to a fellow player\n' +
|
||||||
|
' Send coins by saying \'!gift @player coin_amount\'',
|
||||||
|
async ({ event, words, say }) => {
|
||||||
|
const userId = event.user
|
||||||
|
const user = getUser(userId)
|
||||||
|
const [, target, amountText] = words
|
||||||
|
const amount = amountText === 'all' ? (getCoins(userId)) : parseInt(amountText)
|
||||||
|
if (!amount || amount < 0) {
|
||||||
|
await say('Amount must be a positive integer!')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (!target?.startsWith('<@') || !target.endsWith('>')) {
|
||||||
|
await say('Target must be a valid @')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (user.coins < amount) {
|
||||||
|
await say(`You don't have that many coins! You have ${commas(user.coins)} HVAC.`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const targetId = target.substring(2, target.length - 1)
|
||||||
|
const targetUser = getUser(targetId)
|
||||||
|
user.coins -= amount
|
||||||
|
targetUser.coins += amount
|
||||||
|
await say(`Gifted ${commas(amount)} HVAC to <@${targetId}>`)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
command(
|
||||||
|
['!status', '!s'],
|
||||||
|
'Print your current CPS, HVAC balance, and owned items',
|
||||||
|
async ({ event, say }) => {
|
||||||
|
await say(
|
||||||
`You are currently earning \`${commas(getCPS(event.user))}\` HVAC Coin per second.\n\n` +
|
`You are currently earning \`${commas(getCPS(event.user))}\` HVAC Coin per second.\n\n` +
|
||||||
`You currently have ${commas(getCoins(event.user))} HVAC Coins\n\n` +
|
`You currently have ${commas(getCoins(event.user))} HVAC Coins\n\n` +
|
||||||
`${collection(event.user)}\n\n`
|
`${collection(event.user)}\n\n`
|
||||||
)
|
)
|
||||||
case '!a':
|
}
|
||||||
return listAchievements({ event, say })
|
)
|
||||||
case '!nfts':
|
|
||||||
|
command(
|
||||||
|
['!nfts', '!nft', '!n'],
|
||||||
|
'Show NFTs in the museum\n' +
|
||||||
|
' Call with no arguments to list all NFTs, or \'!nft nft_name\' to show a particular one.',
|
||||||
|
async ({ say, words }) => {
|
||||||
const owner = nft => `Owner: *${slack.ourUsers[nft.owner] || 'NONE'}*`
|
const owner = nft => `Owner: *${slack.ourUsers[nft.owner] || 'NONE'}*`
|
||||||
const nftDisplay = nft => `_"${nft.name}"_\n\n${nft.description}\n\n${commas(nft.price)} HVAC.\n\n${nft.picture}\n\n${owner(nft)}`
|
const nftDisplay = nft => `_"${nft.name}"_\n\n${nft.description}\n\n${commas(nft.price)} HVAC.\n\n${nft.picture}\n\n${owner(nft)}`
|
||||||
|
|
||||||
const filter = words[1] ? nft => words[1]?.toLowerCase() === nft.name : null
|
const filter = words[1] ? nft => words[1]?.toLowerCase() === nft.name : null
|
||||||
|
|
||||||
return say(nfts
|
await say(nfts
|
||||||
.filter(filter || (() => true))
|
.filter(filter || (() => true))
|
||||||
.map(nftDisplay)
|
.map(nftDisplay)
|
||||||
.join('\n-------------------------\n') || (filter ? 'No NFTs with that name exist' : 'No NFTs currently exist.')
|
.join('\n-------------------------\n') || (filter ? 'No NFTs with that name exist' : 'No NFTs currently exist.')
|
||||||
)
|
)
|
||||||
case '!lb':
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
command(
|
||||||
|
['!leaderboard', '!lb'],
|
||||||
|
'Show the top HVAC-earners, ranked by CPS',
|
||||||
|
async ({ event, say }) => {
|
||||||
const user = getUser(event.user)
|
const user = getUser(event.user)
|
||||||
return say('```' +
|
await say('```' +
|
||||||
Object.entries(users)
|
Object.entries(users)
|
||||||
.filter(([id]) => getCPS(id) !== 0)
|
.filter(([id]) => getCPS(id) !== 0)
|
||||||
.sort(([id], [id2]) => getCPS(id) > getCPS(id2) ? -1 : 1)
|
.sort(([id], [id2]) => getCPS(id) > getCPS(id2) ? -1 : 1)
|
||||||
.map(([id]) => `${slack.ourUsers[id] || '???'} - ${commas(getCPS(id))} CPS - ${commas(getCoins(id))} HVAC`)
|
.map(([id]) => `${slack.ourUsers[id] || '???'} - ${commas(getCPS(id))} CPS - ${commas(getCoins(id))} HVAC`)
|
||||||
.join('\n') + '```').then(() => addAchievement(user, 'leaderBoardViewer', say))
|
.join('\n') + '```').then(() => addAchievement(user, 'leaderBoardViewer', say))
|
||||||
case '!buynft':
|
}
|
||||||
return buyNft({event, say, words})
|
)
|
||||||
case '!ligma':
|
|
||||||
return say(':hvacker_angery:')
|
command(['!pog'], 'Displays a poggers hvacker', async ({ say }) => {
|
||||||
case '!pog':
|
await say('<https://i.imgur.com/XCg7WDz.png|poggers>')
|
||||||
return say('<https://i.imgur.com/XCg7WDz.png|poggers>')
|
}, true)
|
||||||
}
|
|
||||||
if (event.user === slack.sageUserId) {
|
command(['!ligma'], 'GRRRR', async ({ say }) => {
|
||||||
const firstWord = words[0]
|
await say(':hvacker_angery:')
|
||||||
if (firstWord === '!addnft') {
|
}, true)
|
||||||
|
|
||||||
|
command(
|
||||||
|
['!addnft'],
|
||||||
|
'Arguments 1 and 2 should be on the first line as name (one word!) and integer price.\n' +
|
||||||
|
' The second line should be the description of the pieces\n' +
|
||||||
|
' the picture is everything after the second line',
|
||||||
|
async ({ event }) => {
|
||||||
const [, name, price] = event.text.substring(0, event.text.indexOf('\n')).split(' ')
|
const [, name, price] = event.text.substring(0, event.text.indexOf('\n')).split(' ')
|
||||||
const rest = event.text.substring(event.text.indexOf('\n') + 1)
|
const rest = event.text.substring(event.text.indexOf('\n') + 1)
|
||||||
const desc = rest.substring(0, rest.indexOf('\n'))
|
const desc = rest.substring(0, rest.indexOf('\n'))
|
||||||
|
@ -496,19 +518,17 @@ slack.onMessage(async ({ event, say }) => {
|
||||||
nfts.push(newNft)
|
nfts.push(newNft)
|
||||||
console.log('addedNft', newNft)
|
console.log('addedNft', newNft)
|
||||||
return saveGame()
|
return saveGame()
|
||||||
} else if (firstWord === '!s') {
|
}, true, adminOnly)
|
||||||
|
|
||||||
|
command(
|
||||||
|
['!ss'],
|
||||||
|
'Show the status for another player: !ss @player',
|
||||||
|
async ({ words, say }) => {
|
||||||
const target = words[1]
|
const target = words[1]
|
||||||
const targetId = target.substring(2, target.length - 1)
|
const targetId = target.substring(2, target.length - 1)
|
||||||
maybeNews(say)
|
await say(
|
||||||
return say(
|
|
||||||
`${target} are currently earning \`${commas(getCPS(targetId))}\` HVAC Coin per second.\n\n` +
|
`${target} are currently earning \`${commas(getCPS(targetId))}\` HVAC Coin per second.\n\n` +
|
||||||
`They currently have ${commas(getCoins(targetId))} HVAC Coins\n\n` +
|
`They currently have ${commas(getCoins(targetId))} HVAC Coins\n\n` +
|
||||||
`${collection(targetId)}\n\n`
|
`${collection(targetId)}\n\n`
|
||||||
)
|
)
|
||||||
} else if (firstWord === '!updatenft') {
|
}, true, adminOnly)
|
||||||
const nft = nfts.find(n => n.name.toLowerCase() === 'quackers')
|
|
||||||
nft.price *= 100
|
|
||||||
return saveGame()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
Loading…
Reference in New Issue