-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add function to record leaderboard data
- Loading branch information
1 parent
a40c3a6
commit d6c46ea
Showing
6 changed files
with
210 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", {value: true}); | ||
exports.LeaderboardRepository = void 0; | ||
|
||
class LeaderboardRepository { | ||
constructor(db) { | ||
this.db = db; | ||
} | ||
leaderboardRef() { | ||
return this.db.collection("leaderboard"); | ||
} | ||
timeBasedLeaderboardRef(timeRange) { | ||
return this.leaderboardRef() | ||
.doc(timeRange) | ||
.collection("data"); | ||
} | ||
|
||
async getWeeklyLeaderboardOfUser(userId) { | ||
try { | ||
const userRef = this.timeBasedLeaderboardRef("weekly").doc(userId); | ||
const weeklyDoc = await userRef.get(); | ||
if (!weeklyDoc.exists) { | ||
return null; | ||
} | ||
return weeklyDoc.data(); | ||
} catch (e) { | ||
console.error("LeaderboardRepository: Error getting weekly data of user:", e); | ||
return null; | ||
} | ||
} | ||
|
||
async getMonthlyLeaderboardOfUser(userId) { | ||
try { | ||
const userRef = this.timeBasedLeaderboardRef("monthly").doc(userId); | ||
const monthlyDoc = await userRef.get(); | ||
if (!monthlyDoc.exists) { | ||
return null; | ||
} | ||
return monthlyDoc.data(); | ||
} catch (e) { | ||
console.error("LeaderboardRepository: Error getting monthly data of user:", e); | ||
return null; | ||
} | ||
} | ||
|
||
async getAllTimeLeaderboardOfUser(userId) { | ||
try { | ||
const userRef = this.timeBasedLeaderboardRef("all_time").doc(userId); | ||
const allTimeDoc = await userRef.get(); | ||
if (!allTimeDoc.exists) { | ||
return null; | ||
} | ||
return allTimeDoc.data(); | ||
} catch (e) { | ||
console.error("LeaderboardRepository: Error getting all time data of user:", e); | ||
return null; | ||
} | ||
} | ||
|
||
async updateWeeklyLeaderboardOfUser(stats) { | ||
try { | ||
const userRef = this.timeBasedLeaderboardRef("weekly").doc(stats.id); | ||
await userRef.set(stats); | ||
} catch (e) { | ||
console.error("LeaderboardRepository: Error in updating weekly data of user:", e); | ||
return null; | ||
} | ||
} | ||
|
||
async updateMonthlyLeaderboardOfUser(stats) { | ||
try { | ||
const userRef = this.timeBasedLeaderboardRef("monthly").doc(stats.id); | ||
await userRef.set(stats); | ||
} catch (e) { | ||
console.error("LeaderboardRepository: Error in updating monthly data of user:", e); | ||
return null; | ||
} | ||
} | ||
|
||
async updateAllTimeLeaderboardOfUser(stats) { | ||
try { | ||
const userRef = this.timeBasedLeaderboardRef("all_time").doc(stats.id); | ||
await userRef.set(stats); | ||
} catch (e) { | ||
console.error("LeaderboardRepository: Error in updating all time data of user:", e); | ||
return null; | ||
} | ||
} | ||
} | ||
exports.LeaderboardRepository=LeaderboardRepository; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", {value: true}); | ||
exports.LeaderboardService = void 0; | ||
const firestore_1 = require("firebase-admin/firestore"); | ||
|
||
class LeaderboardService { | ||
constructor(leaderboardRepository) { | ||
this.leaderboardRepository = leaderboardRepository; | ||
} | ||
|
||
async updateLeaderboard(userId, oldStat, newStat) { | ||
let weekStat = await this.leaderboardRepository.getWeeklyLeaderboardOfUser(userId); | ||
let monthStat = await this.leaderboardRepository.getMonthlyLeaderboardOfUser(userId); | ||
let allTimeStat = await this.leaderboardRepository.getAllTimeLeaderboardOfUser(userId); | ||
|
||
const runDiff = (newStat.batting?.run_scored ?? 0) - (oldStat?.batting?.run_scored ?? 0); | ||
const wicketDiff = (newStat.bowling?.wicket_taken ?? 0) - (oldStat?.bowling?.wicket_taken ?? 0); | ||
const catchDiff = (newStat.fielding?.catches ?? 0) - (oldStat?.fielding?.catches ?? 0); | ||
|
||
const runs = runDiff >= 0 ? runDiff : newStat.batting?.run_scored ?? 0; | ||
const wickets = wicketDiff >= 0 ? wicketDiff : newStat.bowling?.wicket_taken ?? 0; | ||
const catches = catchDiff >= 0 ? catchDiff : newStat.fielding?.catches ?? 0; | ||
|
||
if (!weekStat) { | ||
weekStat = {id: userId, runs: runs, wickets: wickets, catches: catches, date: new Date()}; | ||
} else { | ||
if (weekStat.date instanceof firestore_1.Timestamp && this.isTimeInCurrentWeek(weekStat.date.toDate())) { | ||
weekStat.runs += runs; | ||
weekStat.wickets += wickets; | ||
weekStat.catches += catches; | ||
} else { | ||
weekStat.runs = runs; | ||
weekStat.wickets = wickets; | ||
weekStat.catches = catches; | ||
} | ||
weekStat.date = new Date(); | ||
} | ||
|
||
// Check if monthStat exists, otherwise create a new object | ||
if (!monthStat) { | ||
monthStat = {id: userId, runs: runs, wickets: wickets, catches: catches, date: new Date()}; | ||
} else { | ||
// Update the monthStat if it's within the current month, else reset it | ||
if (monthStat.date instanceof firestore_1.Timestamp && this.isTimeInCurrentMonth(monthStat.date.toDate())) { | ||
monthStat.runs += runs; | ||
monthStat.wickets += wickets; | ||
monthStat.catches += catches; | ||
} else { | ||
monthStat.runs = runs; | ||
monthStat.wickets = wickets; | ||
monthStat.catches = catches; | ||
} | ||
monthStat.date = new Date(); | ||
} | ||
|
||
if (!allTimeStat) { | ||
allTimeStat = {id: userId, runs: runs, wickets: wickets, catches: catches, date: new Date()}; | ||
} else { | ||
allTimeStat.runs += runs; | ||
allTimeStat.wickets += wickets; | ||
allTimeStat.catches += catches; | ||
allTimeStat.date = new Date(); | ||
} | ||
|
||
await this.leaderboardRepository.updateWeeklyLeaderboardOfUser(weekStat); | ||
await this.leaderboardRepository.updateMonthlyLeaderboardOfUser(monthStat); | ||
await this.leaderboardRepository.updateAllTimeLeaderboardOfUser(allTimeStat); | ||
} | ||
|
||
isTimeInCurrentWeek(date) { | ||
// Note: Current week starts from Monday and ends on Sunday | ||
const givenDate = new Date(date); | ||
const currentDate = new Date(); | ||
|
||
const startOfWeek = new Date(currentDate); | ||
startOfWeek.setDate(currentDate.getDate() - currentDate.getDay() + 1); | ||
|
||
const endOfWeek = new Date(startOfWeek); | ||
endOfWeek.setDate(startOfWeek.getDate() + 6); | ||
|
||
return givenDate >= startOfWeek && givenDate <= endOfWeek; | ||
} | ||
|
||
isTimeInCurrentMonth(date) { | ||
const givenDate = new Date(date); | ||
const currentDate = new Date(); | ||
|
||
return ( | ||
givenDate.getMonth() === currentDate.getMonth() && | ||
givenDate.getFullYear() === currentDate.getFullYear() | ||
); | ||
} | ||
} | ||
exports.LeaderboardService = LeaderboardService; |