import { ParameterType } from 'jspsych'; import { html } from './text-stimuli'; import getColourMap from './colours'; const info = { name: 'lobby', version: "1.0", parameters: { user_names: { type: ParameterType.STRING, pretty_name: 'User names', default: [], array: true, }, start_text: { type: ParameterType.STRING, pretty_name: 'Start text', }, end_text: { type: ParameterType.STRING, pretty_name: 'End text', }, start_number: { type: ParameterType.INT, pretty_name: 'Start number', default: null, }, end_number: { type: ParameterType.INT, pretty_name: 'End number', default: 3, }, join_interval: { type: ParameterType.INT, pretty_name: 'Join interval', default: 5000, }, user_object: { type: ParameterType.OBJECT, pretty_name: 'User object', default: {}, }, show_avatars: { type: ParameterType.BOOL, pretty_name: 'Show avatar', default: false, }, data: { type: ParameterType.OBJECT, pretty_name: 'Data', default: {}, }, }, // prettier-ignore citations: '__CITATIONS__' }; class jsPsychLobby { constructor(jsPsych) { this.jsPsych = jsPsych; } static { this.info = info; } trial(display_element, trial) { let current_participants; if (!trial.start_number) { current_participants = Math.floor(Math.random() * trial.end_number) + 1; } else { current_participants = trial.start_number; } const colourMap = getColourMap(' '); display_element.innerHTML = html`
`; const search_text_div = document.getElementById('search-text'); const participants_list_div = document.getElementById('participants-list'); const refresh_warning_div = document.getElementById('refresh-warning'); const loading_animation_div = document.getElementById('loading-animation'); search_text_div.innerHTML = '

' + trial.start_text + ' (' + current_participants + '/' + trial.end_number + ') ' + '

'; refresh_warning_div.innerHTML = `

Please do not refresh or close this page unless you are not connected to your partner within 5 minutes

`; const current_participants_span = document.getElementById('current-participants'); let participants_list = trial.user_names.slice(1, current_participants); let ended = false; const avatar_svg_template = colour => html` `; const createParticipantsHtml = () => { let html = ''; for (let i = 0; i < participants_list.length; i++) { /* if (trial.show_avatars) { html += `
${avatar_svg_template( trial.user_object[participants_list[i]] )}
`; } */ if (participants_list[i] === trial.user_names[0]) { html += `

` + participants_list[i] + ` (you)

`; } else { html += `

` + participants_list[i] + `

`; } } return (html); }; participants_list_div.innerHTML = createParticipantsHtml(); setTimeout(() => { participants_list.push(trial.user_names[0]); participants_list_div.innerHTML = createParticipantsHtml(); }, 100); const updateParticipants = () => { if (current_participants >= trial.end_number && !ended) { ended = true; clearInterval(participants_interval); setTimeout(this.foundParticipants, 1500); } else { participants_list.push(trial.user_names[current_participants]); participants_list_div.innerHTML = createParticipantsHtml(); current_participants++; if (current_participants <= trial.end_number && !ended) { current_participants_span.innerHTML = current_participants; } } }; this.foundParticipants = () => { participants_list_div.innerHTML = html`${trial.end_text}`; loading_animation_div.classList.remove('loading'); setTimeout(() => { this.jsPsych.finishTrial(); }, 2000); }; const participants_interval = setInterval( updateParticipants, trial.join_interval ); } } export default jsPsychLobby;