Eylexander / Project-1B

Learning JS through Discord.js
MIT License
0 stars 0 forks source link

Database value #14

Closed Eylexander closed 2 years ago

Eylexander commented 2 years ago

Hey @dannyhpy, j'ai un problème que j'ai tout d'abord essayer de régler par moi-même mais je n'arrive pas à avancer plus que ça. En gros, j'ai mon système de "minage" qui consomme de l'énergie pour nous donner de l'argent (système fonctionnel), mais le problème c'est que de 1 c'est très peu optimisé (tu peux prendre peur) et de 2 il est très facile d'exploit mon système en tombant dans des valeurs d'énergie négatives.

Fichier concerné : commands/Economy/work.js

Merci d'avance pour ton expertise et ton temps !

Eylexander commented 2 years ago

Finalement j'ai fait de mon mieux pour optimiser, c'est tellement plus lisible et puis ça marche vraiment mieux !

old :

if (args[0] === 'mana') {
        let nb1 = Number(args[1])
        setstats.run({
            id : message.author.id,
            user : message.author.tag,
            money : stats.money,
            mana : (nb1 === null || nb1 === NaN ? stats.mana+10 : stats.mana + nb1)
        })
        message.channel.send(`You cheated and added yourself ${nb1 === null || nb1 === NaN ? '10' : nb1} mana!`)
    }
    else if (!args[0]) {
        const random1 = Math.ceil(Math.random() * 30 + (Math.random() * 10))
        if (stats.mana > 0) {
            setstats.run({
                id : message.author.id,
                user : message.author.tag,
                money : `${(stats.money + random1) < 3 ? (stats.money + random1 * 1.5) : stats.money + random1}`,
                mana : stats.mana - 1
            })
            message.channel.send(`You used 1 mana to work and made ${random1}$`)
        }
    }
    else {
        const amountmana = Number(args[0])
        const randone = Math.ceil(Math.random() * 30 * amountmana + amountmana * (Math.random() * 10))
        if (stats.mana >= amountmana > 0) {
            setstats.run({
                id : message.author.id,
                user : message.author.tag,
                money : `${(stats.money + randone) < (amountmana * 3) ? (stats.money + randone * 1.5) : stats.money + randone}`,
                mana : stats.mana - amountmana
            })
            message.channel.send(`You used ${amountmana} mana to work and made ${(stats.money + randone) < (amountmana * 3) ? (stats.money + randone * 1.5) : stats.money + randone}$`)
        } else if (stats.mana <= 0) {
            return message.channel.send('You already used all your mana!')
        } else if (amountmana > stats.mana >= 0) {
            const allmana = amountmana - (amountmana - stats.mana)
            const randall = Math.ceil(Math.random() * 30 * allmana + allmana * (Math.random() * 10))
            setstats.run({
                id : message.author.id,
                user : message.author.tag,
                money : `${(stats.money + randall) < (allmana * 3) ? (stats.money + randall * 1.5) : stats.money + randall}`,
                mana : stats.mana - allmana
            })
            message.channel.send(`You used all of your mana to work and made ${(stats.money + randall) < (allmana * 3) ? (stats.money + randall * 1.5) : stats.money + randall}$`)
        } else {
            message.channel.send('There was a problem.')
        }
    }

new :

function work(nbMana) {
        inv === new db('./database/stats.sqlite');
        workValue = Math.ceil(Math.random() * 50 * nbMana)
        inv.prepare("INSERT OR REPLACE INTO stats (id, user, money, mana) VALUES (@id, @user, @money, @mana);").run({
            id : message.author.id,
            user : message.author.tag,
            money : stats.money + workValue,
            mana : stats.mana - nbMana
        })
        message.channel.send(`You have used ${nbMana} mana to work and made ${workValue}$`)
    }

    if (!stats) {
        stats = {
            id : message.author.id,
            user : message.author.tag,
            money : 0,
            mana : 10
        }
        setstats.run(stats)
        message.channel.send(`You've just created your own profile!`)
        work(1)
    }

    if (message.author.id === admin && args[0] === 'cheat') {
        setstats.run({
            id : message.author.id,
            user : message.author.tag,
            money : stats.money,
            mana : stats.mana + Number(args[1])
        })
        message.channel.send(`Here you go boss, here's your free ${args[1]} mana!`)
        return
    }

    if (!Number(args[0])) {
        message.channel.send('You have to type a number!')
        return
    }

    if (stats.mana>0) {
        if(!args[0]) {
            work(1)
        }
        else if(args.length<=1) {
            if (stats.mana<=Number(args[0])) {
                work(stats.mana)
            }
            else if (stats.mana>Number(args[0])) {
                work(Number(args[0]))
            }
        }
    } else {
        message.channel.send(`You've run out of mana!`)
    }
};