Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public profiles #72

Merged
merged 4 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
- Targetting back rows deals less damage
- Super effective attacks deal more damage
- Add X target pattern shape
- You can now view other users' profiles with `/trainerinfo`! You can make your profile private in `/settings`.

TODO:

- User profile sharing
- Stretch: Achievements
- Stretch: New event
- Stretch: Analytics
Expand Down
4 changes: 2 additions & 2 deletions src/commands/battle/pvp.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* pvp.js is the encapsulating program for the PVP system.
*/
const { getTrainerInfo } = require("../../services/trainer");
const { getTrainerWithExtraInfo } = require("../../services/trainer");
const { validateParty } = require("../../services/party");
const { Battle } = require("../../battle/engine/Battle");
const { setState } = require("../../services/state");
Expand Down Expand Up @@ -32,7 +32,7 @@ const pvp = async (user, opponentUserId, level) => {
}

// get trainer
const trainer = await getTrainerInfo(user);
const trainer = await getTrainerWithExtraInfo(user);
if (trainer.err) {
return { send: null, err: trainer.err };
}
Expand Down
38 changes: 29 additions & 9 deletions src/commands/trainer/trainerInfo.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
/**
* @file
* @author Elvis Wei
* @date 2023
* @section Description
*
* trainerInfo.js Created to display the trainer card, or info, of the current user.
*/
const { getTrainerInfo } = require("../../services/trainer");
const {
getTrainerWithExtraInfo,
getTrainerFromId,
getExtraTrainerInfo,
} = require("../../services/trainer");
const { buildTrainerEmbed } = require("../../embeds/trainerEmbeds");
const { getPartyPokemons } = require("../../services/party");
const { buildPartyEmbed } = require("../../embeds/battleEmbeds");
const { getUserSelectedDevice } = require("../../utils/trainerUtils");
const { getUserId } = require("../../utils/utils");

/**
* Displays the user's trainer info (trainer card).
* @param {object} user User who initiated the command.
* @returns Embed with user's trainer info.
* @param {string} targetUserId The target user ID.
*/
const trainerInfo = async (user) => {
// get trainer info (contains extra info)
const trainer = await getTrainerInfo(user);
const trainerInfo = async (user, targetUserId) => {
let trainer =
/** @type {Awaited<ReturnType<getTrainerWithExtraInfo>>} */ ({});
if (targetUserId && targetUserId !== user.id) {
const getTrainerFromIdRes = await getTrainerFromId(targetUserId);
const targetTrainer = getTrainerFromIdRes.data;
if (getTrainerFromIdRes.err || !targetTrainer?.settings?.publicProfile) {
return {
embed: null,
err: "Trainer not found or trainer has a private profile.",
};
}
trainer = await getExtraTrainerInfo(targetTrainer);
} else {
// get trainer info (contains extra info)
trainer = await getTrainerWithExtraInfo(user);
}
if (trainer.err) {
return { embed: null, err: trainer.err };
}
Expand All @@ -45,7 +62,9 @@ const trainerInfo = async (user) => {
};

const trainerInfoMessageCommand = async (message) => {
const { send, err } = await trainerInfo(message.author);
const args = message.content.split(" ");
const targetUserId = args[1] ? getUserId(args[1]) : undefined;
const { send, err } = await trainerInfo(message.author, targetUserId);
if (err) {
await message.channel.send(`${err}`);
return { err };
Expand All @@ -54,7 +73,8 @@ const trainerInfoMessageCommand = async (message) => {
};

const trainerInfoSlashCommand = async (interaction) => {
const { send, err } = await trainerInfo(interaction.user);
const targetUserId = interaction.options.getUser("user")?.id;
const { send, err } = await trainerInfo(interaction.user, targetUserId);
if (err) {
await interaction.reply(`${err}`);
return { err };
Expand Down
12 changes: 10 additions & 2 deletions src/config/commandConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,19 @@ const commandConfigRaw = {
trainerinfo: {
name: "Trainer Info",
aliases: ["trainerinfo", "trainer", "ti", "profile", "user"],
description: "Get information about your trainer",
description: "Get information about your or another trainer trainer",
longDescription:
"Displays your trainer card with information such as number of Pokemon, Pokedollars, and level progress.",
execute: "trainerInfo.js",
args: {},
args: {
user: {
type: "user",
description:
"@mention user to view their profile (if their profile is public)",
optional: true,
variable: false,
},
},
stages: [stageNames.ALPHA, stageNames.BETA, stageNames.PROD],
exp: 0,
},
Expand Down
3 changes: 1 addition & 2 deletions src/embeds/trainerEmbeds.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ const buildTrainerEmbed = (trainerInfo) => {
{ name: "Level Progress", value: `${progressBar}`, inline: false }
);

const footerString =
"/gacha to get Pokemon\n/backpack to view your items\n/list to view your Pokemon";
const footerString = "Use /settings to make your profile private";
embed.setFooter({ text: footerString });

return embed;
Expand Down
36 changes: 23 additions & 13 deletions src/services/trainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,34 +205,29 @@ const getTrainer = async (discordUser, refresh = true) => {
const refreshTrainer = async (trainer) => await getTrainer(trainer.user);

/**
*
* @param {DiscordUser} user
* Given a trainer, retrieve extra info for that trainer
* @param {WithId<Trainer>} trainer
* @returns {Promise<{data?: Trainer & {pokemon: object, numPokemon: number}, err?: string}>}
*/
const getTrainerInfo = async (user) => {
const trainer = await getTrainer(user);
if (trainer.err) {
return { data: null, err: trainer.err };
}

const getExtraTrainerInfo = async (trainer) => {
// get extra info
try {
const numPokemonQuery = new QueryBuilder(
collectionNames.USER_POKEMON
).setFilter({ userId: trainer.data.userId });
).setFilter({ userId: trainer.userId });

const numPokemonRes = await numPokemonQuery.countDocuments();

const aggQuery = new QueryBuilder(
collectionNames.POKEMON_AND_USERS
).setFilter({ userId: trainer.data.userId });
).setFilter({ userId: trainer.userId });

let pokemonRes = await aggQuery.findOne();
if (pokemonRes === null) {
// set default values, TODO: fix this probably https://www.mongodb.com/community/forums/t/how-can-i-do-a-left-outer-join-in-mongodb/189735/2
pokemonRes = {
pokemon: {
_id: trainer.data.userId,
_id: trainer.userId,
totalWorth: 0,
totalShiny: 0,
totalPower: 0,
Expand All @@ -247,7 +242,7 @@ const getTrainerInfo = async (user) => {

return {
data: {
...trainer.data,
...trainer,
...extraInfo,
},
err: null,
Expand All @@ -258,6 +253,20 @@ const getTrainerInfo = async (user) => {
}
};

/**
*
* @param {DiscordUser} user
* @returns {Promise<{data?: Trainer & {pokemon: object, numPokemon: number}, err?: string}>}
*/
const getTrainerWithExtraInfo = async (user) => {
const trainer = await getTrainer(user);
if (trainer.err) {
return { data: null, err: trainer.err };
}

return await getExtraTrainerInfo(trainer.data);
};

/**
* TODO: caching?
* @param {DiscordUser} user
Expand Down Expand Up @@ -574,7 +583,8 @@ module.exports = {
getTrainer,
getTrainerFromId,
refreshTrainer,
getTrainerInfo,
getExtraTrainerInfo,
getTrainerWithExtraInfo,
getUserSettings,
setUserSetting,
addExpAndMoneyTrainer,
Expand Down
Loading