# How to figure out a users earnt exp based upon calculating the difference by user and player by x?

How would I figure out a users earnt EXP based upon calculating the difference by user and player by x using this format?

If the user is 60 levels less than me, I would get 0 exp (as that's just bullying).

If the user is within 21 and 59 levels less than me, I would get 1 exp.

If the user is less than 20 levels of mine, I would get 2 exp.

If the user is the equal level they would get 3 exp, however if the user is within my level, and 20 above me I would get 4 exp.

If they were between 21 and 40 levels above me I would get 5 exp

If they were 41 and 60 levels above me I would get get 6 exp

and if they were 61+ levels above me I would get 7 exp

So I'm looking to do something like this -

How would it structure if I wanted to make it (player being me, and the user is the opponent)

if( $PLAYER->level is more than 60 of$USERS->level )
{
$earn = 0; } elseif($PLAYER->level is between 21 and 59 more than $USERS->level ) {$earn = 1;
}
elseif( $PLAYER->level is between 1 and 20 more than$USERS->level )
{
$earn = 2; } elseif($PLAYER->level == $USERS->level ) {$earn = 3;
}


I know in SQL they have a between operator, but I cannot seem to find a a PHP one and/or figure out a solution to display what I intend to do above.

Is it possible to do what I intend? Or do I need to find a new solution?

It looks like you might be better off with some formula:

// keep the sign separate
int sign = enemyLevel > playerLevel ? 1 : -1;
// Calculate the xp as 1 xp per 20 level differences
// Ensures that no negative xp is given
int givenExp = math.min(0, 3 + (sign * math.floor(math.abs(enemyLevel - playerLevel)/20.f)));


What this does is clamp each 20 levels to 1 xp. Everything is rounded down, so 39 levels would be the same as 20 levels, but 40 levels would give another xp.

To make things simple, I removed the sign and added it later, but you could handle negative and positive level differences separately if you want. At least to me, it seems like taking down a mob 20 levels higher than you would be a feat worth more xp.

• So for example if i was level 40 and the enemy was 19 levels lower or higher i would get 3 exp. but if they was 20 levels lower i would get 2 exp? But if they was 20 levels higher i would get 4 exp? Yes thats what i intend, a user hitting a much lower player would result in less exp wheras defeating a higher ranked player would reap its reward and gain more exp – Kyle Feb 19 '14 at 19:17
• Exactly. You may need to change around the formula, but it should prove as a good guideline. – user39686 Feb 19 '14 at 19:37

"x between a and b" is equivalent to satisfying both a <= x and x <= b. In your case, x is the difference between the two levels and a and b the particular bounds you want to test against.

I would recommend that you define a boolean function between(x,a,b) that evaluates the between-ness of a value against a range.

All you've got to do then is phrase your conditionals in the form "x between a and b", like between($PLAYER->level -$USERS->level, 21, 59) to determine whether the signed difference between the player level and user level is between 21 and 59.