|
1 | 1 | import Controller from '@ember/controller'; |
| 2 | +import type AuthenticatorService from 'codecrafters-frontend/services/authenticator'; |
2 | 3 | import type LanguageModel from 'codecrafters-frontend/models/language'; |
| 4 | +import type LeaderboardEntryModel from 'codecrafters-frontend/models/leaderboard-entry'; |
| 5 | +import type LeaderboardModel from 'codecrafters-frontend/models/leaderboard'; |
3 | 6 | import type Store from '@ember-data/store'; |
4 | 7 | import type { ModelType } from 'codecrafters-frontend/routes/leaderboard'; |
5 | 8 | import { inject as service } from '@ember/service'; |
| 9 | +import { task } from 'ember-concurrency'; |
| 10 | +import { tracked } from '@glimmer/tracking'; |
| 11 | +import type LeaderboardRankCalculationModel from 'codecrafters-frontend/models/leaderboard-rank-calculation'; |
6 | 12 |
|
7 | 13 | export default class LeaderboardController extends Controller { |
| 14 | + @service declare authenticator: AuthenticatorService; |
8 | 15 | @service declare store: Store; |
9 | 16 |
|
| 17 | + @tracked surroundingEntries: LeaderboardEntryModel[] = []; |
| 18 | + @tracked topEntries: LeaderboardEntryModel[] = []; |
| 19 | + @tracked userRankCalculation: LeaderboardRankCalculationModel | null = null; |
| 20 | + |
10 | 21 | declare model: ModelType; |
11 | 22 |
|
| 23 | + get allEntries(): LeaderboardEntryModel[] { |
| 24 | + return [...this.surroundingEntries, ...this.topEntries]; |
| 25 | + } |
| 26 | + |
| 27 | + get isLoadingEntries(): boolean { |
| 28 | + return this.loadEntriesTask.isRunning; |
| 29 | + } |
| 30 | + |
| 31 | + get leaderboard(): LeaderboardModel { |
| 32 | + return this.model.language.leaderboard!; |
| 33 | + } |
| 34 | + |
12 | 35 | get sortedLanguagesForDropdown(): LanguageModel[] { |
13 | 36 | return this.store |
14 | 37 | .peekAll('language') |
15 | 38 | .sortBy('sortPositionForTrack') |
16 | 39 | .filter((language) => language.liveOrBetaStagesCount > 0); |
17 | 40 | } |
| 41 | + |
| 42 | + get userEntry(): LeaderboardEntryModel | undefined { |
| 43 | + return this.allEntries.find((entry) => entry.user.id === this.authenticator.currentUserId); |
| 44 | + } |
| 45 | + |
| 46 | + get userIsInTopLeaderboardEntries(): boolean { |
| 47 | + if (!this.authenticator.isAuthenticated) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + return this.topEntries.some((entry) => entry.user.id === this.authenticator.currentUserId); |
| 52 | + } |
| 53 | + |
| 54 | + loadEntriesTask = task({ restartable: true }, async () => { |
| 55 | + this.topEntries = (await this.store.query('leaderboard-entry', { |
| 56 | + include: 'leaderboard,user', |
| 57 | + leaderboard_id: this.leaderboard.id, |
| 58 | + filter_type: 'top', |
| 59 | + })) as unknown as LeaderboardEntryModel[]; |
| 60 | + |
| 61 | + if (!this.authenticator.isAuthenticated || this.userIsInTopLeaderboardEntries) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + this.surroundingEntries = (await this.store.query('leaderboard-entry', { |
| 66 | + include: 'leaderboard,user', |
| 67 | + leaderboard_id: this.leaderboard.id, |
| 68 | + user_id: this.authenticator.currentUserId, // Only used in tests since mirage doesn't have auth context |
| 69 | + filter_type: 'around_me', |
| 70 | + })) as unknown as LeaderboardEntryModel[]; |
| 71 | + |
| 72 | + if (this.surroundingEntries.length === 0) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + const userRankCalculations = (await this.store.query('leaderboard-rank-calculation', { |
| 77 | + include: 'user', |
| 78 | + leaderboard_id: this.model.language.leaderboard!.id, |
| 79 | + user_id: this.authenticator.currentUserId, // Only used in tests since mirage doesn't have auth context |
| 80 | + })) as unknown as LeaderboardRankCalculationModel[]; |
| 81 | + |
| 82 | + this.userRankCalculation = userRankCalculations[0] || null; |
| 83 | + |
| 84 | + // TODO: Also look at "outdated" user rank calculations? |
| 85 | + if (!this.userRankCalculation) { |
| 86 | + this.userRankCalculation = await this.store |
| 87 | + .createRecord('leaderboard-rank-calculation', { |
| 88 | + leaderboard: this.model.language.leaderboard!, |
| 89 | + user: this.authenticator.currentUser!, |
| 90 | + }) |
| 91 | + .save(); |
| 92 | + } |
| 93 | + }); |
18 | 94 | } |
0 commit comments