Skip to content

Commit

Permalink
small codescenes fixes #2273
Browse files Browse the repository at this point in the history
  • Loading branch information
romain22222 committed Jan 15, 2025
1 parent afda0e6 commit 3e46260
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 90 deletions.
24 changes: 12 additions & 12 deletions Core/src/commands/pet/PetCommand.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import {DraftBotPacket, makePacket} from "../../../../Lib/src/packets/DraftBotPacket";
import {Player, Players} from "../../core/database/game/models/Player";
import {CommandPetPacketReq, CommandPetPacketRes} from "../../../../Lib/src/packets/commands/CommandPetPacket";
import {
CommandPetPacketReq,
CommandPetPacketRes,
CommandPetPetNotFound
} from "../../../../Lib/src/packets/commands/CommandPetPacket";
import {PetEntities} from "../../core/database/game/models/PetEntity";
import {PetDataController} from "../../data/Pet";
import {commandRequires, CommandUtils} from "../../core/utils/CommandUtils";
import {SexTypeShort} from "../../../../Lib/src/constants/StringConstants";

export default class PetCommand {
@commandRequires(CommandPetPacketReq, {
Expand All @@ -14,21 +19,16 @@ export default class PetCommand {
const toCheckPlayer = await Players.getAskedPlayer(packet.askedPlayer, player);
const pet = await PetEntities.getById(toCheckPlayer.petId);
if (!pet) {
response.push(makePacket(CommandPetPacketRes, {
foundPet: false
}));
response.push(makePacket(CommandPetPetNotFound, {}));
return;
}
const petModel = PetDataController.instance.getById(pet.typeId);
response.push(makePacket(CommandPetPacketRes, {
foundPet: true,
data: {
nickname: pet.nickname,
petTypeId: petModel.id,
rarity: petModel.rarity,
sex: pet.sex,
loveLevel: pet.getLoveLevelNumber()
}
nickname: pet.nickname,
petTypeId: petModel.id,
rarity: petModel.rarity,
sex: pet.sex as SexTypeShort,
loveLevel: pet.getLoveLevelNumber()
}));
}
}
2 changes: 1 addition & 1 deletion Core/src/core/missions/MissionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ export class MissionsController {
return retMission;
}

public static async addRandomMissionToPlayer(player: Player, difficulty: MissionDifficulty, exception: string = ""): Promise<MissionSlot> {
public static async addRandomMissionToPlayer(player: Player, difficulty: MissionDifficulty, exception = ""): Promise<MissionSlot> {
const mission = MissionDataController.instance.getRandomMission(difficulty, exception);
return await MissionsController.addMissionToPlayer(player, mission.id, difficulty, mission);
}
Expand Down
43 changes: 12 additions & 31 deletions Discord/src/commands/mission/MissionShop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,54 +24,35 @@ function getPacket(): CommandMissionShopPacketReq {
return makePacket(CommandMissionShopPacketReq, {});
}

export async function handleMissionShopBadge(context: PacketContext): Promise<void> {
async function handleBasicMissionShopItem(context: PacketContext, descriptionString: string, descriptionFormat: {
[keys: string]: string | number
}): Promise<void> {
const interaction = DiscordCache.getInteraction(context.discord!.interaction!);
await interaction?.followUp({
embeds: [
new DraftBotEmbed()
.formatAuthor(i18n.t("commands:shop.success", {
.formatAuthor(i18n.t("commands:shop.shopItems.basicMission.giveTitle", {
lng: interaction.userLanguage,
pseudo: interaction.user.username
}), interaction.user)
.setDescription(i18n.t("commands:shop.badgeBought", {
.setDescription(i18n.t(descriptionString, {
lng: interaction.userLanguage,
badgeName: "questMasterBadge"
...descriptionFormat
}))
]
});
}

export async function handleMissionShopBadge(context: PacketContext): Promise<void> {
await handleBasicMissionShopItem(context, "commands:shop.badgeBought", {badgeName: "questMasterBadge"});
}

export async function handleMissionShopMoney(packet: CommandMissionShopMoney, context: PacketContext): Promise<void> {
const interaction = DiscordCache.getInteraction(context.discord!.interaction!);
await interaction?.followUp({
embeds: [
new DraftBotEmbed()
.formatAuthor(i18n.t("commands:shop.shopItems.money.giveTitle", {
lng: interaction.userLanguage,
pseudo: interaction.user.username
}), interaction.user)
.setDescription(i18n.t("commands:shop.shopItems.money.giveDescription", {
lng: interaction.userLanguage,
amount: packet.amount
}))
]
});
await handleBasicMissionShopItem(context, "commands:shop.shopItems.money.giveDescription", {amount: packet.amount});
}

export async function handleMissionShopKingsFavor(context: PacketContext): Promise<void> {
const interaction = DiscordCache.getInteraction(context.discord!.interaction!);
await interaction?.followUp({
embeds: [
new DraftBotEmbed()
.formatAuthor(i18n.t("commands:shop.shopItems.kingsFavor.giveTitle", {
lng: interaction.userLanguage,
pseudo: interaction.user.username
}), interaction.user)
.setDescription(i18n.t("commands:shop.shopItems.kingsFavor.giveDescription", {
lng: interaction.userLanguage
}))
]
});
await handleBasicMissionShopItem(context, "commands:shop.shopItems.kingsFavor.giveDescription", {});
}

export async function handleLovePointsValueShopItem(packet: CommandMissionShopPetInformation, context: PacketContext): Promise<void> {
Expand Down
34 changes: 6 additions & 28 deletions Discord/src/commands/pet/PetCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import {CommandPetPacketReq, CommandPetPacketRes} from "../../../../Lib/src/pack
import {SlashCommandBuilder} from "@discordjs/builders";
import {DraftBotEmbed} from "../../messages/DraftBotEmbed";
import {DiscordCache} from "../../bot/DiscordCache";
import {DraftBotErrorEmbed} from "../../messages/DraftBotErrorEmbed";
import {KeycloakUser} from "../../../../Lib/src/keycloak/KeycloakUser";
import {PetData, PetUtils} from "../../utils/PetUtils";
import {PetUtils} from "../../utils/PetUtils";
import {PacketUtils} from "../../utils/PacketUtils";

/**
Expand All @@ -26,24 +25,8 @@ async function getPacket(interaction: DraftbotInteraction, keycloakUser: Keycloa

export async function handleCommandPetPacketRes(packet: CommandPetPacketRes, context: PacketContext): Promise<void> {
const interaction = DiscordCache.getInteraction(context.discord!.interaction);

if (interaction) {
if (!packet.foundPet) {
await interaction.reply({
embeds: [
new DraftBotErrorEmbed(
interaction.user,
interaction,
i18n.t("error:petDoesntExist", {lng: interaction.userLanguage})
)
]
});
return;
}

const petData: PetData = packet.data!;

const PetCommandEmbed = new DraftBotEmbed()
await interaction?.reply({
embeds: [new DraftBotEmbed()
.formatAuthor(
i18n.t("commands:pet.embedTitle", {
lng: interaction.userLanguage,
Expand All @@ -52,14 +35,9 @@ export async function handleCommandPetPacketRes(packet: CommandPetPacketRes, con
interaction.user
)
.setDescription(
PetUtils.petToString(interaction.userLanguage, petData)
);

await interaction.reply({
embeds: [PetCommandEmbed],
fetchReply: true
});
}
PetUtils.petToString(interaction.userLanguage, packet)
)]
});
}

export const commandInfo: ICommand = {
Expand Down
16 changes: 12 additions & 4 deletions Discord/src/packetHandlers/handlers/CommandHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
} from "../../../../Lib/src/packets/commands/CommandReportPacket";
import {CommandMapDisplayRes} from "../../../../Lib/src/packets/commands/CommandMapPacket";
import {handleCommandMapDisplayRes} from "../../commands/player/MapCommand";
import {CommandPetPacketRes} from "../../../../Lib/src/packets/commands/CommandPetPacket";
import {CommandPetPacketRes, CommandPetPetNotFound} from "../../../../Lib/src/packets/commands/CommandPetPacket";
import {handleCommandPetPacketRes} from "../../commands/pet/PetCommand";
import {
handleCommandPetFreeAcceptPacketRes,
Expand Down Expand Up @@ -183,15 +183,18 @@ import {
import {
CommandTopGuildsEmptyPacket,
CommandTopInvalidPagePacket,
CommandTopPacketResGlory, CommandTopPacketResGuild,
CommandTopPacketResScore, CommandTopPlayersEmptyPacket
CommandTopPacketResGlory,
CommandTopPacketResGuild,
CommandTopPacketResScore,
CommandTopPlayersEmptyPacket
} from "../../../../Lib/src/packets/commands/CommandTopPacket";
import {
handleCommandTopGuildsEmptyPacket,
handleCommandTopInvalidPagePacket,
handleCommandTopPacketResGlory,
handleCommandTopPacketResGuild,
handleCommandTopPacketResScore, handleCommandTopPlayersEmptyPacket
handleCommandTopPacketResScore,
handleCommandTopPlayersEmptyPacket
} from "../../commands/player/TopCommand";
import {
CommandGuildElderAcceptPacketRes,
Expand Down Expand Up @@ -236,6 +239,11 @@ export default class CommandHandlers {
await handleCommandPetPacketRes(packet, context);
}

@packetHandler(CommandPetPetNotFound)
async petNotFound(_packet: CommandPetPetNotFound, context: PacketContext): Promise<void> {
await handleClassicError(context, "error:petDoesntExist");
}

@packetHandler(CommandPetFreePacketRes)
async petFreeRes(packet: CommandPetFreePacketRes, context: PacketContext): Promise<void> {
await handleCommandPetFreePacketRes(packet, context);
Expand Down
12 changes: 6 additions & 6 deletions Discord/src/utils/PetUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import {Language} from "../../../Lib/src/Language";
import {PetConstants} from "../../../Lib/src/constants/PetConstants";
import i18n from "../translations/i18n";
import {EmoteUtils} from "./EmoteUtils";
import {StringConstants} from "../../../Lib/src/constants/StringConstants";
import {SexTypeShort, StringConstants} from "../../../Lib/src/constants/StringConstants";
import {finishInTimeDisplay} from "../../../Lib/src/utils/TimeUtils";

export type PetData = {
petTypeId: number,
nickname: string,
sex: string,
sex: SexTypeShort,
rarity: number,
loveLevel: number
};
Expand Down Expand Up @@ -40,7 +40,7 @@ export class PetUtils {
* @param typeId
* @param sex
*/
static petToShortString(lng: Language, nickname: string | undefined, typeId: number, sex: string): string {
static petToShortString(lng: Language, nickname: string | undefined, typeId: number, sex: SexTypeShort): string {
return i18n.t("commands:pet.shortPetField", {
lng,
emote: PetUtils.getPetIcon(typeId, sex),
Expand All @@ -53,7 +53,7 @@ export class PetUtils {
* @param typeId
* @param sex
*/
static getPetIcon(typeId: number, sex: string): string {
static getPetIcon(typeId: number, sex: SexTypeShort): string {
return EmoteUtils.translateEmojiToDiscord(sex === StringConstants.SEX.MALE.short ? DraftBotIcons.pets[typeId].emoteMale : DraftBotIcons.pets[typeId].emoteFemale);
}

Expand All @@ -80,7 +80,7 @@ export class PetUtils {
* @param typeId
* @param sex
*/
static getPetTypeName(lng: Language, typeId: number, sex: string): string {
static getPetTypeName(lng: Language, typeId: number, sex: SexTypeShort): string {
const sexStringContext: string = sex === StringConstants.SEX.MALE.short ? StringConstants.SEX.MALE.long : StringConstants.SEX.FEMALE.long;
return i18n.t(
`models:pets:${typeId}`,
Expand All @@ -94,7 +94,7 @@ export class PetUtils {
* @param sex
* @param lng
*/
static getLoveLevelDisplay(loveLevel: number, sex: string, lng: Language): string {
static getLoveLevelDisplay(loveLevel: number, sex: SexTypeShort, lng: Language): string {
const sexStringContext: string = sex === StringConstants.SEX.MALE.short ? StringConstants.SEX.MALE.long : StringConstants.SEX.FEMALE.long;
return i18n.t(`commands:pet.loveLevels.${loveLevel}`, {
context: sexStringContext as unknown,
Expand Down
2 changes: 2 additions & 0 deletions Lib/src/constants/StringConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export class Sex {
}
}

export type SexTypeShort = "m" | "f";

export class StringConstants {
static readonly PROGRESS_BAR_SIZE = 20;

Expand Down
21 changes: 13 additions & 8 deletions Lib/src/packets/commands/CommandPetPacket.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {DraftBotPacket, PacketDirection, sendablePacket} from "../DraftBotPacket";
import {SexTypeShort} from "../../constants/StringConstants";

@sendablePacket(PacketDirection.FRONT_TO_BACK)
export class CommandPetPacketReq extends DraftBotPacket {
Expand All @@ -10,13 +11,17 @@ export class CommandPetPacketReq extends DraftBotPacket {

@sendablePacket(PacketDirection.BACK_TO_FRONT)
export class CommandPetPacketRes extends DraftBotPacket {
foundPet!: boolean;
nickname!: string;

data?: {
nickname: string,
petTypeId: number,
rarity: number,
sex: string,
loveLevel: number
};
petTypeId!: number;

rarity!: number;

sex!: SexTypeShort;

loveLevel!: number;
}

@sendablePacket(PacketDirection.BACK_TO_FRONT)
export class CommandPetPetNotFound extends DraftBotPacket {
}

0 comments on commit 3e46260

Please sign in to comment.