diff --git a/src/command.c b/src/command.c index 91e2290..e03572f 100644 --- a/src/command.c +++ b/src/command.c @@ -117,8 +117,10 @@ int polls_handler(const void *key, size_t size, uintptr_t val, void *usr) { int success_handler(const void *key, size_t size, uintptr_t val, void *usr) { struct succ *verified = (struct succ *)usr; - if ((char *)val != verified->sugg) { - //((char *)val)[strlen(((char *)val)) - 1] = '\0'; + char *val2 = (char*) val; + + if ((char *)val != verified->sugg && val2[strlen(val2) - 1] == '\n') { + val2[strlen(val2) - 1] = '\0'; } if (strcmp(verified->sugg, (char *)val) == 0) { @@ -146,8 +148,8 @@ int handle_pages_int(char *command, char *invs) { } } -int suggest_command(char *command, char *command_re, hashmap *polls, - char *name) { +int suggest_command(char *command, char *command_re, hashmap *polls, char *name, + int was_combination) { char *page = handle_pages(command, "/suggest "); @@ -167,8 +169,17 @@ int suggest_command(char *command, char *command_re, hashmap *polls, char *key = calloc(MAX_BUF_LENGTH, sizeof(char)); sprintf(key, "%s_%i", name, (int)rand()); - struct verifier verified = {.name = name, .sugg = val}; + int point_thing = 0; + struct succ succer = {.sugg = val, .points = &point_thing}; + hashmap_iterate(polls, success_handler, (void *)&succer); + + if (was_combination == 2 && point_thing == 0) { + printf("You cannot create unique polls with elements you do not have.\n"); + return 1; + } + + struct verifier verified = {.name = name, .sugg = val}; if (hashmap_iterate(polls, polls_handler, (void *)&verified) == -1) { printf("You already suggested this!\n"); return 1; @@ -176,13 +187,9 @@ int suggest_command(char *command, char *command_re, hashmap *polls, hashmap_set(polls, key, strlen(key), (uintptr_t)val, 1); - int point_thing = 0; - - struct succ succer = {.sugg = val, .points = &point_thing}; - hashmap_iterate(polls, success_handler, (void *)&succer); + point_thing++; if (point_thing == UPVOTE_IN) { - printf("Poll was added into the game!\n"); FILE *fptr; @@ -238,7 +245,7 @@ int polls_command(char *command, hashmap *polls, hashmap *cmd) { } int path_command(char *command, hashmap *elements_rev, hashmap *already_done, - int top, hashmap* inv) { + int top, hashmap *inv) { char *page2; if (top) { page2 = handle_pages(command, "/path "); @@ -250,7 +257,7 @@ int path_command(char *command, hashmap *elements_rev, hashmap *already_done, char *page = malloc(strlen(page2) + 1); strcpy(page, page2); - if (page[strlen(page) - 1] == '\n') { + if (page[strlen(page) - 1] == '\n') { page[strlen(page) - 1] = '\0'; } if (strlen(page) == 0) { @@ -263,7 +270,6 @@ int path_command(char *command, hashmap *elements_rev, hashmap *already_done, already_done = hashmap_create(); } - uintptr_t result; hashmap_get(inv, page, strlen(page), &result); @@ -273,7 +279,7 @@ int path_command(char *command, hashmap *elements_rev, hashmap *already_done, } hashmap_get(already_done, page, strlen(page), &result); - + if (result != 0) { return 1; } @@ -287,7 +293,7 @@ int path_command(char *command, hashmap *elements_rev, hashmap *already_done, // todo: refactor; char *tmp = (char *)result, *tmp2 = 0; - if (tmp[strlen(tmp)-1] == '\n') { + if (tmp[strlen(tmp) - 1] == '\n') { tmp[strlen(tmp) - 1] = '\0'; } @@ -298,7 +304,7 @@ int path_command(char *command, hashmap *elements_rev, hashmap *already_done, if (tmp2[0] == ';') { tmp2++; } - tmp = strstr(tmp2, ";"); + tmp = strstr(tmp2, ";"); if (tmp == 0) { tmp = &tmp2[strlen(tmp2)]; } @@ -319,8 +325,8 @@ int path_command(char *command, hashmap *elements_rev, hashmap *already_done, free(tmp23); } - printf("<- %s\n-> %s\n", (char*) result, (char *)page); - + printf("<- %s\n-> %s\n", (char *)result, (char *)page); + return 1; } diff --git a/src/command.h b/src/command.h index f3694b6..bba66a4 100644 --- a/src/command.h +++ b/src/command.h @@ -2,7 +2,7 @@ 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 was_combination); int help_command(char *command); int polls_command(char *command, hashmap *polls, hashmap *cmd); int path_command(char *command, hashmap *elements_rev, hashmap *already_done, diff --git a/src/main.c b/src/main.c index 584ac29..69f9365 100644 --- a/src/main.c +++ b/src/main.c @@ -50,7 +50,7 @@ int main(int argc, char *argv[]) { char *command_re = calloc(MAX_BUF_LENGTH, sizeof(char)); char **sort_tmp = calloc(MAX_COMBO_LENGTH, sizeof(char **)); - int wasCombination = 0; + int was_combination = 0; init_tables(elements, inv, polls, elements_rev, 1); @@ -80,11 +80,11 @@ int main(int argc, char *argv[]) { command[i] = tolower(command[i]); } - if (wasCombination && suggest_command(command, command_re, polls, name)) { - wasCombination = 0; + if (was_combination && + suggest_command(command, command_re, polls, name, was_combination)) { continue; } - wasCombination = 0; + was_combination = 0; if (help_command(command)) continue; if (polls_command(command, polls, elements)) @@ -111,9 +111,6 @@ int main(int argc, char *argv[]) { } } - if (failed) - continue; - uintptr_t result; hashmap_get(elements, command_re, strlen(command_re), &result); @@ -123,12 +120,21 @@ int main(int argc, char *argv[]) { hashmap_get(elements, command_re, strlen(command_re), &result); } + if (result == 0 && failed) { + was_combination = 2; + printf("Use /suggest to upvote a pre-existing combination.\n"); + continue; + } + if (result == 0) { - wasCombination = 1; + was_combination = 1; printf("You didn't make anything; use /suggest to suggest an element.\n"); continue; } + if (failed) + continue; + char *res_str = (char *)result; uintptr_t result2;