Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from gdsc-konkuk/update/burrito-to-goose
Browse files Browse the repository at this point in the history
Update/burrito to goose
  • Loading branch information
ekgns33 committed Oct 24, 2023
2 parents ba7432d + 6005396 commit 93f0235
Show file tree
Hide file tree
Showing 16 changed files with 178 additions and 179 deletions.
4 changes: 2 additions & 2 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Route from './Route';
import {
getScoreBoard,
getUserStats,
givenBurritosToday,
givenGeeseToday,
getUserScore,
} from '../middleware';
import config from '../config';
Expand Down Expand Up @@ -46,7 +46,7 @@ Route.add({
};
}

const score = await givenBurritosToday(user);
const score = await givenGeeseToday(user);

const data = {
error: false,
Expand Down
58 changes: 28 additions & 30 deletions src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import config from './config';
import BurritoStore from './store/BurritoStore';
import GooseStore from './store/GooseStore';
import LocalStore from './store/LocalStore';
import { parseMessage } from './lib/parseMessage';
import { validBotMention, validMessage } from './lib/validator';
Expand All @@ -26,73 +26,71 @@ interface Updates {
}
const emojis: Array<Emojis> = [];

const incEmojis = emojiInc.split(',').map((emoji) => emoji.trim());
incEmojis.forEach((emoji: string) => emojis.push({ type: 'inc', emoji }));
if (emojiInc) {
const incEmojis = emojiInc.split(',').map((emoji) => emoji.trim());
incEmojis.forEach((emoji: string) => emojis.push({ type: 'inc', emoji }));
}

if (!disableEmojiDec) {
if (!disableEmojiDec && emojiDec) {
const decEmojis = emojiDec.split(',').map((emoji) => emoji.trim());
decEmojis.forEach((emoji: string) => emojis.push({ type: 'dec', emoji }));
}

const giveBurritos = async (giver: string, updates: Updates[]) =>
const giveGeese = async (giver: string, updates: Updates[]) =>
updates.reduce(
async (prev: any, burrito) =>
async (prev: any, goose) =>
prev.then(async () => {
if (burrito.type === 'inc') {
await BurritoStore.giveBurrito(burrito.username, giver);
} else if (burrito.type === 'dec') {
await BurritoStore.takeAwayBurrito(burrito.username, giver);
if (goose.type === 'inc') {
await GooseStore.giveGoose(goose.username, giver);
} else if (goose.type === 'dec') {
await GooseStore.takeAwayGoose(goose.username, giver);
}
}),
Promise.resolve()
);

const notifyUser = (user: string, message: string) => Wbc.sendDM(user, message);

const handleBurritos = async (giver: string, updates: Updates[]) => {
const handleGeese = async (giver: string, updates: Updates[]) => {
if (enableDecrement) {
const burritos = await BurritoStore.givenBurritosToday(giver, 'from');
const diff = dailyCap - burritos;
const geese = await GooseStore.givenGeeseToday(giver, 'from');
const diff = dailyCap - geese;
if (updates.length > diff) {
notifyUser(
giver,
`You are trying to give away ${updates.length} burritos, but you only have ${diff} burritos left today!`
`You are trying to give away ${updates.length} geese, but you only have ${diff} geese left today!`
);
return false;
}
if (burritos >= dailyCap) {
if (geese >= dailyCap) {
return false;
}
await giveBurritos(giver, updates);
await giveGeese(giver, updates);
} else {
const givenBurritos = await BurritoStore.givenToday(giver, 'from', 'inc');
const givenRottenBurritos = await BurritoStore.givenToday(
giver,
'from',
'dec'
);
const givenGeese = await GooseStore.givenToday(giver, 'from', 'inc');
const givenRottenGeese = await GooseStore.givenToday(giver, 'from', 'dec');
const incUpdates = updates.filter((x) => x.type === 'inc');
const decUpdates = updates.filter((x) => x.type === 'dec');
const diffInc = dailyCap - givenBurritos;
const diffDec = dailyDecCap - givenRottenBurritos;
const diffInc = dailyCap - givenGeese;
const diffDec = dailyDecCap - givenRottenGeese;
if (incUpdates.length) {
if (incUpdates.length > diffInc) {
notifyUser(
giver,
`You are trying to give away ${updates.length} burritos, but you only have ${diffInc} burritos left today!`
`You are trying to give away ${updates.length} geese, but you only have ${diffInc} geese left today!`
);
} else {
await giveBurritos(giver, incUpdates);
await giveGeese(giver, incUpdates);
}
}
if (decUpdates.length) {
if (decUpdates.length > diffDec) {
notifyUser(
giver,
`You are trying to give away ${updates.length} rottenburritos, but you only have ${diffDec} rottenburritos left today!`
`You are trying to give away ${updates.length} rottengeese, but you only have ${diffDec} rottengeese left today!`
);
} else {
await giveBurritos(giver, decUpdates);
await giveGeese(giver, decUpdates);
}
}
}
Expand All @@ -109,12 +107,12 @@ const start = () => {
if (result) {
const { giver, updates } = result;
if (updates.length) {
await handleBurritos(giver, updates);
await handleGeese(giver, updates);
}
}
}
}
});
};

export { handleBurritos, notifyUser, start };
export { handleGeese, notifyUser, start };
41 changes: 21 additions & 20 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const isFalse = (input: string) =>
const isTrue = (input: string) =>
input === 'true' || input === 'yes' || input === '1';

export function fixEmoji(input) {
export function fixEmoji(input): string {
if (!input) return '';
let inputFix = input;
if (!input.startsWith(':')) inputFix = `:${inputFix}`;
if (!input.endsWith(':')) inputFix = `${inputFix}:`;
Expand All @@ -41,7 +42,7 @@ const config = {
production: {
db: {
db_driver: process.env.DATABASE_DRIVER || 'file',
db_fileName: 'burrito-prod.db',
db_fileName: 'goose-prod.db',
db_path: process.env.DATABASE_PATH || `${root}data/`,
db_url:
process.env.DATABASE_DRIVER === 'mongodb'
Expand All @@ -56,21 +57,21 @@ const config = {
`${process.env.MONGODB_URL}/${process.env.MONGODB_DATABASE}`,
},
slack: {
bot_name: process.env.BOT_NAME || 'heyburrito',
bot_name: process.env.BOT_NAME || 'heygoose',
api_token: mustHave('SLACK_API_TOKEN'),
emojiInc: fixEmoji(process.env.SLACK_EMOJI_INC || ':burrito:'),
emojiDec: fixEmoji(process.env.SLACK_EMOJI_DEC || ':rottenburrito:'),
emojiInc: fixEmoji(process.env.SLACK_EMOJI_INC || ':duck:'),
emojiDec: fixEmoji(process.env.SLACK_EMOJI_DEC),
disableEmojiDec: getBool(process.env.DISABLE_EMOJI_DEC, false),
dailyCap: getNum(process.env.SLACK_DAILY_CAP, 5),
dailyDecCap: getNum(process.env.SLACK_DAILY_DEC_CAP, 5),
enableDecrement: getBool(process.env.ENABLE_DECREMENT, true),
dailyCap: getNum(process.env.SLACK_DAILY_CAP, 7),
dailyDecCap: getNum(process.env.SLACK_DAILY_DEC_CAP, 7),
enableDecrement: getBool(process.env.ENABLE_DECREMENT, false),
},
http: {
http_port: process.env.PORT || process.env.HTTP_PORT || 3333,
wss_port: process.env.WSS_PORT || 3334,
web_path: process.env.WEB_PATH
? fixPath(process.env.WEB_PATH)
: '/heyburrito/',
: '/heygoose/',
api_path: process.env.API_PATH ? fixPath(process.env.API_PATH) : '/api/',
},
theme: {
Expand All @@ -93,7 +94,7 @@ const config = {
development: {
db: {
db_driver: process.env.DATABASE_DRIVER || 'file',
db_fileName: 'burrito-dev.db',
db_fileName: 'goose-dev.db',
db_path: process.env.DATABASE_PATH || `${root}data/`,
db_url:
process.env.DATABASE_DRIVER === 'mongodb'
Expand All @@ -108,10 +109,10 @@ const config = {
`${process.env.MONGODB_URL}/${process.env.MONGODB_DATABASE}`,
},
slack: {
bot_name: process.env.BOT_NAME || 'heyburrito',
bot_name: process.env.BOT_NAME || 'heygoose',
api_token: mustHave('SLACK_API_TOKEN'),
emojiInc: fixEmoji(process.env.SLACK_EMOJI_INC || ':burrito:'),
emojiDec: fixEmoji(process.env.SLACK_EMOJI_DEC || ':rottenburrito:'),
emojiInc: fixEmoji(process.env.SLACK_EMOJI_INC || ':duck:'),
emojiDec: fixEmoji(process.env.SLACK_EMOJI_DEC),
disableEmojiDec: getBool(process.env.DISABLE_EMOJI_DEC, false),
dailyCap: getNum(process.env.SLACK_DAILY_CAP, 5000),
dailyDecCap: getNum(process.env.SLACK_DAILY_DEC_CAP, 5000),
Expand All @@ -122,7 +123,7 @@ const config = {
wss_port: getNum(process.env.WSS_PORT, 3334),
web_path: process.env.WEB_PATH
? fixPath(process.env.WEB_PATH)
: '/heyburrito/',
: '/heygoose/',
api_path: process.env.API_PATH ? fixPath(process.env.API_PATH) : '/api/',
},
theme: {
Expand All @@ -138,23 +139,23 @@ const config = {
scoreRotation: getNum(process.env.SCORE_ROTATION, 500),
},
misc: {
slackMock: true,
slackMock: false,
log_level: process.env.LOG_LEVEL || 'debug',
},
},
testing: {
db: {
db_driver: process.env.DATABASE_DRIVER || 'file',
db_fileName: 'burrito-test.db',
db_fileName: 'goose-test.db',
db_path: process.env.DATABASE_PATH || `${root}data/`,
db_url: '',
db_name: '',
},
slack: {
bot_name: process.env.BOT_NAME || 'heyburrito',
bot_name: process.env.BOT_NAME || 'heygoose',
api_token: process.env.SLACK_API_TOKEN || '',
emojiInc: fixEmoji(process.env.SLACK_EMOJI_INC || ':burrito:'),
emojiDec: fixEmoji(process.env.SLACK_EMOJI_DEC || ':rottenburrito:'),
emojiInc: fixEmoji(process.env.SLACK_EMOJI_INC || ':duck:'),
emojiDec: fixEmoji(process.env.SLACK_EMOJI_DEC),
disableEmojiDec: getBool(process.env.DISABLE_EMOJI_DEC, false),
dailyCap: getNum(process.env.SLACK_DAILY_CAP, 5000),
dailyDecCap: getNum(process.env.SLACK_DAILY_DEC_CAP, 5000),
Expand All @@ -165,7 +166,7 @@ const config = {
wss_port: process.env.WSS_PORT || 3334,
web_path: process.env.WEB_PATH
? fixPath(process.env.WEB_PATH)
: '/heyburrito/',
: '/heygoose/',
api_path: process.env.API_PATH ? fixPath(process.env.API_PATH) : '/api/',
},
theme: {
Expand Down
20 changes: 10 additions & 10 deletions src/database/drivers/MongoDBDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class MongoDBDriver {
}

give(to: string, from: string, date: any) {
return this.store('burritos', {
return this.store('geese', {
to,
from,
value: 1,
Expand All @@ -59,7 +59,7 @@ class MongoDBDriver {
}

takeAway(to: string, from: string, date: any) {
return this.store('burritos', {
return this.store('geese', {
to,
from,
value: -1,
Expand All @@ -68,7 +68,7 @@ class MongoDBDriver {
}

/**
* @param { string } collection - like burrito
* @param { string } collection - like goose
* @param { Object } query - searchObject to search for
* @return { Find[] }
*/
Expand All @@ -78,7 +78,7 @@ class MongoDBDriver {
}

/**
* @param { string } collection - burrito
* @param { string } collection - goose
* @param { string | null } match - matchObject to search for
* @param { string } listType - defaults to 'to'
* @return { Object } sum[] - data
Expand All @@ -105,7 +105,7 @@ class MongoDBDriver {
* @returns {Find[]}
*/
findFromToday(user: string, listType: string): Promise<Find[]> {
return this.find('burritos', {
return this.find('geese', {
[listType]: user,
given_at: {
$gte: time().start,
Expand All @@ -124,16 +124,16 @@ class MongoDBDriver {
const match = { [listType]: user };

if (num) {
const data = await this.sum('burritos', match, listType);
const data = await this.sum('geese', match, listType);
return data.length ? data[0].score : 0;
}
return this.find('burritos', match);
return this.find('geese', match);
}

/**
* Returns scoreboard
* Should be able to return burrito List ( scoreType inc ) and
* listtype ( dec ) AKA rottenburritoList
* Should be able to return goose List ( scoreType inc ) and
* listtype ( dec ) AKA rottengooseList
*/
async getScoreBoard({ user, listType, today }) {
let match: any = {};
Expand All @@ -144,7 +144,7 @@ class MongoDBDriver {
if (today) {
match.given_at = { $gte: time().start, $lt: time().end };
}
return this.find('burritos', match);
return this.find('geese', match);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/lib/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ function sentToBot(message: any, allBots: any) {
return !!toBot.length;
}

function burritoToBot(message: any, emojis: any) {
const burritoSentToBot = emojis.filter((x: any) =>
function gooseToBot(message: any, emojis: any) {
const gooseSentToBot = emojis.filter((x: any) =>
message.text.match(`${x.id}`)
);
return !!burritoSentToBot.length;
return !!gooseSentToBot.length;
}

function validMessage(message: any, emojis: any, allBots: any): boolean {
Expand Down Expand Up @@ -77,5 +77,5 @@ export {
selfMention,
sentFromBot,
sentToBot,
burritoToBot,
gooseToBot,
};
Loading

0 comments on commit 93f0235

Please sign in to comment.