Skip to content

Instantly share code, notes, and snippets.

@clouddueling
Created April 12, 2013 15:47
Show Gist options
  • Save clouddueling/5373002 to your computer and use it in GitHub Desktop.
Save clouddueling/5373002 to your computer and use it in GitHub Desktop.
php game class. lets you get levels and points
<?php
class Game
{
public static function pointsFromLevel($level)
{
// http://rsdo.net/rsdonline/guides/Experience%20formula.html#PHP
$a = 0;
for($x = 1; $x < $level; $x++) {
$a += floor($x + 300 * pow(6, ($x / 7)));
}
if ($a > 0)
return floor($a / 2);
return 0;
}
public static function levelFromPoints($points)
{
for ($i = 0; $i < 200; ++$i) {
$pointsCheck = self::pointsFromLevel($i);
if ($points > $pointsCheck && $points < self::pointsFromLevel($i + 1))
return $i;
}
return 1;
}
public static function percentRemaining($points)
{
$currentLevel = self::levelFromPoints($points);
$nextLevelPoints = self::pointsFromLevel($currentLevel + 1);
$levelProgress = $nextLevelPoints - $points;
$levelDifference = $nextLevelPoints - self::pointsFromLevel($currentLevel);
return 100 - floor($levelProgress / $levelDifference * 100);
}
}
@RobinMalfait
Copy link

Nice, wrote it yourself? :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment