added crafting database
This commit is contained in:
parent
a59b352b57
commit
da43fd41db
Binary file not shown.
BIN
Database/crafting/__pycache__/id.cpython-311.pyc
Normal file
BIN
Database/crafting/__pycache__/id.cpython-311.pyc
Normal file
Binary file not shown.
2
Database/crafting/data/ammo.csv
Normal file
2
Database/crafting/data/ammo.csv
Normal file
@ -0,0 +1,2 @@
|
||||
id,result,input1,qty1,input2,qty2,input3,qty3,input4,qty4
|
||||
NULL,10000,20000,1,20002,1,20005,1,20001,15
|
|
2
Database/crafting/data/food.csv
Normal file
2
Database/crafting/data/food.csv
Normal file
@ -0,0 +1,2 @@
|
||||
id,result,input1,qty1,input2,qty2,input3,qty3,input4,qty4
|
||||
NULL,10000,20000,1,20002,1,20005,1,20001,15
|
|
2
Database/crafting/data/gear.csv
Normal file
2
Database/crafting/data/gear.csv
Normal file
@ -0,0 +1,2 @@
|
||||
id,result,input1,qty1,input2,qty2,input3,qty3,input4,qty4
|
||||
NULL,10000,20000,1,20002,1,20005,1,20001,15
|
|
2
Database/crafting/data/material.csv
Normal file
2
Database/crafting/data/material.csv
Normal file
@ -0,0 +1,2 @@
|
||||
id,result,input1,qty1,input2,qty2,input3,qty3,input4,qty4
|
||||
NULL,10000,20000,1,20002,1,20005,1,20001,15
|
|
2
Database/crafting/data/med.csv
Normal file
2
Database/crafting/data/med.csv
Normal file
@ -0,0 +1,2 @@
|
||||
id,result,input1,qty1,input2,qty2,input3,qty3,input4,qty4
|
||||
NULL,10000,20000,1,20002,1,20005,1,20001,15
|
|
2
Database/crafting/data/scrap.csv
Normal file
2
Database/crafting/data/scrap.csv
Normal file
@ -0,0 +1,2 @@
|
||||
id,result,input1,qty1,input2,qty2,input3,qty3,input4,qty4
|
||||
NULL,10000,20000,1,20002,1,20005,1,20001,15
|
|
2
Database/crafting/data/weapon.csv
Normal file
2
Database/crafting/data/weapon.csv
Normal file
@ -0,0 +1,2 @@
|
||||
id,result,input1,qty1,input2,qty2,input3,qty3,input4,qty4
|
||||
NULL,10000,20000,1,20002,1,20005,1,20001,15
|
|
BIN
Database/crafting/database.db
Normal file
BIN
Database/crafting/database.db
Normal file
Binary file not shown.
10
Database/crafting/id.py
Normal file
10
Database/crafting/id.py
Normal file
@ -0,0 +1,10 @@
|
||||
# starting_ids.py
|
||||
|
||||
starting_ids = {
|
||||
"ammo": 1000,
|
||||
"food": 2000,
|
||||
"gear": 3000,
|
||||
"material": 4000,
|
||||
"weapon":5000,
|
||||
"scrap": 9000,
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
id,type,name,rarity,weight,height,width,stack,value
|
||||
NULL,meds,Bandage,1,0.1411,1,1,5,6500
|
||||
NULL,meds,Small Health Kit (SHK),1,0.18,1,1,1,32000
|
||||
NULL,meds,Individual First Aid Kit,2,0.22046,1,1,1,60000
|
||||
NULL,meds,Dual First Aid Kit,2,0.44092,1,2,2,100000
|
||||
NULL,meds,Group First Aid Kit,3,0.88184,2,2,4,120000
|
||||
NULL,meds,Adaptive First Aid Kit,4,0.22046,1,1,1,90000
|
||||
NULL,meds,Surgery Kit,2,0.88185,1,1,2,100000
|
||||
NULL,meds,Surgery Kit (L),3,1.32278,1,2,3,150000
|
||||
NULL,meds,Adrenaline,3,0.22046,1,1,1,50000
|
||||
NULL,meds,Pain killer,1,0.05512,1,1,4,10000
|
||||
NULL,meds,Morphine,2,0.22046,1,1,1,40000
|
||||
NULL,meds,Caffeine (pill),2,0.05512,1,1,4,2500
|
||||
NULL,meds,Tourniquet,1,0.37371,1,1,2,11000
|
||||
NULL,meds,Muscle Stimulant,4,0,0,0,0,0
|
||||
NULL,meds,Rad-x,0,0,0,0,0,0
|
|
BIN
Database/items/__pycache__/id.cpython-311.pyc
Normal file
BIN
Database/items/__pycache__/id.cpython-311.pyc
Normal file
Binary file not shown.
78
Database/items/createTables.py
Normal file
78
Database/items/createTables.py
Normal file
@ -0,0 +1,78 @@
|
||||
import sqlite3
|
||||
import os
|
||||
import csv
|
||||
|
||||
|
||||
def get_data_type(value):
|
||||
try:
|
||||
int(value)
|
||||
return "INTEGER"
|
||||
except ValueError:
|
||||
try:
|
||||
float(value)
|
||||
return "REAL"
|
||||
except ValueError:
|
||||
return "TEXT"
|
||||
|
||||
|
||||
# Connect to the SQLite database and delete existing tables
|
||||
conn = sqlite3.connect("database.db")
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Get a list of CSV files in the "data" directory
|
||||
data_dir = "data" # Change this to your data directory path
|
||||
csv_files = [f for f in os.listdir(data_dir) if f.endswith(".csv")]
|
||||
|
||||
# Drop all existing tables except for sqlite_sequence
|
||||
cursor.execute("PRAGMA foreign_keys = OFF;")
|
||||
cursor.execute(
|
||||
"SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_sequence';"
|
||||
)
|
||||
existing_tables = cursor.fetchall()
|
||||
for table in existing_tables:
|
||||
cursor.execute(f"DROP TABLE IF EXISTS {table[0]};")
|
||||
|
||||
# Commit the changes to delete existing tables
|
||||
conn.commit()
|
||||
|
||||
# Iterate through CSV files and create new tables
|
||||
for csv_file in csv_files:
|
||||
table_name = os.path.splitext(csv_file)[0]
|
||||
|
||||
# Read the first row of the CSV file to determine the column names
|
||||
with open(os.path.join(data_dir, csv_file), newline="") as csvfile:
|
||||
csv_reader = csv.reader(csvfile)
|
||||
header = next(csv_reader)
|
||||
|
||||
# Read the second row to determine data types
|
||||
with open(os.path.join(data_dir, csv_file), newline="") as csvfile:
|
||||
csv_reader = csv.reader(csvfile)
|
||||
next(csv_reader) # Skip the header row
|
||||
data_row = next(csv_reader)
|
||||
data_types = [get_data_type(value) for value in data_row]
|
||||
|
||||
# Add a primary key column if needed (replace 'unique_id' with your unique identifier column name)
|
||||
if "unique_id" in header:
|
||||
header[header.index("unique_id")] += " PRIMARY KEY"
|
||||
|
||||
# Generate the CREATE TABLE statement dynamically based on the column names and data types
|
||||
create_table_sql = f"CREATE TABLE IF NOT EXISTS {table_name} (\n"
|
||||
for column_name, data_type in zip(header, data_types):
|
||||
create_table_sql += f" {column_name} {data_type},\n"
|
||||
create_table_sql = create_table_sql.rstrip(",\n") + "\n);"
|
||||
|
||||
# Execute the CREATE TABLE statement
|
||||
cursor.execute(create_table_sql)
|
||||
|
||||
# Read and insert data from the CSV file into the table
|
||||
with open(os.path.join(data_dir, csv_file), newline="") as csvfile:
|
||||
csv_reader = csv.reader(csvfile)
|
||||
next(csv_reader) # Skip the header row
|
||||
for row in csv_reader:
|
||||
placeholders = ",".join(["?"] * len(row))
|
||||
insert_sql = f"INSERT INTO {table_name} VALUES ({placeholders});"
|
||||
cursor.execute(insert_sql, row)
|
||||
|
||||
# Commit the changes and close the connection
|
||||
conn.commit()
|
||||
conn.close()
|
16
Database/items/data/med.csv
Normal file
16
Database/items/data/med.csv
Normal file
@ -0,0 +1,16 @@
|
||||
id,type,name,rarity,weight,height,width,stack,value
|
||||
NULL,bleed1,Bandage,1,0.1411,1,1,5,6500
|
||||
NULL,health,Small Health Kit (SHK),1,0.18,1,1,1,32000
|
||||
NULL,health,Individual First Aid Kit,2,0.22046,1,1,1,60000
|
||||
NULL,health,Dual First Aid Kit,2,0.44092,1,2,2,100000
|
||||
NULL,health,Group First Aid Kit,3,0.88184,2,2,4,120000
|
||||
NULL,health,Adaptive First Aid Kit,4,0.22046,1,1,1,90000
|
||||
NULL,surgery,Surgery Kit,2,0.88185,1,1,2,100000
|
||||
NULL,surgery,Surgery Kit (L),3,1.32278,1,2,3,150000
|
||||
NULL,stim,Adrenaline,3,0.22046,1,1,1,50000
|
||||
NULL,stim,Pain killer,1,0.05512,1,1,4,10000
|
||||
NULL,stim,Morphine,2,0.22046,1,1,1,40000
|
||||
NULL,stim,Caffeine (pill),2,0.05512,1,1,4,2500
|
||||
NULL,bleed2,Tourniquet,1,0.37371,1,1,2,11000
|
||||
NULL,stim,Muscle Stimulant,4,0,0,0,0,0
|
||||
NULL,stim,Rad-x,0,0,0,0,0,0
|
|
Binary file not shown.
@ -1,9 +1,10 @@
|
||||
# starting_ids.py
|
||||
|
||||
starting_ids = {
|
||||
"reloading": 20000,
|
||||
"reload": 20000,
|
||||
"ammo": 10000,
|
||||
"craft": 20000,
|
||||
"gear": 30000,
|
||||
"parts": 40000,
|
||||
"part": 40000,
|
||||
"med":50000,
|
||||
}
|
47
Database/items/importData.py
Normal file
47
Database/items/importData.py
Normal file
@ -0,0 +1,47 @@
|
||||
import csv
|
||||
import os
|
||||
import sqlite3
|
||||
from id import starting_ids
|
||||
|
||||
# Connect to the SQLite database
|
||||
conn = sqlite3.connect("database.db")
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Define the directory where the CSV files are located
|
||||
csv_directory = "data" # Change this to your directory path
|
||||
|
||||
|
||||
# Function to load data from a CSV file into a table
|
||||
def load_csv_data(csv_path, table_name, cursor, starting_id):
|
||||
# Delete existing data in the table
|
||||
delete_query = f"DELETE FROM {table_name}"
|
||||
cursor.execute(delete_query)
|
||||
|
||||
with open(csv_path, newline="") as csvfile:
|
||||
csv_reader = csv.reader(csvfile)
|
||||
next(csv_reader) # Skip the header row if it exists in the CSV
|
||||
for row in csv_reader:
|
||||
# Exclude the first column (id) from the row
|
||||
values = row[1:]
|
||||
placeholders = ", ".join(["?"] * len(values))
|
||||
insert_query = f"INSERT INTO {table_name} VALUES (?, {placeholders})"
|
||||
cursor.execute(insert_query, [starting_id] + values)
|
||||
starting_id += 1
|
||||
|
||||
|
||||
# Get a list of CSV files in the data directory
|
||||
csv_files = [f for f in os.listdir(csv_directory) if f.endswith(".csv")]
|
||||
|
||||
# Loop through the CSV files and load data into respective tables
|
||||
for csv_file in csv_files:
|
||||
table_name = os.path.splitext(csv_file)[
|
||||
0
|
||||
] # Remove the file extension to get the table name
|
||||
csv_path = os.path.join(csv_directory, csv_file)
|
||||
if table_name in starting_ids:
|
||||
starting_id = starting_ids[table_name]
|
||||
load_csv_data(csv_path, table_name, cursor, starting_id)
|
||||
|
||||
# Commit the changes and close the connection
|
||||
conn.commit()
|
||||
conn.close()
|
21
Database/items/main.py
Normal file
21
Database/items/main.py
Normal file
@ -0,0 +1,21 @@
|
||||
import subprocess
|
||||
|
||||
# Define the paths to your Python scripts
|
||||
create_tables_script = "createTables.py"
|
||||
import_data_script = "importData.py"
|
||||
|
||||
# Run the createTables.py script
|
||||
try:
|
||||
subprocess.run(["python", create_tables_script], check=True)
|
||||
print("createTables.py script executed successfully.")
|
||||
except subprocess.CalledProcessError:
|
||||
print("Error running createTables.py script.")
|
||||
exit(1)
|
||||
|
||||
# Run the importData.py script
|
||||
try:
|
||||
subprocess.run(["python", import_data_script], check=True)
|
||||
print("importData.py script executed successfully.")
|
||||
except subprocess.CalledProcessError:
|
||||
print("Error running importData.py script.")
|
||||
exit(1)
|
Loading…
Reference in New Issue
Block a user