9 @brief Note to self: Rewrite more than half of that class
12from global_variables
import Commands, Items, TranslationReference
17 Class in charge of converting and translating the data received from the outside into commands understood by the internal program
20 def __init__(self, data, error: int = 84, success: int = 0) ->
None:
45 data (any): _description_: The type of data is meant to be of type dict or string or a set of byte string.
46 Other formats will return an error.
47 If the content is empty, an error is returned.
50 int: _description_: success if it succeeded to update the raw data, error if it failed to update the raw data.
52 if isinstance(data, (str, dict, bytes))
is True:
53 if isinstance(data, bytes):
65 data (any): _description_: The type of data is meant to be of type dict or string or a set of byte string.
66 Other formats will return an error.
67 If the content is empty, an error is returned.
70 int: _description_: success if it succeeded to update the raw data, error if it failed to update the raw data.
76 This function is to be used to process the inventory list.
78 data_list (list[str]): _description_: The raw list
81 dict[Commands, list[str]]: _description_: The converted version
86 name, quantity = i.split(
" ")
87 if quantity.isnumeric()
is False:
88 return {Commands.UNKNOWN:
""}
89 processed_result.append([name.strip(), int(quantity)])
90 return {Commands.INVENTORY: processed_result}
92 def _process_look(self, data: list[str]) -> dict[Commands, list[Items]]:
94 This function is in charge of converting the input to a list of items.
97 data (list[str]): _description_: The semi processed list based of the input
100 dict[Commands, list[Items or str]]: _description_: The final converted version of the input
103 processed_result = []
104 temporary_result = []
107 for j
in i.split(
" "):
108 temporary_result.append(j)
110 temporary_result.append(i)
112 for i
in temporary_result:
117 processed_result.append(Items.UNKNOWN)
118 return {Commands.LOOK: processed_result}
122 Function in charge of the converting a list input to the correct response command.
125 dict[Commands, any]: _description_: The converted list
128 if "[" == self.
data[0]:
130 if "]" == self.
data[-1]:
132 if key
not in self.
data:
133 processed_list = {previous_command: [self.
data]}
134 return processed_list
135 data_list = self.
data.split(key)
136 if data_list[-1] ==
"":
138 if len(data_list) == 0:
139 processed_response = {previous_command: []}
140 return processed_response
141 if " " in data_list[0]
and data_list[0][-1].isnumeric():
143 return processed_inventory
145 return processed_look
149 This is a function that will extract the command that was used to get the response.
151 triger_command (str): _description_: The command
154 Commands: _description_: The corresponding command but using the internal command tracking.
156 if isinstance(triger_command, (str, dict))
is not True:
157 return Commands.UNKNOWN
158 if isinstance(triger_command, dict):
159 return list(triger_command.keys())[0]
160 if triger_command ==
"":
161 return Commands.UNKNOWN
162 if "\n" == triger_command[-1]:
163 triger_command = triger_command[:-1]
164 if " " in triger_command:
165 triger_command = triger_command.split()[0]
168 return Commands.UNKNOWN
170 def to_internal(self, triger_command: str =
"") -> dict[Commands, any]:
172 Convert the raw data to the internal language used by the ai
175 dict[Commands, any]: _description_: the format of a node of data comming back
180 if isinstance(self.
data, str)
is False:
181 return {command: arguments}
182 if "\n" == self.
data[-1]:
186 return processed_data
187 if len(self.
data) == 0:
188 return {command: arguments}
189 arguments = self.
data
190 return {command: arguments}
196 "[tile1, tile2, ...]"
197 ex: "[player remedy, ...]"
198 Look, command format (output):
199 {Command.look:[["player", "food", "Linemate", "deraumere", "sibur", "mendiane", "phiras", "thystame"], ... ]}
202 data (str): _description_: The arguments provided alongside the command
205 list[list[str]]: _description_: The Internal process data
208 data = data.split(
', ')
210 data[0] = data[0][1:]
212 data[-1] = data[-1][:-1]
228 "[linemate n, sibur n, ...]"
229 ex: "[linemate 20 sibur 10, ...]"
230 Look, command format (output):
231 {Command.INVENTORY:[{Items.LINEMATE: 20}, {Items.SIBUR, 10}, ...]}
234 data (str): _description_: The arguments provided alongside the command
237 list[dict[Items, int | str]]: _description_: The Internal process data
240 data = data.split(
', ')
243 data[0] = data[0][1:]
245 data[-1] = data[-1][:-1]
248 term, definition = i.split(
" ", 1)
250 if definition.isnumeric():
251 definition = int(definition)
258 result.append({term: definition})
265 result.append({term: definition})
273 {Command.INVENTORY:[{Items.LINEMATE: 20}, {Items.SIBUR, 10}, ...]}
274 Look, command format (output):
275 "[linemate n, sibur n, ...]"
276 ex: "[linemate 20 sibur 10, ...]"
279 data (str): _description_: The arguments provided alongside the command
282 list[dict[Items, int]]: _description_: The Internal process data
289 tmp = term +
" " + definition
291 result +=
", ".join(nodes) +
"]" +
"\n"
296 Convert the internal data to the external language used by the over programs
299 str: _description_: a string containing the command for the other programs
302 if isinstance(self.
data, dict)
is False:
303 if isinstance(self.
data, str):
306 if self.
data[-1] !=
'\n':
310 if len(self.
data) == 0:
312 command = list(self.
data)[0]
313 arguments = self.
data[command]
314 if command == Commands.UNKNOWN:
324 if isinstance(arguments, list)
is True:
325 result_order +=
" ".join(arguments)
327 result_order +=
" " + str(arguments)
int update_raw_data(self, any data)
Commands _get_launch_command(self, str triger_command)
None __init__(self, data, int error=84, int success=0)
int set_raw_data(self, any data)
dict[Commands, any] to_internal(self, str triger_command="")
dict[Commands, any] _special_list_treatment(self, Commands previous_command)
list[dict[Items, int]] _input_parse_inventory(self, str data)
dict[Commands, list[Items]] _process_look(self, list[str] data)
list[list[str]] _input_parse_look(self, str data)
str _output_parse_inventory(self, list[dict[Items, int]] data)
dict[Commands, list[list[str, int]]] _process_inventory(self, list[str] data_list)