Skip to content

Commit

Permalink
Fix #1589
Browse files Browse the repository at this point in the history
  • Loading branch information
Pietagorh committed Nov 17, 2022
1 parent 3c8204e commit 09ba1c7
Show file tree
Hide file tree
Showing 27 changed files with 66 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/commands/CommandsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class CommandsManager {
*/
static async effectError(user: User, tr: TranslationModule, interaction: CommandInteraction, shouldReply = false): Promise<void> {
const player = await Players.getByDiscordUserId(user.id);
const textValues = effectsErrorTextValue(interaction.user, tr.language, player, interaction.createdAt);
const textValues = effectsErrorTextValue(interaction.user, tr.language, player);
const embed = new DraftBotEmbed().setErrorColor()
.formatAuthor(textValues.title, user)
.setDescription(textValues.description);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/admin/testCommands/Map/TravelTestCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const travelTestCommand = async (language: string, interaction: CommandInteracti
throw new Error(`Erreur travel : Maps non reliées. Maps reliées avec la map ${mapStart} : ${conMapsWthStart.toString()}`);
}

await Maps.startTravel(player, link, interaction.createdAt.valueOf(), NumberChangeReason.TEST, interaction.createdAt);
await Maps.startTravel(player, link, Date.now(), NumberChangeReason.TEST);
await player.save();
return format(commandInfo.messageWhenExecuted, {
mapNameStart: (await MapLocations.getById(mapStart)).getDisplayName(language),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const playerSuicideTestCommand = async (language: string, interaction: CommandIn
overHealCountsForMission: true,
shouldPokeMission: true
});
await player.killIfNeeded(interaction.channel, language, NumberChangeReason.TEST, interaction.createdAt);
await player.killIfNeeded(interaction.channel, language, NumberChangeReason.TEST);
await Promise.all([player.save(), player.save()]);

return commandInfo.messageWhenExecuted;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ const skipTutorialTestCommand = async (language: string, interaction: CommandInt
player.effectEndDate = new Date();
player.effectDuration = 0;
player.health = await player.getMaxHealth();
await TravelTime.removeEffect(player, NumberChangeReason.TEST, interaction.createdAt);
await Maps.startTravel(player, await MapLinks.getRandomLink(), 0, NumberChangeReason.TEST, interaction.createdAt);
await TravelTime.removeEffect(player, NumberChangeReason.TEST);
await Maps.startTravel(player, await MapLinks.getRandomLink(), 0, NumberChangeReason.TEST);
await player.save();

return commandInfo.messageWhenExecuted;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const commandInfo: ITestCommand = {
*/
const jailPlayerTestCommand = async (language: string, interaction: CommandInteraction, args: string[]): Promise<string> => {
const [player] = await Players.getOrRegister(getIdFromMention(args[0]));
await TravelTime.applyEffect(player, EffectsConstants.EMOJI_TEXT.LOCKED, 0, new Date(), NumberChangeReason.TEST, interaction.createdAt);
await TravelTime.applyEffect(player, EffectsConstants.EMOJI_TEXT.LOCKED, 0, new Date(), NumberChangeReason.TEST);
await player.save();
return format(commandInfo.messageWhenExecuted, {player: args[0]});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const playerEffectTestCommand = async (language: string, interaction: CommandInt
const [player] = await Players.getOrRegister(interaction.user.id);
const effectMalus = ":" + args[0] + ":";
if (Object.keys(PlayerConstants.EFFECT_MALUS).includes(effectMalus)) {
await TravelTime.applyEffect(player, effectMalus, 0, new Date(), NumberChangeReason.TEST, interaction.createdAt);
await TravelTime.applyEffect(player, effectMalus, 0, new Date(), NumberChangeReason.TEST);
await player.save();
return format(commandInfo.messageWhenExecuted, {effect: effectMalus});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const commandInfo: ITestCommand = {
const removePlayerEffectTestCommand = async (language: string, interaction: CommandInteraction): Promise<string> => {
const [player] = await Players.getOrRegister(interaction.user.id);

await TravelTime.removeEffect(player, NumberChangeReason.TEST, interaction.createdAt);
await TravelTime.removeEffect(player, NumberChangeReason.TEST);
await player.save();

return commandInfo.messageWhenExecuted;
Expand Down
4 changes: 2 additions & 2 deletions src/commands/guild/GuildDailyCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ async function alterationHealEveryMember(guildLike: GuildLike, stringInfos: Stri
}
else if (member.effect !== EffectsConstants.EMOJI_TEXT.DEAD && member.effect !== EffectsConstants.EMOJI_TEXT.LOCKED) {
noAlteHeal = false;
await TravelTime.removeEffect(member, NumberChangeReason.GUILD_DAILY, stringInfos.interaction.createdAt);
await TravelTime.removeEffect(member, NumberChangeReason.GUILD_DAILY);
}
});
if (!needsHeal && noAlteHeal) {
Expand Down Expand Up @@ -394,7 +394,7 @@ async function executeCommand(interaction: CommandInteraction, language: string,
return;
}

const time = millisecondsToHours(interaction.createdAt.valueOf() - guild.lastDailyAt.valueOf());
const time = millisecondsToHours(Date.now() - guild.lastDailyAt.valueOf());
if (time < GuildDailyConstants.TIME_BETWEEN_DAILIES && !forcedReward) {
await replyErrorMessage(
interaction,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/player/DailyCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async function isWrongObjectForDaily(activeObject: ObjectItem, interaction: Comm
*/
async function dailyNotReady(interaction: CommandInteraction, player: Player, language: string, dailyModule: TranslationModule): Promise<boolean> {
const inventoryInfo = await InventoryInfos.getOfPlayer(player.id);
const time = millisecondsToHours(interaction.createdAt.valueOf() - inventoryInfo.lastDailyAt.valueOf());
const time = millisecondsToHours(Date.now() - inventoryInfo.lastDailyAt.valueOf());
if (time < DailyConstants.TIME_BETWEEN_DAILIES) {
await replyErrorMessage(
interaction,
Expand Down
6 changes: 3 additions & 3 deletions src/commands/player/FightCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function getAcceptCallback(
): (user: User) => Promise<boolean> {
return async function(user: User): Promise<boolean> {
const incomingFighterPlayer = await Players.getByDiscordUserId(user.id);
const attackerFightErrorStatus = await canFight(incomingFighterPlayer, true, interaction.createdAt);
const attackerFightErrorStatus = await canFight(incomingFighterPlayer, true, new Date());
if (askedPlayer !== null && incomingFighterPlayer.discordUserId !== askedPlayer.discordUserId) {
return false;
}
Expand Down Expand Up @@ -226,14 +226,14 @@ async function executeCommand(interaction: CommandInteraction, language: string,
await replyErrorMessage(interaction, language, fightTranslationModule.get("error.fightHimself"));
return;
}
const attackerFightErrorStatus = await canFight(player, true, interaction.createdAt);
const attackerFightErrorStatus = await canFight(player, true, new Date());
if (attackerFightErrorStatus !== FightConstants.FIGHT_ERROR.NONE) {
await sendError(interaction, fightTranslationModule, attackerFightErrorStatus, false, true);
return;
}
let askedFighter: PlayerFighter | null;
if (askedEntity) {
const defenderFightErrorStatus = await canFight(askedEntity, true, interaction.createdAt);
const defenderFightErrorStatus = await canFight(askedEntity, true, new Date());
if (defenderFightErrorStatus !== FightConstants.FIGHT_ERROR.NONE) {
await sendError(interaction, fightTranslationModule, defenderFightErrorStatus, true, true);
return;
Expand Down
9 changes: 4 additions & 5 deletions src/commands/player/ProfileCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,13 @@ function getPetField(profileModule: TranslationModule, pet: PetEntity, petModel:
* Get the time left field of the profile
* @param profileModule
* @param askedPlayer
* @param interaction
*/
function getTimeLeftField(profileModule: TranslationModule, askedPlayer: Player, interaction: CommandInteraction): EmbedField {
function getTimeLeftField(profileModule: TranslationModule, askedPlayer: Player): EmbedField {
return {
name: profileModule.get("timeLeft.fieldName"),
value: profileModule.format("timeLeft.fieldValue", {
effect: askedPlayer.effect,
timeLeft: minutesDisplay(millisecondsToMinutes(askedPlayer.effectEndDate.valueOf() - interaction.createdAt.valueOf()))
timeLeft: minutesDisplay(millisecondsToMinutes(askedPlayer.effectEndDate.valueOf() - Date.now()))
}),
inline: false
};
Expand Down Expand Up @@ -282,12 +281,12 @@ async function generateFields(
getRankingField(profileModule, rank, numberOfPlayers, askedPlayer));

if (!askedPlayer.checkEffect()) {
if (interaction.createdAt >= askedPlayer.effectEndDate) {
if (new Date() >= askedPlayer.effectEndDate) {
titleEffect = Constants.DEFAULT_HEALED_EFFECT;
fields.push(getNoTimeLeftField(profileModule));
}
else {
fields.push(getTimeLeftField(profileModule, askedPlayer, interaction));
fields.push(getTimeLeftField(profileModule, askedPlayer));
}
}

Expand Down
22 changes: 10 additions & 12 deletions src/commands/player/ReportCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,11 @@ type TextInformation = { interaction: CommandInteraction, language: string, tr?:
/**
* Initiates a new player on the map
* @param player
* @param now
*/
async function initiateNewPlayerOnTheAdventure(player: Player, now: Date): Promise<void> {
async function initiateNewPlayerOnTheAdventure(player: Player): Promise<void> {
await Maps.startTravel(player, await MapLinks.getById(Constants.BEGINNING.START_MAP_LINK),
getTimeFromXHoursAgo(Constants.REPORT.HOURS_USED_TO_CALCULATE_FIRST_REPORT_REWARD).valueOf(),
NumberChangeReason.NEW_PLAYER,
now);
NumberChangeReason.NEW_PLAYER);
await player.save();
}

Expand Down Expand Up @@ -128,7 +126,7 @@ async function executeSmallEvent(interaction: CommandInteraction, language: stri
}

// Save
await PlayerSmallEvents.createPlayerSmallEvent(player.id, event, interaction.createdAt.valueOf()).save();
await PlayerSmallEvents.createPlayerSmallEvent(player.id, event, Date.now()).save();
}

/**
Expand Down Expand Up @@ -193,7 +191,7 @@ async function sendTravelPath(player: Player, interaction: CommandInteraction, l
inline: true
});
if (effect !== null) {
const errorMessageObject = effectsErrorTextValue(interaction.user, language, player, interaction.createdAt);
const errorMessageObject = effectsErrorTextValue(interaction.user, language, player);
travelEmbed.addFields({
name: errorMessageObject.title,
value: errorMessageObject.description,
Expand Down Expand Up @@ -332,7 +330,7 @@ async function chooseDestination(

if (destinationMaps.length === 1 || RandomUtils.draftbotRandom.bool(1, 3) && player.mapLinkId !== Constants.BEGINNING.LAST_MAP_LINK) {
const newLink = await MapLinks.getLinkByLocations(await player.getDestinationId(), destinationMaps[0]);
await Maps.startTravel(player, newLink, Date.now(), NumberChangeReason.BIG_EVENT, new Date());
await Maps.startTravel(player, newLink, Date.now(), NumberChangeReason.BIG_EVENT);
await destinationChoseMessage(player, destinationMaps[0], interaction, language);
return;
}
Expand All @@ -357,7 +355,7 @@ async function chooseDestination(
collector.on("end", async (collected) => {
const mapId = collected.first() ? destinationMaps[destinationChoiceEmotes.indexOf(collected.first().emoji.name)] : destinationMaps[RandomUtils.randInt(0, destinationMaps.length)];
const newLink = await MapLinks.getLinkByLocations(await player.getDestinationId(), mapId);
await Maps.startTravel(player, newLink, Date.now(), NumberChangeReason.BIG_EVENT, new Date());
await Maps.startTravel(player, newLink, Date.now(), NumberChangeReason.BIG_EVENT);
await destinationChoseMessage(player, mapId, interaction, language);
BlockingUtils.unblockPlayer(player.discordUserId, BlockingConstants.REASONS.CHOOSE_DESTINATION);
});
Expand Down Expand Up @@ -402,7 +400,7 @@ async function updatePlayerInfos(

await player.setLastReportWithEffect(
randomPossibility.lostTime,
randomPossibility.effect, textInformation.interaction.createdAt
randomPossibility.effect
);
if (randomPossibility.item) {
await giveRandomItem((await textInformation.interaction.guild.members.fetch(player.discordUserId)).user, textInformation.interaction.channel, textInformation.language, player);
Expand Down Expand Up @@ -514,7 +512,7 @@ async function doPossibility(
BlockingUtils.unblockPlayer(player.discordUserId, BlockingConstants.REASONS.REPORT);
const resultMsg = await textInformation.interaction.channel.send({content: result});

if (!await player.killIfNeeded(textInformation.interaction.channel, textInformation.language, NumberChangeReason.BIG_EVENT, textInformation.interaction.createdAt)) {
if (!await player.killIfNeeded(textInformation.interaction.channel, textInformation.language, NumberChangeReason.BIG_EVENT)) {
await chooseDestination(player, textInformation.interaction, textInformation.language, randomPossibility.restrictedMaps);
}

Expand Down Expand Up @@ -647,7 +645,7 @@ async function executeCommand(
forceSmallEvent: string = null
): Promise<void> {
if (player.score === 0 && player.effect === EffectsConstants.EMOJI_TEXT.BABY) {
await initiateNewPlayerOnTheAdventure(player, interaction.createdAt);
await initiateNewPlayerOnTheAdventure(player);
}

if (await sendBlockedError(interaction, language)) {
Expand Down Expand Up @@ -682,7 +680,7 @@ async function executeCommand(
}

if (player.mapLinkId === null) {
await Maps.startTravel(player, await MapLinks.getRandomLink(), interaction.createdAt.valueOf(), NumberChangeReason.DEBUG, interaction.createdAt);
await Maps.startTravel(player, await MapLinks.getRandomLink(), Date.now(), NumberChangeReason.DEBUG);
return BlockingUtils.unblockPlayer(player.discordUserId, "reportCommand");
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/player/RespawnCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ async function executeCommand(interaction: CommandInteraction, language: string,

await player.save();

await TravelTime.removeEffect(player, NumberChangeReason.RESPAWN, interaction.createdAt);
await TravelTime.removeEffect(player, NumberChangeReason.RESPAWN);
await Maps.stopTravel(player);
const newlink = await MapLinks.getLinkByLocations(
await player.getPreviousMapId(),
await player.getDestinationId()
);
await Maps.startTravel(player, newlink, interaction.createdAt.valueOf(), NumberChangeReason.RESPAWN, interaction.createdAt);
await Maps.startTravel(player, newlink, Date.now(), NumberChangeReason.RESPAWN);

await PlayerSmallEvents.removeSmallEventsOfPlayer(player.id);

Expand Down
4 changes: 2 additions & 2 deletions src/commands/player/ShopCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ function getHealAlterationShopItem(translationModule: TranslationModule, interac
translationModule,
async (message) => {
const [player] = await Players.getOrRegister(message.user.id);
if (player.currentEffectFinished(interaction.createdAt)) {
if (player.currentEffectFinished(new Date())) {
await sendErrorMessage(message.user, interaction, message.language, translationModule.get("error.nothingToHeal"));
return false;
}
if (player.effect !== EffectsConstants.EMOJI_TEXT.DEAD && player.effect !== EffectsConstants.EMOJI_TEXT.LOCKED) {
await TravelTime.removeEffect(player, NumberChangeReason.SHOP, interaction.createdAt);
await TravelTime.removeEffect(player, NumberChangeReason.SHOP);
await player.save();
}
await MissionsController.update(player, message.sentMessage.channel, translationModule.language, {missionId: "recoverAlteration"});
Expand Down
2 changes: 1 addition & 1 deletion src/commands/player/SwitchCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async function buildSwitchChoiceItems(toSwitchItems: InventorySlot[], language:
*/
function addDailyTimeBecauseSwitch(interaction: CommandInteraction, invInfo: InventoryInfo): void {
const nextDailyDate = moment(invInfo.lastDailyAt).add(DailyConstants.TIME_BETWEEN_DAILIES, "h"); // eslint-disable-line new-cap
const timeToCheck = millisecondsToHours(nextDailyDate.valueOf() - interaction.createdAt.valueOf());
const timeToCheck = millisecondsToHours(nextDailyDate.valueOf() - Date.now());
const maxTime = DailyConstants.TIME_BETWEEN_DAILIES - SwitchConstants.TIME_ADDED_MULTIPLIER;
if (timeToCheck < 0) {
invInfo.updateLastDailyAt();
Expand Down
4 changes: 2 additions & 2 deletions src/commands/player/TopCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ async function displayTop(
}));
let description = [];
const pseudos = await getPseudosOfList(playersToShow, language);
const badgeStates = await getBadgeStatesOfList(playersToShow, language, interaction.createdAt);
const badgeStates = await getBadgeStatesOfList(playersToShow, language, new Date());
for (const playerToShow of playersToShow) {
const rank = playersToShow.indexOf(playerToShow);
description.push(topModule.format("playerRankLine", {
Expand Down Expand Up @@ -186,7 +186,7 @@ async function displayTop(
if (timing === TopConstants.TIMING_WEEKLY) {
topDisplay.setFooter({
text: topModule.format("nextReset", {
time: parseTimeDifference(interaction.createdAt.valueOf(), getNextSundayMidnight(), language)
time: parseTimeDifference(Date.now(), getNextSundayMidnight(), language)
}),
iconURL: TopConstants.LINK_CLOCK_FOOTER
});
Expand Down
2 changes: 1 addition & 1 deletion src/commands/player/UnlockCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function callbackUnlockCommand(
const [playerToUnlock] = await Players.getOrRegister(entityCouple.locked.discordUserId); // released player
const [playerUnlocker] = await Players.getOrRegister(entityCouple.unlocker.discordUserId); // player who unlocks
if (reaction.first().emoji.name === Constants.MENU_REACTION.ACCEPT) {
await TravelTime.removeEffect(playerToUnlock, NumberChangeReason.UNLOCK, textInformation.interaction.createdAt);
await TravelTime.removeEffect(playerToUnlock, NumberChangeReason.UNLOCK);
await playerUnlocker.addMoney({
amount: -UnlockConstants.PRICE_FOR_UNLOCK,
channel: textInformation.interaction.channel,
Expand Down
8 changes: 4 additions & 4 deletions src/core/bot/DraftBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ export class DraftBot {

/**
* Updates the global log files
* @param now
*/
updateGlobalLogsFile(now: Date): void {
updateGlobalLogsFile(): void {
const now = new Date();
/* Find first available log file */
let i = 1;
do {
Expand All @@ -313,7 +313,7 @@ export class DraftBot {
this.manageLogs(originalConsoleError);
}

this.updateGlobalLogsFile(new Date());
this.updateGlobalLogsFile();
this.currLogsCount = 0;

const addConsoleLog = this.functionToAddLogToFile(this);
Expand Down Expand Up @@ -393,7 +393,7 @@ export class DraftBot {
);
thisInstance.currLogsCount++;
if (thisInstance.currLogsCount > Constants.LOGS.LOG_COUNT_LINE_LIMIT) {
thisInstance.updateGlobalLogsFile(now);
thisInstance.updateGlobalLogsFile();
thisInstance.currLogsCount = 0;
}
}
Expand Down
14 changes: 6 additions & 8 deletions src/core/database/game/models/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,9 @@ export class Player extends Model {
* This function is called when a player receives an effect after a report
* @param timeMalus
* @param effectMalus
* @param now
*/
public async setLastReportWithEffect(timeMalus: number, effectMalus: string, now: Date): Promise<void> {
await TravelTime.applyEffect(this, effectMalus, timeMalus, new Date(), NumberChangeReason.BIG_EVENT, now);
public async setLastReportWithEffect(timeMalus: number, effectMalus: string): Promise<void> {
await TravelTime.applyEffect(this, effectMalus, timeMalus, new Date(), NumberChangeReason.BIG_EVENT);
await this.save();
}

Expand All @@ -356,13 +355,12 @@ export class Player extends Model {
* @param channel
* @param language
* @param reason
* @param now
*/
public async killIfNeeded(channel: TextBasedChannel, language: string, reason: NumberChangeReason, now: Date): Promise<boolean> {
public async killIfNeeded(channel: TextBasedChannel, language: string, reason: NumberChangeReason): Promise<boolean> {
if (this.health > 0) {
return false;
}
await TravelTime.applyEffect(this, EffectsConstants.EMOJI_TEXT.DEAD, 0, new Date(), reason, now);
await TravelTime.applyEffect(this, EffectsConstants.EMOJI_TEXT.DEAD, 0, new Date(), reason);
const tr = Translations.getModule("models.players", language);
await channel.send({content: tr.format("ko", {pseudo: this.getPseudo(language)})});

Expand Down Expand Up @@ -406,13 +404,13 @@ export class Player extends Model {
/**
* get the amount of time remaining before the effect ends
*/
public effectRemainingTime(now: Date): number {
public effectRemainingTime(): number {
let remainingTime = 0;
if (Object.values(EffectsConstants.EMOJI_TEXT).includes(this.effect) || this.effect === EffectsConstants.EMOJI_TEXT.OCCUPIED) {
if (!this.effectEndDate || this.effectEndDate.valueOf() === 0) {
return 0;
}
remainingTime = this.effectEndDate.valueOf() - now.valueOf();
remainingTime = this.effectEndDate.valueOf() - Date.now();
}
if (remainingTime < 0) {
remainingTime = 0;
Expand Down

0 comments on commit 09ba1c7

Please sign in to comment.