Skip to content

Commit

Permalink
Merge pull request #163 from Corpelijn/master
Browse files Browse the repository at this point in the history
Merge changes for immutable field
  • Loading branch information
Agadar authored Feb 28, 2024
2 parents 93082f4 + b439c37 commit fa060bd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/bot-commands/commands/danktimesbot-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,9 @@ export class DankTimesBotCommands implements IDankTimesBotCommands {

const recipient: User = chat.getOrCreateUser(recipientId, msg.reply_to_message?.from?.username);
chat.alterUserScore(new AlterUserScoreArgs(user, -amount, AlterUserScoreArgs.DANKTIMESBOT_ORIGIN_NAME,
AlterUserScoreArgs.DONATION_GIVEN_REASON));
AlterUserScoreArgs.DONATION_GIVEN_REASON, undefined, true));
amount = chat.alterUserScore(new AlterUserScoreArgs(recipient, amount,
AlterUserScoreArgs.DANKTIMESBOT_ORIGIN_NAME, AlterUserScoreArgs.DONATION_RECEIVED_REASON));
AlterUserScoreArgs.DANKTIMESBOT_ORIGIN_NAME, AlterUserScoreArgs.DONATION_RECEIVED_REASON, undefined, true));

return `🎉 ${user.name} donated ${amount} internet points to ${recipient.name} 🎉`;
}
Expand Down
2 changes: 2 additions & 0 deletions src/chat/alter-user-score-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ export class AlterUserScoreArgs {
* not being caused by a plugin.
* @param reason The reason for the score change, e.g. 'random.danktime' or 'hardcoremode.punishment'.
* @param timestamp Optional timestamp of the score change. Used for hardmode punishment.
* @param immutable Disallows the score to be changed by a plugin. Default is false.
*/
constructor(
public readonly user: User,
public readonly amount: number,
public readonly nameOfOriginPlugin: string,
public readonly reason: string,
public readonly timestamp?: number,
public readonly immutable = false,
) {}
}
2 changes: 1 addition & 1 deletion src/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ export class Chat {
return 0;
}
const preEvent = new PreUserScoreChangedEventArguments(this, alterUserScoreArgs.user, alterUserScoreArgs.amount,
alterUserScoreArgs.reason, alterUserScoreArgs.nameOfOriginPlugin);
alterUserScoreArgs.reason, alterUserScoreArgs.nameOfOriginPlugin, alterUserScoreArgs.immutable);
this.pluginHost.triggerEvent(PluginEvent.PreUserScoreChange, preEvent);

const correctedAmount = alterUserScoreArgs.user.alterScore(preEvent.changeInScore, alterUserScoreArgs.timestamp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,21 @@ export class PreUserScoreChangedEventArguments extends PluginEventArguments {
public set changeInScore(newChangeInScore: number) {
if (isNaN(newChangeInScore)) {
console.error(`Failed to set new score change for source '${this.nameOfOriginPlugin}' for reason '${this.reason}': not a number`);
} else if (this.isImmutable) {
console.warn(`Cannot change the value for an immutable event from source '${this.nameOfOriginPlugin}' with reason '${this.reason}'.`);
} else {
this.myChangeInScore = newChangeInScore;
}
}

/**
* Indicates if the event is immutable.
*/
public get immutable(): boolean {
return this.isImmutable;
}

private isImmutable = false;
private myChangeInScore = 0;

/**
Expand All @@ -41,11 +51,13 @@ export class PreUserScoreChangedEventArguments extends PluginEventArguments {
* @param reason The reason for the score change, e.g. 'random.danktime' or 'hardcoremode.punishment'.
* @param nameOfOriginPlugin The name of the plugin that is causing the score change, or empty if it is
* not being caused by a plugin.
* @param immutable Disallows the score delta to be changed.
*/
constructor(chat: Chat, user: User, changeInScore: number, reason: string, nameOfOriginPlugin: string) {
constructor(chat: Chat, user: User, changeInScore: number, reason: string, nameOfOriginPlugin: string, immutable: boolean) {
super(nameOfOriginPlugin, reason);
this.chat = chat;
this.user = user;
this.changeInScore = changeInScore;
this.isImmutable = immutable;
}
}

0 comments on commit fa060bd

Please sign in to comment.