Page MenuHomeContribution Center

Script to convert Arma 3 Import loadout to "SQL-friendly" format.
Open, NormalPublic

Description

(When I say import loadout, I am refereing to the BIS function which puts a loadout on an entity. Not the Arsenal)
Created a script which takes the standard Arma 3 import loadout string and parses it into a format which can easily be imported into a SQL table.
It should be easily possible to export from the SQL db a loadout and parse it into a Arma 3 Import loadout, by using append and concatenate operators.

I have worked on the Export string which Arma 3 generates, but the format is weird and I am unsure if this is possible.

Output:

Code:

<?php
// A sexy af script that takes Arma 3 import strings and formats them to be integrated in a mysql database.
// This can be done the other way arround to facilitate loadout management.

// Input String formated as Arma 3 importable loadout
$str = '
class UTF_R {
	displayName = "00|Rifleman";
	//icon = "null";
	role = "AMFOR";

	uniformClass = "rhs_uniform_acu_oefcp";
	backpack = "";
	
	weapons[] = {
		"rhs_weap_m4a1"
	};

	linkedItems[] = {
		"rhsusf_iotv_ocp_Rifleman",
		"rhsusf_ach_helmet_ocp",
		"rhsusf_acc_M952V",
		"rhsusf_acc_compm4",
		"rhsusf_acc_grip2",
		"ItemCompass",
		"ItemWatch",
		"ItemMap"
	};
	
	items[] = {
		"ACE_elasticBandage",
        "ACE_elasticBandage",
        "ACE_elasticBandage",
        "ACE_elasticBandage",
        "ACE_elasticBandage",
        "ACE_elasticBandage",
        "ACE_elasticBandage",
        "ACE_elasticBandage",
        "ACE_elasticBandage",
        "ACE_elasticBandage",
        "ACE_elasticBandage",
        "ACE_elasticBandage",
        "ACE_elasticBandage",
        "ACE_elasticBandage",
        "ACE_elasticBandage",
        "ACE_elasticBandage",
        "ACE_elasticBandage",
        "ACE_elasticBandage",
        "ACE_elasticBandage",
        "ACE_elasticBandage",
        "ACE_morphine",
        "ACE_morphine",
        "ACE_tourniquet",
        "ACE_tourniquet",
        "ACE_tourniquet",
        "ACE_tourniquet",
        "ACE_CableTie",
        "ACE_CableTie",
        "ACE_EarPlugs",
        "ACE_Splint",
        "ACE_Splint",
        "rhssaf_beret_green",
        "rhs_mag_30Rnd_556x45_M855A1_Stanag",
        "rhs_mag_30Rnd_556x45_M855A1_Stanag",
        "rhs_mag_30Rnd_556x45_M855A1_Stanag",
        "rhs_mag_30Rnd_556x45_M855A1_Stanag",
        "rhs_mag_30Rnd_556x45_M855A1_Stanag",
        "rhs_mag_30Rnd_556x45_M855A1_Stanag",
        "rhs_mag_30Rnd_556x45_M855A1_Stanag",
        "rhs_mag_30Rnd_556x45_M855A1_Stanag",
        "SmokeShell",
        "SmokeShell",
        "SmokeShell",
		"ACRE_PRC343"
	};
};
';


// Find name
$findme  = 'displayName = "';
$findme2 = ';';

$pos1 = stripos($str, $findme);
$pos2 = stripos($str, $findme2,$pos1);
$loadoutName = substr($str,($pos1 + 15 ),($pos2 - $pos1 - 16));


// Find role
$findme  = 'role = ';
$findme2 = ';';

$pos1 = stripos($str, $findme);
$pos2 = stripos($str, $findme2,$pos1);
$loadoutRole = substr($str,($pos1 + 8 ),($pos2 - $pos1 - 9));


// Find uniform
$findme  = 'uniformClass = "';
$findme2 = ';';

$pos1 = stripos($str, $findme);
$pos2 = stripos($str, $findme2,$pos1);
$loadoutUniform = substr($str,($pos1 + 16 ),($pos2 - $pos1 - 17));


// Find bag
$findme  = 'backpack = "';
$findme2 = ';';

$pos1 = stripos($str, $findme);
$pos2 = stripos($str, $findme2,$pos1);
$loadoutBag = substr($str,($pos1 + 12 ),($pos2 - $pos1 - 13));


// Find weapon
$findme  = 'weapons[] = {';
$findme2 = ';';

$pos1 = stripos($str, $findme);
$pos2 = stripos($str, $findme2,$pos1);
$loadoutWeapon = substr($str,($pos1 + 18 ),($pos2 - $pos1 - 23));

// Find linkedItems
$findme  = 'linkedItems';
$findme2 = ';';
    
$pos1 = stripos($str, $findme);
$pos2 = stripos($str, $findme2,$pos1);
// Temp string without the "
$tempStr = str_replace('"','', substr($str,($pos1 + 22 ),($pos2 - $pos1 - 23)));
// preg_replace to get rid of all whitespace "like" characters
$loadoutLinkedItems = preg_replace('/\s/', '', $tempStr);

// Find Items
$findme  = 'items';
$findme2 = ';';
    
$pos1 = strpos($str, $findme);
$pos2 = stripos($str, $findme2,$pos1);
// Temp string without the "
$tempStr = str_replace('"','', substr($str,($pos1 + 16 ),($pos2 - $pos1 - 17)));
// preg_replace to get rid of all whitespace "like" characters
$loadoutItems = preg_replace('/\s/', '', $tempStr);


// Display bruh ha ha
echo '<h1>String Input</h1><br>';
echo $str;
echo '<h1>String Output</h1>';
echo '<br>';
echo 'Name: '. $loadoutName;
echo '<br>';
echo 'Role: '. $loadoutRole;
echo '<br>';
echo 'Uniform: '. $loadoutUniform;
echo '<br>';
echo 'Bag: '. $loadoutBag;
echo '<br>';
echo 'Weapon: '. $loadoutWeapon;
echo '<br>';
echo 'Linked Items: '.$loadoutLinkedItems;
echo '<br>';
echo 'Items: '.$loadoutItems;


// String to Array magic by Pidu. Not optimal, but it works. For Each loop from array of values to array of values would be sexier, but ain't nobody got time for that.
echo '<h1>Array Magic</h1><br>';

$arrName[0]= $loadoutName;
$arrRole[0]= $loadoutRole;
$arrUniform[0]= $loadoutUniform;
$arrBag[0]= $loadoutBag;
$arrWeapon[0]= $loadoutWeapon;
$arrLinkedItems = explode(',',$loadoutLinkedItems);
$arrItems = explode(',',$loadoutItems);

// Even more display bruh ha ha
echo '<br>';
print_r($arrName);
echo '<br>';
print_r($arrRole);
echo '<br>';
print_r($arrUniform);
echo '<br>';
print_r($arrBag);
echo '<br>';
print_r($arrWeapon);
echo '<br>';
print_r($arrLinkedItems);
echo '<br>';
print_r($arrItems);
echo '<br>';


// Count duplicates in array and display item with quantity in a sexy array format
$vals = array_count_values($arrItems);
echo '<h1>Even More Array Magic! Showing Item and quantity </h1><br>';
echo '<pre>'; 
print_r($vals);
echo '</pre>';

?>

Event Timeline

Added Import into database feature:

Inserts data into two tables:

loadouts:

and loadout_detail:

// Insert into database

$servername = "localhost";
$username = "root";
$password = "1234";
$dbname = "utfn";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO loadouts (loadoutName, Role)
VALUES ('$arrName[0]', '$arrRole[0]')";

if (mysqli_query($conn, $sql)) {
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}


$sql = "SELECT id FROM loadouts WHERE loadoutName ='$loadoutName'";

$result = mysqli_query($conn, $sql);

while ($row = mysqli_fetch_assoc($result)) {
    $loadoutid = $row['id'];
     }

echo '<br>'.$loadoutid;


// Insert Weapon
$sql = "INSERT INTO loadout_details (id, classname, quantity, importSlot)
VALUES ('$loadoutid', '$loadoutWeapon', 1, 'Weapon')";

if (mysqli_query($conn, $sql)) {
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}


// Insert Uniform
$sql = "INSERT INTO loadout_details (id, classname, quantity, importSlot)
VALUES ('$loadoutid', '$loadoutUniform', 1, 'Uniform')";

if (mysqli_query($conn, $sql)) {
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

// Insert Bag
$sql = "INSERT INTO loadout_details (id, classname, quantity, importSlot)
VALUES ('$loadoutid', '$loadoutBag', 1, 'Bag')";

if (mysqli_query($conn, $sql)) {
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}


// Insert Linked Items
// This requires to append multiple values with a foreach loop


// begin the sql statement
$sql = "INSERT INTO loadout_details (id, classname, quantity, importSlot ) VALUES ";

// this is where the magic happens
$it = new ArrayIterator( $vals );

// a new caching iterator gives us access to hasNext()
$cit = new CachingIterator( $it );

// loop over the array
foreach ( $cit as $value )
{
// add to the query
$sql .= "('$loadoutid','" .$cit->key()."','" .$cit->current()."','Item')";
// if there is another array member, add a comma
if( $cit->hasNext() )
{
$sql .= ",";
}
}

// now we can use a single database connection and query

mysqli_query( $conn, $sql );



// Insert  Items
// This requires to append multiple values with a foreach loop


// begin the sql statement
$sql = "INSERT INTO loadout_details (id, classname, quantity, importSlot ) VALUES ";

// this is where the magic happens
$it = new ArrayIterator( $arrLinkedItems );

// a new caching iterator gives us access to hasNext()
$cit = new CachingIterator( $it );

// loop over the array
foreach ( $cit as $value )
{
// add to the query
$sql .= "('$loadoutid','" .$cit->current()."',1,'LinkedItem')";
// if there is another array member, add a comma
if( $cit->hasNext() )
{
$sql .= ",";
}
}

// now we can use a single database connection and query

mysqli_query( $conn, $sql );

Achtung! Connection still open at this point!

This system allows to inner join the relevant tables so results such as this can be easily displayed:

<?php
    
	// Setup connection to mySQL db
	$link = new mysqli("localhost", "root", "1234", "utfn");
	
	// Error handler if connection to db fails
	if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
	}

    // Double Inner Join
    $sql = "SELECT * 
    FROM `loadouts`
    JOIN `loadout_details`
    ON loadouts.id = loadout_details.id
    JOIN `arsenal`
    ON loadout_details.classname = arsenal.classname";

    $totalweight = 0.00;
    
    
    // Display bruh ha ha
 
    if($result = mysqli_query($link, $sql)){
        if(mysqli_num_rows($result) > 0){
            echo "<table>";
                echo "<tr>";
                    echo "<th>name</th>";
                    echo "<th>Type</th>";
                    echo "<th>Quantity</th>";
                    echo "<th>Weight in kg</th>";
                    echo "<th>Total Weight</th>";
                echo "</tr>";
            while($row = mysqli_fetch_array($result)){
                echo "<tr>";
                    echo "<td>" . $row['name'] . "</td>";
                    echo "<td>" . $row['type'] . "</td>";
                    echo "<td>" . $row['quantity'] . "</td>";
                    echo "<td>" . $row['weight'] . "</td>";
                    // Calculate Weight of quantity items
                    echo "<td>" . $row['weight'] * $row['quantity'] . "</td>";
                echo "</tr>";
                // Update Total Weight
                $totalweight += $row['weight'];
            }
            echo "</table>";
            // Free result set
            mysqli_free_result($result);
        } else{
            echo "No records matching your query were found.";
        }
        } else{
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
        }
    
    // Display total Weight
     echo "<br>Total Weight:" . $totalweight;
    // Close connection
    mysqli_close($link);
   
?>