|
@ -7,11 +7,31 @@ export default class ScoreRepo { |
|
|
|
|
|
|
|
|
constructor(db) { |
|
|
constructor(db) { |
|
|
this.realmDB = db; |
|
|
this.realmDB = db; |
|
|
|
|
|
this.scoreCache = []; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
updateScoreCache = (newScore) => { |
|
|
|
|
|
this.scoreCache.push(newScore); |
|
|
|
|
|
this.scoreCache.sort((a, b) => { |
|
|
|
|
|
a.value < b.value |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (this.scoreCache.length > 99) { |
|
|
|
|
|
this.scoreCache = this.scoreCache.slice(99, this.scoreCache.length - 1); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
createScore = (user, score) => { |
|
|
createScore = (user, score) => { |
|
|
let scoreID = uuidv4(); |
|
|
let scoreID = uuidv4(); |
|
|
|
|
|
|
|
|
|
|
|
let newScore = { |
|
|
|
|
|
id: scoreID, |
|
|
|
|
|
user: user, |
|
|
|
|
|
value: score |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
this.updateScoreCache(newScore); |
|
|
|
|
|
|
|
|
this.realmDB.write(() => { |
|
|
this.realmDB.write(() => { |
|
|
this.realmDB.create( |
|
|
this.realmDB.create( |
|
|
ScoreEntity.name, |
|
|
ScoreEntity.name, |
|
@ -23,7 +43,7 @@ export default class ScoreRepo { |
|
|
true, |
|
|
true, |
|
|
); |
|
|
); |
|
|
}); |
|
|
}); |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
getHighScores = () => { |
|
|
getHighScores = () => { |
|
|
let score = this.realmDB.objects(ScoreEntity.name).sorted('value', true); |
|
|
let score = this.realmDB.objects(ScoreEntity.name).sorted('value', true); |
|
@ -36,5 +56,13 @@ export default class ScoreRepo { |
|
|
highScores.push({user: row.user, value: row.value}); |
|
|
highScores.push({user: row.user, value: row.value}); |
|
|
}); |
|
|
}); |
|
|
return highScores; |
|
|
return highScores; |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
loadCache = () => { |
|
|
|
|
|
this.scoreCache = this.getHighScores(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
getCachedScores = () => { |
|
|
|
|
|
return this.scoreCache; |
|
|
|
|
|
} |
|
|
} |
|
|
} |