nhn-cd-sdk
Version:
NHN Cheating Detection SDK
93 lines (82 loc) • 2.16 kB
JavaScript
import { useEventEmitter } from './event-emitter.js';
import { STEP_PREPARE, STEP_READY, STEP_RUN } from './constant.js';
const eventEmitter = useEventEmitter();
eventEmitter.on('update:user-info', (userInfo) => {
localStore.collector && localStore.collector.updateUserInfo({ ...userInfo });
});
const stepMapper = {
STEP_PREPARE,
STEP_READY,
STEP_RUN
};
// 공통 데이터 제어
const localStore = {
currentStepIndex: 0, // 0 : 사용자 정보 입력, 1: 시험 대기, 2: 시험 중
steps: [STEP_PREPARE, STEP_READY, STEP_RUN],
communicator: null,
collector: null,
userData: null,
apiBaseUrl: ''
};
function init() {
if (window.nhnCDSDK) {
localStore.communicator = new window.nhnCDSDK.Communicator();
}
showCurrentStep();
}
function showCurrentStep() {
[...document.querySelectorAll('.step')].forEach((step) => {
if (stepMapper[step.dataset.step] === localStore.steps[localStore.currentStepIndex]) {
step.style.display = 'block';
} else {
step.style.display = 'none';
}
});
}
const store = {
getApiBaseUrl() {
return localStore.apiBaseUrl;
},
setApiBaseUrl(apiBaseUrl) {
localStore.apiBaseUrl = apiBaseUrl;
},
getCommunicator() {
return localStore.communicator;
},
getCollector() {
return localStore.collector;
},
setCollector(collector) {
localStore.collector = collector;
},
setUserData(userData) {
localStore.userData = userData;
},
getUserData() {
return localStore.userData;
},
nextStep() {
if (localStore.currentStepIndex >= localStore.steps.length - 1) {
return;
}
localStore.currentStepIndex++;
eventEmitter.fire('change:step', localStore.steps[localStore.currentStepIndex]);
showCurrentStep();
},
prevStep() {
if (localStore.currentStepIndex <= 0) {
return;
}
localStore.currentStepIndex--;
eventEmitter.fire('change:step', localStore.steps[localStore.currentStepIndex]);
showCurrentStep();
if (localStore.currentStepIndex <= 0) {
this.userData = null;
}
}
};
init();
export function useGlobalStore() {
return store;
}
window.store = store;