2020-08-29 01:23:27 +08:00
|
|
|
import Meetings from '/imports/api/meetings';
|
|
|
|
import Logger from '/imports/startup/server/logger';
|
|
|
|
import { check } from 'meteor/check';
|
|
|
|
|
2021-10-28 02:47:48 +08:00
|
|
|
const SELECT_RANDOM_USER_COUNTDOWN = Meteor.settings.public.selectRandomUser.countdown;
|
|
|
|
|
2021-11-04 23:16:47 +08:00
|
|
|
// Time intervals in milliseconds
|
|
|
|
// for iteration in animation.
|
2023-03-14 00:59:52 +08:00
|
|
|
const intervals = [0, 200, 450, 750, 1100, 1500];
|
2021-10-28 02:47:48 +08:00
|
|
|
|
2021-11-04 23:16:47 +08:00
|
|
|
// Used to togle to the first value of intervals to
|
|
|
|
// differenciare whether this function has been called
|
2021-10-28 02:47:48 +08:00
|
|
|
let updateIndicator = true;
|
|
|
|
|
2021-11-04 23:16:47 +08:00
|
|
|
// A finction that toggles
|
|
|
|
// the first interval on each call
|
|
|
|
function toggleIndicator() {
|
|
|
|
if (updateIndicator) {
|
2021-10-28 02:47:48 +08:00
|
|
|
intervals[0] = 1;
|
|
|
|
} else {
|
|
|
|
intervals[0] = 0;
|
|
|
|
}
|
|
|
|
updateIndicator = !updateIndicator;
|
|
|
|
}
|
|
|
|
|
2021-11-04 23:16:47 +08:00
|
|
|
function getFiveRandom(userList, userIds) {
|
|
|
|
let IDs = userIds.slice();
|
2023-03-14 00:59:52 +08:00
|
|
|
for (let i = 0; i < intervals.length - 1; i += 1) {
|
2021-11-04 23:16:47 +08:00
|
|
|
if (IDs.length === 0) { // we used up all the options
|
|
|
|
IDs = userIds.slice(); // start over
|
|
|
|
let userId = IDs.splice(0, 1);
|
2023-03-14 00:59:52 +08:00
|
|
|
if (userList[userList.length] === [userId, intervals[i]]) {
|
|
|
|
// If we start over with the one we finnished, change it
|
2021-11-04 23:16:47 +08:00
|
|
|
IDs.push(userId);
|
|
|
|
userId = IDs.splice(0, 1);
|
|
|
|
}
|
|
|
|
userList.push([userId, intervals[i]]);
|
|
|
|
} else {
|
|
|
|
const userId = IDs.splice(Math.floor(Math.random() * IDs.length), 1);
|
|
|
|
userList.push([userId, intervals[i]]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// All possible combinations of 3 elements
|
|
|
|
// to speed up randomizing
|
|
|
|
const optionsFor3 = [
|
2021-10-28 02:47:48 +08:00
|
|
|
[0, 1, 2],
|
|
|
|
[0, 2, 1],
|
|
|
|
[1, 2, 0],
|
|
|
|
[1, 0, 2],
|
|
|
|
[2, 0, 1],
|
2021-11-04 23:16:47 +08:00
|
|
|
[2, 1, 0],
|
2021-10-28 02:47:48 +08:00
|
|
|
];
|
|
|
|
|
2023-03-14 00:59:52 +08:00
|
|
|
export default async function updateRandomUser(meetingId, userIds, choice, requesterId) {
|
2020-08-29 01:23:27 +08:00
|
|
|
check(meetingId, String);
|
2021-04-15 23:12:10 +08:00
|
|
|
check(userIds, Array);
|
2021-12-08 01:36:34 +08:00
|
|
|
check(choice, String);
|
2020-08-29 01:23:27 +08:00
|
|
|
check(requesterId, String);
|
|
|
|
|
2021-10-28 02:47:48 +08:00
|
|
|
let userList = [];
|
|
|
|
|
2020-08-29 01:23:27 +08:00
|
|
|
const selector = {
|
|
|
|
meetingId,
|
|
|
|
};
|
|
|
|
|
2021-10-28 02:47:48 +08:00
|
|
|
toggleIndicator();
|
|
|
|
|
|
|
|
const numberOfUsers = userIds.length;
|
2021-04-15 23:12:10 +08:00
|
|
|
|
2023-03-14 00:59:52 +08:00
|
|
|
if (choice === '') {
|
|
|
|
// no viewer
|
2021-11-04 23:16:47 +08:00
|
|
|
userList = [
|
|
|
|
[requesterId, intervals[0]],
|
|
|
|
[requesterId, 0],
|
|
|
|
[requesterId, 0],
|
|
|
|
[requesterId, 0],
|
|
|
|
[requesterId, 0],
|
|
|
|
[requesterId, 0],
|
|
|
|
];
|
|
|
|
} else if (numberOfUsers === 1) { // If user is only one, obviously it is the chosen one
|
|
|
|
userList = [
|
|
|
|
[userIds[0], intervals[0]],
|
|
|
|
[userIds[0], 0],
|
|
|
|
[userIds[0], 0],
|
|
|
|
[userIds[0], 0],
|
|
|
|
[userIds[0], 0],
|
|
|
|
[userIds[0], 0],
|
|
|
|
];
|
2023-03-14 00:59:52 +08:00
|
|
|
} else if (!SELECT_RANDOM_USER_COUNTDOWN) {
|
|
|
|
// If animation is disabled, we only care about the chosen one
|
2021-11-04 23:16:47 +08:00
|
|
|
userList = [
|
2022-03-01 22:01:21 +08:00
|
|
|
[choice, intervals[0]],
|
|
|
|
[choice, 0],
|
|
|
|
[choice, 0],
|
|
|
|
[choice, 0],
|
|
|
|
[choice, 0],
|
|
|
|
[choice, 0],
|
2021-11-04 23:16:47 +08:00
|
|
|
];
|
2023-03-14 00:59:52 +08:00
|
|
|
} else if (numberOfUsers === 2) { // If there are only two users, we can just chow them in turns
|
2021-10-28 02:47:48 +08:00
|
|
|
const IDs = userIds.slice();
|
|
|
|
IDs.splice(choice, 1);
|
2021-11-04 23:16:47 +08:00
|
|
|
userList = [
|
|
|
|
[IDs[0], intervals[0]],
|
2022-03-01 22:01:21 +08:00
|
|
|
[choice, intervals[1]],
|
2021-11-04 23:16:47 +08:00
|
|
|
[IDs[0], intervals[2]],
|
2022-03-01 22:01:21 +08:00
|
|
|
[choice, intervals[3]],
|
2021-11-04 23:16:47 +08:00
|
|
|
[IDs[0], intervals[4]],
|
2022-03-01 22:01:21 +08:00
|
|
|
[choice, intervals[5]],
|
2021-11-04 23:16:47 +08:00
|
|
|
];
|
2023-03-14 00:59:52 +08:00
|
|
|
} else if (numberOfUsers === 3) {
|
|
|
|
// If there are 3 users, the number of combinations is small, so we'll use that
|
2021-10-28 02:47:48 +08:00
|
|
|
const option = Math.floor(Math.random() * 6);
|
|
|
|
const order = optionsFor3[option];
|
2021-11-04 23:16:47 +08:00
|
|
|
userList = [
|
|
|
|
[userIds[order[0]], intervals[0]],
|
|
|
|
[userIds[order[1]], intervals[1]],
|
|
|
|
[userIds[order[2]], intervals[2]],
|
|
|
|
[userIds[order[0]], intervals[3]],
|
|
|
|
[userIds[order[1]], intervals[4]],
|
2022-03-01 22:01:21 +08:00
|
|
|
[choice, intervals[5]],
|
2021-11-04 23:16:47 +08:00
|
|
|
];
|
2023-03-14 00:59:52 +08:00
|
|
|
} else { // We generate 5 users randomly, just for animation, and last one is the chosen one
|
2021-11-04 02:51:32 +08:00
|
|
|
getFiveRandom(userList, userIds);
|
2022-03-01 22:01:21 +08:00
|
|
|
userList.push([choice, intervals[intervals.length]]);
|
2021-04-15 23:12:10 +08:00
|
|
|
}
|
|
|
|
|
2020-08-29 01:23:27 +08:00
|
|
|
const modifier = {
|
|
|
|
$set: {
|
2021-04-15 23:12:10 +08:00
|
|
|
randomlySelectedUser: userList,
|
2020-08-29 01:23:27 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-01-19 09:53:13 +08:00
|
|
|
try {
|
2023-03-14 00:59:52 +08:00
|
|
|
const { insertedId } = await Meetings.upsertAsync(selector, modifier);
|
2021-01-19 09:53:13 +08:00
|
|
|
if (insertedId) {
|
2021-04-15 23:12:10 +08:00
|
|
|
Logger.info(`Set randomly selected userId and interval = ${userList} by requesterId=${requesterId} in meeitingId=${meetingId}`);
|
2020-08-29 01:23:27 +08:00
|
|
|
}
|
2021-01-19 09:53:13 +08:00
|
|
|
} catch (err) {
|
2021-04-15 23:12:10 +08:00
|
|
|
Logger.error(`Setting randomly selected userId and interval = ${userList} by requesterId=${requesterId} in meetingId=${meetingId}`);
|
2021-01-19 09:53:13 +08:00
|
|
|
}
|
2020-08-29 01:23:27 +08:00
|
|
|
}
|