This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMongoDBRepositoryFactory.js
124 lines (122 loc) · 3.78 KB
/
MongoDBRepositoryFactory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const uuid = require('uuid')
module.exports = MongoDBRepositoryFactory
function MongoDBRepositoryFactory ({ db }) {
return {
createRankingEntryRepository () {
const rankingEntryCollection = db.collection('RankingEntry')
rankingEntryCollection.createIndex({ md5: 1, playMode: 1, playerId: 1 }, { unique: true })
return {
fetchLeaderboardEntries ({ md5, playMode, max }) {
return (rankingEntryCollection
.find({ md5: String(md5), playMode: String(playMode) })
.sort([ [ 'data.score', -1 ] ])
.limit(Math.max(1, Math.min(+max || 50, 50)))
.toArray()
)
},
fetchPlayerEntries ({ md5s, playerId }) {
return (rankingEntryCollection
.find({ md5: { $in: md5s.map(x => String(x)) }, playerId: String(playerId) })
.toArray()
)
},
fetchLeaderboardEntry ({ md5, playMode, playerId }) {
return (rankingEntryCollection
.find({ md5: String(md5), playMode: String(playMode), playerId: String(playerId) })
.limit(1)
.toArray()
.then((result) => result[0] || null)
)
},
saveLeaderboardEntry ({ md5, playMode, playerId, data }) {
return (rankingEntryCollection
.updateOne(
{ md5: String(md5), playMode: String(playMode), playerId: String(playerId) },
{ $set: { data: data, updatedAt: new Date() } },
{ upsert: true }
)
)
},
calculateRank ({ md5, playMode, score }) {
return (rankingEntryCollection
.count({
md5: String(md5),
playMode: String(playMode),
'data.score': { $gt: +score }
})
.then(count => count + 1)
)
}
}
},
createLegacyUserRepository () {
return {
findByEmail (email) {
return (db
.collection('LegacyUser')
.find({ email: String(email) })
.limit(1)
.toArray()
.then((result) => result[0])
)
},
findByUsername (username) {
return (db
.collection('LegacyUser')
.find({ username: String(username) })
.limit(1)
.toArray()
.then((result) => result[0])
)
}
}
},
createPlayerRepository () {
const playerCollection = db.collection('Player')
playerCollection.createIndex({ playerName: 1 }, { unique: true })
playerCollection.createIndex({ linkedTo: 1 })
return {
findByName (playerName) {
return (playerCollection
.find({ playerName: String(playerName) })
.limit(1)
.toArray()
.then((result) => result[0])
)
},
findById (playerId) {
return (playerCollection
.find({ _id: String(playerId) })
.limit(1)
.toArray()
.then((result) => result[0])
)
},
findByUserId (userId) {
return (playerCollection
.find({ linkedTo: String(userId) })
.limit(1)
.toArray()
.then((result) => result[0])
)
},
register (playerName) {
const playerId = uuid.v4()
return (playerCollection
.insertOne({ _id: playerId, playerName: String(playerName) })
.then((result) => playerId)
)
},
saveLink (playerId, userId) {
return (playerCollection
.updateOne(
{ _id: String(playerId) },
{ $set: { linkedTo: String(userId) } }
)
.then((result) => null)
)
}
}
}
}
}