Skip to content

Commit

Permalink
system/gambling: Fix deletion of !duel users when new user is added
Browse files Browse the repository at this point in the history
_ref: #914
  • Loading branch information
sogehige committed Apr 19, 2018
1 parent 7763f0d commit 0d2c3fb
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 9 deletions.
18 changes: 10 additions & 8 deletions libs/systems/gambling.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Gambling {
set duelUsers (v) {
if (_.isNil(v)) global.db.engine.remove(`${this.collection}.duel`, { key: '_users' })
else {
for (let [user, tickets] of Object.entries(v)) global.db.engine.update(`${this.collection}.duel`, { key: '_users' }, { user: user, tickets: tickets })
for (let [user, tickets] of Object.entries(v)) global.db.engine.update(`${this.collection}.duel`, { key: '_users', user: user }, { tickets: tickets })
}
}

Expand All @@ -112,6 +112,7 @@ class Gambling {

if (timestamp === 0 || new Date().getTime() - timestamp < 1000 * 60 * duelDuration) return setTimeout(() => this.pickDuelWinner(), 30000)

debug('Duel users: %j', users)
let total = 0
for (let user of Object.entries(users)) total += parseInt(user[1], 10)

Expand All @@ -137,7 +138,7 @@ class Gambling {
tickets: tickets.toString(),
winner: username
})
debug(m); global.commons.sendMessage(m, { username: username }, { force: true })
debug(m); global.commons.sendMessage(m, { username: global.commons.getOwner() }, { force: true })

// give user his points
await global.db.engine.insert('users.points', { username: username, points: parseInt(total, 10) })
Expand All @@ -158,21 +159,22 @@ class Gambling {
if (_.isNil(parsed)) throw Error(ERROR_NOT_ENOUGH_OPTIONS)

const user = await global.users.get(sender.username)
points = parsed[1] === 'all' && !_.isNil(await global.systems.points.getPointsOf(user.username)) ? await global.systems.points.getPointsOf(user.username) : parsed[1]
const points = await global.systems.points.getPointsOf(user.username)
const bet = parsed[1] === 'all' ? points : parsed[1]

if (parseInt(points, 10) === 0) throw Error(ERROR_ZERO_BET)
if (_.isNil(await global.systems.points.getPointsOf(user.username)) || await global.systems.points.getPointsOf(user.username) < points) throw Error(ERROR_NOT_ENOUGH_POINTS)
if (points < (await global.configuration.getValue('duelMinimalBet'))) throw Error(ERROR_MINIMAL_BET)
if (points < bet) throw Error(ERROR_NOT_ENOUGH_POINTS)
if (bet < (await global.configuration.getValue('duelMinimalBet'))) throw Error(ERROR_MINIMAL_BET)

await global.db.engine.insert('users.points', { username: sender.username, points: parseInt(points, 10) * -1 })
await global.db.engine.insert('users.points', { username: sender.username, points: parseInt(bet, 10) * -1 })

// check if user is already in duel and add points
let newDuelist = true
let users = await self.duelUsers
_.each(users, function (value, key) {
if (key.toLowerCase() === sender.username.toLowerCase()) {
let userToUpdate = {}
userToUpdate[key] = parseInt(users[key], 10) + parseInt(points, 10)
userToUpdate[key] = parseInt(users[key], 10) + parseInt(bet, 10)
self.duelUsers = userToUpdate
newDuelist = false
return false
Expand All @@ -187,7 +189,7 @@ class Gambling {
// save new cooldown if not bypassed
if (!(await global.configuration.getValue('gamblingCooldownBypass') && (isMod || global.commons.isBroadcaster(sender)))) self.duelCooldown = new Date().getTime()
let newUser = {}
newUser[sender.username.toLowerCase()] = parseInt(points, 10)
newUser[sender.username.toLowerCase()] = parseInt(bet, 10)
self.duelUsers = newUser
} else {
message = await global.commons.prepare('gambling.fightme.cooldown', {
Expand Down
9 changes: 8 additions & 1 deletion test/helpers/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const assert = require('chai').assert
const until = require('test-until')
const sinon = require('sinon')
const _ = require('lodash')
const util = require('util')

module.exports = {
prepare: function () {
Expand Down Expand Up @@ -78,6 +79,12 @@ module.exports = {
try {
let isCorrectlyCalled = false
for (let e of expected) {
/*
console.log(util.inspect(global.log.chatOut.args))
console.log(e)
console.log(user)
*/
delete user['message-type'] // remove unnecessary message-type
if (global.log.chatOut.calledWith(e, sinon.match(user))) {
isCorrectlyCalled = true
break
Expand All @@ -88,7 +95,7 @@ module.exports = {
return true
} catch (err) {
return setError(
'\nExpected message: "' + JSON.stringify(expected) + '"\nActual message: "' + (!_.isNil(global.log.chatOut.lastCall) ? global.log.chatOut.lastCall.args[0] : '') + '"' +
'\nExpected message: "' + JSON.stringify(expected) + '"\nActual message: "' + (!_.isNil(global.log.chatOut.args) ? util.inspect(global.log.chatOut.args) : '') + '"' +
'\n\nExpected user: "' + JSON.stringify(user) + '"\nActual user: "' + (!_.isNil(global.log.chatOut.lastCall) ? JSON.stringify(global.log.chatOut.lastCall.args[1]) : '') + '"')
}
}, 10000)
Expand Down
60 changes: 60 additions & 0 deletions test/tests/gambling/duel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,72 @@ require('../../general.js')
const db = require('../../general.js').db
const message = require('../../general.js').message

const _ = require('lodash')

const owner = { username: 'soge__' }
const user1 = { username: 'user1' }
const user2 = { username: 'user2' }

describe('Gambling - duel', () => {
before(async () => {
await db.cleanup()
await message.prepare()
})

describe('#914 - user1 is not correctly added to duel, if he is challenger', () => {
it('add points for users', async () => {
await global.db.engine.insert('users.points', { username: user1.username, points: 100 })
await global.db.engine.insert('users.points', { username: user2.username, points: 100 })
})

it('user 1 is challenging', async () => {
global.systems.gambling.duel(global.systems.gambling, user1, 'all')
await message.isSent('gambling.duel.new', user1, {
minutesName: global.commons.getLocalizedName(5, 'core.minutes'),
minutes: await global.configuration.getValue('duelDuration')
})
await message.isSent('gambling.duel.joined', user1, {
pointsName: await global.systems.points.getPointsName(100),
points: 100
})
})

it('user 2 is added to duel', async () => {
await global.systems.gambling.duel(global.systems.gambling, user2, 'all')
await message.isSent('gambling.duel.joined', user2, {
pointsName: await global.systems.points.getPointsName(100),
points: 100
})
})

it('set duel timestamp to force duel to end', async () => {
// cannot set as 0 - duel is then ignored
await global.db.engine.update(`gambling.duel`, { key: '_timestamp' }, { value: 1 })
})

it('call pickDuelWinner()', () => {
global.systems.gambling.pickDuelWinner()
})

it('winner should be announced', async () => {
await message.isSent('gambling.duel.winner', owner, [{
pointsName: await global.systems.points.getPointsName(200),
points: 200,
probability: _.round(50, 2),
ticketsName: await global.systems.points.getPointsName(100),
tickets: 100,
winner: user1.username
}, {
pointsName: await global.systems.points.getPointsName(200),
points: 200,
probability: _.round(50, 2),
ticketsName: await global.systems.points.getPointsName(100),
tickets: 100,
winner: user2.username
}])
})
})

describe('Pick winner from huge tickets', () => {
it('create duel', async () => {
global.systems.gambling.duelTimestamp = new Date().getTime()
Expand Down

0 comments on commit 0d2c3fb

Please sign in to comment.