From c278da17050705d44a15a52da885ca93379ce513 Mon Sep 17 00:00:00 2001 From: biglyderv Date: Thu, 27 Mar 2025 22:12:19 -0400 Subject: [PATCH] add detailed help and polls commands --- src/command.c | 69 ++++++++++++++++++++++++++++++++++++++++----------- src/command.h | 4 ++- src/main.c | 8 ++++-- 3 files changed, 63 insertions(+), 18 deletions(-) diff --git a/src/command.c b/src/command.c index b3b52f5..8c3a5d1 100644 --- a/src/command.c +++ b/src/command.c @@ -6,9 +6,12 @@ #include "main.h" +// huge cleanup operation soon + struct pager { int page; int i; + int is_poll; }; struct verifier { @@ -55,11 +58,21 @@ int inv_handler(const void *key, size_t size, uintptr_t val, void *usr) { if (val == 0) return 0; char *key2 = (char *)key; + char* val2 = (char*) val; + + if (i->is_poll) { + if (val2[strlen(val2) - 1] == '\n') { + printf("- %s suggested %s", key2, val2); + } else { + printf("- %s suggested %s\n", key2, val2); + } + return 1; + } if (key2[strlen(key2) - 1] == '\n') { - printf("- %s", (char *)key); + printf("- %s", key2); } else { - printf("- %s\n", (char *)key); + printf("- %s\n", key2); } return 1; } @@ -94,13 +107,7 @@ int success_handler(const void *key, size_t size, uintptr_t val, void *usr) { int suggest_command(char *command, char *command_re, hashmap *polls, char *name) { - if (command[0] == '/') { - command++; - } else { - return 0; - } - - char *invs = "suggest "; + char *invs = "/suggest "; char *page; @@ -111,7 +118,8 @@ int suggest_command(char *command, char *command_re, hashmap *polls, } page[strlen(page) - 1] = '\0'; - if (strstr(page,"\n") || strstr(page,";") || strstr(page,",") || strstr(page,"+")) { + if (strstr(page, "\n") || strstr(page, ";") || strstr(page, ",") || + strstr(page, "+")) { printf("This element contains illegal characters.\n"); return 1; } @@ -168,15 +176,46 @@ int suggest_command(char *command, char *command_re, hashmap *polls, return 1; } -int slash_command(char *command, hashmap *inv) { +int help_command(char *command) { + char *invs = "/help"; - if (command[0] == '/') { - command++; + int page; + + if (strncmp(command, invs, strlen(invs)) == 0) { + + printf("Available " + "commands:\n- elem1;elem2...\n- elem1+elem2...\n- elem1,elem2...\n- " + "/help\n- /inv [page]\n- /suggest [combo]\n- /polls [page]\n"); } else { return 0; } - char *invs = "inv "; + return 1; +} + +int polls_command(char *command, hashmap *polls) { + + char *invs = "/polls "; + + int page; + + if (strncmp(command, invs, strlen(invs)) == 0) { + page = stoi(&command[strlen(invs)]); + } else if (strncmp(command, invs, strlen(invs) - 1) == 0) { + page = 1; + } else { + return 0; + } + + printf("Current polls (page %i):\n", page); + struct pager i = {.page = page - 1, .i = -1, .is_poll = 1}; + hashmap_iterate(polls, inv_handler, &i); + return 1; +} + +int slash_command(char *command, hashmap *inv) { + + char *invs = "/inv "; int page; @@ -189,7 +228,7 @@ int slash_command(char *command, hashmap *inv) { } printf("Your inventory (page %i):\n", page); - struct pager i = {.page = page - 1, .i = -1}; + struct pager i = {.page = page - 1, .i = -1, .is_poll = 1}; hashmap_iterate(inv, inv_handler, &i); return 1; } diff --git a/src/command.h b/src/command.h index a91b2ea..24d7e36 100644 --- a/src/command.h +++ b/src/command.h @@ -2,4 +2,6 @@ int get_command(char *command, char *command_re, char **sort_tmp); int slash_command(char *command, hashmap *inv); int suggest_command(char *command, char *command_re, hashmap *polls, - char *name); \ No newline at end of file + char *name); +int help_command(char *command); +int polls_command(char *command, hashmap *polls); \ No newline at end of file diff --git a/src/main.c b/src/main.c index 849deca..b4c6f9c 100644 --- a/src/main.c +++ b/src/main.c @@ -8,6 +8,8 @@ #include "main.h" +// todo: polls, help commands, spacing in combos + void init_tables(hashmap *elements, hashmap *inv, hashmap *polls, int do_inv) { load_elements(elements, "../elem_data/" combo_file, 0); load_elements(elements, combo_file, 0) || @@ -25,7 +27,6 @@ void init_tables(hashmap *elements, hashmap *inv, hashmap *polls, int do_inv) { load_elements(inv, inv_file, 1); } -// todo: sanitize, valgrind, look through todos int main(int argc, char *argv[]) { char *name; if (argc < 2) { @@ -64,7 +65,10 @@ int main(int argc, char *argv[]) { continue; } wasCombination = 0; - + if (help_command(command)) + continue; + if (polls_command(command, polls)) + continue; if (slash_command(command, inv)) continue;