mirror of
https://github.com/Aurorastation/Aurora.3.git
synced 2026-08-28 15:39:57 +01:00
Various Cargo Improvements (#4435)
Implements #4434 The reason for the order can now be viewed in the cargo control application Calculate the shuttle fee per order It is now possible to pay for orders that have been approved by cargo before they are shipped to the station A mainfest spawns in the crates Schema Changes: The supplier, path and amount columns have been retired and are no longer used Instead the following columns will be used: supplier - the short name of the supplier that will be used price - the price for the items items - the items and their variables If you are storing the cargo items in the SQL DB, then you dont have to do anything. They will be migrated automatically to the new format If you are storing the cargo items in JSON Files then you have to rewrite the files to adhere to the new format. A example file of the new format is included
This commit is contained in:
@@ -18,7 +18,7 @@ var/datum/controller/subsystem/cargo/SScargo
|
||||
//Shipment stuff
|
||||
var/shipmentnum
|
||||
var/list/cargo_shipments = list() //List of the shipments to the station
|
||||
var/datum/cargo_shipment/current_shipment = null // The current cargo shipment
|
||||
var/datum/cargo_shipment/current_shipment = null //The current cargo shipment
|
||||
|
||||
//order stuff
|
||||
var/ordernum
|
||||
@@ -32,15 +32,18 @@ var/datum/controller/subsystem/cargo/SScargo
|
||||
var/credits_per_platinum = 140 //Per sheet
|
||||
var/credits_per_phoron = 100 //Per sheet
|
||||
var/cargo_handlingfee = 50 //The handling fee cargo takes per crate
|
||||
var/cargo_handlingfee_min = 0 // The minimum handling fee
|
||||
var/cargo_handlingfee_max = 500 // The maximum handling fee
|
||||
var/cargo_handlingfee_change = 1 // If the handlingfee can be changed -> For a random event
|
||||
var/cargo_handlingfee_min = 0 //The minimum handling fee
|
||||
var/cargo_handlingfee_max = 500 //The maximum handling fee
|
||||
var/cargo_handlingfee_change = 1 //If the handlingfee can be changed -> For a random event
|
||||
var/datum/money_account/supply_account
|
||||
|
||||
//shuttle movement
|
||||
var/movetime = 1200
|
||||
var/datum/shuttle/ferry/supply/shuttle
|
||||
|
||||
//Item vars
|
||||
var/last_item_id = 0 //The ID of the last item that has been added
|
||||
|
||||
/datum/controller/subsystem/cargo/Recover()
|
||||
src.shuttle = SScargo.shuttle
|
||||
src.cargo_items = SScargo.cargo_items
|
||||
@@ -50,13 +53,14 @@ var/datum/controller/subsystem/cargo/SScargo
|
||||
src.cargo_handlingfee = SScargo.cargo_handlingfee
|
||||
src.cargo_handlingfee_change = SScargo.cargo_handlingfee_change
|
||||
src.supply_account = SScargo.supply_account
|
||||
src.last_item_id = SScargo.last_item_id
|
||||
|
||||
/datum/controller/subsystem/cargo/Initialize(timeofday)
|
||||
//Generate the ordernumber and shipmentnumber to start with
|
||||
ordernum = rand(1,8000)
|
||||
shipmentnum = rand(500,700)
|
||||
|
||||
//Fetch the cargo account once the round is started - TODO: Repalce that once economy gets its own subsystem
|
||||
//Fetch the cargo account once the round is started - TODO-CARGO: Repalce that once economy gets its own subsystem
|
||||
SSticker.OnRoundstart(CALLBACK(GLOBAL_PROC, .proc/fetch_supply_account))
|
||||
|
||||
//Load in the cargo items config
|
||||
@@ -67,7 +71,7 @@ var/datum/controller/subsystem/cargo/SScargo
|
||||
log_debug("SScargo: Attempting to Load from JSON")
|
||||
load_from_json()
|
||||
else
|
||||
log_game("SSCargo: invalid load option specified in config")
|
||||
log_game("SScargo: invalid load option specified in config")
|
||||
|
||||
var/datum/cargospawner/spawner = new
|
||||
spawner.start()
|
||||
@@ -78,17 +82,61 @@ var/datum/controller/subsystem/cargo/SScargo
|
||||
NEW_SS_GLOBAL(SScargo)
|
||||
|
||||
|
||||
/*
|
||||
Data Migrations
|
||||
*/
|
||||
/datum/controller/subsystem/cargo/proc/run_migration_1()
|
||||
if(!establish_db_connection(dbcon))
|
||||
log_debug("SScargo: SQL ERROR - Failed to connect. - Migration not executed")
|
||||
return
|
||||
//Gets those that havnt been migrated
|
||||
var/DBQuery/item_query = dbcon.NewQuery("SELECT id, name, path_old, suppliers_old FROM ss13_cargo_items WHERE deleted_at is NULL AND items is NULL")
|
||||
item_query.Execute()
|
||||
while(item_query.NextRow())
|
||||
CHECK_TICK
|
||||
|
||||
//Gets the vars of the row
|
||||
var/id = item_query.item[1]
|
||||
var/name = item_query.item[2]
|
||||
var/path = item_query.item[3]
|
||||
var/list/suppliers = list()
|
||||
try
|
||||
suppliers = json_decode(item_query.item[4])
|
||||
catch(var/exception/e)
|
||||
log_debug("SScargo: Error Decoding supplier data for item: [id] - [e]")
|
||||
continue
|
||||
|
||||
// Calculate the following items
|
||||
//Supplier - The first supplier in the suppliers list
|
||||
var/supplier
|
||||
//Price - The price of the supplier
|
||||
var/price
|
||||
//Items - A list containing the items (only one for legacy items) and the attributes of the supplier
|
||||
var/list/items = list()
|
||||
for(var/sup in suppliers)
|
||||
supplier = sup
|
||||
price = suppliers[sup]["base_purchase_price"]
|
||||
items[name] = list()
|
||||
items[name]["path"] = path
|
||||
items[name]["vars"] = suppliers[sup]["vars"]
|
||||
break
|
||||
|
||||
log_debug("Data Conversion for id: [id] - Supplier: [supplier] - Price: [price] - items: [json_encode(items)]")
|
||||
var/DBQuery/update_query = dbcon.NewQuery("UPDATE ss13_cargo_items SET supplier = :supplier:, price = :price:, items = :items: WHERE id = :id:")
|
||||
update_query.Execute(list("id"=id,"price"=price,"supplier"=supplier,"items"=json_encode(items)))
|
||||
|
||||
/*
|
||||
Loading Data
|
||||
*/
|
||||
//Reset cargo to prep for loading in new items
|
||||
/datum/controller/subsystem/cargo/proc/reset_cargo()
|
||||
cargo_shipments = list() //List of the shipments to the station
|
||||
current_shipment = null // The current cargo shipment
|
||||
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()
|
||||
@@ -99,137 +147,60 @@ var/datum/controller/subsystem/cargo/SScargo
|
||||
//Reset the currently loaded data
|
||||
reset_cargo()
|
||||
|
||||
//Populate the items missing a name or a description with the missing name / description
|
||||
var/DBQuery/item_null_query = dbcon.NewQuery("SELECT path, name, description, suppliers, id FROM ss13_cargo_items WHERE (name IS NULL OR description IS NULL OR suppliers IS NULL) AND deleted_at IS NULL" )
|
||||
item_null_query.Execute()
|
||||
while(item_null_query.NextRow())
|
||||
CHECK_TICK
|
||||
|
||||
//Spawn the item with the path
|
||||
var/oldpath = item_null_query.item[1]
|
||||
var/oldname = item_null_query.item[2]
|
||||
var/olddsec = item_null_query.item[3]
|
||||
var/oldsuppliers = item_null_query.item[4]
|
||||
var/item_id = item_null_query.item[5]
|
||||
var/path = text2path(oldpath)
|
||||
|
||||
log_debug("SScargo: Got path without name or description - updating: [oldpath]")
|
||||
if(path)
|
||||
var/obj/cargoitem = new path
|
||||
//The following sql queries arnt overly optimized. Its just not worht it, because they are only run once if a item is added without the complete data
|
||||
if(cargoitem)
|
||||
//Update the name of the item if its null
|
||||
if(!oldname)
|
||||
log_debug("SScargo: Updating name to [cargoitem.name]")
|
||||
var/DBQuery/item_update_name_query = dbcon.NewQuery("UPDATE ss13_cargo_items SET name = :name: WHERE id = :item_id:")
|
||||
item_update_name_query.Execute(list("name" = cargoitem.name, "item_id" = item_id ))
|
||||
//Update the name of the item if its null
|
||||
if(!olddsec)
|
||||
log_debug("SScargo: Updating desc to [cargoitem.desc]")
|
||||
var/DBQuery/item_update_desc_query = dbcon.NewQuery("UPDATE ss13_cargo_items SET description = :desc: WHERE id = :item_id:")
|
||||
item_update_desc_query.Execute(list("desc" = cargoitem.desc, "item_id" = item_id ))
|
||||
//Set NT as default supplier with a price 1.5 times of the item value
|
||||
if(!oldsuppliers)
|
||||
var/price = get_value(cargoitem) * 1.5
|
||||
var/list/sups = list()
|
||||
sups["nt"] = list()
|
||||
sups["nt"]["base_purchase_price"] = price
|
||||
sups["nt"]["vars"] = list()
|
||||
log_debug("SScargo: Updating suppliers string to [json_encode(sups)]")
|
||||
var/DBQuery/item_update_supl_query = dbcon.NewQuery("UPDATE ss13_cargo_items SET suppliers = '[json_encode(sups)]' WHERE id = [item_id]")
|
||||
item_update_supl_query.Execute()
|
||||
qdel(cargoitem)
|
||||
//Run the data migration
|
||||
run_migration_1()
|
||||
|
||||
//Load the categories
|
||||
var/DBQuery/category_query = dbcon.NewQuery("SELECT 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())
|
||||
var/datum/cargo_category/cc = new()
|
||||
cc.name = category_query.item[1]
|
||||
cc.display_name = category_query.item[2]
|
||||
cc.description = category_query.item[3]
|
||||
cc.icon = category_query.item[4]
|
||||
cc.price_modifier = text2num(category_query.item[5])
|
||||
|
||||
//Add the category to the cargo_categories list
|
||||
cargo_categories[cc.name] = cc
|
||||
|
||||
CHECK_TICK
|
||||
|
||||
try
|
||||
add_category(
|
||||
category_query.item[1],
|
||||
category_query.item[2],
|
||||
category_query.item[3],
|
||||
category_query.item[4],
|
||||
text2num(category_query.item[5]))
|
||||
catch(var/exception/ec)
|
||||
log_debug("SScargo: Error when loading category: [ec]")
|
||||
//Load the suppliers
|
||||
var/DBQuery/supplier_query = dbcon.NewQuery("SELECT 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())
|
||||
var/datum/cargo_supplier/cs = new()
|
||||
cs.short_name = supplier_query.item[1]
|
||||
cs.name = supplier_query.item[2]
|
||||
cs.description = supplier_query.item[3]
|
||||
cs.tag_line = supplier_query.item[4]
|
||||
cs.shuttle_time = text2num(supplier_query.item[5])
|
||||
cs.shuttle_price = text2num(supplier_query.item[6])
|
||||
cs.available = text2num(supplier_query.item[7])
|
||||
cs.price_modifier = text2num(supplier_query.item[8])
|
||||
|
||||
cargo_suppliers[cs.short_name] = cs
|
||||
|
||||
CHECK_TICK
|
||||
|
||||
try
|
||||
add_supplier(
|
||||
supplier_query.item[1],
|
||||
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])
|
||||
catch(var/exception/es)
|
||||
log_debug("SScargo: Error when loading supplier: [es]")
|
||||
//Load the items
|
||||
var/DBQuery/item_query = dbcon.NewQuery("SELECT path, name, description, categories, suppliers, amount, access, container_type, groupable FROM ss13_cargo_items WHERE deleted_at is NULL ORDER BY order_by ASC, name ASC")
|
||||
var/DBQuery/item_query = 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 supplier IS NOT NULL ORDER BY order_by ASC, name ASC, supplier ASC")
|
||||
item_query.Execute()
|
||||
while(item_query.NextRow())
|
||||
CHECK_TICK
|
||||
|
||||
var/itempath = text2path(item_query.item[1])
|
||||
if(!ispath(itempath))
|
||||
log_debug("SScargo: Warning - Attempted to add item with invalid path: [item_query.item[2]] - [item_query.item[1]]")
|
||||
continue
|
||||
var/datum/cargo_item/ci = new()
|
||||
try
|
||||
ci.path = item_query.item[1]
|
||||
ci.name = item_query.item[2]
|
||||
ci.description = item_query.item[3]
|
||||
ci.categories = json_decode(item_query.item[4])
|
||||
ci.suppliers = json_decode(item_query.item[5])
|
||||
ci.amount = text2num(item_query.item[6])
|
||||
ci.access = text2num(item_query.item[7]) //TODO: Maybe add the option to specify access as string instead of number
|
||||
ci.container_type = item_query.item[8]
|
||||
ci.groupable = text2num(item_query.item[9])
|
||||
catch(var/exception/e)
|
||||
log_debug("SScargo: Error when loading item: [e]")
|
||||
qdel(ci)
|
||||
continue
|
||||
|
||||
//Check if a valid container is specified
|
||||
if(!(ci.container_type == CARGO_CONTAINER_CRATE || ci.container_type == CARGO_CONTAINER_FREEZER || ci.container_type == CARGO_CONTAINER_BOX))
|
||||
log_debug("SScargo: Invalid container type specified for item - Aborting")
|
||||
qdel(ci)
|
||||
continue
|
||||
|
||||
//Verify the suppliers exist
|
||||
for(var/sup in ci.suppliers)
|
||||
var/datum/cargo_supplier/cs = get_supplier_by_name(sup)
|
||||
if(!cs)
|
||||
log_debug("SScargo: [sup] is not a valid supplier for item [ci.name].")
|
||||
|
||||
//Setting the supplier
|
||||
ci.suppliers[sup]["supplier_datum"] = cs
|
||||
|
||||
//Add the item to the cargo_items list
|
||||
cargo_items[ci.path] = ci
|
||||
|
||||
//Log a message if no categories are specified
|
||||
if(ci.categories.len == 0)
|
||||
log_debug("SScargo: No categories specified for item [ci.name].")
|
||||
|
||||
//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)
|
||||
else
|
||||
log_debug("SScargo: Warning - Attempted to add [ci.name] item to category [category] that does not exist.")
|
||||
|
||||
add_item(
|
||||
item_query.item[1],
|
||||
item_query.item[2],
|
||||
item_query.item[3],
|
||||
item_query.item[4],
|
||||
json_decode(item_query.item[5]),
|
||||
item_query.item[6],
|
||||
json_decode(item_query.item[7]),
|
||||
item_query.item[8],
|
||||
item_query.item[9],
|
||||
item_query.item[10],
|
||||
item_query.item[11])
|
||||
catch(var/exception/ei)
|
||||
log_debug("SScargo: Error when loading item from sql: [ei]")
|
||||
return 1
|
||||
|
||||
//Loads the cargo data from JSON
|
||||
@@ -237,8 +208,8 @@ var/datum/controller/subsystem/cargo/SScargo
|
||||
var/list/cargoconfig = list()
|
||||
try
|
||||
cargoconfig = json_decode(return_file_text("config/cargo.json"))
|
||||
catch(var/exception/e)
|
||||
log_debug("SScargo: Warning: Could not load config, as cargo.json is missing - [e]")
|
||||
catch(var/exception/ej)
|
||||
log_debug("SScargo: Warning: Could not load config, as cargo.json is missing - [ej]")
|
||||
return
|
||||
|
||||
//Reset the currently loaded data
|
||||
@@ -246,86 +217,168 @@ var/datum/controller/subsystem/cargo/SScargo
|
||||
|
||||
//Load the cargo categories
|
||||
for (var/category in cargoconfig["categories"])
|
||||
var/datum/cargo_category/cc = new()
|
||||
cc.name = cargoconfig["categories"][category]["name"]
|
||||
cc.display_name = cargoconfig["categories"][category]["display_name"]
|
||||
cc.description = cargoconfig["categories"][category]["description"]
|
||||
cc.icon = cargoconfig["categories"][category]["icon"]
|
||||
cc.price_modifier = cargoconfig["categories"][category]["price_modifier"]
|
||||
|
||||
//Add the category to the cargo_categories list
|
||||
cargo_categories[cc.name] = cc
|
||||
|
||||
CHECK_TICK
|
||||
|
||||
try
|
||||
add_category(
|
||||
cargoconfig["categories"][category]["name"],
|
||||
cargoconfig["categories"][category]["display_name"],
|
||||
cargoconfig["categories"][category]["description"],
|
||||
cargoconfig["categories"][category]["icon"],
|
||||
cargoconfig["categories"][category]["price_modifier"])
|
||||
catch(var/exception/ec)
|
||||
log_debug("SScargo: Error when loading category: [ec]")
|
||||
//Load the suppliers
|
||||
for (var/supplier in cargoconfig["suppliers"])
|
||||
var/datum/cargo_supplier/cs = new()
|
||||
cs.short_name = supplier
|
||||
cs.name = cargoconfig["suppliers"][supplier]["name"]
|
||||
cs.description = cargoconfig["suppliers"][supplier]["description"]
|
||||
cs.tag_line = cargoconfig["suppliers"][supplier]["tag_line"]
|
||||
cs.shuttle_time = cargoconfig["suppliers"][supplier]["shuttle_time"]
|
||||
cs.shuttle_price = cargoconfig["suppliers"][supplier]["shuttle_price"]
|
||||
cs.available = cargoconfig["suppliers"][supplier]["available"]
|
||||
cs.price_modifier = cargoconfig["suppliers"][supplier]["price_modifier"]
|
||||
|
||||
cargo_suppliers[cs.short_name] = cs
|
||||
|
||||
CHECK_TICK
|
||||
|
||||
try
|
||||
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"])
|
||||
catch(var/exception/es)
|
||||
log_debug("SScargo: Error when loading supplier: [es]")
|
||||
//Load the cargoitems
|
||||
for (var/item in cargoconfig["items"])
|
||||
CHECK_TICK
|
||||
|
||||
var/itempath = text2path(item)
|
||||
if(!ispath(itempath))
|
||||
log_debug("SScargo: Warning - Attempted to add item with invalid path: [item]")
|
||||
continue
|
||||
var/datum/cargo_item/ci = new()
|
||||
ci.path = item
|
||||
ci.name = cargoconfig["items"][item]["name"]
|
||||
ci.description = cargoconfig["items"][item]["description"]
|
||||
ci.categories = cargoconfig["items"][item]["categories"]
|
||||
ci.suppliers = cargoconfig["items"][item]["suppliers"]
|
||||
ci.amount = cargoconfig["items"][item]["amount"]
|
||||
ci.access = cargoconfig["items"][item]["access"]
|
||||
ci.container_type = cargoconfig["items"][item]["container_type"]
|
||||
ci.groupable = cargoconfig["items"][item]["groupable"]
|
||||
|
||||
//Verify the suppliers exist
|
||||
for(var/sup in ci.suppliers)
|
||||
var/datum/cargo_supplier/cs = get_supplier_by_name(sup)
|
||||
if(!cs)
|
||||
log_debug("SScargo: [sup] is not a valid supplier for item [ci.name].")
|
||||
|
||||
//Setting the supplier
|
||||
ci.suppliers[sup]["supplier_datum"] = cs
|
||||
|
||||
//Add the item to the cargo_items list
|
||||
cargo_items[ci.path] = ci
|
||||
|
||||
//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)
|
||||
else
|
||||
log_debug("SScargo: Warning - Attempted to add item [ci.name] to category that does not exist.")
|
||||
try
|
||||
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"])
|
||||
catch(var/exception/ei)
|
||||
log_debug("SScargo: Error when loading supplier: [ei]")
|
||||
return 1
|
||||
|
||||
//Add a new Category to the Cargo Subsystem
|
||||
/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
|
||||
/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 and CAN NOT be passed as a json string.
|
||||
//Decoding of the string MUST take place before
|
||||
/datum/controller/subsystem/cargo/proc/add_item(var/id=null,var/name,var/supplier="nt",var/description,var/list/categories,var/price,var/list/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
|
||||
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_debug("SScargo: Error when loading item: [e]")
|
||||
qdel(ci)
|
||||
return
|
||||
|
||||
for(var/item in ci.items)
|
||||
var/itempath = text2path(ci.items[item]["path"])
|
||||
if(!ispath(itempath))
|
||||
log_debug("SScargo: Warning - Attempted to add item with invalid path - [ci.id] - [ci.name] - [ci.items[item]["path"]]")
|
||||
return
|
||||
|
||||
//Check if a valid container is specified
|
||||
if(!(ci.container_type == CARGO_CONTAINER_CRATE || ci.container_type == CARGO_CONTAINER_FREEZER || ci.container_type == CARGO_CONTAINER_BOX))
|
||||
log_debug("SScargo: Invalid container type specified for item - Aborting")
|
||||
qdel(ci)
|
||||
return
|
||||
|
||||
//Verify the suppliers exist
|
||||
var/datum/cargo_supplier/cs = get_supplier_by_name(ci.supplier)
|
||||
if(!cs)
|
||||
log_debug("SScargo: [ci.supplier] is not a valid supplier for item [ci.name].")
|
||||
QDEL_NULL(ci)
|
||||
return
|
||||
|
||||
//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_debug("SScargo: No categories specified for item [ci.name].")
|
||||
|
||||
//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)
|
||||
else
|
||||
log_debug("SScargo: Warning - Attempted to add [ci.name] item to category [category] that does not exist.")
|
||||
|
||||
return ci
|
||||
|
||||
/*
|
||||
Getting items, categories, suppliers and shipments
|
||||
*/
|
||||
/datum/controller/subsystem/cargo/proc/get_order_count()
|
||||
return all_orders.len
|
||||
//Increments the orderid and returns it
|
||||
//Returns the order id and increments it
|
||||
/datum/controller/subsystem/cargo/proc/get_next_order_id()
|
||||
. = ordernum
|
||||
ordernum++
|
||||
return .
|
||||
|
||||
//Increments the itemid and returns it
|
||||
/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]
|
||||
@@ -414,62 +467,24 @@ var/datum/controller/subsystem/cargo/SScargo
|
||||
suppliers[supplier] = supplier
|
||||
return suppliers
|
||||
|
||||
//Submits an order to cargo
|
||||
/datum/controller/subsystem/cargo/proc/submit_order(var/datum/cargo_order/co)
|
||||
co.status = "submitted"
|
||||
co.time_submitted = worldtime2text()
|
||||
co.order_id = get_next_order_id()
|
||||
all_orders.Add(co)
|
||||
return 1
|
||||
|
||||
//Approve a order - Returns a status message
|
||||
/datum/controller/subsystem/cargo/proc/approve_order(var/datum/cargo_order/co, var/approved_by)
|
||||
if(co.status == "submitted")
|
||||
co.status = "approved"
|
||||
co.time_approved = worldtime2text()
|
||||
co.authorized_by = approved_by
|
||||
return "The order has been approved"
|
||||
else
|
||||
return "The order could not be approved - Invalid Status"
|
||||
|
||||
//Reject a order - Returns a status message
|
||||
/datum/controller/subsystem/cargo/proc/reject_order(var/datum/cargo_order/co)
|
||||
if(co.status == "submitted")
|
||||
co.status = "rejected"
|
||||
co.time_approved = worldtime2text()
|
||||
return "The order has been rejected"
|
||||
else
|
||||
return "The order could not be rejected - Invalid Status"
|
||||
|
||||
//Deliver a order
|
||||
/datum/controller/subsystem/cargo/proc/deliver_order(var/datum/cargo_order/co, var/received_by, var/payment_status = 1)
|
||||
if(co.status == "shipped")
|
||||
co.status = "delivered"
|
||||
co.time_delivered = worldtime2text()
|
||||
co.received_by = received_by
|
||||
co.payment_status = payment_status
|
||||
return "The order has been delivered"
|
||||
else
|
||||
return "The order could not be delivered - Invalid Status"
|
||||
|
||||
//Checks if theorder can be shipped and marks it as shipped if possible
|
||||
/datum/controller/subsystem/cargo/proc/ship_order(var/datum/cargo_order/co)
|
||||
//Get the price cargo has to pay for the order
|
||||
var/item_price = co.get_value(1)
|
||||
|
||||
//Get the maximum shipment costs for the order
|
||||
var/max_shipment_cost = co.get_max_shipment_cost()
|
||||
var/shipment_cost = co.get_shipment_cost()
|
||||
|
||||
//Check if cargo has enough money to pay for the shipment of the item and the maximum shipment cost
|
||||
if(item_price + max_shipment_cost > get_cargo_money())
|
||||
log_debug("SScargo: Order could not be shipped. Insufficient money. [item_price] + [max_shipment_cost] > [get_cargo_money()].")
|
||||
if(item_price + shipment_cost > get_cargo_money())
|
||||
log_debug("SScargo: Order could not be shipped. Insufficient money. [item_price] + [shipment_cost] > [get_cargo_money()].")
|
||||
return 0
|
||||
|
||||
co.status = "shipped"
|
||||
co.time_shipped = worldtime2text()
|
||||
co.set_shipped()
|
||||
current_shipment.shipment_cost_purchase += item_price //Increase the price of the shipment
|
||||
current_shipment.orders.Add(co) //Add the order to the order list
|
||||
return 1
|
||||
|
||||
//Generate a new cargo shipment
|
||||
/datum/controller/subsystem/cargo/proc/new_shipment()
|
||||
current_shipment = new()
|
||||
@@ -594,7 +609,7 @@ var/datum/controller/subsystem/cargo/SScargo
|
||||
|
||||
//Sells stuff on the shuttle to centcom
|
||||
/datum/controller/subsystem/cargo/proc/sell()
|
||||
//TODO: Only pay for specific stuff on the shuttle and not for everything else - Charge a cleanup fee for the rest
|
||||
//TODO-CARGO: Only pay for specific stuff on the shuttle and not for everything else - Charge a cleanup fee for the rest
|
||||
var/area/area_shuttle = shuttle.get_location_area()
|
||||
if(!area_shuttle) return
|
||||
|
||||
@@ -657,26 +672,22 @@ var/datum/controller/subsystem/cargo/SScargo
|
||||
|
||||
for(var/datum/cargo_order/co in approved_orders)
|
||||
if(!co)
|
||||
break
|
||||
continue
|
||||
|
||||
//Check if theres space to place the order
|
||||
if(!clear_turfs.len)
|
||||
log_debug("SScargo: Order [co.order_id] could not be placed on the shuttle because the shuttle is full")
|
||||
break
|
||||
|
||||
//Check if the supplier is still active
|
||||
//Check if the supplier is still available
|
||||
for(var/datum/cargo_order_item/coi in co.items)
|
||||
var/datum/cargo_supplier/csu = get_supplier_by_name(coi.supplier)
|
||||
if(!csu)
|
||||
log_debug("SScargo: Order [co.order_id] could not be placed on the shuttle because supplier [coi.supplier] for item [coi.ci.name] is invalid")
|
||||
break
|
||||
if(!csu.available)
|
||||
log_debug("SScargo: Order [co.order_id] could not be placed on the shuttle because supplier [coi.supplier] for item [coi.ci.name] is unavailable")
|
||||
break
|
||||
if(!coi.ci.supplier_datum.available)
|
||||
log_debug("SScargo: 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")
|
||||
continue
|
||||
|
||||
//Check if there is enough money to ship the order
|
||||
if(!ship_order(co))
|
||||
break
|
||||
continue
|
||||
|
||||
var/i = rand(1,clear_turfs.len)
|
||||
var/turf/pickedloc = clear_turfs[i]
|
||||
@@ -687,8 +698,8 @@ var/datum/controller/subsystem/cargo/SScargo
|
||||
var/obj/A = new containertype(pickedloc)
|
||||
|
||||
//Label the crate
|
||||
//TODO: Look into wrapping it in a NT paper
|
||||
A.name = "[co.order_id] - [co.customer]"
|
||||
//TODO-CARGO: Look into wrapping it in a the suppliers paper
|
||||
A.name = "[co.order_id] - [co.ordered_by]"
|
||||
|
||||
//Set the access requirement
|
||||
if(co.required_access.len > 0)
|
||||
@@ -698,26 +709,22 @@ var/datum/controller/subsystem/cargo/SScargo
|
||||
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_debug("SScargo: Bad variable name [var_name] for item name: [coi.ci.name] id: [coi.ci.id] - [e]")
|
||||
|
||||
//Spawn the specified amount
|
||||
for(i=0, i < coi.ci.amount, i++)
|
||||
var/atom/item = new coi.ci.path(A)
|
||||
|
||||
//Customize items with supplier data
|
||||
var/list/suppliers = coi.ci.suppliers
|
||||
for(var/var_name in suppliers[coi.supplier]["vars"])
|
||||
try
|
||||
item.vars[var_name] = suppliers[coi.supplier]["vars"][var_name]
|
||||
catch(var/exception/e)
|
||||
log_debug("SScargo: Bad variable name [var_name] for item [coi.ci.path] - [e]")
|
||||
//Spawn the Paper Inside
|
||||
var/obj/item/weapon/paper/P = new(A)
|
||||
P.set_content_unsafe("[co.order_id] - [co.ordered_by]", co.get_report_delivery_order())
|
||||
|
||||
//Shuttle is loaded now - Charge cargo for it
|
||||
charge_cargo("Shipment #[current_shipment.shipment_num] - Expense",current_shipment.shipment_cost_purchase)
|
||||
|
||||
if(current_shipment.orders.len)
|
||||
//Now calculate the aliquot shipment cost for the orders and add it to each order
|
||||
var/aliquot_shipment_cost = current_shipment.shuttle_fee / current_shipment.orders.len
|
||||
for(var/datum/cargo_order/co in current_shipment.orders)
|
||||
co.partial_shipment_fee = aliquot_shipment_cost
|
||||
|
||||
return 1
|
||||
|
||||
Reference in New Issue
Block a user