Fixed login
Started building page cleanup Modified code pools Backported layout/blocks/templates 3-step view system Reforged Overview page Moved graphics, added topnav template, fixed number render bug Added BCMath optionnal usage Updated BC Math comparison for capped resources Fixed planet resource production updates Various additions Signed-off-by: Gregory PLANCHAT <g.planchat@gmail.com>
This commit is contained in:
parent
3fd8832b45
commit
f2d4b0f7e2
384 changed files with 2727 additions and 1364 deletions
130
includes/application/code/libraries/Math.php
Normal file
130
includes/application/code/libraries/Math.php
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
<?php
|
||||
|
||||
class Math
|
||||
{
|
||||
const DEFAULT_PRECISTION = 0;
|
||||
|
||||
protected static $_singleton = null;
|
||||
|
||||
public static function setPrecision($precision = null)
|
||||
{
|
||||
if ($precision === null) {
|
||||
$precision = self::DEFAULT_PRECISTION;
|
||||
}
|
||||
|
||||
self::getSingleton()->setPrecision($precision);
|
||||
}
|
||||
|
||||
public static function getSingleton()
|
||||
{
|
||||
if (self::$_singleton === null) {
|
||||
if (extension_loaded('bcmath') && defined('BCNUMBERS')) {
|
||||
self::$_singleton = new Math_Bcmath();
|
||||
} else {
|
||||
self::$_singleton = new Math_Native();
|
||||
}
|
||||
}
|
||||
return self::$_singleton;
|
||||
}
|
||||
|
||||
public static function add($a, $b)
|
||||
{
|
||||
return self::getSingleton()->add($a, $b);
|
||||
}
|
||||
|
||||
public static function sub($a, $b)
|
||||
{
|
||||
return self::getSingleton()->sub($a, $b);
|
||||
}
|
||||
|
||||
public static function mul($a, $b)
|
||||
{
|
||||
return self::getSingleton()->mul($a, $b);
|
||||
}
|
||||
|
||||
public static function div($a, $b)
|
||||
{
|
||||
return self::getSingleton()->div($a, $b);
|
||||
}
|
||||
|
||||
public static function comp($a, $b)
|
||||
{
|
||||
return self::getSingleton()->comp($a, $b);
|
||||
}
|
||||
|
||||
public static function pow($a, $b)
|
||||
{
|
||||
return self::getSingleton()->pow($a, $b);
|
||||
}
|
||||
|
||||
public static function mod($a, $b)
|
||||
{
|
||||
return self::getSingleton()->mod($a, $b);
|
||||
}
|
||||
|
||||
public static function ceil($a)
|
||||
{
|
||||
return self::getSingleton()->ceil($a);
|
||||
}
|
||||
|
||||
public static function floor($a)
|
||||
{
|
||||
return self::getSingleton()->floor($a);
|
||||
}
|
||||
|
||||
public static function round($a, $range = 0)
|
||||
{
|
||||
return self::getSingleton()->round($a, $range);
|
||||
}
|
||||
|
||||
public static function render($a)
|
||||
{
|
||||
return self::getSingleton()->render($a);
|
||||
}
|
||||
|
||||
public static function isNegative($a)
|
||||
{
|
||||
return (bool) (self::getSingleton()->comp($a, 0) < 0);
|
||||
}
|
||||
|
||||
public static function isPositive($a)
|
||||
{
|
||||
return (bool) (self::getSingleton()->comp($a, 0) > 0);
|
||||
}
|
||||
|
||||
public static function isNegativeOrZero($a)
|
||||
{
|
||||
return (bool) (self::getSingleton()->comp($a, 0) <= 0);
|
||||
}
|
||||
|
||||
public static function isPositiveOrZero($a)
|
||||
{
|
||||
return (bool) (self::getSingleton()->comp($a, 0) >= 0);
|
||||
}
|
||||
|
||||
public static function isZero($a)
|
||||
{
|
||||
return (bool) (self::getSingleton()->comp($a, 0) == 0);
|
||||
}
|
||||
|
||||
public static function isNotZero($a)
|
||||
{
|
||||
return (bool) (self::getSingleton()->comp($a, 0) != 0);
|
||||
}
|
||||
|
||||
public static function min($a, $b)
|
||||
{
|
||||
if (self::getSingleton()->comp($a, $b) > 0) {
|
||||
return $b;
|
||||
}
|
||||
return $a;
|
||||
}
|
||||
|
||||
public static function max($a, $b)
|
||||
{
|
||||
if (self::getSingleton()->comp($a, $b) < 0) {
|
||||
return $b;
|
||||
}
|
||||
return $a;
|
||||
}
|
||||
}
|
||||
127
includes/application/code/libraries/Math/Bcmath.php
Normal file
127
includes/application/code/libraries/Math/Bcmath.php
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
<?php
|
||||
|
||||
class Math_Bcmath
|
||||
{
|
||||
public function setPrecision($precision)
|
||||
{
|
||||
bcscale($precision);
|
||||
}
|
||||
|
||||
public function add($a, $b)
|
||||
{
|
||||
return bcadd($a, $b);
|
||||
}
|
||||
|
||||
public function mul($a, $b)
|
||||
{
|
||||
return bcmul($a, $b);
|
||||
}
|
||||
|
||||
public function sub($a, $b)
|
||||
{
|
||||
return bcsub($a, $b);
|
||||
}
|
||||
|
||||
public function div($a, $b)
|
||||
{
|
||||
return bcdiv($a, $b);
|
||||
}
|
||||
|
||||
public function mod($a, $b)
|
||||
{
|
||||
return bcmod($a, $b);
|
||||
}
|
||||
|
||||
public function pow($a, $b)
|
||||
{
|
||||
return bcpow($a, $b);
|
||||
}
|
||||
|
||||
public function comp($a, $b)
|
||||
{
|
||||
return bccomp($a, $b);
|
||||
}
|
||||
|
||||
public function ceil($x)
|
||||
{
|
||||
$integer = bcmul($x, 1, 0);
|
||||
$decimals = bcsub($x, $integer);
|
||||
|
||||
if (bccomp($decimals, 0) > 0) {
|
||||
return bcadd($integer, 1);
|
||||
} else {
|
||||
return $integer;
|
||||
}
|
||||
}
|
||||
|
||||
public function floor($x)
|
||||
{
|
||||
$integer = bcmul($x, 1, 0);
|
||||
$decimals = bcsub($x, $integer);
|
||||
|
||||
if (bccomp($decimals, 0) < 0) {
|
||||
return bcsub($integer, 1);
|
||||
} else {
|
||||
return $integer;
|
||||
}
|
||||
}
|
||||
|
||||
public function round($x, $range = 0)
|
||||
{
|
||||
$integer = bcmul($x, 1, 0);
|
||||
$decimals = bcsub($x, $integer, $range);
|
||||
|
||||
if (bccomp($decimals, 0) > 0) {
|
||||
return bcadd($integer, '0.' . str_pad('5', $range, STR_PAD_LEFT), $range);
|
||||
} else {
|
||||
return bcsub($integer, '0.' . str_pad('5', $range, STR_PAD_LEFT), $range);
|
||||
}
|
||||
}
|
||||
|
||||
public function render($a)
|
||||
{
|
||||
if (!$a) {
|
||||
return '0';
|
||||
}
|
||||
$a = strval($a);
|
||||
|
||||
$pos = strrpos($a, '.');
|
||||
if ($pos !== false) {
|
||||
$a = substr($a, 0, $pos);
|
||||
}
|
||||
|
||||
$length = $pos + ((3 - ($pos % 3)) % 3);
|
||||
$a = str_pad($a, $length, ' ', STR_PAD_LEFT);
|
||||
$parts = str_split($a, 3);
|
||||
switch ((int) (count($parts) / 3)) {
|
||||
case 0:
|
||||
case 1:
|
||||
return implode('.', $parts);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
return implode('.', array_slice($parts, 0, 2)) . 'M';
|
||||
break;
|
||||
|
||||
case 3:
|
||||
return implode('.', array_slice($parts, 0, 2)) . 'G';
|
||||
break;
|
||||
|
||||
case 4:
|
||||
return implode('.', array_slice($parts, 0, 2)) . 'T';
|
||||
break;
|
||||
|
||||
case 5:
|
||||
return implode('.', array_slice($parts, 0, 2)) . 'P';
|
||||
break;
|
||||
|
||||
case 6:
|
||||
return implode('.', array_slice($parts, 0, 2)) . 'Y';
|
||||
break;
|
||||
|
||||
default:
|
||||
return implode('.', array_slice($parts, 0, sizeof($parts) - 18)) . 'Z';
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
63
includes/application/code/libraries/Math/Native.php
Normal file
63
includes/application/code/libraries/Math/Native.php
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
class Math_Native
|
||||
{
|
||||
public function setPrecision($precision)
|
||||
{
|
||||
}
|
||||
|
||||
public function add($a, $b)
|
||||
{
|
||||
return $a + $b;
|
||||
}
|
||||
|
||||
public function mul($a, $b)
|
||||
{
|
||||
return $a * $b;
|
||||
}
|
||||
|
||||
public function sub($a, $b)
|
||||
{
|
||||
return $a - $b;
|
||||
}
|
||||
|
||||
public function div($a, $b)
|
||||
{
|
||||
return $a / $b;
|
||||
}
|
||||
|
||||
public function mod($a, $b)
|
||||
{
|
||||
return $a % $b;
|
||||
}
|
||||
|
||||
public function pow($a, $b)
|
||||
{
|
||||
return pow($a, $b);
|
||||
}
|
||||
|
||||
public function comp($a, $b)
|
||||
{
|
||||
return ($a > $b) ? 1 : (($a < $b) ? -1 : 0);
|
||||
}
|
||||
|
||||
public function ceil($x)
|
||||
{
|
||||
return ceil($x);
|
||||
}
|
||||
|
||||
public function floor($x)
|
||||
{
|
||||
return floor($x);
|
||||
}
|
||||
|
||||
public function round($range = 0)
|
||||
{
|
||||
return round($x, $range);
|
||||
}
|
||||
|
||||
public function render($a)
|
||||
{
|
||||
return number_format($a, 0, ',', '.');
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue