add detailed help and polls commands

This commit is contained in:
biglyderv 2025-03-27 22:12:19 -04:00
parent 15f6ca9715
commit c278da1705
Signed by: biglyderv
GPG key ID: 0E2EB0B4CD7397B5
3 changed files with 63 additions and 18 deletions

View file

@ -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;
}

View file

@ -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);
char *name);
int help_command(char *command);
int polls_command(char *command, hashmap *polls);

View file

@ -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;