Skip to content

Commit

Permalink
can now chose how many tacos to give and protect if not enough
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinleiba committed Mar 1, 2019
1 parent c9e20af commit 5bfad3a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
32 changes: 22 additions & 10 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,31 @@ const forTaco = controller => {
const ids = DB.getIDs();
const userIndex = ids.indexOf(id);
const senderIndex = ids.indexOf(message.user);
const tacosGiven = parser.countTacos(message.text);
if (userIndex > -1 && userIndex !== senderIndex) {
taco.giveTaco(userIndex);
taco.removeLeft(senderIndex);
const user = DB.getUser(userIndex);
const giver = DB.getUser(senderIndex);
bot.reply(
message,
`Congratz ! <@${user.id}> now has ${user.tacos} tacos !`
);
bot.reply(
message,
`<@${message.user}> now has ${giver.left} tacos left... Nice job guys`
);
if (giver.left >= tacosGiven) {
taco.giveTaco(userIndex, tacosGiven);
taco.removeLeft(senderIndex, tacosGiven);
bot.reply(
message,
`Congratz ! <@${user.id}> now has ${user.tacos +
tacosGiven} tacos !`
);
bot.reply(
message,
`<@${message.user}> now has ${giver.left -
tacosGiven} tacos left... Nice job guys`
);
} else {
bot.reply(
message,
`Sorry <@${message.user}>, you only have ${
giver.left
} tacos remaning.. And you tried to give *${tacosGiven}*`
);
}
}
}
});
Expand Down
6 changes: 5 additions & 1 deletion parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { compose, removeStr } = require("./utils.js");

const ID_REGEX = /<@\S*>/;
const TACO_REGEX = /:taco:/g;

const parseId = id =>
compose(
Expand All @@ -13,6 +14,9 @@ const findID = message => {
return matching === null ? null : parseId(matching[0]);
};

const countTacos = message => message.match(TACO_REGEX).length;

module.exports = {
findID
findID,
countTacos
};
17 changes: 12 additions & 5 deletions taco.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,30 @@ const writeMembers = APIMembers => {
DB.saveUsers(formattedDBUsers);
};

const giveTaco = index => {
const giveTaco = (index, amount = 1) => {
const DBUsers = DB.getUsers();
const user = DBUsers[index];
DBUsers[index] = { ...user, tacos: user.tacos + 1 };
DBUsers[index] = { ...user, tacos: user.tacos + amount };
DB.saveUsers(DBUsers);
};

const removeLeft = index => {
const removeLeft = (index, amount = 1) => {
const DBUsers = DB.getUsers();
const user = DBUsers[index];
DBUsers[index] = { ...user, left: user.left - 1 };
DBUsers[index] = { ...user, left: user.left - amount };
DB.saveUsers(DBUsers);
};

const resetLeft = () => {
const DBUsers = DB.getUsers();
const fullUsers = DBUsers.map(user => ({ ...user, left: 5 }));
DB.saveUsers(fullUsers);
};

module.exports = {
init,
writeMembers,
giveTaco,
removeLeft
removeLeft,
resetLeft
};

0 comments on commit 5bfad3a

Please sign in to comment.