Skip to content

Commit

Permalink
Improved Error handling
Browse files Browse the repository at this point in the history
Improved error handling. Detect when commands fail. Exit script on failure.
Removed use of shell_exec (it does not detect command failures).
  • Loading branch information
PViddy72 committed Apr 5, 2024
1 parent db84022 commit bba443f
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 146 deletions.
19 changes: 15 additions & 4 deletions www/include/Git.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

// Improved error handling. Detect when commands fail. Exit script on failure.
// Removed use of shell_exec (does not reliably detect command failures).
class Git
{
private $repositoryUrl;
Expand All @@ -17,20 +19,29 @@ public function clone($destinationDirectory, $branch = 'master', $sparseFolders
$escBranch = escapeshellarg($branch);

if ($sparseFolders) {
$this->execute("git clone --filter=blob:none --depth 1 --sparse {$escRepo} --branch {$escBranch} {$escDestDir}");
$out = $this->execute("git clone --filter=blob:none --depth 1 --sparse {$escRepo} --branch {$escBranch} {$escDestDir}");
print_r($out);

foreach ($sparseFolders as $folder) {
$escFolder = escapeshellarg($folder);
$this->execute("cd {$escDestDir} && git sparse-checkout add {$escFolder}");
$out = $this->execute("cd {$escDestDir} && git sparse-checkout add {$escFolder}");
print_r($out);
}
} else {
$this->execute("git clone {$escRepo} {$escDestDir}");
$out = $this->execute("git clone {$escRepo} {$escDestDir}");
print_r($out);
}
}

private function execute($command)
{
$output = shell_exec($command);
$output = null;
$retval = null;
logDebug($command);
exec($command, $output, $retval);
logDebug("Returned with status $retval and output: \n");
if ($retval != 0) exit ($retval);

return $output;
}
}
Expand Down
5 changes: 4 additions & 1 deletion www/index.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php
// PHP Issue - There is an issue under PHP 8.3.1. With XDebug enabled, it will cause 100% CPU Usage
// Disable XDebug, or use an earlier version of PHP - PV

ini_set('display_errors', 1);
error_reporting(E_ALL & ~E_NOTICE);
set_error_handler(function($errno, $errstr, $errfile, $errline) {
Expand Down Expand Up @@ -396,4 +399,4 @@

<?php echo file_get_contents("LICENSE"); ?>
</body>
</html>
</html>
86 changes: 60 additions & 26 deletions www/res/scripts/calculations.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,53 @@
<?php
function calculateFireCycle($weapon){
function calculateFireCycle($weapon, $unitID){

/// Muzzle Salvo Size and Muzzle Count
$mss = 0;
$MuzzleCount = 0;
$firecycle = 0;

/// Bring in the trueSalvoSize calculation from the DPS to this function. It should only be in 1 place!
if (property_exists($weapon, 'RackBones')) { // dummy weapons dont have racks

/// OK, this is a legit weapon, let's set our most basic values.
$mss = $weapon->MuzzleSalvoSize;
if ($mss == 1){
$bones = 0;
foreach($weapon->RackBones as $rack){
$bones += count((array)$rack->MuzzleBones);

/// Now we need determine if the weapon fires from multiple muzzles simultaneously (or close to it).
if (property_exists($weapon, 'RackFireTogether') && ($weapon->RackFireTogether == 1)) {
/// Count up all the muzzles in all the racks
foreach ($weapon->RackBones as $rack) {
$MuzzleCount += count((array)$rack->MuzzleBones);
}
return $bones;
$firecycle = $mss * $MuzzleCount;
}
else{
return $mss * count((array)$weapon->RackBones);
// Do all real weapons have MuzzleSalvoDelay?
// else if (property_exists($weapon, 'MuzzleSalvoDelay') && $weapon->MuzzleSalvoDelay == 0) {
else if ($weapon->MuzzleSalvoDelay == 0) {
/// Count only the muzzles in the first rack, since RackFireTogether is false
/// We do not use MuzzleSalvoSize (mss) when MuzzleSalvoDelay is 0.
/// Cast to an array, in case it is only a string instead of an array, (PHP 8)
$MuzzleCount = count((array)$weapon->RackBones[0]->MuzzleBones);
$firecycle = $MuzzleCount;
} else {
$firecycle = $mss;
}
}

return $firecycle;

}

// Source : https://github.com/spooky/unitdb/blob/master/app/js/dps.js
// (calculations provided by Exotic_retard)
function calculateDps($stdClassWeapon, $unitID){
function calculateDps($stdClassWeapon, $unitID, $Projectile){

$weapon = arrayCastRecursive($stdClassWeapon); // StdClass are a PAIN to use in PHP

// Hardcoded exceptions
// Hardcoded exceptions - Removed arties, handled by Fragmentation code below
// 'UEL0103', // lobo
// 'XSL0103', // zthuee

$specials = [
'UEL0103', // lobo
'XSL0103', // zthuee
'DAA0206', // mercy
'XAA0306' // solace
];
Expand All @@ -31,7 +56,7 @@ function calculateDps($stdClassWeapon, $unitID){

$shots = 1;

if (isset($weapon["MuzzleSalvoSize"])) $shots = calculateFireCycle($stdClassWeapon);
if (isset($weapon["MuzzleSalvoSize"])) $shots = calculateFireCycle($stdClassWeapon, $unitID);


// fall back to the old calculation formula for the special snowflakes
Expand All @@ -46,30 +71,39 @@ function calculateDps($stdClassWeapon, $unitID){
// in theory if your total MuzzleSalvoDelay is longer than the reload time your weapon waits for the reload time twice,
// but thats pretty much a bug so not taken into account here


/// BTW - SpookyDB uses round(), not floor(), based on the values seen there. Values will not match between DBs. floor() is the correct method.
$trueReload = max(0.1*floor(10 / $weapon["RateOfFire"]), 0.1);
$trueReload = max(
($weapon["RackSalvoChargeTime"] ?? 0) + ($weapon["RackSalvoReloadTime"] ?? 0) +
($weapon["MuzzleSalvoDelay"] ?? 0)*(($weapon["MuzzleSalvoSize"] ?? 1)-1),
$trueReload
);

$trueSalvoSize = 1;
if (($weapon["MuzzleSalvoDelay"] ?? 0) > 0) { // if theres no muzzle delay, all muzzles fire at the same time
$trueSalvoSize = ($weapon["MuzzleSalvoSize"] ?? 1);
} else if ($weapon["RackBones"] && count($weapon["RackBones"]) > 0) { // dummy weapons dont have racks
if ($weapon["RackFireTogether"]) {
$trueSalvoSize = count($weapon["RackBones"]) * count($weapon["RackBones"][0]["MuzzleBones"]);
} else if (count($weapon["RackBones"]) > 0) {
$trueSalvoSize = count($weapon["RackBones"][0]["MuzzleBones"]);
}
/*
Previous code for calculating missile/projectile count was re-written to improve accuracy and moved to calculateFireCycle.
We don't want to calculate fire cycles in two places. Just use the value returned from that function.
*/
$trueSalvoSize = $shots;

/// Added for Salvation
if ($unitID == "XAB2307") {
/// Salvation uses a shell that fragments into 6 shells, which then fragments into 6 more. I'm not going to try to code that. Just multiply by 36.
$trueSalvoSize = $trueSalvoSize * 36;
}

$trueDamage = $weapon["Damage"] * ($weapon["DoTPulses"] ?? 1) + ($weapon["InitialDamage"] ?? 0);

/// Added for weapons with fragmentation shells (Lobo, Zthuee).
if (isset($Projectile) && property_exists($Projectile->Physics, 'Fragments')) {
$trueSalvoSize = $trueSalvoSize * $Projectile->Physics->Fragments;
}

$trueDamage = $weapon["Damage"]*($weapon["DoTPulses"] ?? 1) + ($weapon["InitialDamage"] ?? 0);

// beam weapons are a thing and do their own thing. yeah good luck working out that.
$trueDamage = max((floor(($weapon["BeamLifetime"] ?? 0) / (($weapon["BeamCollisionDelay"] ?? 0)+0.1))+1)*$weapon["Damage"], $trueDamage);
$trueDamage = max((floor(($weapon["BeamLifetime"] ?? 0) / (($weapon["BeamCollisionDelay"] ?? 0) + 0.1)) + 1) * $weapon["Damage"], $trueDamage);
if ($trueSalvoSize == 0) $trueSalvoSize = 1; // Adjustment needed for beam weapons

$salvoDamage = $trueSalvoSize * $trueDamage * ($isSpecial ? $shots : 1);

$trueDPS = ($salvoDamage / $trueReload);

return $trueDPS;
Expand Down
Loading

0 comments on commit bba443f

Please sign in to comment.