Home > other >  Formula for calculating user experience
Formula for calculating user experience

Time:07-28

I would like (without using a gem) to create a level and experience point system for my users.

When creating his account, he starts level 0 with 0 experience points. To reach level 1 he will need 10 additional points, for a total of 10 points. For level 3 he will need 30 additional points, for a total of 60 experience points. Each additional level requires 10 more experience points. (see example below).

Level  |  Total XP  |  XP for next level|
----------------------------------------|
0      |    0       |    10             |
1      |    10      |    20             |               
2      |    30      |    30             |
3      |    60      |    40             |
4      |    100     |    50             |
5      |    150     |    60             |
etc...

I would like an xp column in my user table that would represent the total experience of a user.

In my view I would like to display its level. But what is the formula for calculating this?

Let's imagine that my user has 157 experience points in total. Which corresponds to a level 5. How to calculate a level only via the total experience points and how to calculate this regardless of its total experience point whether it is 38 like 369 or 4393.

CodePudding user response:

I'm not sure if this is exactly what you wanted. It's more of an algorithm than a formula, but here is a method that will return the proper level based on the experience given. Think of the bar variable like the bar you have to get over to meet a new level, and think of the step variable like the amount of experience added between each level (as you describe in your right-most column).

def calculate_level(experience)
  level = 0
  bar   = 10
  step  = 10
  while experience >= bar
    level  = 1
    step   = 10
    bar    = step
  end
  level
end

Example output:

irb(main):312:0> calculate_level(0)
=> 0
irb(main):313:0> calculate_level(9)
=> 0
irb(main):314:0> calculate_level(10)
=> 1
irb(main):315:0> calculate_level(11)
=> 1
irb(main):316:0> calculate_level(29)
=> 1
irb(main):317:0> calculate_level(30)
=> 2
irb(main):318:0> calculate_level(31)
=> 2
irb(main):319:0> calculate_level(59)
=> 2
irb(main):320:0> calculate_level(60)
=> 3
irb(main):321:0> calculate_level(61)
=> 3
irb(main):322:0> calculate_level(99)
=> 3
irb(main):323:0> calculate_level(100)
=> 4
irb(main):324:0> calculate_level(101)
=> 4
irb(main):325:0> calculate_level(149)
=> 4
irb(main):326:0> calculate_level(150)
=> 5
irb(main):327:0> calculate_level(151)
=> 5

CodePudding user response:

You can use such "formula"

def calculate_level(experience)
  (1..).find { |i| (experience -= 10 * i).negative? } - 1
end

Just decrease experience on 10x steps every time and check if result is not negative

calculate_level(0)
# => 0

calculate_level(9)
# => 0

calculate_level(10)
# => 1

calculate_level(11)
# => 1

calculate_level(29)
# => 1

calculate_level(30)
# => 2

calculate_level(31)
# => 2

calculate_level(59)
# => 2

calculate_level(60)
# => 3

calculate_level(61)
# => 3

calculate_level(99)
# => 3

calculate_level(100)
# => 4

calculate_level(101)
# => 4

calculate_level(149)
# => 4

calculate_level(150)
# => 5

calculate_level(151)
# => 5

calculate_level(209)
# => 5

calculate_level(210)
# => 6
  • Related