Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactors #18

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.DS_Store*
.hubot_history
db
17 changes: 17 additions & 0 deletions schemas/users.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- DDL generated by Postico 1.5.10
-- Not all database features are supported. Do not use for backup.

-- Table Definition ----------------------------------------------

CREATE TABLE users (
id SERIAL PRIMARY KEY,
user_id text NOT NULL,
room_id text,
year integer,
week integer,
is_external boolean
);

-- Indices -------------------------------------------------------

CREATE UNIQUE INDEX users_pkey ON users(id int4_ops);
4 changes: 4 additions & 0 deletions scripts/fulbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ module.exports = function main(robot) {
robot.hear(/@(\S+) (juega|va)$/, service.addAnotherUser);
robot.hear(/^(me bajo|-1|no juego|no voy)$/i, service.removeUser);
robot.hear(/@(\S+) no (juega|va)$/, service.removeAnotherUser);

robot.router.get('/', (req, res) => {
res.send('OK');
});
};
24 changes: 14 additions & 10 deletions src/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function getUsersWithLimit(users, limit) {
} else if (usersToComplete > 0) {
message += `Faltan ${usersToComplete}`;
} else {
message += '!Completamos! 🙌';
message += '¡Completamos! 🙌';
}

if (users.length > limit) {
Expand All @@ -36,14 +36,16 @@ function getUsersWithLimit(users, limit) {
return 'No hay jugadores anotad☀️s';
}

function addUser(users, userId, exists, limit) {
function addUser(users, user, exists, limit) {
const formatted = formatUser(user);

if (!exists) {
let replyMessage;

if (users.length > limit) {
replyMessage = `anotad☀️ de suplente <@${userId}>`;
replyMessage = `anotad☀️ de suplente ${formatted}`;
} else {
replyMessage = `anotad☀️ <@${userId}>`;
replyMessage = `anotad☀️ ${formatted}`;
}

if (users.length < limit) {
Expand All @@ -59,15 +61,17 @@ function addUser(users, userId, exists, limit) {
return getUsersWithLimit(users, limit);
}

return `ya estabas anotad☀️, <@${userId}>`;
return `ya estabas anotad☀️, ${formatted}`;
}

function removeUser(users, userId) {
return `removido <@${userId}>`;
function removeUser(user) {
return `removid☀️ ${formatUser(user)}`;
}

function listUsers(users) {
return users
.map(({ userId, userName }) => `- ${userName || `<@${userId}>`}`)
.join('\n');
return users.map((pair) => `- ${formatUser(pair)}`).join('\n');
}

function formatUser({ userName, userId }) {
return userName || `<@${userId}>`;
}
195 changes: 91 additions & 104 deletions src/service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const repository = require('./repository');
const messages = require('./messages');

const limit = 12;

module.exports = function commands(robot) {
return {
buildRandomTeams,
Expand All @@ -13,62 +15,37 @@ module.exports = function commands(robot) {

async function getUsers(res) {
const { room } = res.message;

const users = await repository.getUsers({ room });
const limit = 12;
const message = messages.getUsersWithLimit(users, limit);

robot.messageRoom(room, message);
}

async function addUser(res) {
async function buildRandomTeams(res) {
const { room } = res.message;
const { id: userId } = res.message.user;
const exists = await repository.addUser({
userId,
room
});
const users = await repository.getUsers({
room
});
const limit = 12;
const message = messages.addUser(users, userId, exists, limit);

robot.messageRoom(room, message);
}
const users = await repository.getUsers({ room });

async function buildRandomTeams(res) {
const { room } = res.message;
const users = await repository.getUsers({
room
});
const limit = 12;

if (users.length) {
if (users.length >= limit) {
const newUsers = [];
const tempUsers = users.slice(0, limit);
for (let i = 0; i < limit; i += 1) {
const pos = Math.floor(Math.random() * limit - i);
// eslint-disable-next-line
newUsers[i] = tempUsers.splice(pos, 1)[0];
}

const teamOne = newUsers.slice(0, limit / 2);
const teamTwo = newUsers.slice(limit / 2, limit);

robot.messageRoom(room, showTeam('*Equipo 1*', teamOne));
robot.messageRoom(room, showTeam('*Equipo 2*', teamTwo));
} else {
robot.messageRoom(
room,
`No hay suficientes jugadores anotad☀️s. Faltan ${limit -
users.length}`
);
if (users.length && users.length >= limit) {
const newUsers = [];
const tempUsers = users.slice(0, limit);
for (let i = 0; i < limit; i += 1) {
const pos = Math.floor(Math.random() * limit - i);
// eslint-disable-next-line
newUsers[i] = tempUsers.splice(pos, 1)[0];
}

const teamOne = newUsers.slice(0, limit / 2);
const teamTwo = newUsers.slice(limit / 2, limit);

robot.messageRoom(room, showTeam('*Equipo 1*', teamOne));
robot.messageRoom(room, showTeam('*Equipo 2*', teamTwo));
} else {
robot.messageRoom(
room,
`No hay suficientes jugadores anotad☀️s. Faltan ${limit - users.length}`
`No hay suficientes jugadores anotad☀️s. Faltan ${limit -
(users ? users.length : 0)}`
);
}
}
Expand All @@ -81,96 +58,106 @@ module.exports = function commands(robot) {
return message;
}

async function addAnotherUser(res) {
const { room } = res.message;
const match = /<@(\S+)> (juega|va)$/.exec(res.message.rawText);

if (match) {
const userId = match[1];

const exists = await repository.addUser({
userId,
room
});
const users = await repository.getUsers({
room
});
const limit = 12;
const message = messages.addUser(users, userId, exists, limit);
// ********************************************************************
// ********** Adding **********
// ********************************************************************

robot.messageRoom(room, message);
function matchAddUser(res) {
let user;

return;
const match = /<@(\S+)> (juega|va)$/.exec(res.message.rawText);
if (match) {
user = { userId: match[1] };
}

const noUserMatch = /@(\S+) (juega|va)$/.exec(res.message.text);

if (noUserMatch) {
const userName = noUserMatch[1];
user = { userName: noUserMatch[1] };
}

await repository.addUser({
userName,
room
});
return user;
}

const message = `anotad☀️ ${userName}`;
robot.messageRoom(room, message);
}
async function addUser(res) {
const { room } = res.message;

const user = { userId: res.message.user.id };

await doAddUser({ user, room });
}

async function removeUser(res) {
async function addAnotherUser(res) {
const { room } = res.message;
const { id: userId } = res.message.user;

await repository.removeUser({
userId,
room
});
const user = matchAddUser(res);
if (!user) {
// no user found!
return;
}

const users = await repository.getUsers({
await doAddUser({ user, room });
}

async function doAddUser({ user, room }) {
const exists = await repository.addUser({
...user,
room
});
const limit = 12;
const message = messages.removeUser(users, userId, limit);

const users = await repository.getUsers({ room });

const message = messages.addUser(users, user, exists, limit);

robot.messageRoom(room, message);
}

async function removeAnotherUser(res) {
const { room } = res.message;
const match = /<@(\S+)> no (juega|va)$/.exec(res.message.rawText);
// ********************************************************************
// ********** Removing **********
// ********************************************************************

function matchRemoveUser(res) {
let user;

const match = /<@(\S+)> no (juega|va)$/.exec(res.message.rawText);
if (match) {
const userId = match[1];
user = { userId: match[1] };
}

const noUserMatch = /@(\S+) no (juega|va)$/.exec(res.message.text);
if (noUserMatch) {
user = { userName: noUserMatch[1] };
}

return user;
}

async function removeUser(res) {
const { room } = res.message;

await repository.removeUser({
userId,
room
});
const user = { userId: res.message.user.id };

const users = await repository.getUsers({
room
});
const limit = 12;
const message = messages.removeUser(users, userId, limit);
await doRemoveUser({ user, room });
}

robot.messageRoom(room, message);
async function removeAnotherUser(res) {
const { room } = res.message;

const user = matchRemoveUser(res);
if (!user) {
// no user found!
return;
}

const noUserMatch = /@(\S+) no (juega|va)$/.exec(res.message.text);
await doRemoveUser({ user, room });
}

if (noUserMatch) {
const userName = noUserMatch[1];
async function doRemoveUser({ user, room }) {
await repository.removeUser({
...user,
room
});

await repository.removeUser({
room,
userName
});
const message = messages.removeUser(user);

const message = `removido ${userName}`;
robot.messageRoom(room, message);
}
robot.messageRoom(room, message);
}
};