spectatr-player-sdk
Version:
A custom video player built with Stencil with Shaka Player integration
129 lines (128 loc) • 5.26 kB
JavaScript
import { submitPollAnswer, submitQuizAnswer } from "../../services/quizService";
export function checkForQuiz(component) {
if (!component.quizzes || component.showQuiz)
return;
const currentQuiz = component.quizzes.quizQuestions.find(quiz => {
return Math.abs(quiz.timestampSeconds - component.currentTime) < 5 && !component.quizAnswered.some(answered => answered.quizQuestion.id === quiz.id);
});
if (component.showQuiz && currentQuiz) {
component.currentQuiz = currentQuiz;
component.showQuiz = true;
component.quizTriggered.emit(currentQuiz);
}
else {
component.showQuiz = false;
component.currentQuiz = null;
}
}
export function findNearbyQuizQuestion(quiz, currentTime, // in seconds
answeredQuizzes) {
// Find all questions that are currently active (within their display window)
const activeQuestions = quiz.quizQuestions.filter(question => {
const startTime = question.timestampSeconds;
const endTime = startTime + question.durationSeconds; // durationSeconds buffer
// const endTime = startTime + 5; // 5 second buffer
return currentTime >= startTime && currentTime <= endTime;
});
// Among active questions, find the one that hasn't been answered yet
const unansweredActiveQuestion = activeQuestions.find(question => !answeredQuizzes.some(a => a.quizQuestion.id === question.id));
// If we found an active, unanswered question, return the one with earliest timestamp
if (unansweredActiveQuestion) {
return activeQuestions.reduce((earliest, current) => (current.timestampSeconds < earliest.timestampSeconds ? current : earliest));
}
return undefined;
}
export function findNearbyPoll(polls, currentTime) {
// Find all questions that are currently active (within their display window)
const activeQuestions = polls.filter(poll => {
const startTime = poll.timestampSeconds;
const endTime = startTime + poll.durationSeconds;
return currentTime >= startTime && currentTime <= endTime;
});
// If we found an active, unanswered question, return the one with earliest timestamp
if (!!activeQuestions.length) {
return activeQuestions.reduce((earliest, current) => (current.timestampSeconds < earliest.timestampSeconds ? current : earliest));
}
return undefined;
}
export function findNearbyStat(stats, currentTime) {
// Find all questions that are currently active (within their display window)
const activeStats = stats.filter(stat => {
const startTime = stat.timestampSeconds;
const endTime = startTime + stat.durationSeconds;
return currentTime >= startTime && currentTime <= endTime;
});
// If we found an active, unanswered question, return the one with earliest timestamp
if (!!activeStats.length) {
return activeStats.reduce((earliest, current) => (current.timestampSeconds < earliest.timestampSeconds ? current : earliest));
}
return undefined;
}
export function findIsAnswered(pollId, answeredPoll) {
return answeredPoll.find(arr => arr.poll.id === pollId);
}
export async function handleQuizAnswer(component, optionIds) {
if (!component.currentQuiz)
return Promise.resolve();
// Check if this quiz question was already answered
try {
const existingAnswerIndex = component.quizAnswered.findIndex((item) => item.quizQuestion.id === component.currentQuiz.id);
const isSuccess = await submitQuizAnswer(component, optionIds);
if (!isSuccess) {
return false;
}
if (existingAnswerIndex >= 0) {
component.quizAnswered[existingAnswerIndex].answer = optionIds;
}
else {
component.quizAnswered.push({
quizQuestion: component.currentQuiz,
answer: optionIds,
});
}
return true;
}
catch (error) {
throw new Error(error);
}
}
export async function handlePollAnswer(component, optionIds) {
if (!component.currentPoll)
return Promise.resolve();
try {
const existingAnswerIndex = component.pollAnswered.findIndex((item) => item.poll.id === component.currentPoll.id);
const isSuccess = await submitPollAnswer(component, optionIds);
if (!isSuccess) {
return false;
}
if (existingAnswerIndex >= 0) {
component.pollAnswered[existingAnswerIndex].answer = optionIds;
}
else {
component.pollAnswered.push({
poll: component.currentPoll,
answer: optionIds,
});
}
return true;
}
catch (error) {
throw new Error(error);
}
}
export function closeQuiz(component) {
const video = component.videoElement;
video.paused && video.play();
component.showQuiz = false;
}
export function closePoll(component) {
const video = component.videoElement;
video.paused && video.play();
component.showPoll = false;
}
export function closeStats(component) {
const video = component.videoElement;
video.paused && video.play();
component.showStatistics = false;
}
//# sourceMappingURL=quiz-handler.js.map