Skip to content

Commit

Permalink
You can now only give one cake every 2 hours
Browse files Browse the repository at this point in the history
  • Loading branch information
RDIL committed Feb 6, 2021
1 parent 00f5850 commit 9d2ce11
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 11 deletions.
30 changes: 24 additions & 6 deletions src/commands/internal/cake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@
*/

import type { User } from "discord.js"
import { addUserById, getLeaderboard, inMemoryDB } from "../../data/database"
import {
addUserById,
commitCooldown,
getCooldowns,
getLeaderboard,
inMemoryDB,
removeCooldown,
} from "../../data/database"
import Command from "../commands"
import { makeError } from "../../util/constants"
import createEmbed from "../../util/embeds"
Expand All @@ -35,7 +42,9 @@ function give(to: User): void {
addUserById(to.id)
}

commitCooldown(to.id)
inMemoryDB.users[to.id].cakeCount += 1
setInterval(() => removeCooldown(to.id), 7_200_000)
}

const Cake: Command = {
Expand Down Expand Up @@ -69,6 +78,13 @@ const Cake: Command = {
return
}

if (getCooldowns().includes(message.author.id)) {
message.channel.send(
makeError("You can only give a cake every *2 hours*!")
)
return
}

give(user)

message.channel.send(
Expand All @@ -90,11 +106,13 @@ const Cake: Command = {
createEmbed(
"Leaderboard",
"The people with the most cakes!",
getLeaderboard().reverse().map((ent) => ({
name: ent.name,
value: `${ent.cakes} cakes.`,
inline: false,
}))
getLeaderboard()
.reverse()
.map((ent) => ({
name: ent.name,
value: `${ent.cakes} cakes.`,
inline: false,
}))
)
)
}
Expand Down
14 changes: 14 additions & 0 deletions src/data/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import random from "random"
import { usersWithTicketsOpen } from "./remote/runtime-data"
import type { LeaderboardEntry, Schema } from "./types"
import { error } from "../util/logging"
import { removeIf } from "../util/array-polyfills"

/* eslint-disable promise/no-nesting */

Expand All @@ -32,6 +33,7 @@ export const dbPath = `${process.cwd()}${sep}database.json`
* @see commitLeaderboardData
*/
let leaderboard: LeaderboardEntry[] = []
let cakeCooldowns: string[] = []

/**
* Loads the database.
Expand Down Expand Up @@ -166,3 +168,15 @@ export function commitLeaderboardData(dat: LeaderboardEntry[]): void {
export function getLeaderboard(): LeaderboardEntry[] {
return leaderboard
}

export function commitCooldown(userId: string): void {
cakeCooldowns.push(userId)
}

export function removeCooldown(userId: string): void {
cakeCooldowns = removeIf<string>(cakeCooldowns, (item) => item === userId)
}

export function getCooldowns(): string[] {
return cakeCooldowns
}
2 changes: 1 addition & 1 deletion src/data/remote/geocoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export interface LookupResult {
}

// Distance function taken from
// http://www.movable-type.co.uk/scripts/latlong.html
// https://www.movable-type.co.uk/scripts/latlong.html
export function distanceFunc(x: NumberPoint, y: NumberPoint): number {
function toRadians(num: number): number {
return (num * Math.PI) / 180
Expand Down
35 changes: 35 additions & 0 deletions src/util/array-polyfills.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Cakebot - A fun and helpful Discord bot
* Copyright (C) 2021-current year Reece Dunham
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

// 16 bit integer limit
export const REMOVAL_MARKER = 32_766

/**
* Remove an object from the array if a condition is met.
*/
export function removeIf<T>(array: T[], cond: (item: T) => boolean): T[] {
array.forEach((value, index) => {
if (cond.call(array, value)) {
// @ts-ignore
array[index] = REMOVAL_MARKER
}
})

// @ts-ignore
return array.filter((value) => value !== REMOVAL_MARKER, array)
}
6 changes: 2 additions & 4 deletions src/util/scheduling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ function callback(): void {
export function scheduleTasks(botClient: Client): void {
setInterval(() => callback(), 150_000)
setInterval(() => {
recalculateLeaderboard(botClient)
.catch((e) => warn(e))
recalculateLeaderboard(botClient).catch((e) => warn(e))
}, 900_000)
// also first time
recalculateLeaderboard(botClient)
.catch((e) => warn(e))
recalculateLeaderboard(botClient).catch((e) => warn(e))
}

export async function recalculateLeaderboard(botClient: Client): Promise<void> {
Expand Down

0 comments on commit 9d2ce11

Please sign in to comment.