Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gamble): add !gamble wins to see how many jackpots user won #4740

Merged
merged 1 commit into from Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions locales/en.json
Expand Up @@ -1190,6 +1190,7 @@
"loseWithJackpot": "$sender, you LOST! You now have $points $pointsName. Jackpot increased to $jackpot $jackpotName",
"lose": "$sender, you LOST! You now have $points $pointsName",
"currentJackpot": "$sender, current jackpot for $command is $points $pointsName",
"winJackpotCount": "$sender, you won $count jackpots",
"jackpotIsDisabled": "$sender, jackpot is disabled for $command."
}
},
Expand Down
1 change: 1 addition & 0 deletions src/database/entity/user.ts
Expand Up @@ -12,6 +12,7 @@ export interface UserInterface {
subscribeTier?: string; subscribeCumulativeMonths?: number; subscribeStreak?: number; giftedSubscribes?: number;
messages?: number;
extra: {
jackpotWins?: number;
levels?: {
xp: string; // we need to use string as we cannot stringify bigint in typeorm
xpOfflineGivenAt: number;
Expand Down
31 changes: 27 additions & 4 deletions src/games/gamble.ts
@@ -1,4 +1,6 @@
import _ from 'lodash';
import {
get, isNil, random, set,
} from 'lodash';
import { getRepository } from 'typeorm';

import { User } from '../database/entity/user';
Expand All @@ -11,6 +13,7 @@ import { getUserHighestPermission } from '../helpers/permissions/';
import { getPointsName } from '../helpers/points';
import pointsSystem from '../systems/points';
import { translate } from '../translate';
import users from '../users';
import Game from './_interface';

const ERROR_NOT_ENOUGH_OPTIONS = '0';
Expand Down Expand Up @@ -55,7 +58,7 @@ class Gamble extends Game {
opts.sender['message-type'] = 'chat'; // force responses to chat
try {
const parsed = opts.parameters.trim().match(/^([\d]+|all)$/);
if (_.isNil(parsed)) {
if (isNil(parsed)) {
throw Error(ERROR_NOT_ENOUGH_OPTIONS);
}

Expand All @@ -79,9 +82,14 @@ class Gamble extends Game {

const chanceToWin = await this.getPermissionBasedSettingsValue('chanceToWin');
const chanceToTriggerJackpot = await this.getPermissionBasedSettingsValue('chanceToTriggerJackpot');
if (this.enableJackpot && _.random(0, 100, false) <= chanceToTriggerJackpot[permId]) {
if (this.enableJackpot && random(0, 100, false) <= chanceToTriggerJackpot[permId]) {
const incrementPointsWithJackpot = (points * 2) + this.jackpotValue;
await getRepository(User).increment({ userId: opts.sender.userId }, 'points', incrementPointsWithJackpot);

const user = await users.getUserByUsername(opts.sender.username);
set(user, 'extra.jackpotWins', get(user, 'extra.jackpotWins', 0) + 1);
await getRepository(User).save(user);

const currentPointsOfUser = await pointsSystem.getPointsOf(opts.sender.userId);
message = prepare('gambling.gamble.winJackpot', {
pointsName: getPointsName(currentPointsOfUser),
Expand All @@ -90,7 +98,7 @@ class Gamble extends Game {
jackpot: this.jackpotValue,
});
this.jackpotValue = 0;
} else if (_.random(0, 100, false) <= chanceToWin[permId]) {
} else if (random(0, 100, false) <= chanceToWin[permId]) {
await getRepository(User).increment({ userId: opts.sender.userId }, 'points', points * 2);
const updatedPoints = await pointsSystem.getPointsOf(opts.sender.userId);
message = prepare('gambling.gamble.win', {
Expand Down Expand Up @@ -158,6 +166,21 @@ class Gamble extends Game {
}
return [{ response: message, ...opts }];
}

@command('!gamble wins')
async wins (opts: CommandOptions): Promise<CommandResponse[]> {
let message: string;
const user = await users.getUserByUsername(opts.sender.username);
if (this.enableJackpot) {
message = prepare('gambling.gamble.winJackpotCount', {
command: this.getCommand('!gamble wins'),
count: get(user, 'extra.jackpotWins', 0),
});
} else {
message = prepare('gambling.gamble.jackpotIsDisabled', { command: this.getCommand('!gamble') });
}
return [{ response: message, ...opts }];
}
}

export default new Gamble();