FMCorz / moodle-block_xp

A gamification plugin for Moodle allowing students to gain experience points and level up.
https://levelup.plus/?ref=github
150 stars 41 forks source link

Levels generated using the algorithm not working well? #74

Closed Canx closed 6 years ago

Canx commented 6 years ago

I'm trying to create levels with linear increment of points (1000,2000,3000,...).

If I use the algorithm with parameters:

Algorithm base: 1000 Algorithm coeficient: 1

The levels generated are: 0, 1000, 2001, 3003, 4006, 5010, 6015, 7021, 8028, 9036,...

I can manually disable the levels and enter them by hand but I wanted to know why this is happening. In algo_levels_info.php there is is method:

public static function get_xp_with_algo($levelcount, $base, $coef) {
        $list = [];
        for ($i = 1; $i <= $levelcount; $i++) {
            if ($i == 1) {
                $list[$i] = 0;
            } else if ($i == 2) {
                $list[$i] = $base;
            } else {
                $list[$i] = $base + round($list[$i - 1] * $coef);
            }
        }
        return $list;
    }

Apparently this method works ok (I tested the code) so I'll try to figure out where comes the error from...

Canx commented 6 years ago

I found the bug, it was in levels_with_algo.php (line 99):

$coef = max((float) $mform->exportValue('coefxp'), 1.001);

The problem is the "1.001" value in the max function.

Changing it for for "1.0" solves the problem, that decimal was adding an incremental error when using "1.0" as a coeficient.

FMCorz commented 6 years ago

Thanks for debugging this. I think I initially did this to ensure that regardless of the input in the form, it would generate sensible levels. But as the base must be set to 1 at the very least, there doesn't seem to be any reason to use 1.001 instead of 1, at least none that I can think of.

Will get onto fixing that. Thanks for the report.

FMCorz commented 6 years ago

I have fixed this. That should not break anything. Thanks!