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 pathcreateRoot.js
189 lines (182 loc) · 6.12 KB
/
createRoot.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const ScoreData = require('./ScoreData')
module.exports = createRoot
function createRoot ({
rankingEntryRepository,
legacyUserRepository,
playerRepository,
playerTokenService,
userTokenValidator
}) {
const root = {
chart ({ md5 }) {
return {
level ({ playMode }) {
return {
leaderboard ({ max }) {
max = Math.max(1, Math.min(max || 50, 50))
const options = { md5, playMode, max }
return (rankingEntryRepository
.fetchLeaderboardEntries(options)
.then(rank)
)
},
myRecord ({ playerToken }) {
return playerTokenService.validatePlayerToken(playerToken)
.then(tokenInfo => {
const playerId = tokenInfo.playerId
return fetchLeaderboardRow({ md5, playMode, playerId })
})
}
}
}
}
},
player ({ name }) {
return playerRepository.findByName(name)
.then(foundPlayer => {
return foundPlayer || legacyUserRepository.findByUsername(name)
.then(foundLegacyUser => {
return foundLegacyUser && playerRepository.register(name)
.then(() => playerRepository.findByName(name))
})
})
.then(player => {
return player && PublicPlayerData(player)
})
},
me ({ playerToken }) {
return playerTokenService.validatePlayerToken(playerToken)
.then(tokenInfo => {
const playerId = tokenInfo.playerId
return {
records ({ md5s }) {
return rankingEntryRepository.fetchPlayerEntries({ md5s, playerId })
.then(results => {
return results.map(rankingEntry => {
return {
md5: rankingEntry.md5,
playMode: rankingEntry.playMode,
entry: RankingEntry(rankingEntry)
}
})
})
}
}
})
},
registerPlayer ({ name }) {
return playerRepository.findByName(name)
.then(player => {
return player || playerRepository.register(name)
.then(() => playerRepository.findByName(name))
})
.then(player => {
return player && PublicPlayerData(player)
})
},
linkPlayer ({ jwt }) {
return userTokenValidator.validateToken(jwt)
.then(tokenInfo => {
const playerId = tokenInfo.playerId
const userId = tokenInfo.userId
return playerRepository.findById(playerId).then(player => {
if (!player) throw new Error('Player with specified ID not found.')
if (player.linkedTo && player.linkedTo !== userId) {
throw new Error('Player linked to incorrect ID.')
}
return playerRepository.saveLink(playerId, userId).then(() => {
return playerRepository.findById(playerId)
.then(player => PublicPlayerData(player))
})
})
})
},
registerScore ({ playerToken, md5, playMode, input }) {
return playerTokenService.validatePlayerToken(playerToken)
.then(tokenInfo => {
const playerId = tokenInfo.playerId
return rankingEntryRepository
.fetchLeaderboardEntry({ md5, playMode, playerId })
.then(existingEntry => {
const existingData = existingEntry && existingEntry.data
const nextData = ScoreData.update(existingData, input)
return rankingEntryRepository
.saveLeaderboardEntry({ md5, playMode, playerId, data: nextData })
})
.then(() => {
return fetchLeaderboardRow({ md5, playMode, playerId })
})
.then(row => {
return {
resultingRow: row,
level: root.chart({ md5 }).level({ playMode })
}
})
})
},
authenticatePlayer ({ jwt }) {
return userTokenValidator.validateToken(jwt)
.then(tokenInfo => {
const userId = tokenInfo.userId
return playerRepository.findByUserId(userId).then(player => {
if (!player) throw new Error('Player with specified user ID not found.')
const playerId = player._id
return playerTokenService.generatePlayerToken(playerId)
.then(token => ({ playerToken: token }))
})
})
},
renewPlayerToken ({ playerToken }) {
return playerTokenService.validatePlayerToken(playerToken)
.then(tokenInfo => {
const playerId = tokenInfo.playerId
return playerTokenService.generatePlayerToken(playerId)
.then(token => ({ playerToken: token }))
})
}
}
function fetchLeaderboardRow ({ md5, playMode, playerId }) {
return rankingEntryRepository
.fetchLeaderboardEntry({ md5, playMode, playerId })
.then(entry => {
return rankingEntryRepository
.calculateRank({ md5, playMode, score: entry.data.score })
.then(rank => {
return { rank, entry: RankingEntry(entry) }
})
})
}
function PublicPlayerData (player) {
return {
id: player._id,
name: player.playerName,
linked: () => {
if (player.linkedTo) return true
return legacyUserRepository.findByUsername(player.playerName)
.then(foundLegacyUser => !!foundLegacyUser)
}
}
}
function RankingEntry (entry) {
return {
id: String(entry._id),
score: entry.data.score,
combo: entry.data.combo,
count: entry.data.count,
total: entry.data.total,
recordedAt: entry.data.recordedAt.toJSON(),
playNumber: entry.data.playNumber,
playCount: entry.data.playCount,
player: () => playerRepository.findById(entry.playerId).then(player =>
player && PublicPlayerData(player)
)
}
}
function rank (entries) {
return entries.map((entry, index) => ({
rank: index + 1,
entry: RankingEntry(entry)
}))
}
return root
}