From e0bf82fe2af66ef55872569ae15733f70b4913d4 Mon Sep 17 00:00:00 2001 From: Mosder Date: Mon, 20 Jul 2026 15:39:23 +0200 Subject: feat: added parsing of normal items --- analyzer/parser/fight_results.c | 76 +++++++++++++++++++++++++++++++++++------ 1 file changed, 66 insertions(+), 10 deletions(-) (limited to 'analyzer/parser/fight_results.c') diff --git a/analyzer/parser/fight_results.c b/analyzer/parser/fight_results.c index d7b185a..5cda649 100644 --- a/analyzer/parser/fight_results.c +++ b/analyzer/parser/fight_results.c @@ -11,10 +11,11 @@ void cleanup_fight_results(void *data) { FightResults *fight_results = data; for (size_t i = 0; i < fight_results->num_friendly; i++) { - free(fight_results->friendly_results[i].name); - free(fight_results->friendly_results[i].gear); - free(fight_results->friendly_results[i].items); - free(fight_results->friendly_results[i].drifs); + FriendlyResults friendly = fight_results->friendly_results[i]; + free(friendly.name); + for (size_t j = 0; j < friendly.num_items; j++) + free(friendly.items[j]); + free(friendly.items); } for (size_t i = 0; i < fight_results->num_enemy; i++) { free(fight_results->enemy_results[i].name); @@ -86,6 +87,59 @@ char *get_item_name(int item_id) { return item_id < id_to_item_name_len ? id_to_item_name[item_id] : "null"; } +char **expand_items(char **items, size_t *items_capacity, size_t to_fit) { + while (*items_capacity < to_fit) + *items_capacity <<= 1; + return realloc(items, *items_capacity * sizeof(char *)); +} + +void add_gear(FriendlyResults *results, size_t *items_capacity, char *gear_str) { + if (strlen(gear_str) == 0) + return; + + // TODO: parse + if (results->num_items >= *items_capacity) + results->items = expand_items(results->items, items_capacity, results->num_items + 1); + + results->items[results->num_items++] = strdup(gear_str); +} + +void add_drifs(FriendlyResults *results, size_t *items_capacity, char *drifs_str) { + if (strlen(drifs_str) == 0) + return; + + // TODO: parse + if (results->num_items >= *items_capacity) + results->items = expand_items(results->items, items_capacity, results->num_items + 1); + + results->items[results->num_items++] = strdup(drifs_str); +} + +void add_items(FriendlyResults *results, size_t *items_capacity, char *items_str) { + if (strlen(items_str) == 0) + return; + + char *items_separator; + do { + char buffer[SINGLE_ITEM_BUFFER_LEN]; + items_separator = strstr(items_str, SEPARATOR_ITEMS); + + int item_id, item_count; + sscanf(items_str, "%d,%d", &item_id, &item_count); + sprintf(buffer, "%dx %s", item_count, get_item_name(item_id)); + + // if there are still items - modify items_str pointer + if (items_separator) + items_str = items_separator + strlen(SEPARATOR_ITEMS); + + // expand if needed + if (results->num_items >= *items_capacity) + results->items = expand_items(results->items, items_capacity, results->num_items + 1); + + results->items[results->num_items++] = strdup(buffer); + } while (items_separator); +} + char *get_saturation_type(int saturation_id) { return saturation_id < id_to_saturation_type_len ? id_to_saturation_type[saturation_id] : "null"; } @@ -97,16 +151,18 @@ FriendlyResults parse_to_friendly(EntityResults entity_results) { results.name = strdup(entity_results.name); results.exp = entity_results.exp; results.gold = entity_results.gold; - // TODO: parse that to more readable format - results.gear = strdup(entity_results.gear); - // TODO: parse that to more readable format, determine types from ids - results.items = strdup(entity_results.items); results.level = entity_results.level; results.psycho = entity_results.psycho; - // TODO: parse that to more readable format - results.drifs = strdup(entity_results.drifs); results.splinters = entity_results.splinters; + // create items array and add to it gear, drifs and items + results.num_items = 0; + results.items = malloc(INIT_ITEMS_CAPACITY * sizeof(char *)); + size_t items_cap = INIT_ITEMS_CAPACITY; + add_gear(&results, &items_cap, entity_results.gear); + add_drifs(&results, &items_cap, entity_results.drifs); + add_items(&results, &items_cap, entity_results.items); + results.saturation = 0; if (strlen(entity_results.saturation) > 0) { int saturation_id; -- cgit v1.2.3