MCPcopy Create free account
hub / github.com/codecombat/codecombat / updateCourseLevels

Method updateCourseLevels

app/views/play/CampaignView.js:597–652  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

595 }
596
597 updateCourseLevels () {
598 if (!this.campaign.loaded || !this.courseLevelsLoaded) {
599 return false
600 }
601 const existing = this.campaign.get('levels')
602 const courseLevels = this.courseLevels.toArray()
603 const classroomCourse = globalVar.currentView.classroom.get('courses').find(c => c._id === globalVar.currentView.course.id)
604 const levelPositions = {}
605 for (const level of classroomCourse.levels) {
606 if (level.position) {
607 levelPositions[level.original] = level.position
608 }
609 }
610 for (const [k, v] of Object.entries(courseLevels)) {
611 const idx = v.get('original')
612 if (!existing[idx]) {
613 // a level which has been removed from the campaign but is saved in the course
614 this.courseLevelsFake[idx] = v.toJSON()
615 } else {
616 this.courseLevelsFake[idx] = existing[idx]
617 // carry over positions stored in course, if there are any
618 if (levelPositions[idx]) {
619 this.courseLevelsFake[idx].position = levelPositions[idx]
620 }
621 }
622 this.courseLevelsFake[idx].courseIdx = parseInt(k)
623 this.courseLevelsFake[idx].requiresSubscription = false
624 }
625 // Fill in missing positions, for courses which have levels that no longer exist in campaigns
626 for (const [k, v] of Object.entries(courseLevels)) {
627 const kInt = parseInt(k)
628 const idx = v.get('original')
629 if (!this.courseLevelsFake[idx].position) {
630 const prevLevel = courseLevels[kInt - 1]
631 const nextLevel = courseLevels[kInt + 1]
632 if (prevLevel && nextLevel) {
633 const prevIdx = prevLevel.get('original')
634 const nextIdx = nextLevel.get('original')
635 const prevPosition = this.courseLevelsFake[prevIdx].position
636 const nextPosition = this.courseLevelsFake[nextIdx].position
637 if (prevPosition && nextPosition) {
638 // split the diff between the previous, next levels
639 this.courseLevelsFake[idx].position = {
640 x: (prevPosition.x + nextPosition.x) / 2,
641 y: (prevPosition.y + nextPosition.y) / 2,
642 }
643 } else {
644 // otherwise just line them up along the bottom
645 const x = 10 + ((kInt / courseLevels.length) * 80)
646 this.courseLevelsFake[idx].position = { x, y: 10 }
647 }
648 }
649 }
650 }
651 return this.render()
652 }
653
654 updateClassroomSessions () {

Callers 1

constructorMethod · 0.95

Calls 2

getMethod · 0.45
renderMethod · 0.45

Tested by

no test coverage detected