Codeside Cargo Refactor (#20030)

**NOTE TO SYSADMINS: See "SQL Details" section below for information on
SQL modifications.**

Moves the data containing cargo items (i.e. the ones you order from ops
and get in the cargo shuttle) from the online database to the codebase.
Everything from suppliers to categories to individual items is now
code-side and editable by developers/contributors.

Refactors cargo items to use `singletons` instead of `datums` for
`cargo_supplier`, `cargo_category`, and `cargo_item`. Multiple-instnace
things like cargo_orders, etc. still use `datums`.

Fixed a bunch of strange discrepancies in categories, suppliers, and
pricing for various cargo items. I did a little bit, but it's exhausting
to go through all of it right now.

Clicking the 'Details' button on the Cargo Order app now actually gives
you details instead of bluescreening. Also added some UI elements to the
Cargo Order app - Cargo Control and Delivery remain untouched.

Overhauled the Cargo Order console TGUI window. It now has tabs on the
left, displays restricted access, supplier information, and boasts
search functionality.

### SQL Details
<details>
<summary>SQL Details [Click to Expand]</summary>

The following SQL tables should be deleted or deprecated from the server
database, as they are no longer in use:

- `ss13_cargo_items`
- `ss13_cargo_categories`
- `ss13_cargo_suppliers`

The included migration file, `V011__codeside_cargo`, creates a new table
`ss13_cargo_item_orderlog` to the DB. This **replaces**
`ss13_cargo_orderlog_items`. Because of this,
`ss13_cargo_orderlog_items` is deprecated and should either be deleted
or locked & preserved for logging purposes.

</details>

## Screenshots


![image](https://github.com/user-attachments/assets/79129923-1fb6-4cee-ac8d-5505a52270a4)

![image](https://github.com/user-attachments/assets/a323be35-8ce6-4ec4-98f7-ee701d0931a3)

![image](https://github.com/user-attachments/assets/5ddb02c5-152f-4715-b2da-20903fa11c93)

![image](https://github.com/user-attachments/assets/420e45b0-6a9f-4420-beb8-a2c8423a5be4)

![image](https://github.com/user-attachments/assets/114f4755-ee51-41e6-8670-07aacc5326ae)

---------

Signed-off-by: naut <55491249+nauticall@users.noreply.github.com>
Co-authored-by: Fluffy <65877598+FluffyGhoster@users.noreply.github.com>
This commit is contained in:
naut
2025-03-05 02:18:00 +08:00
committed by GitHub
parent 14d7425cee
commit 55f4e12614
32 changed files with 7869 additions and 583 deletions
+173 -286
View File
@@ -1,6 +1,6 @@
//Config stuff
#define SUPPLY_DOCKZ 1 //Z-level of the Dock.
#define SUPPLY_STATIONZ 6 //Z-level of the Station.
#define SUPPLY_DOCKZ 1 //Z-level of the Dock.
#define SUPPLY_STATIONZ 6 //Z-level of the Station.
#define SUPPLY_STATION_AREATYPE /area/supply/station //Type of the supply shuttle area for station
#define SUPPLY_DOCK_AREATYPE /area/supply/dock //Type of the supply shuttle area for dock
@@ -70,14 +70,7 @@ SUBSYSTEM_DEF(cargo)
supply_account = SSeconomy.get_department_account("Operations")
//Load in the cargo items config
if(GLOB.config.cargo_load_items_from == "sql")
log_subsystem_cargo("SScargo: Attempting to Load from SQL")
load_from_sql()
else if(GLOB.config.cargo_load_items_from == "json")
log_subsystem_cargo("SScargo: Attempting to Load from JSON")
load_from_json()
else
log_game("SScargo: invalid load option specified in config")
load_cargo_files()
setupExports()
setupBounties()
@@ -96,247 +89,73 @@ SUBSYSTEM_DEF(cargo)
/datum/controller/subsystem/cargo/proc/reset_cargo()
cargo_shipments = list() //List of the shipments to the station
current_shipment = null //The current cargo shipment
cargo_items = list() //The list of items
cargo_categories = list() //The list of categories
cargo_suppliers = list() //The list of suppliers
all_orders = list() //All orders
last_item_id = 0
//Load the cargo data from SQL
/datum/controller/subsystem/cargo/proc/load_from_sql()
if(!establish_db_connection(GLOB.dbcon))
log_subsystem_cargo("SQL ERROR - Failed to connect. - Falling back to JSON")
return load_from_json()
else
//Reset the currently loaded data
reset_cargo()
//Load categories
/datum/controller/subsystem/cargo/proc/load_cargo_categories()
for(var/singleton/cargo_category/C as anything in (GET_SINGLETON_SUBTYPE_LIST(/singleton/cargo_category)))
log_subsystem_cargo("Loading category '[C.name]'.")
SScargo.cargo_categories[C.name] = C
//Load the categories
var/DBQuery/category_query = GLOB.dbcon.NewQuery("SELECT id, name, display_name, description, icon, price_modifier FROM ss13_cargo_categories WHERE deleted_at IS NULL ORDER BY order_by ASC")
category_query.Execute()
while(category_query.NextRow())
CHECK_TICK
add_category(
category_query.item[2],
category_query.item[3],
category_query.item[4],
category_query.item[5],
text2num(category_query.item[6]))
//Load the suppliers
var/DBQuery/supplier_query = GLOB.dbcon.NewQuery("SELECT id, short_name, name, description, tag_line, shuttle_time, shuttle_price, available, price_modifier FROM ss13_cargo_suppliers WHERE deleted_at is NULL")
supplier_query.Execute()
while(supplier_query.NextRow())
CHECK_TICK
add_supplier(
supplier_query.item[2],
supplier_query.item[3],
supplier_query.item[4],
supplier_query.item[5],
supplier_query.item[6],
supplier_query.item[7],
supplier_query.item[8],
supplier_query.item[9])
//Load the items
var/DBQuery/item_query = GLOB.dbcon.NewQuery("SELECT id, name, supplier, description, categories, price, items, access, container_type, groupable, item_mul FROM ss13_cargo_items WHERE deleted_at IS NULL AND approved_at IS NOT NULL AND supplier IS NOT NULL ORDER BY order_by ASC, name ASC, supplier ASC")
item_query.Execute()
while(item_query.NextRow())
CHECK_TICK
var/item_id = item_query.item[1]
var/error_message = add_item(
item_query.item[1],
item_query.item[2],
item_query.item[3],
item_query.item[4],
item_query.item[5],
item_query.item[6],
item_query.item[7],
item_query.item[8],
item_query.item[9],
item_query.item[10],
item_query.item[11])
if(error_message && istext(error_message))
log_subsystem_cargo("SScargo: Error when loading item [item_id] from sql: [error_message]")
var/DBQuery/item_error_query = GLOB.dbcon.NewQuery("UPDATE ss13_cargo_items SET error_message = :error_message: WHERE id = :id:")
item_error_query.Execute(list("id"=item_id,"error_message"=error_message))
//Load Suppliers
/datum/controller/subsystem/cargo/proc/load_cargo_suppliers()
for(var/singleton/cargo_supplier/S as anything in (GET_SINGLETON_SUBTYPE_LIST(/singleton/cargo_supplier)))
log_subsystem_cargo("Loading supplier '[S.name]', with short name '[S.short_name]'.")
SScargo.cargo_suppliers[S.short_name] = S
/datum/controller/subsystem/cargo/proc/load_cargo_items()
log_subsystem_cargo("Loading cargo items.")
var/id = 1
//Loads the cargo data from JSON
/datum/controller/subsystem/cargo/proc/load_from_json()
var/list/cargoconfig = list()
if(!(rustg_file_exists("config/cargo.json") == "true"))
log_config("The file config/cargo.json was not found, cargo items will not be loaded.")
return
try
cargoconfig = json_decode(return_file_text("config/cargo.json"))
catch(var/exception/ej)
log_subsystem_cargo("Warning: Could not load config, as cargo.json is missing - [ej]")
return
//Reset the currently loaded data
reset_cargo()
//Load the cargo categories
for (var/category in cargoconfig["categories"])
CHECK_TICK
add_category(
cargoconfig["categories"][category]["name"],
cargoconfig["categories"][category]["display_name"],
cargoconfig["categories"][category]["description"],
cargoconfig["categories"][category]["icon"],
cargoconfig["categories"][category]["price_modifier"])
//Load the suppliers
for (var/supplier in cargoconfig["suppliers"])
CHECK_TICK
add_supplier(
supplier,
cargoconfig["suppliers"][supplier]["name"],
cargoconfig["suppliers"][supplier]["description"],
cargoconfig["suppliers"][supplier]["tag_line"],
cargoconfig["suppliers"][supplier]["shuttle_time"],
cargoconfig["suppliers"][supplier]["shuttle_price"],
cargoconfig["suppliers"][supplier]["available"],
cargoconfig["suppliers"][supplier]["price_modifier"])
//Load the cargoitems
for (var/item in cargoconfig["items"])
CHECK_TICK
var/error_message = add_item(
null,
cargoconfig["items"][item]["name"],
cargoconfig["items"][item]["supplier"],
cargoconfig["items"][item]["description"],
cargoconfig["items"][item]["categories"],
cargoconfig["items"][item]["price"],
cargoconfig["items"][item]["items"],
cargoconfig["items"][item]["access"],
cargoconfig["items"][item]["container_type"],
cargoconfig["items"][item]["groupable"],
cargoconfig["items"][item]["item_mul"])
if(error_message && istext(error_message))
log_subsystem_cargo("Error when loading item: [error_message]")
return 1
// Get the list of all valid cargo items
for (var/singleton/cargo_item/I as anything in (GET_SINGLETON_SUBTYPE_LIST(/singleton/cargo_item)))
cargo_items[I.name] = I
I.id = id++
I.get_adjusted_price()
//Add a new Category to the Cargo Subsystem
//Returns the /datum/cargo_category on success or a error message
/datum/controller/subsystem/cargo/proc/add_category(var/name,var/display_name,var/description,var/icon,var/price_modifier)
var/datum/cargo_category/cc = new()
cc.name = name
cc.display_name = display_name
cc.description = description
cc.icon = icon
cc.price_modifier = text2num(price_modifier)
//Add the category to the cargo_categories list
cargo_categories[cc.name] = cc
return cc
//Add a new Supplier to the Cargo Subsystem
//Returns the /datum/cargo_supplier/ on success or a error message
/datum/controller/subsystem/cargo/proc/add_supplier(var/short_name,var/name,var/description,var/tag_line,var/shuttle_time,var/shuttle_price,var/available,var/price_modifier)
var/datum/cargo_supplier/cs = new()
cs.short_name = short_name
cs.name = name
cs.description = description
cs.tag_line = tag_line
cs.shuttle_time = text2num(shuttle_time)
cs.shuttle_price = text2num(shuttle_price)
cs.available = text2num(available)
cs.price_modifier = text2num(price_modifier)
cargo_suppliers[cs.short_name] = cs
return cs
//Add a new item to the cargo subsystem, the categories and the items need to be a list or a JSON String
//Decoding of the string MUST take place before
//Returns the /datum/cargo_item on success or a error message
/datum/controller/subsystem/cargo/proc/add_item(var/id=null,var/name,var/supplier="nt",var/description,var/categories,var/price,var/items,var/access=0,var/container_type=CARGO_CONTAINER_CRATE,var/groupable=1,var/item_mul=1)
//TODO-CARGO: Maybe add the option to specify access as string instead of number
//If no item ID is supplied generate one ourselfs (use the next free id)
//If one is supplied, update the item id if the one supplied is higher
if(!id)
id=get_next_item_id()
else
id = text2num(id)
if(id > last_item_id)
last_item_id = id
var/datum/cargo_item/ci = new()
try
//Check if categories and items are a list and try decode them (from json) if they are not.
if(!islist(categories))
categories = json_decode(categories)
if(!islist(items))
items = json_decode(items)
ci.id = id
ci.name = name
ci.supplier = supplier
ci.description = description
ci.categories = categories
ci.price = text2num(price)
ci.items = items
ci.access = text2num(access)
ci.container_type = container_type
ci.groupable = text2num(groupable)
ci.item_mul = text2num(item_mul)
ci.amount = length(ci.items)*ci.item_mul
catch(var/exception/e)
log_subsystem_cargo("Error when loading item [name] - [id]: [e]")
qdel(ci)
return "Error when loading item [name] - [id]: [e]"
var/path_error = null
for(var/item in ci.items)
var/itempath = text2path(ci.items[item]["path"])
if(!ispath(itempath))
log_subsystem_cargo("Warning - Attempted to add item with invalid path - [ci.id] - [ci.name] - [ci.items[item]["path"]]")
path_error += "Attempted to add item with invalid path - [ci.id] - [ci.name] - [ci.items[item]["path"]].\n"
if(path_error)
return path_error
//Check if a valid container is specified
if(!(ci.container_type in list(CARGO_CONTAINER_CRATE, CARGO_CONTAINER_FREEZER, CARGO_CONTAINER_BOX, CARGO_CONTAINER_BODYBAG)))
log_subsystem_cargo("Invalid container type specified for item [name] - [id]: Aborting")
qdel(ci)
return "Invalid container type specified for item [name] - [id]"
//Verify the suppliers exist
var/datum/cargo_supplier/cs = get_supplier_by_name(ci.supplier)
if(!cs)
log_subsystem_cargo("[supplier] is not a valid supplier for item [name] - [id]")
qdel(ci)
return "[supplier] is not a valid supplier for item [name] - [id]"
//Setting the supplier
ci.supplier_datum = cs
//Add the item to the cargo_items list
cargo_items["[ci.id]"] = ci
//Add the item to the suppliers items list
ci.supplier_datum.items.Add(ci)
//Log a message if no categories are specified
if(ci.categories.len == 0)
log_subsystem_cargo("No categories specified for item [ci.name]")
ci.supplier_datum = null
qdel(ci)
return "No categories specified for item [name] - [id]"
//Add the item to the categories
for(var/category in ci.categories)
var/datum/cargo_category/cc = cargo_categories[category]
if(cc) //Check if the category exists
cc.items.Add(ci)
// Check if the category exists in SScargo.cargo_categories
if (!(I.category || SScargo.cargo_categories[I.category]))
log_subsystem_cargo("Error: Unable to find category '[I.category]' for item '[I.name]. Skipping.")
continue
else
log_subsystem_cargo("Warning - Attempted to add [ci.name] item to category [category] that does not exist.")
var/singleton/cargo_category/item_category = SScargo.cargo_categories[I.category]
if (!item_category.items)
item_category.items = list()
item_category.items += I
log_subsystem_cargo("Inserted item '[I.name]' into category '[I.category]' with ID '[I.id]'.")
return ci
if (!(I.supplier || SScargo.cargo_suppliers[I.supplier]))
log_subsystem_cargo("Error: Unable to find supplier '[I.supplier]' for item '[I.name]. Skipping.")
continue
else
var/singleton/cargo_supplier/item_supplier = SScargo.cargo_suppliers[I.supplier]
if (!item_supplier.items)
item_supplier.items = list()
item_supplier.items += I
I.supplier_data = item_supplier
log_subsystem_cargo("Finished loading cargo items.")
//Load cargo data from cargo_items.dm
/datum/controller/subsystem/cargo/proc/load_cargo_files()
log_subsystem_cargo("Starting to load cargo data from files.")
//Reset the loaded cargo data
reset_cargo()
//Add categories
load_cargo_categories()
//Load suppliers
load_cargo_suppliers()
//Load cargo items
load_cargo_items()
log_subsystem_cargo("Finished loading cargo data from files.")
/*
Getting items, categories, suppliers and shipments
@@ -352,23 +171,87 @@ SUBSYSTEM_DEF(cargo)
/datum/controller/subsystem/cargo/proc/get_next_item_id()
last_item_id++
return last_item_id
//Gets the items from a category
/datum/controller/subsystem/cargo/proc/get_items_for_category(var/category)
var/datum/cargo_category/cc = cargo_categories[category]
if(cc)
return cc.get_item_list()
else
return list()
//Gets the categories
/// Returns a "default" cargo_category for usage in situations where one is needed.
/datum/controller/subsystem/cargo/proc/get_default_category()
var/list/categories = GET_SINGLETON_SUBTYPE_LIST(/singleton/cargo_category)
var/singleton/cargo_category/default_cat = categories[1]
log_subsystem_cargo("Default category set to '[initial(default_cat.name)]'.")
return initial(default_cat.name)
/// Returns a string-formatted list of data for all suppliers and their information for use in TGUI.
/datum/controller/subsystem/cargo/proc/get_supplier_data(var/supplier_short_name)
var/list/supplier_data = list()
var/singleton/cargo_supplier/S = cargo_suppliers[supplier_short_name]
if(!S)
log_subsystem_cargo("Error: Unable to find supplier '[supplier_short_name]'.")
return
supplier_data += list(
"short_name" = S.short_name,
"name" = S.name,
"description" = S.description,
"tag_line" = S.tag_line,
"shuttle_time" = S.shuttle_time,
"shuttle_price" = S.shuttle_price,
"available" = S.available,
"price_modifier" = S.price_modifier,
)
return supplier_data
/// Returns a string-formatted list of every item in each category and their information for use in TGUI.
/datum/controller/subsystem/cargo/proc/get_items_for_category(var/category_name)
var/list/item_list = list()
var/singleton/cargo_category/C = cargo_categories[category_name]
if(!C)
log_subsystem_cargo("Error: get_items_for_category() was unable to find category '[category_name]'.")
return
for(var/singleton/cargo_item/ci as anything in C.items)
item_list += list(list(
"name" = ci.name,
"description" = ci.description,
"price" = ci.price,
"id" = ci.id,
"price_adjusted" = ci.adjusted_price,
"supplier" = ci.supplier,
"supplier_data" = get_supplier_data(ci.supplier),
"access" = ci.access != 0 ? get_access_desc(ci.access) : "none",
))
return item_list
/// Returns a string-formatted list of categories for use in TGUI.
/datum/controller/subsystem/cargo/proc/get_category_list()
var/list/category_list = list()
for (var/cat_name in cargo_categories)
var/datum/cargo_category/cc = cargo_categories[cat_name]
category_list.Add(list(cc.get_list()))
// Get the singleton instance for the current category
var/singleton/cargo_category/cc = cargo_categories[cat_name]
category_list += list(list(
"name" = cc.name,
"display_name" = cc.display_name,
"description" = cc.description,
"icon" = cc.icon,
"price_modifier" = cc.price_modifier,
))
return category_list
//Get a category by name
//Get category names
/datum/controller/subsystem/cargo/proc/get_category_by_name(var/name)
// Check if the category exists in cargo_categories
if (!cargo_categories[name])
log_subsystem_cargo("Error: Requested category '[name]' does not exist.")
return
// Return the category if it exists
return cargo_categories[name]
//Gets a order by order id
@@ -400,6 +283,7 @@ SUBSYSTEM_DEF(cargo)
/*
Submitting, Approving, Rejecting and Shipping Orders
*/
//Gets the orders based on their status (submitted, approved, shipped)
/datum/controller/subsystem/cargo/proc/get_orders_by_status(var/status, var/data_list=0)
if(!status)
@@ -413,6 +297,7 @@ SUBSYSTEM_DEF(cargo)
else
orders.Add(co)
return orders
//Gets the value of orders based on their status, type is passed on to co.get_value
/datum/controller/subsystem/cargo/proc/get_orders_value_by_status(var/status, var/type=0)
if(!status)
@@ -423,6 +308,7 @@ SUBSYSTEM_DEF(cargo)
if(co.status == status)
value += co.get_value(type)
return value
//Gets the suppliers of the orders of a specific type
/datum/controller/subsystem/cargo/proc/get_order_suppliers_by_status(var/status, var/pretty_names=0)
if(!status)
@@ -434,7 +320,7 @@ SUBSYSTEM_DEF(cargo)
//Get the list of supplirs and add it to the suppliers list
for(var/supplier in co.get_supplier_list())
if(pretty_names)
var/datum/cargo_supplier/cs = SScargo.cargo_suppliers[supplier]
var/singleton/cargo_supplier/cs = SScargo.cargo_suppliers[supplier]
suppliers[cs.short_name] = cs.name
else
suppliers[supplier] = supplier
@@ -498,13 +384,14 @@ SUBSYSTEM_DEF(cargo)
log_subsystem_cargo("Warning: Tried to charge supply account but supply acount doesnt exist")
return 0
return SSeconomy.charge_to_account(supply_account.account_number, "[commstation_name()] - Operations", "[charge_text]", "[commstation_name()] - Banking System", -charge_credits)
//Gets the pending shipment costs for the items that are about to be shipped to the station
/datum/controller/subsystem/cargo/proc/get_pending_shipment_cost(var/status="approved")
//Loop through all the orders marked as shipped and get the suppliers into a list of involved suppliers
var/list/suppliers = get_order_suppliers_by_status(status)
var/price = 0
for(var/supplier in suppliers)
var/datum/cargo_supplier/cs = SScargo.cargo_suppliers[supplier]
var/singleton/cargo_supplier/cs = SScargo.cargo_suppliers[supplier]
if(cs)
price += cs.shuttle_price
return price
@@ -514,7 +401,7 @@ SUBSYSTEM_DEF(cargo)
var/list/suppliers = get_order_suppliers_by_status(status)
var/time = 0
for(var/supplier in suppliers)
var/datum/cargo_supplier/cs = SScargo.cargo_suppliers[supplier]
var/singleton/cargo_supplier/cs = SScargo.cargo_suppliers[supplier]
if(cs)
time += cs.shuttle_time
return time
@@ -686,8 +573,8 @@ SUBSYSTEM_DEF(cargo)
//Check if the supplier is still available
for(var/datum/cargo_order_item/coi in co.items)
if(!coi.ci.supplier_datum.available)
log_subsystem_cargo("Order [co.order_id] could not be placed on the shuttle because supplier [coi.ci.supplier_datum.name] for item [coi.ci.name] is unavailable")
if(!coi.ci.supplier_data.available)
log_subsystem_cargo("Order [co.order_id] could not be placed on the shuttle because supplier [coi.ci.supplier_data.name] for item [coi.ci.name] is unavailable")
continue
//Check if there is enough money to ship the order
@@ -700,34 +587,27 @@ SUBSYSTEM_DEF(cargo)
//Spawn the crate
var/containertype = co.get_container_type()
var/obj/A = new containertype(pickedloc)
var/obj/crate = new containertype(pickedloc)
//Label the crate
A.name_unlabel = A.name
A.name = "[A.name] ([co.order_id] - [co.ordered_by])"
A.verbs += /atom/proc/remove_label
crate.name_unlabel = crate.name
crate.name = "[crate.name] ([co.order_id] - [co.ordered_by])"
crate.verbs += /atom/proc/remove_label
//Set the access requirement
if(co.required_access.len > 0)
A.req_access = co.required_access.Copy()
crate.req_access = co.required_access.Copy()
//Loop through the items and spawn them
for(var/datum/cargo_order_item/coi in co.items)
if(!coi)
continue
for(var/j=1;j<=coi.ci.item_mul;j++)
for(var/name in coi.ci.items)
var/path = coi.ci.items[name]["path"]
var/atom/item = new path(A)
//Customize the items
for(var/var_name in coi.ci.items[name]["vars"])
try
item.vars[var_name] = coi.ci.items[name]["vars"][var_name]
catch(var/exception/e)
log_subsystem_cargo("Bad variable name [var_name] for item name: [coi.ci.name] id: [coi.ci.id] - [e]")
for(var/_ in 1 to coi.ci.spawn_amount)
for(var/item_typepath in coi.ci.items)
new item_typepath(crate)
//Spawn the Paper Inside
var/obj/item/paper/P = new(A)
var/obj/item/paper/P = new(crate)
P.set_content_unsafe("[co.order_id] - [co.ordered_by]", co.get_report_delivery_order())
//Shuttle is loaded now - Charge cargo for it
@@ -735,16 +615,13 @@ SUBSYSTEM_DEF(cargo)
return 1
//Dumps the cargo orders to the database when the round ends
/// Dumps the cargo orders to the database when the round ends.
/datum/controller/subsystem/cargo/proc/dump_orders()
if(dumped_orders)
log_subsystem_cargo("Order Data Dump Aborted - Orders already dumped")
return
if(GLOB.config.cargo_load_items_from != "sql")
log_subsystem_cargo("Order Data Dump Aborted - Cargo not loaded from database")
log_subsystem_cargo("SQL: Orders already dumped. Cargo data dump has been aborted.")
return
if(!establish_db_connection(GLOB.dbcon))
log_subsystem_cargo("SQL ERROR - Failed to connect. - Unable to dump order data")
log_subsystem_cargo("SQL: Unable to connect to SQL database. Cargo data dump has been aborted.")
return
dumped_orders = TRUE
@@ -752,18 +629,24 @@ SUBSYSTEM_DEF(cargo)
// Loop through all the orders and dump them all
var/DBQuery/dump_query = GLOB.dbcon.NewQuery("INSERT INTO `ss13_cargo_orderlog` (`game_id`, `order_id`, `status`, `price`, `ordered_by_id`, `ordered_by`, `authorized_by_id`, `authorized_by`, `received_by_id`, `received_by`, `paid_by_id`, `paid_by`, `time_submitted`, `time_approved`, `time_shipped`, `time_delivered`, `time_paid`, `reason`) \
VALUES (:game_id:, :order_id:, :status:, :price:, :ordered_by_id:, :ordered_by:, :authorized_by_id:, :authorized_by:, :received_by_id:, :received_by:, :paid_by_id:, :paid_by:, :time_submitted:, :time_approved:, :time_shipped:, :time_delivered:, :time_paid:, :reason:)")
var/DBQuery/dump_item_query = GLOB.dbcon.NewQuery("INSERT INTO `ss13_cargo_orderlog_items` (`cargo_orderlog_id`, `cargo_item_id`, `amount`) \
VALUES (:cargo_orderlog_id:, :cargo_item_id:, :amount:)")
var/DBQuery/dump_item_query = GLOB.dbcon.NewQuery("INSERT INTO `ss13_cargo_item_orderlog` (`cargo_orderlog_id`, `item_name`, `amount`) \
VALUES (:cargo_orderlog_id:, :item_name:, :amount:)")
var/DBQuery/log_id = GLOB.dbcon.NewQuery("SELECT LAST_INSERT_ID() AS log_id")
for(var/datum/cargo_order/co in all_orders)
//Iterate over the items in the order and build the a list with the item count
var/list/itemcount = list()
var/list/itemnames = list()
for(var/datum/cargo_order_item/coi in co.items)
if(!isnull(itemcount["[coi.ci.id]"]))
itemcount["[coi.ci.id]"] = itemcount["[coi.ci.id]"] + 1
else
itemcount["[coi.ci.id]"] = 1
itemnames["[coi.ci.id]"] = coi.ci.name
if(!dump_query.Execute(list(
"game_id"=GLOB.round_id,
"order_id"=co.order_id,
@@ -784,7 +667,7 @@ SUBSYSTEM_DEF(cargo)
"time_paid"=co.time_paid,
"reason"=co.reason
)))
log_subsystem_cargo("SQL ERROR - Cound not write order to database")
log_subsystem_cargo("SQL: Could not write order to database. Cargo data dump has been aborted.")
continue
//Run the query to get the inserted id
@@ -796,13 +679,17 @@ SUBSYSTEM_DEF(cargo)
if(db_log_id)
for(var/item_id in itemcount)
dump_item_query.Execute(list(
"cargo_orderlog_id"=db_log_id,
"cargo_item_id"=item_id,
"amount"=itemcount[item_id]
))
if(!dump_item_query.Execute(list(
"cargo_orderlog_id" = db_log_id,
"item_name"= itemnames[item_id],
"amount" = itemcount[item_id]
)))
log_subsystem_cargo("SQL: Cound not write details of a cargo order.")
continue
CHECK_TICK
log_subsystem_cargo("SQL: Saved cargo order log to database.")
/hook/roundend/proc/dump_cargoorders()
SScargo.dump_orders()