Skip to content

Commit

Permalink
Restore UnitDB function - Update and improve displayed data (#18)
Browse files Browse the repository at this point in the history
* Improved Error handling

Improved error handling. Detect when commands fail. Exit script on failure.
Removed use of shell_exec (it does not detect command failures).

* Restore UnitDB function + Update/Improve Unit data

Correct script errors related to differences between PHP 8.x and earlier versions.
Example:
count() generates an error if the argument is not an array object (string being passed).  Previously, a string argument would return a 1.

Correct update.php to read in blueprint data in the correct order (3599 files serve as a baseline, and must be read first).
Improved error handling. Add code to properly handle reformatted blueprints.

Overhaul of unit DPS and fire cycle calculations to improve accuracy.
General improvements in accuracy and completeness of unit data.

* Code Cleanup

Requested changes of 2024-04-05 (code cleanup).
Corrected DPS code for Salvation.

* Requested change

Updated comment per request.
  • Loading branch information
PViddy72 authored Apr 7, 2024
1 parent db84022 commit 41c6933
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 151 deletions.
20 changes: 16 additions & 4 deletions www/include/Git.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

// Wrapper for Git, exits on git failures.
class Git
{
private $repositoryUrl;
Expand All @@ -17,20 +18,31 @@ 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}");

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}");
}
} 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);

if ($retval != 0) {
logDebug("Returned with status $retval and output: \n");
print_r($output);
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>
80 changes: 56 additions & 24 deletions www/res/scripts/calculations.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,50 @@
<?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
$specials = [
'UEL0103', // lobo
'XSL0103', // zthuee
'DAA0206', // mercy
'XAA0306' // solace
];
Expand All @@ -31,7 +53,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 +68,40 @@ 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"]);
/*
Code for calculating missile/projectile count should only be done in calculateFireCycle.
We don't want to calculate fire cycles in two places. Just use the value returned from that function.
*/
$trueSalvoSize = $shots;

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

/// For weapons with fragmentation shells (Lobo, Zthuee, Salvation).
if (isset($Projectile) && property_exists($Projectile->Physics, 'Fragments')) {
$trueSalvoSize = $trueSalvoSize * $Projectile->Physics->Fragments;
/// Exception for Salvation
if ($unitID == "XAB2307") {
/// Salvation uses a shell that fragments into 6 shells, which then fragments into 6 more.
/// Only first fragmentation is accounted for above. Hard code the 2nd one by multiplying by 6.
$trueSalvoSize = $trueSalvoSize * 6;
}

}

$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 41c6933

Please sign in to comment.