diff --git a/src/commands/admin/testCommands/Missions/SetCampaignBlobTestCommand.ts b/src/commands/admin/testCommands/Missions/SetCampaignBlobTestCommand.ts index aa85472b1..a82bc8b4f 100644 --- a/src/commands/admin/testCommands/Missions/SetCampaignBlobTestCommand.ts +++ b/src/commands/admin/testCommands/Missions/SetCampaignBlobTestCommand.ts @@ -23,13 +23,14 @@ const setCampaignBlobTestCommand = async (language: string, interaction: Command const [player] = await Players.getOrRegister(interaction.user.id); const missionsInfo = await PlayerMissionsInfos.getOfPlayer(player.id); const givenBlob = args[0]; - if (givenBlob.length !== Campaign.getMaxCampaignNumber() || !givenBlob.match(/^[01]+$/)) { + const isGoodBlob = new RegExp(/^[01]+$/).test(givenBlob); + if (givenBlob.length !== Campaign.getMaxCampaignNumber() || !isGoodBlob) { throw Error(`Blob invalide. Il doit être composé uniquement de 0 et de 1 et faire ${ Campaign.getMaxCampaignNumber() } caractères (actuellement ${ givenBlob.length } caractères, uniquement 0/1 : ${ - !!givenBlob.match(/^[01]+$/) + isGoodBlob }))`); } missionsInfo.campaignBlob = givenBlob; diff --git a/src/commands/admin/testCommands/Missions/SetCampaignTestCommand.ts b/src/commands/admin/testCommands/Missions/SetCampaignTestCommand.ts index c39c20631..69fd1c6b9 100644 --- a/src/commands/admin/testCommands/Missions/SetCampaignTestCommand.ts +++ b/src/commands/admin/testCommands/Missions/SetCampaignTestCommand.ts @@ -29,7 +29,7 @@ const setCampaignTestCommand = async (_language: string, interaction: CommandInt const campaignMission = Data.getModule("campaign").getObjectFromArray("missions", progression - 1) as unknown as MissionSlot; missionsInfo.campaignProgression = progression; - missionsInfo.campaignBlob = missionsInfo.campaignBlob.slice(0, progression - 1) + "0" + missionsInfo.campaignBlob.slice(progression); + missionsInfo.campaignBlob = `${missionsInfo.campaignBlob.slice(0, progression - 1)}0${missionsInfo.campaignBlob.slice(progression)}`; campaign.missionId = campaignMission.missionId; campaign.missionObjective = campaignMission.missionObjective; campaign.missionVariant = campaignMission.missionVariant; diff --git a/src/core/missions/Campaign.ts b/src/core/missions/Campaign.ts index 8399e271b..b011ad34b 100644 --- a/src/core/missions/Campaign.ts +++ b/src/core/missions/Campaign.ts @@ -12,6 +12,9 @@ export class Campaign { private static campaignModule: DataModule = null; + /** + * Get the maximum number of campaign missions + */ static getMaxCampaignNumber(): number { if (this.maxCampaignCache === -1) { this.maxCampaignCache = Campaign.getDataModule().getListSize("missions"); @@ -19,11 +22,18 @@ export class Campaign { return this.maxCampaignCache; } - // If the campaign blob is full of 1, it means that the player has completed all the campaign missions + /** + * Check if the given campaign blob has a next mission to complete + * @param campaignBlob + */ static hasNextCampaign(campaignBlob: string): boolean { return campaignBlob.includes("0"); } + /** + * Find the next campaign index to complete in the given campaign blob + * @param campaignBlob + */ static findNextCampaignIndex(campaignBlob: string): number { return campaignBlob.indexOf("0"); } @@ -43,7 +53,7 @@ export class Campaign { await missionModel.formatDescription(campaign.missionObjective, campaign.missionVariant, language, campaign.saveBlob), CompletedMissionType.CAMPAIGN) ); - missionInfo.campaignBlob = missionInfo.campaignBlob.slice(0, missionInfo.campaignProgression - 1) + "1" + missionInfo.campaignBlob.slice(missionInfo.campaignProgression); + missionInfo.campaignBlob = `${missionInfo.campaignBlob.slice(0, missionInfo.campaignProgression - 1)}1${missionInfo.campaignBlob.slice(missionInfo.campaignProgression)}`; missionInfo.campaignProgression = this.hasNextCampaign(missionInfo.campaignBlob) ? this.findNextCampaignIndex(missionInfo.campaignBlob) + 1 : 0; draftBotInstance.logsDatabase.logMissionCampaignProgress(player.discordUserId, missionInfo.campaignProgression).then(); } @@ -87,10 +97,17 @@ export class Campaign { return []; } + /** + * Get the default campaign blob + */ static getDefautCampaignBlob(): string { return "0".repeat(this.getMaxCampaignNumber()); } + /** + * Get the amount of missions completed in the given campaign blob + * @param campaignBlob + */ static getAmountOfCampaignCompleted(campaignBlob: string): number { return campaignBlob.split("1").length - 1; }