Skip to content

Commit

Permalink
fixes Codescenes
Browse files Browse the repository at this point in the history
  • Loading branch information
romain22222 committed Nov 16, 2024
1 parent 93180fb commit 4bab663
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 141 deletions.
18 changes: 10 additions & 8 deletions Core/src/core/missions/interfaces/fromPlaceToPlace.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {IMission} from "../IMission";
import {hoursToMilliseconds} from "../../../../../Lib/src/utils/TimeUtils";
import {MissionUtils} from "../../../../../Lib/src/utils/MissionUtils";
import {FromPlaceToPlaceParams, MissionUtils} from "../../../../../Lib/src/utils/MissionUtils";

const saveBlobFromData = function(startTimestamp: number, startMap: number): Buffer {
const saveBlob = Buffer.alloc(10);
Expand All @@ -9,20 +9,22 @@ const saveBlobFromData = function(startTimestamp: number, startMap: number): Buf
return saveBlob;
};

function checkLink(variantParams: FromPlaceToPlaceParams, startMap: number, endMap: number): boolean {
return variantParams.fromMap === startMap && variantParams.toMap === endMap;
}

export const missionInterface: IMission = {
areParamsMatchingVariantAndBlob: (variant, params, saveBlob) => {
if (!saveBlob) {
return false;
}
const variantParams = MissionUtils.fromPlaceToPlaceParamsFromVariant(variant);
const saveData = MissionUtils.fromPlaceToPlaceDataFromSaveBlob(saveBlob);
if (variantParams.orderMatter) {
return variantParams.toMap === params.mapId && variantParams.fromMap === saveData.startMap
&& saveData.startTimestamp + hoursToMilliseconds(variantParams.time) > Date.now();
}
return (variantParams.toMap === params.mapId && variantParams.fromMap === saveData.startMap
|| variantParams.fromMap === params.mapId && variantParams.toMap === saveData.startMap)
&& saveData.startTimestamp + hoursToMilliseconds(variantParams.time) > Date.now();
const otherMap = params.mapId as number;

return saveData.startTimestamp + hoursToMilliseconds(variantParams.time) > Date.now()
&& (checkLink(variantParams, saveData.startMap, otherMap)
|| !variantParams.orderMatter && checkLink(variantParams, otherMap, saveData.startMap));
},

generateRandomVariant: () => 0,
Expand Down
21 changes: 4 additions & 17 deletions Discord/src/commands/guild/GuildCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,18 @@ import {KeycloakUser} from "../../../../Lib/src/keycloak/KeycloakUser";
import {KeycloakUtils} from "../../../../Lib/src/keycloak/KeycloakUtils";
import {keycloakConfig} from "../../bot/DraftBotShard";
import {progressBar} from "../../../../Lib/src/utils/StringUtils";
import {Effect} from "../../../../Lib/src/enums/Effect";
import {PacketUtils} from "../../utils/PacketUtils";

/**
* Display all the information about a guild
*/
async function getPacket(interaction: DraftbotInteraction, keycloakUser: KeycloakUser): Promise<CommandGuildPacketReq | null> {

const guildNameOption = interaction.options.get("guild");
const askedGuildName = guildNameOption ? <string>guildNameOption.value : undefined;


let askedPlayer: { keycloakId?: string, rank?: number } = {keycloakId: keycloakUser.id};
const user = interaction.options.getUser("user");
if (user) {
const keycloakId = await KeycloakUtils.getKeycloakIdFromDiscordId(keycloakConfig, user.id, user.displayName);
if (!keycloakId) {
await interaction.reply({embeds: [new DraftBotErrorEmbed(interaction.user, interaction, i18n.t("error:playerDoesntExist", {lng: interaction.userLanguage}))]});
return null;
}
askedPlayer = {keycloakId};
}

const rankOption = interaction.options.get("rank");
if (rankOption) {
askedPlayer = {rank: <number>rankOption.value};
const askedPlayer = await PacketUtils.prepareAskedPlayer(interaction, keycloakUser);
if (!askedPlayer) {
return null;
}

return makePacket(CommandGuildPacketReq, {askedPlayer, askedGuildName});
Expand Down
126 changes: 67 additions & 59 deletions Discord/src/commands/mission/MissionsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,30 @@ import {
CommandMissionsPacketReq,
CommandMissionsPacketRes
} from "../../../../Lib/src/packets/commands/CommandMissionsPacket";
import {Effect} from "../../../../Lib/src/enums/Effect";
import {DraftBotEmbed} from "../../messages/DraftBotEmbed";
import {User} from "discord.js";
import {MissionType} from "../../../../Lib/src/interfaces/CompletedMission";
import {MissionUtils} from "../../utils/MissionUtils";
import {datesAreOnSameDay, finishInTimeDisplay, getTomorrowMidnight} from "../../../../Lib/src/utils/TimeUtils";
import {PacketUtils} from "../../utils/PacketUtils";

/**
* Get the packet to send to the server
* @param interaction
* @param keycloakUser
*/
async function getPacket(interaction: DraftbotInteraction, keycloakUser: KeycloakUser): Promise<CommandMissionsPacketReq | null> {
let askedPlayer: {
keycloakId?: string,
rank?: number
} = {keycloakId: keycloakUser.id};

const user = interaction.options.getUser("user");
if (user) {
const keycloakId = await KeycloakUtils.getKeycloakIdFromDiscordId(keycloakConfig, user.id, user.displayName);
if (!keycloakId) {
await interaction.reply({
embeds: [
new DraftBotErrorEmbed(interaction.user, interaction, i18n.t("error:playerDoesntExist", {lng: interaction.userLanguage}))
]
});
return null;
}
askedPlayer = {keycloakId};
const askedPlayer = await PacketUtils.prepareAskedPlayer(interaction, keycloakUser);
if (!askedPlayer) {
return null;
}
const rank = interaction.options.get("rank");
if (rank) {
askedPlayer = {rank: <number>rank.value};
}

return makePacket(CommandMissionsPacketReq, {askedPlayer});
}

/*
* Handle the response of the server to the missions command
* This is triggered when the player has not been found
/**
* Handle the case where the player is not found
* @param packet
* @param context
*/
export async function handleCommandMissionPlayerNotFoundPacket(packet: CommandMissionPlayerNotFoundPacket, context: PacketContext): Promise<void> {
const interaction = DiscordCache.getInteraction(context.discord!.interaction);
Expand All @@ -61,29 +48,14 @@ export async function handleCommandMissionPlayerNotFoundPacket(packet: CommandMi
});
}

/*
* Handle the response of the server to the missions command
* This is triggered when the player has been found
/**
* Get the campaign mission part of the missions command's response
* @param packet
* @param interaction
*/
export async function handleCommandMissionsPacketRes(packet: CommandMissionsPacketRes, context: PacketContext): Promise<void> {
const interaction = DiscordCache.getInteraction(context.discord!.interaction);
if (!interaction) {
return;
}
const keycloakUser = (await KeycloakUtils.getUserByKeycloakId(keycloakConfig, packet.keycloakId!))!;
if (!keycloakUser.attributes.discordId) {
throw new Error(`User of keycloakId ${packet.keycloakId} has no discordId`);
}
const discordUser = draftBotClient.users.cache.get(keycloakUser.attributes.discordId[0]) as User;
const missionCommandEmbed = new DraftBotEmbed();

missionCommandEmbed.formatAuthor(i18n.t("commands:missions.title", {
lng: interaction.userLanguage,
pseudo: discordUser.username
}), discordUser);

function getCampaignMissionPart(packet: CommandMissionsPacketRes, interaction: DraftbotInteraction): string {
const campaignMission = packet.missions.find(mission => mission.missionType === MissionType.CAMPAIGN);
const campaignMissionDescription = `${i18n.t("commands:missions.subcategories.campaign", {
return `${i18n.t("commands:missions.subcategories.campaign", {
lng: interaction.userLanguage,
current: packet.campaignProgression,
max: packet.maxCampaignNumber,
Expand All @@ -100,10 +72,17 @@ export async function handleCommandMissionsPacketRes(packet: CommandMissionsPack
})}`
: ""
}`;
}

/**
* Get the daily mission part of the missions command's response
* @param packet
* @param interaction
*/
function getDailyMissionPart(packet: CommandMissionsPacketRes, interaction: DraftbotInteraction): string {
const dailyMission = packet.missions.find(mission => mission.missionType === MissionType.DAILY)!;

const dailyMissionDescription = `${i18n.t("commands:missions.subcategories.daily", {
return `${i18n.t("commands:missions.subcategories.daily", {
lng: interaction.userLanguage
})}
${i18n.t(`commands:missions.${datesAreOnSameDay(
Expand All @@ -119,9 +98,20 @@ ${i18n.t(`commands:missions.${datesAreOnSameDay(
context: "other",
interpolation: {escapeValue: false}
})}`;
}

/**
* Get the side missions part of the missions command's response
* @param packet
* @param interaction
*/
function getSideMissionsPart(packet: CommandMissionsPacketRes, interaction: DraftbotInteraction): string {
const sideMissions = packet.missions.filter(mission => mission.missionType === MissionType.NORMAL);
const sideMissionsList: string = sideMissions.length > 0
return `${i18n.t("commands:missions.subcategories.sideMissions", {
lng: interaction.userLanguage,
current: sideMissions.length,
max: packet.maxSideMissionSlots
})}\n${sideMissions.length > 0
? sideMissions.map(mission => i18n.t("commands:missions.missionDisplay", {
lng: interaction.userLanguage,
mission: MissionUtils.formatBaseMission(mission, interaction.userLanguage),
Expand All @@ -134,15 +124,36 @@ ${i18n.t(`commands:missions.${datesAreOnSameDay(
})).join("\n")
: i18n.t("commands:missions.noCurrentMissions", {
lng: interaction.userLanguage
});
const sideMissionsDescription = `${i18n.t("commands:missions.subcategories.sideMissions", {
})}`;
}

/**
* Handle the response of the missions command
* @param packet
* @param context
*/
export async function handleCommandMissionsPacketRes(packet: CommandMissionsPacketRes, context: PacketContext): Promise<void> {
const interaction = DiscordCache.getInteraction(context.discord!.interaction);
if (!interaction) {
return;
}
const keycloakUser = (await KeycloakUtils.getUserByKeycloakId(keycloakConfig, packet.keycloakId!))!;
if (!keycloakUser.attributes.discordId) {
throw new Error(`User of keycloakId ${packet.keycloakId} has no discordId`);
}
const discordUser = draftBotClient.users.cache.get(keycloakUser.attributes.discordId[0]) as User;
const missionCommandEmbed = new DraftBotEmbed();

missionCommandEmbed.formatAuthor(i18n.t("commands:missions.title", {
lng: interaction.userLanguage,
current: sideMissions.length,
max: packet.maxSideMissionSlots
})}
${sideMissionsList}`;
pseudo: discordUser.username
}), discordUser);

missionCommandEmbed.setDescription([campaignMissionDescription, dailyMissionDescription, sideMissionsDescription].join("\n"));
missionCommandEmbed.setDescription([
getCampaignMissionPart(packet, interaction),
getDailyMissionPart(packet, interaction),
getSideMissionsPart(packet, interaction)
].join("\n"));
await interaction?.reply({
embeds: [missionCommandEmbed]
});
Expand All @@ -157,8 +168,5 @@ export const commandInfo: ICommand = {
SlashCommandBuilderGenerator.generateOption("missions", "rank", option)
.setRequired(false)) as SlashCommandBuilder,
getPacket,
requirements: {
disallowEffects: [Effect.NOT_STARTED, Effect.DEAD]
},
mainGuildCommand: false
};
23 changes: 4 additions & 19 deletions Discord/src/commands/pet/PetCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,17 @@ import {DraftBotEmbed} from "../../messages/DraftBotEmbed";
import {DiscordCache} from "../../bot/DiscordCache";
import {DraftBotErrorEmbed} from "../../messages/DraftBotErrorEmbed";
import {KeycloakUser} from "../../../../Lib/src/keycloak/KeycloakUser";
import {KeycloakUtils} from "../../../../Lib/src/keycloak/KeycloakUtils";
import {keycloakConfig} from "../../bot/DraftBotShard";
import {Effect} from "../../../../Lib/src/enums/Effect";
import {PetData, PetUtils} from "../../utils/PetUtils";
import {PacketUtils} from "../../utils/PacketUtils";

/**
* Display all the information about a Pet
*/
async function getPacket(interaction: DraftbotInteraction, keycloakUser: KeycloakUser): Promise<CommandPetPacketReq | null> {

let askedPlayer: { keycloakId?: string, rank?: number } = {keycloakId: keycloakUser.id};
const user = interaction.options.getUser("user");
if (user) {
const keycloakId = await KeycloakUtils.getKeycloakIdFromDiscordId(keycloakConfig, user.id, user.displayName);
if (!keycloakId) {
await interaction.reply({embeds: [new DraftBotErrorEmbed(interaction.user, interaction, i18n.t("error:playerDoesntExist", {lng: interaction.userLanguage}))]});
return null;
}
askedPlayer = {keycloakId};
const askedPlayer = await PacketUtils.prepareAskedPlayer(interaction, keycloakUser);
if (!askedPlayer) {
return null;
}

const rankOption = interaction.options.get("rank");
if (rankOption) {
askedPlayer = {rank: <number>rankOption.value};
}

return makePacket(CommandPetPacketReq, {askedPlayer});
}

Expand Down
26 changes: 4 additions & 22 deletions Discord/src/commands/player/InventoryCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,12 @@ import {
} from "../../../../Lib/src/packets/commands/CommandInventoryPacket";
import {DiscordItemUtils} from "../../utils/DiscordItemUtils";
import {sendInteractionNotForYou} from "../../utils/ErrorUtils";
import {Effect} from "../../../../Lib/src/enums/Effect";
import {PacketUtils} from "../../utils/PacketUtils";

async function getPacket(interaction: DraftbotInteraction, keycloakUser: KeycloakUser): Promise<CommandInventoryPacketReq | null> {
let askedPlayer: {
keycloakId?: string,
rank?: number
} = {keycloakId: keycloakUser.id};

const user = interaction.options.getUser("user");
if (user) {
const keycloakId = await KeycloakUtils.getKeycloakIdFromDiscordId(keycloakConfig, user.id, user.displayName);
if (!keycloakId) {
await interaction.reply({
embeds: [
new DraftBotErrorEmbed(interaction.user, interaction, i18n.t("error:playerDoesntExist", {lng: interaction.userLanguage}))
]
});
return null;
}
askedPlayer = {keycloakId};
}
const rank = interaction.options.get("rank");
if (rank) {
askedPlayer = {rank: <number>rank.value};
const askedPlayer = await PacketUtils.prepareAskedPlayer(interaction, keycloakUser);
if (!askedPlayer) {
return null;
}

return makePacket(CommandInventoryPacketReq, {askedPlayer});
Expand Down
20 changes: 4 additions & 16 deletions Discord/src/commands/player/ProfileCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,19 @@ import {KeycloakUser} from "../../../../Lib/src/keycloak/KeycloakUser";
import {KeycloakUtils} from "../../../../Lib/src/keycloak/KeycloakUtils";
import {keycloakConfig} from "../../bot/DraftBotShard";
import {DraftBotIcons} from "../../../../Lib/src/DraftBotIcons";
import {Effect} from "../../../../Lib/src/enums/Effect";
import {PetUtils} from "../../utils/PetUtils";
import {ClassUtils} from "../../utils/ClassUtils";
import {EmoteUtils} from "../../utils/EmoteUtils";
import {PacketUtils} from "../../utils/PacketUtils";

/**
* Display the profile of a player
*/
async function getPacket(interaction: DraftbotInteraction, keycloakUser: KeycloakUser): Promise<CommandProfilePacketReq | null> {
let askedPlayer: { keycloakId?: string, rank?: number } = {keycloakId: keycloakUser.id};

const user = interaction.options.getUser("user");
if (user) {
const keycloakId = await KeycloakUtils.getKeycloakIdFromDiscordId(keycloakConfig, user.id, user.displayName);
if (!keycloakId) {
await interaction.reply({embeds: [new DraftBotErrorEmbed(interaction.user, interaction, i18n.t("error:playerDoesntExist", {lng: interaction.userLanguage}))]});
return null;
}
askedPlayer = {keycloakId};
const askedPlayer = await PacketUtils.prepareAskedPlayer(interaction, keycloakUser);
if (!askedPlayer) {
return null;
}
const rank = interaction.options.get("rank");
if (rank) {
askedPlayer = {rank: <number>rank.value};
}

return makePacket(CommandProfilePacketReq, {askedPlayer});
}

Expand Down
Loading

0 comments on commit 4bab663

Please sign in to comment.