Created
April 12, 2013 15:47
-
-
Save clouddueling/5373002 to your computer and use it in GitHub Desktop.
php game class. lets you get levels and points
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice, wrote it yourself? :P