clean up, format code

This commit is contained in:
Hash Borgir 2023-06-01 23:32:48 -06:00
parent e277ca9a44
commit 80c1395efc
2 changed files with 130 additions and 105 deletions

View File

@ -48,127 +48,147 @@
class D2Database { class D2Database {
/** /**
* * D2Database constructor.
*/ */
public function __construct() { public function __construct() {
PDO_Connect("sqlite:" . DB_FILE); PDO_Connect("sqlite:" . DB_FILE); // Connect to the SQLite database using the specified file.
} }
/** /**
* @param $file * Create tables based on file and data.
* @param $data *
* @param string $file The file name.
* @param array $data The data array.
* @return void * @return void
*/ */
public function createTables($file, $data) { public function createTables($file, $data) {
$tableName = basename($file); $tableName = basename($file); // Extract the base name of the file.
$tableName = strtolower(substr($tableName, 0, -4)); $tableName = strtolower(substr($tableName, 0, -4)); // Convert the table name to lowercase and remove the file extension.
$sql = ''; $sql = '';
if (!empty($data[0])) {
$sql = "CREATE TABLE IF NOT EXISTS `$tableName` ("; if (!empty($data[0])) { // Check if the data array is not empty.
foreach ($data[0] as $k => $v) { $sql = "CREATE TABLE IF NOT EXISTS `$tableName` ("; // Start building the SQL query to create a table.
if (is_numeric($v)) {
$dataType = "INT"; foreach ($data[0] as $k => $v) { // Iterate over the columns and their corresponding values.
} else { if (is_numeric($v)) { // Check if the value is numeric.
$dataType = "VARCHAR(255)"; $dataType = "INT"; // Set the data type as INT.
} } else {
$sql .= "`$k` $dataType DEFAULT '',"; $dataType = "VARCHAR(255)"; // Set the data type as VARCHAR(255).
} }
$sql = rtrim($sql, ","); $sql .= "`$k` $dataType DEFAULT '',"; // Add the column and its data type to the SQL query.
$sql .= ")"; }
$res = PDO_Execute($sql);
} $sql = rtrim($sql, ","); // Remove the trailing comma.
} $sql .= ")"; // Close the table creation query.
$res = PDO_Execute($sql); // Execute the query to create the table.
}
}
/** /**
* @param $file * Fill tables based on file and data.
* @param $data *
* @param string $file The file name.
* @param array $data The data array.
* @return void * @return void
*/ */
public function fillsTables($file, $data) { public function fillsTables($file, $data) {
$tableName = basename($file); $tableName = basename($file); // Extract the base name of the file.
$tableName = strtolower(substr($tableName, 0, -4)); $tableName = strtolower(substr($tableName, 0, -4)); // Convert the table name to lowercase and remove the file extension.
$sql = '';
$sql = ''; if (!empty($data)) { // Check if the data array is not empty.
if (!empty($data)) { foreach ($data as $d) { // Iterate over the data rows.
foreach ($data as $d) { if (!empty($d)) { // Check if the row is not empty.
$sql = "INSERT INTO `$tableName` ("; // Start building the SQL query to insert into the table.
if (!empty($d)) { foreach ($d as $k => $v) { // Iterate over the column names and values.
$sql = "INSERT INTO `$tableName` ("; $sql .= "`$k`" . ","; // Add the column name to the SQL query.
foreach ($d as $k => $v) { }
$sql .= "`$k`" . ",";
}
$sql = rtrim($sql, ",");
$sql .= ") ";
}
}
}
if (!empty($data)) { $sql = rtrim($sql, ","); // Remove the trailing comma.
$sql .= "VALUES "; $sql .= ") ";
foreach ($data as $d) { }
if (!empty($d)) { }
$sql .= "("; }
foreach ($d as $k => $v) {
$sql .= '"' . $v . '"' . ",";
}
$sql = rtrim($sql, ",");
$sql .= "), ";
}
}
$sql = rtrim($sql, ", ");
$sql .= ";";
$res = PDO_Execute($sql); if (!empty($data)) { // Check if the data array is not empty.
} $sql .= "VALUES ";
}
foreach ($data as $d) { // Iterate over the data rows.
if (!empty($d)) { // Check if the row is not empty.
$sql .= "(";
foreach ($d as $k => $v) { // Iterate over the column values.
$sql .= '"' . $v . '"' . ","; // Add the value to the SQL query.
}
$sql = rtrim($sql, ","); // Remove the trailing comma.
$sql .= "), ";
}
}
$sql = rtrim($sql, ", "); // Remove the trailing comma and space.
$sql .= ";";
$res = PDO_Execute($sql); // Execute the query to insert data into the table.
}
}
/** /**
* @param $data * Write data to the 'strings' table.
*
* @param array $data The data array.
* @return void * @return void
*/ */
public function writeTbl($data) { public function writeTbl($data) {
$sql = 'CREATE TABLE IF NOT EXISTS `strings` (`Key` VARCHAR(255), `String` VARCHAR(255));'; $sql = 'CREATE TABLE IF NOT EXISTS `strings` (`Key` VARCHAR(255), `String` VARCHAR(255));'; // Create the 'strings' table if it doesn't exist.
$res = PDO_Execute($sql); $res = PDO_Execute($sql); // Execute the query to create the table.
$sql = "INSERT INTO `strings` (`Key`,`String`) VALUES "; $sql = "INSERT INTO `strings` (`Key`,`String`) VALUES ";
foreach ($data as $k => $v) {
$sql .= "(\"$k\",\"$v\"),";
}
$sql = rtrim($sql, ", ");
$sql .= ";";
$res = PDO_Execute($sql); foreach ($data as $k => $v) { // Iterate over the key-value pairs in the data array.
} $sql .= "(\"$k\",\"$v\"),"; // Add the key-value pair to the SQL query.
}
$sql = rtrim($sql, ", "); // Remove the trailing comma and space.
$sql .= ";";
$res = PDO_Execute($sql); // Execute the query to insert data into the 'strings' table.
}
/** /**
* @param $data * Write data to a specific table.
*
* @param string $table The table name.
* @param array $data The data array.
* @return void * @return void
*/ */
public function writeTbls($table,$data) { public function writeTbls($table, $data) {
$sql = "CREATE TABLE IF NOT EXISTS `$table` (`Key` VARCHAR(255), `String` VARCHAR(255));"; $sql = "CREATE TABLE IF NOT EXISTS `$table` (`Key` VARCHAR(255), `String` VARCHAR(255));"; // Create the specified table if it doesn't exist.
$res = PDO_Execute($sql); $res = PDO_Execute($sql); // Execute the query to create the table.
$sql = "INSERT INTO `$table` (`Key`,`String`) VALUES "; $sql = "INSERT INTO `$table` (`Key`,`String`) VALUES ";
foreach ($data as $k => $v) {
$sql .= "(\"$k\",\"$v\"),"; foreach ($data as $k => $v) { // Iterate over the key-value pairs in the data array.
} $sql .= "(\"$k\",\"$v\"),"; // Add the key-value pair to the SQL query.
$sql = rtrim($sql, ", "); }
$sql .= ";";
$sql = rtrim($sql, ", "); // Remove the trailing comma and space.
$sql .= ";";
$res = PDO_Execute($sql); // Execute the query to insert data into the specified table.
}
$res = PDO_Execute($sql);
}
/** /**
* @param $key * Get a string value based on the key.
* @return mixed *
* @param string $key The key.
* @return mixed The fetched row.
*/ */
public function getString($key) { public function getString($key) {
$sql = "SELECT String FROM `strings` WHERE `Key`='$key'"; $sql = "SELECT String FROM `strings` WHERE `Key`='$key'"; // Construct the SQL query to retrieve the string value.
$res = PDO_FetchRow($sql); // Execute the query and fetch the row.
$res = PDO_FetchRow($sql); return $res; // Return the fetched row.
return $res; }
}
} }

View File

@ -43,21 +43,22 @@
*/ */
/** /**
* * D2Files class.
*/ */
class D2Files { class D2Files {
/** /**
* @var array * @var array Holds the list of files.
*/ */
public $files = []; public $files = [];
/** /**
* @var * @var array Holds the character files.
*/ */
public $charFiles; public $charFiles;
/** /**
* * D2Files constructor.
*/ */
public function __construct() { public function __construct() {
$filesToIgnore = [ $filesToIgnore = [
@ -68,26 +69,30 @@ class D2Files {
"treasureclass.txt", "treasureclass.txt",
"treasureclassex.txt" "treasureclassex.txt"
]; ];
$glob = glob($_SESSION['path'] . '*.txt');
$glob = glob($_SESSION['path'] . '*.txt'); // Get a list of all ".txt" files in the specified path.
foreach ($glob as $g) { foreach ($glob as $g) {
$files[] = basename($g); $files[] = basename($g); // Add the base name of each file to the $files array.
} }
$this->files = array_udiff($files, $filesToIgnore, 'strcasecmp');
return $this->files; $this->files = array_udiff($files, $filesToIgnore, 'strcasecmp'); // Remove the ignored files from the list, case-insensitive.
return $this->files; // Return the list of files.
} }
/** /**
* @return mixed * Get the list of character save files.
*
* @return mixed The list of character save files.
*/ */
public function getSaveFiles() { public function getSaveFiles() {
$glob = glob($_SESSION['savepath'] . '*.d2s'); $glob = glob($_SESSION['savepath'] . '*.d2s'); // Get a list of all ".d2s" files in the specified save path.
foreach ($glob as $g) { foreach ($glob as $g) {
$this->charFiles[] = basename($g); $this->charFiles[] = basename($g); // Add the base name of each file to the $charFiles array.
} }
return $this->charFiles; return $this->charFiles; // Return the list of character save files.
} }
} }