Skip to content

Commit

Permalink
redo social achievements
Browse files Browse the repository at this point in the history
  • Loading branch information
sspenst committed May 15, 2024
1 parent 78849c5 commit 94699b6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 61 deletions.
67 changes: 33 additions & 34 deletions constants/achievements/AchievementRulesSocial.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,60 @@
import Comment from '@root/models/db/comment';
import { IAchievementInfo } from './achievementInfo';
import AchievementType from './achievementType';

interface IAchievementInfoSocial extends IAchievementInfo {
unlocked: ({ welcomedComments, commentCount }: { welcomedComments: Comment[]; commentCount: number }) => boolean;
unlocked: ({ commentCount, hasWelcomed }: { commentCount: number, hasWelcomed: boolean }) => boolean;
}

const AchievementRulesSocial: { [achievementType: string]: IAchievementInfoSocial } = {};

AchievementRulesSocial[AchievementType.SOCIAL_COMMENT_1_USER] = {
getDescription: () => 'Wrote a comment to a user',
name: 'Social Butterfly',
emoji: '🦋',
AchievementRulesSocial[AchievementType.COMMENT_1] = {
getDescription: () => 'Wrote a comment',
name: 'Chatty',
emoji: '💬',
discordNotification: true,
secret: false,
unlocked: ({ commentCount }) => {
return (commentCount > 0);
return (commentCount >= 1);
},
};
AchievementRulesSocial[AchievementType.SOCIAL_WELCOMED_1_USER] = {
getDescription: () => 'Welcomed a new user to the community!',
name: 'Newbie Nod',
emoji: '👋',
AchievementRulesSocial[AchievementType.COMMENT_5] = {
getDescription: () => 'Wrote 5 comments',
name: 'Conversationalist',
emoji: '🗣️',
discordNotification: true,
secret: true,
unlocked: ({ welcomedComments }) => {
return (welcomedComments.length > 0);
secret: false,
unlocked: ({ commentCount }) => {
return (commentCount >= 5);
},
};
AchievementRulesSocial[AchievementType.SOCIAL_WELCOMED_5_USERS] = {
getDescription: () => 'Welcomed 5 new user to the community!',
name: 'Thinky-Mart Greeter',
emoji: '🛒',
AchievementRulesSocial[AchievementType.COMMENT_10] = {
getDescription: () => 'Wrote 10 comments',
name: 'Commentator',
emoji: '🎙️',
discordNotification: true,
secret: true,
unlocked: ({ welcomedComments }) => {
return (welcomedComments.length >= 5);
secret: false,
unlocked: ({ commentCount }) => {
return (commentCount >= 10);
},
};
AchievementRulesSocial[AchievementType.SOCIAL_WELCOMED_10_USERS] = {
getDescription: () => 'Welcomed 10 new users to the community!',
name: 'Welcoming Committee',
emoji: '🎈',
AchievementRulesSocial[AchievementType.COMMENT_25] = {
getDescription: () => 'Wrote 25 comments',
name: 'Social Butterfly',
emoji: '🦋',
discordNotification: true,
secret: true,
unlocked: ({ welcomedComments }) => {
return (welcomedComments.length >= 10);
secret: false,
unlocked: ({ commentCount }) => {
return (commentCount >= 25);
},
};
AchievementRulesSocial[AchievementType.SOCIAL_WELCOMED_25_USERS] = {
getDescription: () => 'Welcomed 25 new users to the community!',
name: 'Thinky Ambassador',
emoji: '🤝',
AchievementRulesSocial[AchievementType.WELCOME] = {
getDescription: () => 'Welcomed a new user to the community!',
name: 'Newbie Nod',
emoji: '👋',
discordNotification: true,
secret: true,
unlocked: ({ welcomedComments }) => {
return (welcomedComments.length >= 25);
unlocked: ({ hasWelcomed }) => {
return hasWelcomed;
},
};

Expand Down
10 changes: 5 additions & 5 deletions constants/achievements/achievementType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ enum AchievementType {
CREATOR_CREATED_10_HIGH_QUALITY_LEVELS = 'CREATOR_CREATED_10_HIGH_QUALITY_LEVELS',
CREATOR_CREATED_25_HIGH_QUALITY_LEVELS = 'CREATOR_CREATED_25_HIGH_QUALITY_LEVELS',
RECORD_AFTER_1_YEAR = 'RECORD_AFTER_1_YEAR',
SOCIAL_COMMENT_1_USER = 'SOCIAL_COMMENT_1_USER',
SOCIAL_WELCOMED_1_USER = 'SOCIAL_WELCOMED_1_USER',
SOCIAL_WELCOMED_5_USERS = 'SOCIAL_WELCOMED_5_USERS',
SOCIAL_WELCOMED_10_USERS = 'SOCIAL_WELCOMED_10_USERS',
SOCIAL_WELCOMED_25_USERS = 'SOCIAL_WELCOMED_25_USERS',
COMMENT_1 = 'COMMENT_1',
COMMENT_5 = 'COMMENT_5',
COMMENT_10 = 'COMMENT_10',
COMMENT_25 = 'COMMENT_25',
WELCOME = 'WELCOME',
}

export default AchievementType;
29 changes: 8 additions & 21 deletions helpers/refreshAchievements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import AchievementType from '@root/constants/achievements/achievementType';
import DiscordChannel from '@root/constants/discordChannel';
import { GameId } from '@root/constants/GameId';
import { Games } from '@root/constants/Games';
import Role from '@root/constants/role';
import { getSolvesByDifficultyTable } from '@root/helpers/getSolvesByDifficultyTable';
import { createNewAchievement } from '@root/helpers/notificationHelper';
import { getDifficultyRollingSum } from '@root/helpers/playerRankHelper';
Expand All @@ -17,11 +16,12 @@ import { AchievementModel, CommentModel, LevelModel, MultiplayerMatchModel, Mult
import { Types } from 'mongoose';
import queueDiscordWebhook from './discordWebhook';
import { getRecordsByUserId } from './getRecordsByUserId';
import isGuest from './isGuest';

const AchievementCategoryFetch = {
// no game ID as this is a global
[AchievementCategory.SOCIAL]: async (_gameId: GameId, userId: Types.ObjectId) => {
// no game ID as this is a global
const [commentCount, welcomedComments] = await Promise.all([
const [commentCount, welcomeComments] = await Promise.all([
CommentModel.countDocuments({
author: userId,
deletedAt: null,
Expand All @@ -35,31 +35,18 @@ const AchievementCategoryFetch = {
}).populate('target').lean<Comment[]>()
]);

// loop through the welcomed comments and filter out where comment.createdAt - 1000*comment.user.ts > 24 hours
const alreadyWelcomed = {} as { [key: string]: boolean };
const welcomedCommentsFiltered = welcomedComments.filter((comment) => {
const user = comment.target as User;
const hasWelcomed = welcomeComments.some((comment) => {
const user = comment.target as unknown as User;

if (!user || alreadyWelcomed[user._id.toString()]) {
// TODO: looks like some comments should have been deleted but weren't
return false;
}
// also check if user role contains Guest

if (user.roles && user.roles.includes(Role.GUEST)) {
return false;
}

alreadyWelcomed[user._id.toString()] = true;

if (!user.ts) {
if (!user || isGuest(user) || !user.ts) {
return false;
}

// if the comment was made in the first 24 hrs since account creation
return comment.createdAt.getTime() - 1000 * user.ts < 24 * 60 * 60 * 1000;
});

return { welcomedComments: welcomedCommentsFiltered, commentCount: commentCount };
return { commentCount: commentCount, hasWelcomed: hasWelcomed };
},
[AchievementCategory.PROGRESS]: async (gameId: GameId, userId: Types.ObjectId) => {
const userConfig = await UserConfigModel.findOne({ userId: userId, gameId: gameId }).lean<User>();
Expand Down
2 changes: 1 addition & 1 deletion models/db/comment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface Comment {
author: Types.ObjectId & User;
createdAt: Date;
deletedAt: Date;
target: Types.ObjectId & User | Types.ObjectId & Comment | Types.ObjectId & string;
target: Types.ObjectId;
targetModel: User | Comment | string;
text: string;
updatedAt: Date;
Expand Down

0 comments on commit 94699b6

Please sign in to comment.