mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-02-24 23:04:00 +08:00
added daily bonus
This commit is contained in:
parent
fd74052eac
commit
fdcc1337ae
3 changed files with 43 additions and 13 deletions
|
@ -88,6 +88,7 @@ interface AddResultData {
|
|||
insertedId: ObjectId;
|
||||
dailyLeaderboardRank?: number;
|
||||
xp: number;
|
||||
dailyXpBonus: boolean;
|
||||
}
|
||||
|
||||
export async function addResult(
|
||||
|
@ -353,9 +354,10 @@ export async function addResult(
|
|||
);
|
||||
}
|
||||
|
||||
const xpGained = calculateXp(
|
||||
const xpGained = await calculateXp(
|
||||
result,
|
||||
req.ctx.configuration.users.xpGainMultiplier
|
||||
req.ctx.configuration.users.xpGainMultiplier,
|
||||
uid
|
||||
);
|
||||
|
||||
if (result.bailedOut === false) delete result.bailedOut;
|
||||
|
@ -374,7 +376,7 @@ export async function addResult(
|
|||
|
||||
const addedResult = await ResultDAL.addResult(uid, result);
|
||||
|
||||
await UserDAL.incrementXp(uid, xpGained);
|
||||
await UserDAL.incrementXp(uid, xpGained.xp);
|
||||
|
||||
if (isPb) {
|
||||
Logger.logToDb(
|
||||
|
@ -390,7 +392,8 @@ export async function addResult(
|
|||
isPb,
|
||||
tagPbs,
|
||||
insertedId: addedResult.insertedId,
|
||||
xp: xpGained,
|
||||
xp: xpGained.xp,
|
||||
dailyXpBonus: xpGained.dailyBonus ?? false,
|
||||
};
|
||||
|
||||
if (dailyLeaderboardRank !== -1) {
|
||||
|
@ -401,9 +404,18 @@ export async function addResult(
|
|||
return new MonkeyResponse("Result saved", data);
|
||||
}
|
||||
|
||||
function calculateXp(result, configurationMultiplier): number {
|
||||
async function calculateXp(
|
||||
result,
|
||||
configurationMultiplier,
|
||||
uid
|
||||
): Promise<{
|
||||
xp: number;
|
||||
dailyBonus?: boolean;
|
||||
}> {
|
||||
if (result.mode === "zen") {
|
||||
return 0;
|
||||
return {
|
||||
xp: 0,
|
||||
};
|
||||
}
|
||||
|
||||
const seconds = result.testDuration - result.afkDuration;
|
||||
|
@ -448,8 +460,21 @@ function calculateXp(result, configurationMultiplier): number {
|
|||
|
||||
const accuracyModifier = (result.acc - 50) / 50;
|
||||
|
||||
return (
|
||||
Math.round(seconds * 2 * modifier * accuracyModifier + incompleteXp) *
|
||||
configurationMultiplier
|
||||
);
|
||||
let dailyBonus = 0;
|
||||
const lastResultTimestamp = (await ResultDAL.getLastResult(uid)).timestamp;
|
||||
if (lastResultTimestamp) {
|
||||
const lastResultDay = new Date(lastResultTimestamp).getDay();
|
||||
const today = new Date().getDay();
|
||||
if (lastResultDay != today) {
|
||||
dailyBonus = 1000;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
xp:
|
||||
Math.round(seconds * 2 * modifier * accuracyModifier + incompleteXp) *
|
||||
configurationMultiplier +
|
||||
dailyBonus,
|
||||
dailyBonus: true,
|
||||
};
|
||||
}
|
||||
|
|
|
@ -71,7 +71,8 @@ export async function update(
|
|||
|
||||
export async function updateXpBar(
|
||||
currentXp: number,
|
||||
addedXp: number
|
||||
addedXp: number,
|
||||
_withDailyBonus: boolean
|
||||
): Promise<void> {
|
||||
const startingLevel = Misc.getLevel(currentXp);
|
||||
const endingLevel = Misc.getLevel(currentXp + addedXp);
|
||||
|
|
|
@ -1238,7 +1238,11 @@ export async function retrySavingResult(): Promise<void> {
|
|||
|
||||
if (response.data.xp) {
|
||||
const snapxp = DB.getSnapshot().xp;
|
||||
AccountButton.updateXpBar(snapxp, response.data.xp);
|
||||
AccountButton.updateXpBar(
|
||||
snapxp,
|
||||
response.data.xp,
|
||||
response.data.dailyXpBonus
|
||||
);
|
||||
DB.addXp(response.data.xp);
|
||||
}
|
||||
|
||||
|
@ -1644,7 +1648,7 @@ export async function finish(difficultyFailed = false): Promise<void> {
|
|||
|
||||
if (response.data.xp) {
|
||||
const snapxp = DB.getSnapshot().xp;
|
||||
AccountButton.updateXpBar(snapxp, response.data.xp);
|
||||
AccountButton.updateXpBar(snapxp, response.data.xp, response.data.dailyXpBonus);
|
||||
DB.addXp(response.data.xp);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue