42 lines
2.2 KiB
JavaScript
42 lines
2.2 KiB
JavaScript
// Wire projection only: never send snapshots, answers, settings or audio state.
|
|
const CONTENT_VERSION = 'season-01'
|
|
const pad = value => String(value).padStart(2, '0')
|
|
const record = value => value && typeof value === 'object' && !Array.isArray(value) ? value : {}
|
|
const list = value => Array.isArray(value) ? value : []
|
|
function emptyProgress() {
|
|
return { completedHotspots: {}, completedChapters: [], lastChapter: 1,
|
|
collectedMemoryCards: [], comicReaderByChapter: {}, lastPageId: '' }
|
|
}
|
|
function projectProgress(input) {
|
|
const value = record(input)
|
|
const result = emptyProgress()
|
|
result.lastChapter = Number.isInteger(value.lastChapter) && value.lastChapter >= 1 && value.lastChapter <= 15 ? value.lastChapter : 1
|
|
for (let n = 1; n <= 15; n++) {
|
|
const id = `S01-C${pad(n)}`
|
|
const saved = record(record(value.comicReaderByChapter)[id])
|
|
const supplied = new Set(list(saved.completedEventIds || record(value.completedHotspots)[id]))
|
|
const events = []
|
|
for (let i = 1; i <= 4; i++) {
|
|
const event = `S01-H${pad((n - 1) * 4 + i)}`
|
|
if (!supplied.has(event)) break
|
|
events.push(event)
|
|
}
|
|
const finished = events.length === 4 && (typeof saved.chapterFinished === 'boolean'
|
|
? saved.chapterFinished : list(value.completedChapters).includes(id))
|
|
if (Object.keys(saved).length || events.length || Object.prototype.hasOwnProperty.call(record(value.completedHotspots), id)) {
|
|
const requested = String(saved.currentPageId || (n === result.lastChapter ? value.lastPageId : '') || '')
|
|
const match = requested.match(new RegExp(`^${id}-P(0[1-8])$`))
|
|
const page = Math.max(1, Math.min(finished ? 8 : 3 + events.length, match ? Number(match[1]) : 1))
|
|
const currentPageId = `${id}-P${pad(page)}`
|
|
result.completedHotspots[id] = events
|
|
result.comicReaderByChapter[id] = { currentPageId, completedEventIds: events.slice(), chapterFinished: finished }
|
|
if (finished) result.completedChapters.push(id)
|
|
if (n === result.lastChapter) result.lastPageId = currentPageId
|
|
}
|
|
const card = `${id}-MC01`
|
|
if (list(value.collectedMemoryCards).includes(card)) result.collectedMemoryCards.push(card)
|
|
}
|
|
return result
|
|
}
|
|
module.exports = { CONTENT_VERSION, emptyProgress, projectProgress }
|