[MIRROR] Adds associative list manipulation to circuits, updates a few list circuit components. [MDB IGNORE] (#23362)

* Adds associative list manipulation to circuits, updates a few list circuit components. (#77803)

* Adds associative list manipulation to circuits, updates a few list circuit components.

---------

Co-authored-by: Watermelon914 <37270891+Watermelon914@users.noreply.github.com>
This commit is contained in:
SkyratBot
2023-08-27 10:38:13 -07:00
committed by GitHub
co-authored by Watermelon914
parent 9bc7156137
commit fd1ac2de54
16 changed files with 190 additions and 39 deletions
@@ -0,0 +1,25 @@
/**
* # Associative List Remove Component
*
* Removes an element from an assoc list.
*/
/obj/item/circuit_component/variable/assoc_list/list_remove
display_name = "Associative List Remove"
desc = "Removes a key from an associative list variable."
category = "List"
/// Key to remove to the list
var/datum/port/input/to_remove
circuit_flags = CIRCUIT_FLAG_INPUT_SIGNAL|CIRCUIT_FLAG_OUTPUT_SIGNAL
/obj/item/circuit_component/variable/assoc_list/list_remove/populate_ports()
to_remove = add_input_port("To Remove", PORT_TYPE_STRING)
/obj/item/circuit_component/variable/assoc_list/list_remove/input_received(datum/port/input/port, list/return_values)
if(!current_variable)
return
var/list/info = current_variable.value
var/value_to_remove = to_remove.value
info -= value_to_remove
@@ -0,0 +1,54 @@
/**
* # Assoc List Set Component
*
* Sets a string value on an assoc list.
*/
/obj/item/circuit_component/variable/assoc_list/list_set
display_name = "Associative List Set"
desc = "Sets a string key on an associative list to a specific value."
category = "List"
/// Key to set
var/datum/port/input/key
/// Value to set the key to.
var/datum/port/input/value
/// For when the list is too long, a signal is sent here.
var/datum/port/output/failed
var/max_list_size = 500
circuit_flags = CIRCUIT_FLAG_INPUT_SIGNAL|CIRCUIT_FLAG_OUTPUT_SIGNAL
/obj/item/circuit_component/variable/assoc_list/list_set/get_ui_notices()
. = ..()
. += create_ui_notice("Max List Size: [max_list_size]", "orange", "sitemap")
/obj/item/circuit_component/variable/assoc_list/list_set/populate_ports()
key = add_input_port("Key", PORT_TYPE_STRING)
value = add_input_port("Value", PORT_TYPE_ANY)
failed = add_output_port("Failed", PORT_TYPE_SIGNAL)
/obj/item/circuit_component/variable/assoc_list/list_set/pre_input_received(datum/port/input/port)
. = ..()
if(current_variable)
value.set_datatype(current_variable.datatype_handler.get_datatype(2))
/obj/item/circuit_component/variable/assoc_list/list_set/input_received(datum/port/input/port, list/return_values)
if(!current_variable)
return
var/list/info = current_variable.value
var/key_to_set = key.value
var/value_to_set = value.value
if(!key_to_set)
failed.set_output(COMPONENT_SIGNAL)
return
if(length(info) >= max_list_size)
failed.set_output(COMPONENT_SIGNAL)
return
if(isdatum(value_to_set))
value_to_set = WEAKREF(value_to_set)
info[key_to_set] = value_to_set
@@ -1,39 +0,0 @@
/**
* # Get Column Component
*
* Gets the column of a table and returns it as a regular list.
*/
/obj/item/circuit_component/get_column
display_name = "Get Column"
desc = "Gets the column of a table and returns it as a regular list."
category = "List"
circuit_flags = CIRCUIT_FLAG_INPUT_SIGNAL|CIRCUIT_FLAG_OUTPUT_SIGNAL
/// The list to perform the filter on
var/datum/port/input/received_table
/// The name of the column to check
var/datum/port/input/column_name
/// The filtered list
var/datum/port/output/output_list
/obj/item/circuit_component/get_column/populate_ports()
received_table = add_input_port("Input", PORT_TYPE_TABLE)
column_name = add_input_port("Column Name", PORT_TYPE_STRING)
output_list = add_output_port("Output", PORT_TYPE_LIST(PORT_TYPE_ANY))
/obj/item/circuit_component/get_column/input_received(datum/port/input/port)
var/list/input_list = received_table.value
if(!islist(input_list) || isnum(column_name.value))
return
var/list/new_list = list()
for(var/list/entry in input_list)
var/anything = entry[column_name.value]
if(islist(anything))
continue
new_list += anything
output_list.set_output(new_list)
@@ -1,39 +0,0 @@
/**
* # Index Table Component
*
* Gets the row of a table as an associative list using the index inputted. Will return no value if the index is invalid or a proper table is not returned.
*/
/obj/item/circuit_component/index_table
display_name = "Index Table"
desc = "Gets the row of a table as an associative list using the index inputted. Will return no value if the index is invalid or a proper table is not returned."
category = "List"
circuit_flags = CIRCUIT_FLAG_INPUT_SIGNAL|CIRCUIT_FLAG_OUTPUT_SIGNAL
/// The list to perform the filter on
var/datum/port/input/received_table
/// The target index
var/datum/port/input/target_index
/// The filtered list
var/datum/port/output/output_list
/obj/item/circuit_component/index_table/populate_ports()
received_table = add_input_port("Input", PORT_TYPE_TABLE)
target_index = add_input_port("Index", PORT_TYPE_NUMBER)
output_list = add_output_port("Output", PORT_TYPE_ASSOC_LIST(PORT_TYPE_STRING, PORT_TYPE_ANY))
/obj/item/circuit_component/index_table/input_received(datum/port/input/port)
var/list/target_list = received_table.value
if(!islist(target_list) || !length(target_list))
output_list.set_output(null)
return
var/index = target_index.value
if(index < 1 || index > length(target_list))
output_list.set_output(null)
return
output_list.set_output(target_list[index])
@@ -1,11 +1,11 @@
/**
* # List Add Component
* # List Find Component
*
* Adds an element to a list.
* Finds an element in a list and returns the index.
*/
/obj/item/circuit_component/listin
display_name = "Element Find"
desc = "Checks if an element is in a list."
desc = "Checks if an element is in a list and returns the index it is as if it is. Index is set to 0 on failure."
category = "List"
/// The list type we're checking
@@ -22,6 +22,8 @@
var/datum/port/output/not_found
/// Result of the search
var/datum/port/output/result
/// Index of the element if found.
var/datum/port/output/index
circuit_flags = CIRCUIT_FLAG_INPUT_SIGNAL
@@ -35,6 +37,7 @@
found = add_output_port("Succeeded", PORT_TYPE_SIGNAL)
not_found = add_output_port("Failed", PORT_TYPE_SIGNAL)
result = add_output_port("Result", PORT_TYPE_NUMBER)
index = add_output_port("Index", PORT_TYPE_NUMBER)
/obj/item/circuit_component/listin/pre_input_received(datum/port/input/port)
. = ..()
@@ -50,9 +53,11 @@
if(isdatum(data_to_check))
data_to_check = WEAKREF(data_to_check)
var/actual_result = (data_to_check in info)
if(actual_result)
var/actual_result = info.Find(data_to_check)
index.set_output(actual_result)
if(actual_result != 0)
result.set_output(TRUE)
found.set_output(COMPONENT_SIGNAL)
else
result.set_output(FALSE)
not_found.set_output(COMPONENT_SIGNAL)
result.set_output(actual_result)
@@ -1,91 +0,0 @@
/**
* # Select Component
*
* Selects a list from a list of lists by a specific column. Used only by USBs for communications to and from computers with lists of varying sizes.
*/
/obj/item/circuit_component/select
display_name = "Select Query"
desc = "A component used with USB cables that can perform select queries on a list based on the column name selected. The values are then compared with the comparison input."
category = "List"
circuit_flags = CIRCUIT_FLAG_INPUT_SIGNAL|CIRCUIT_FLAG_OUTPUT_SIGNAL
var/datum/port/input/option/comparison_options
/// The list to perform the filter on
var/datum/port/input/received_table
/// The name of the column to check
var/datum/port/input/column_name
/// The input to compare with
var/datum/port/input/comparison_input
/// The filtered list
var/datum/port/output/filtered_table
var/current_type = PORT_TYPE_ANY
/obj/item/circuit_component/select/populate_options()
var/static/component_options = list(
COMP_COMPARISON_EQUAL,
COMP_COMPARISON_NOT_EQUAL,
COMP_COMPARISON_GREATER_THAN,
COMP_COMPARISON_LESS_THAN,
COMP_COMPARISON_GREATER_THAN_OR_EQUAL,
COMP_COMPARISON_LESS_THAN_OR_EQUAL,
)
comparison_options = add_option_port("Comparison Options", component_options)
/obj/item/circuit_component/select/populate_ports()
received_table = add_input_port("Input", PORT_TYPE_TABLE)
column_name = add_input_port("Column Name", PORT_TYPE_STRING)
comparison_input = add_input_port("Comparison Input", PORT_TYPE_ANY)
filtered_table = add_output_port("Output", PORT_TYPE_TABLE)
/obj/item/circuit_component/select/pre_input_received(datum/port/input/port)
var/current_option = comparison_options.value
switch(current_option)
if(COMP_COMPARISON_EQUAL, COMP_COMPARISON_NOT_EQUAL)
if(current_type != PORT_TYPE_ANY)
current_type = PORT_TYPE_ANY
comparison_input.set_datatype(PORT_TYPE_ANY)
else
if(current_type != PORT_TYPE_NUMBER)
current_type = PORT_TYPE_NUMBER
comparison_input.set_datatype(PORT_TYPE_NUMBER)
/obj/item/circuit_component/select/input_received(datum/port/input/port)
var/current_option = comparison_options.value
var/list/input_list = received_table.value
if(!islist(input_list) || isnum(column_name.value))
return
var/comparison_value = comparison_input.value
var/list/new_list = list()
for(var/list/entry in input_list)
var/anything = entry[column_name.value]
if(islist(anything))
continue
if(current_option != COMP_COMPARISON_EQUAL && current_option != COMP_COMPARISON_NOT_EQUAL && !isnum(anything))
continue
var/add_to_list = FALSE
switch(current_option)
if(COMP_COMPARISON_EQUAL)
add_to_list = anything == comparison_value
if(COMP_COMPARISON_NOT_EQUAL)
add_to_list = anything != comparison_value
if(COMP_COMPARISON_GREATER_THAN)
add_to_list = anything > comparison_value
if(COMP_COMPARISON_GREATER_THAN_OR_EQUAL)
add_to_list = anything >= comparison_value
if(COMP_COMPARISON_LESS_THAN)
add_to_list = anything < comparison_value
if(COMP_COMPARISON_LESS_THAN_OR_EQUAL)
add_to_list = anything <= comparison_value
if(add_to_list)
new_list += list(entry)
filtered_table.set_output(new_list)