Improvement based on comments

This commit is contained in:
Bohdan Zhemelinskyi 2021-11-04 15:16:47 +00:00
parent a20ee430ed
commit d5c80fc060
3 changed files with 122 additions and 81 deletions

View File

@ -2,22 +2,20 @@ import Meetings from '/imports/api/meetings';
import Logger from '/imports/startup/server/logger';
import { check } from 'meteor/check';
const SELECT_RANDOM_USER_ENABLED = Meteor.settings.public.selectRandomUser.enabled;
const SELECT_RANDOM_USER_COUNTDOWN = Meteor.settings.public.selectRandomUser.countdown;
//Time intervals in milliseconds
//for iteration in animation
// Time intervals in milliseconds
// for iteration in animation.
let intervals = [0, 200, 450, 750, 1100, 1500];
//Used to togle to the first value of intervals to
//differenciare whether this function has been called
// Used to togle to the first value of intervals to
// differenciare whether this function has been called
let updateIndicator = true;
//A finction that toggles
//the first interval on each call
function toggleIndicator(){
if(updateIndicator){
// A finction that toggles
// the first interval on each call
function toggleIndicator() {
if (updateIndicator) {
intervals[0] = 1;
} else {
intervals[0] = 0;
@ -25,15 +23,33 @@ function toggleIndicator(){
updateIndicator = !updateIndicator;
}
//All possible combinations of 3 elements
//to to speed up randomizing
const optionsFor3 = [
function getFiveRandom(userList, userIds) {
let IDs = userIds.slice();
for (let i = 0; i < intervals.length - 1; i++) {
if (IDs.length === 0) { // we used up all the options
IDs = userIds.slice(); // start over
let userId = IDs.splice(0, 1);
if (userList[userList.length] === [userId, intervals[i]]) { // If we start over with the one we finnished, change it
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 = [
[0, 1, 2],
[0, 2, 1],
[1, 2, 0],
[1, 0, 2],
[2, 0, 1],
[2, 1, 0]
[2, 1, 0],
];
export default function updateRandomUser(meetingId, userIds, choice, requesterId) {
@ -55,29 +71,60 @@ export default function updateRandomUser(meetingId, userIds, choice, requesterId
const chosenUser = userIds[choice];
if (choice < 0) { // no viewer
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]];
}
else if(!SELECT_RANDOM_USER_COUNTDOWN) { //If animation is disabled, we only care about the chosen one
userList = [[chosenUser,intervals[0]], [chosenUser,0], [chosenUser,0], [chosenUser,0], [chosenUser,0], [chosenUser,0]];
}
else if(numberOfUsers == 2){ // If there are only two users, we can just chow them in turns
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],
];
}
else if (!SELECT_RANDOM_USER_COUNTDOWN) { // If animation is disabled, we only care about the chosen one
userList = [
[chosenUser, intervals[0]],
[chosenUser, 0],
[chosenUser, 0],
[chosenUser, 0],
[chosenUser, 0],
[chosenUser, 0],
];
}
else if (numberOfUsers === 2) { // If there are only two users, we can just chow them in turns
const IDs = userIds.slice();
IDs.splice(choice, 1);
userList = [[IDs[0],intervals[0]], [chosenUser,intervals[1]], [IDs[0],intervals[2]], [chosenUser,intervals[3]], [IDs[0],intervals[4]], [chosenUser,intervals[5]]];
}
else if(numberOfUsers == 3){ //If there are 3 users, the number of combinations is small, so we'll use that
userList = [
[IDs[0], intervals[0]],
[chosenUser, intervals[1]],
[IDs[0], intervals[2]],
[chosenUser, intervals[3]],
[IDs[0], intervals[4]],
[chosenUser, intervals[5]],
];
} else if (numberOfUsers === 3) { // If there are 3 users, the number of combinations is small, so we'll use that
const option = Math.floor(Math.random() * 6);
const order = optionsFor3[option];
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]], [chosenUser,intervals[5]]];
}
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]],
[chosenUser, intervals[5]],
];
}
else { // We generate 5 users randomly, just for animation, and last one is the chosen one
getFiveRandom(userList, userIds);
userList.push([chosenUser, intervals[intervals.length]]);
@ -98,21 +145,3 @@ export default function updateRandomUser(meetingId, userIds, choice, requesterId
Logger.error(`Setting randomly selected userId and interval = ${userList} by requesterId=${requesterId} in meetingId=${meetingId}`);
}
}
function getFiveRandom(userList, userIds){
let IDs = userIds.slice();
for(let i = 0; i < intervals.length - 1; i++ ){
if(IDs == 0) { // we used up all the options
IDs = userIds.slice(); //start over
let userId = IDs.splice(0, 1);
if(userList[userList.length] == [userId, intervals[i]]){ //If we start over with the one we finnished, change it
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]]);
}
}
}

View File

@ -56,7 +56,7 @@ class RandomUserSelect extends Component {
props.randomUserReq();
}
if(SELECT_RANDOM_USER_COUNTDOWN) {
if (SELECT_RANDOM_USER_COUNTDOWN) {
this.state = {
count: 0,
};
@ -86,8 +86,8 @@ class RandomUserSelect extends Component {
}
componentDidUpdate(prevProps, prevState) {
if(SELECT_RANDOM_USER_COUNTDOWN) {
if (this.props.currentUser.presenter && this.state.count == 0) {
if (SELECT_RANDOM_USER_COUNTDOWN) {
if (this.props.currentUser.presenter && this.state.count === 0) {
this.iterateSelection();
}
@ -105,7 +105,7 @@ class RandomUserSelect extends Component {
}
reselect() {
if(SELECT_RANDOM_USER_COUNTDOWN) {
if (SELECT_RANDOM_USER_COUNTDOWN) {
this.setState({
count: 0,
});
@ -169,7 +169,7 @@ class RandomUserSelect extends Component {
{selectedUser.name}
</div>
{currentUser.presenter
&& countDown == 0
&& countDown === 0
&& (
<Button
label={intl.formatMessage(messages.reselect)}
@ -182,7 +182,7 @@ class RandomUserSelect extends Component {
</div>
);
}
if(keepModalOpen){
if (keepModalOpen) {
return (
<Modal
hideBorder

View File

@ -7,17 +7,18 @@ import { withModalMounter } from '/imports/ui/components/modal/service';
import { makeCall } from '/imports/ui/services/api';
import RandomUserSelect from './component';
import { UsersContext } from '/imports/ui/components/components-data/users-context/context';
import logger from '/imports/startup/client/logger';
const SELECT_RANDOM_USER_ENABLED = Meteor.settings.public.selectRandomUser.enabled;
//A value that is used by component to remember
//whether it should be open or closed after a render
// A value that is used by component to remember
// whether it should be open or closed after a render
let keepModalOpen = true;
//A value that stores the previous indicator
// A value that stores the previous indicator
let updateIndicator = 1;
const toggleKeepModalOpen = () => { keepModalOpen = ! keepModalOpen; }
const toggleKeepModalOpen = () => { keepModalOpen = ! keepModalOpen; };
const RandomUserSelectContainer = (props) => {
const usingUsersContext = useContext(UsersContext);
@ -26,21 +27,30 @@ const RandomUserSelectContainer = (props) => {
let mappedRandomlySelectedUsers = [];
const currentUser = { userId: Auth.userID, presenter: users[Auth.meetingID][Auth.userID].presenter };
const currentUser = {
userId: Auth.userID,
presenter: users[Auth.meetingID][Auth.userID].presenter
};
try{
if(!currentUser.presenter && //this functionality does not bother presenter
(!keepModalOpen) && //we only ween a change if modal has been closed before
(randomlySelectedUser[0][1] != updateIndicator)//if tey are different, a user was generated
){ keepModalOpen = true; } //reopen modal
if(!currentUser.presenter){ updateIndicator = randomlySelectedUser[0][1]; } //keep indicator up to date
}catch(err){
console.error(
"Issue in Random User container caused by back-end crash\n"
+ "Value of 6 randomly selected users was passed as {"
+ randomlySelectedUser
+ "}\nHowever, it is handled.");
try {
if (!currentUser.presenter // this functionality does not bother presenter
&& (!keepModalOpen) // we only ween a change if modal has been closed before
&& (randomlySelectedUser[0][1] !== updateIndicator)// if tey are different, a user was generated
) { keepModalOpen = true; } // reopen modal
if (!currentUser.presenter) { updateIndicator = randomlySelectedUser[0][1]; } // keep indicator up to date
} catch (err) {
logger.error({
logCode: 'Random_USer_Error',
extraInfo: {
stackTrace: err,
},
},
'\nIssue in Random User Select container caused by back-end crash'
+ '\nValue of 6 randomly selected users was passed as '
+ `{${randomlySelectedUser}}`
+ '\nHowever, it is handled.'
+ '\nError message:'
+ `\n${err}`);
}
if (randomlySelectedUser) {
@ -55,12 +65,14 @@ const RandomUserSelectContainer = (props) => {
});
}
return <RandomUserSelect
{...props}
mappedRandomlySelectedUsers={mappedRandomlySelectedUsers}
currentUser={currentUser}
keepModalOpen={keepModalOpen}
/>;
return (
<RandomUserSelect
{...props}
mappedRandomlySelectedUsers={mappedRandomlySelectedUsers}
currentUser={currentUser}
keepModalOpen={keepModalOpen}
/>
);
};
export default withModalMounter(withTracker(({ mountModal }) => {
const viewerPool = Users.find({