-
Notifications
You must be signed in to change notification settings - Fork 5
/
TaskMultichoice.vue
147 lines (125 loc) · 4.18 KB
/
TaskMultichoice.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<template>
<div id="jspsych-target" class="game-target" translate="no" />
<div v-if="!gameStarted" class="col-full text-center">
<h1>{{ $t('tasks.preparing') }}</h1>
<AppSpinner />
</div>
</template>
<script setup>
import { onMounted, watch, ref, onBeforeUnmount } from 'vue';
import { useRouter } from 'vue-router';
import { storeToRefs } from 'pinia';
import { useQuery } from '@tanstack/vue-query';
import { useAuthStore } from '@/store/auth';
import { useGameStore } from '@/store/game';
import _get from 'lodash/get';
import { fetchDocById } from '@/helpers/query/utils';
import packageLockJson from '../../../package-lock.json';
const props = defineProps({
taskId: { type: String, default: 'multichoice' },
language: { type: String, default: 'en' },
});
let TaskLauncher;
const taskId = props.taskId;
const { version } = packageLockJson.packages['node_modules/@bdelab/roar-multichoice'];
const router = useRouter();
const gameStarted = ref(false);
const authStore = useAuthStore();
const gameStore = useGameStore();
const { isFirekitInit, roarfirekit, roarUid } = storeToRefs(authStore);
const initialized = ref(false);
let unsubscribe;
const init = () => {
if (unsubscribe) unsubscribe();
initialized.value = true;
};
const handlePopState = () => {
router.go(0);
};
unsubscribe = authStore.$subscribe(async (mutation, state) => {
if (state.roarfirekit.restConfig) init();
});
const { isLoading: isLoadingUserData, data: userData } = useQuery({
queryKey: ['userData', roarUid, 'studentData'],
queryFn: () => fetchDocById('users', roarUid.value, ['studentData']),
keepPreviousData: true,
enabled: initialized,
staleTime: 5 * 60 * 1000, // 5 minutes
});
// The following code intercepts the back button and instead forces a refresh.
// We add { once: true } to prevent an infinite loop.
window.addEventListener(
'popstate',
() => {
handlePopState();
},
{ once: true },
);
onMounted(async () => {
try {
TaskLauncher = (await import('@bdelab/roar-multichoice')).default;
} catch (error) {
console.error('An error occurred while importing the game module.', error);
}
if (roarfirekit.value.restConfig) init();
});
onBeforeUnmount(() => {
window.removeEventListener('popstate', handlePopState);
});
watch(
[isFirekitInit, isLoadingUserData],
async ([newFirekitInitValue, newLoadingUserData]) => {
if (newFirekitInitValue && !newLoadingUserData) await startTask();
},
{ immediate: true },
);
const { selectedAdmin } = storeToRefs(gameStore);
async function startTask() {
try {
let checkGameStarted = setInterval(function () {
// Poll for the preload trials progress bar to exist and then begin the game
let gameLoading = document.querySelector('.jspsych-content-wrapper');
if (gameLoading) {
gameStarted.value = true;
clearInterval(checkGameStarted);
}
}, 100);
const appKit = await authStore.roarfirekit.startAssessment(selectedAdmin.value.id, taskId, version);
const userDob = _get(userData.value, 'studentData.dob');
const userDateObj = new Date(userDob);
const userParams = {
grade: _get(userData.value, 'studentData.grade'),
birthMonth: userDateObj.getMonth() + 1,
birthYear: userDateObj.getFullYear(),
language: props.language,
};
const gameParams = { ...appKit._taskInfo.variantParams };
const roarApp = new TaskLauncher(appKit, gameParams, userParams, 'jspsych-target');
await roarApp.run().then(async () => {
// Handle any post-game actions.
await authStore.completeAssessment(selectedAdmin.value.id, taskId);
// Navigate to home, but first set the refresh flag to true.
gameStore.requireHomeRefresh();
router.push({ name: 'Home' });
});
} catch (error) {
console.error('An error occurred while starting the task:', error);
alert(
'An error occurred while starting the task. Please refresh the page and try again. If the error persists, please submit an issue report.',
);
}
}
</script>
<style>
@import '@bdelab/roar-multichoice/lib/resources/roar-multichoice.css';
.game-target {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.game-target:focus {
outline: none;
}
</style>