Skip to content

Commit

Permalink
fix: deepsource
Browse files Browse the repository at this point in the history
  • Loading branch information
romain22222 committed Oct 16, 2023
1 parent e663247 commit c363ec5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 19 additions & 2 deletions src/core/missions/Campaign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,28 @@ 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");
}
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");
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit c363ec5

Please sign in to comment.