Skip to content

Commit

Permalink
fix(raffles): join with max tickets if bet more (#3550)
Browse files Browse the repository at this point in the history
Fixes #3547
  • Loading branch information
sogehige committed Apr 9, 2020
1 parent e6b7174 commit e93bd28
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/bot/systems/raffles.ts
Expand Up @@ -358,9 +358,12 @@ class Raffles extends System {
opts.message = opts.message.toString().replace(raffle.keyword, '');
let tickets = opts.message.trim() === 'all' && !_.isNil(await points.getPointsOf(opts.sender.userId)) ? await points.getPointsOf(opts.sender.userId) : parseInt(opts.message.trim(), 10);

if ((!_.isFinite(tickets) || tickets <= 0 || tickets > raffle.maxTickets || tickets < raffle.minTickets) && raffle.type === TYPE_TICKETS) {
if ((!_.isFinite(tickets) || tickets <= 0 || tickets < raffle.minTickets) && raffle.type === TYPE_TICKETS) {
return false;
}
if (tickets > raffle.maxTickets && raffle.type === TYPE_TICKETS) {
tickets = raffle.maxTickets;
}
if (!_.isFinite(tickets)) {
tickets = 0;
}
Expand Down
59 changes: 59 additions & 0 deletions test/tests/raffles/bug#3547.js
@@ -0,0 +1,59 @@
/* eslint-disable @typescript-eslint/no-var-requires */
/* global describe it before */

require('../../general.js');

const db = require('../../general.js').db;
const message = require('../../general.js').message;
const user = require('../../general.js').user;
const commons = require('../../../dest/commons');

const { getRepository } = require('typeorm');
const { User } = require('../../../dest/database/entity/user');
const { Raffle } = require('../../../dest/database/entity/raffle');

const raffles = (require('../../../dest/systems/raffles')).default;

const assert = require('assert');

describe('Raffles - over max limit points not adding to raffle #3547', () => {
before(async () => {
await db.cleanup();
await message.prepare();
await user.prepare();
raffles.allowOverTicketing = true;
});

it('create ticket raffle', async () => {
raffles.open({ sender: user.owner, parameters: '!winme -min 0 -max 100' });
await message.isSent('raffles.announce-ticket-raffle', { username: 'bot' }, {
keyword: '!winme',
eligibility: await commons.prepare('raffles.eligibility-everyone-item'),
min: 1,
max: 100,
});
});

it('Update viewer to have 1000 points', async () => {
await getRepository(User).save({ username: user.viewer.username, userId: user.viewer.userId, points: 1000 });
});

it('Viewer bets over max points', async () => {
const a = await raffles.participate({ sender: user.viewer, message: '!winme 1000' });
assert(a);
});

it('expecting 1 participant to have bet of 100', async () => {
const raffle = await getRepository(Raffle).findOne({
relations: ['participants'],
where: { winner: null, isClosed: false },
});
assert(raffle.participants.length === 1, 'Participant not found in raffle - ' + JSON.stringify(raffle.participants));
assert(raffle.participants[0].tickets === 100, `Participant doesn't have correct points - ${raffle.participants[0].tickets} === 100`);
});

it('User should have 900 points', async () => {
const result = await getRepository(User).findOne({ username: user.viewer.username, userId: user.viewer.userId });
assert(result.points === 900);
});
});

0 comments on commit e93bd28

Please sign in to comment.