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

Method recordScores

app/models/LevelSession.js:101–127  ·  view source on GitHub ↗
(scores, level)

Source from the content-addressed store, hash-verified

99 }
100
101 recordScores (scores, level) {
102 let left
103 if (!scores) { return }
104 const state = this.get('state')
105 const oldTopScores = state.topScores != null ? state.topScores : []
106 const newTopScores = []
107 const now = new Date()
108 for (let scoreType of Array.from((left = level.get('scoreTypes')) != null ? left : [])) {
109 if (scoreType.type) { scoreType = scoreType.type }
110 const oldTopScore = _.find(oldTopScores, { type: scoreType })
111 let newScore = scores[scoreType]
112 if (newScore == null) {
113 if (oldTopScore) { newTopScores.push(oldTopScore) }
114 continue
115 }
116 if (Array.from(LevelConstants.lowerIsBetterScoreTypes).includes(scoreType)) { newScore *= -1 } // Index relies on "top" scores being higher numbers
117 if ((oldTopScore == null) || (newScore > oldTopScore.score)) {
118 newTopScores.push({ type: scoreType, date: now, score: newScore })
119 } else {
120 newTopScores.push(oldTopScore)
121 }
122 }
123 state.topScores = newTopScores
124 this.set('state', state)
125 scores = LevelSession.getTopScores({ level: level.toJSON(), session: this.toJSON() })
126 return Backbone.Mediator.publish('level:top-scores-updated', { scores })
127 }
128
129 static getTopScores ({ level, session }) {
130 let score

Callers 1

onSubmissionCompleteMethod · 0.80

Calls 4

getTopScoresMethod · 0.80
publishMethod · 0.80
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected