allow suggesting polls of elems you don't have, and allow suggestion correction

This commit is contained in:
biglyderv 2025-04-03 11:49:48 -04:00
parent 735915db89
commit 988ac26e44
Signed by: biglyderv
GPG key ID: 0E2EB0B4CD7397B5
3 changed files with 39 additions and 27 deletions

View file

@ -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) { int success_handler(const void *key, size_t size, uintptr_t val, void *usr) {
struct succ *verified = (struct succ *)usr; struct succ *verified = (struct succ *)usr;
if ((char *)val != verified->sugg) { char *val2 = (char*) val;
//((char *)val)[strlen(((char *)val)) - 1] = '\0';
if ((char *)val != verified->sugg && val2[strlen(val2) - 1] == '\n') {
val2[strlen(val2) - 1] = '\0';
} }
if (strcmp(verified->sugg, (char *)val) == 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, int suggest_command(char *command, char *command_re, hashmap *polls, char *name,
char *name) { int was_combination) {
char *page = handle_pages(command, "/suggest "); 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)); char *key = calloc(MAX_BUF_LENGTH, sizeof(char));
sprintf(key, "%s_%i", name, (int)rand()); 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) { if (hashmap_iterate(polls, polls_handler, (void *)&verified) == -1) {
printf("You already suggested this!\n"); printf("You already suggested this!\n");
return 1; 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); hashmap_set(polls, key, strlen(key), (uintptr_t)val, 1);
int point_thing = 0; point_thing++;
struct succ succer = {.sugg = val, .points = &point_thing};
hashmap_iterate(polls, success_handler, (void *)&succer);
if (point_thing == UPVOTE_IN) { if (point_thing == UPVOTE_IN) {
printf("Poll was added into the game!\n"); printf("Poll was added into the game!\n");
FILE *fptr; 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 path_command(char *command, hashmap *elements_rev, hashmap *already_done,
int top, hashmap* inv) { int top, hashmap *inv) {
char *page2; char *page2;
if (top) { if (top) {
page2 = handle_pages(command, "/path "); 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); char *page = malloc(strlen(page2) + 1);
strcpy(page, page2); strcpy(page, page2);
if (page[strlen(page) - 1] == '\n') { if (page[strlen(page) - 1] == '\n') {
page[strlen(page) - 1] = '\0'; page[strlen(page) - 1] = '\0';
} }
if (strlen(page) == 0) { if (strlen(page) == 0) {
@ -263,7 +270,6 @@ int path_command(char *command, hashmap *elements_rev, hashmap *already_done,
already_done = hashmap_create(); already_done = hashmap_create();
} }
uintptr_t result; uintptr_t result;
hashmap_get(inv, page, strlen(page), &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); hashmap_get(already_done, page, strlen(page), &result);
if (result != 0) { if (result != 0) {
return 1; return 1;
} }
@ -287,7 +293,7 @@ int path_command(char *command, hashmap *elements_rev, hashmap *already_done,
// todo: refactor; // todo: refactor;
char *tmp = (char *)result, *tmp2 = 0; char *tmp = (char *)result, *tmp2 = 0;
if (tmp[strlen(tmp)-1] == '\n') { if (tmp[strlen(tmp) - 1] == '\n') {
tmp[strlen(tmp) - 1] = '\0'; tmp[strlen(tmp) - 1] = '\0';
} }
@ -298,7 +304,7 @@ int path_command(char *command, hashmap *elements_rev, hashmap *already_done,
if (tmp2[0] == ';') { if (tmp2[0] == ';') {
tmp2++; tmp2++;
} }
tmp = strstr(tmp2, ";"); tmp = strstr(tmp2, ";");
if (tmp == 0) { if (tmp == 0) {
tmp = &tmp2[strlen(tmp2)]; tmp = &tmp2[strlen(tmp2)];
} }
@ -319,8 +325,8 @@ int path_command(char *command, hashmap *elements_rev, hashmap *already_done,
free(tmp23); free(tmp23);
} }
printf("<- %s\n-> %s\n", (char*) result, (char *)page); printf("<- %s\n-> %s\n", (char *)result, (char *)page);
return 1; return 1;
} }

View file

@ -2,7 +2,7 @@
int get_command(char *command, char *command_re, char **sort_tmp); int get_command(char *command, char *command_re, char **sort_tmp);
int slash_command(char *command, hashmap *inv); int slash_command(char *command, hashmap *inv);
int suggest_command(char *command, char *command_re, hashmap *polls, int suggest_command(char *command, char *command_re, hashmap *polls,
char *name); char *name, int was_combination);
int help_command(char *command); int help_command(char *command);
int polls_command(char *command, hashmap *polls, hashmap *cmd); int polls_command(char *command, hashmap *polls, hashmap *cmd);
int path_command(char *command, hashmap *elements_rev, hashmap *already_done, int path_command(char *command, hashmap *elements_rev, hashmap *already_done,

View file

@ -50,7 +50,7 @@ int main(int argc, char *argv[]) {
char *command_re = calloc(MAX_BUF_LENGTH, sizeof(char)); char *command_re = calloc(MAX_BUF_LENGTH, sizeof(char));
char **sort_tmp = calloc(MAX_COMBO_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); init_tables(elements, inv, polls, elements_rev, 1);
@ -80,11 +80,11 @@ int main(int argc, char *argv[]) {
command[i] = tolower(command[i]); command[i] = tolower(command[i]);
} }
if (wasCombination && suggest_command(command, command_re, polls, name)) { if (was_combination &&
wasCombination = 0; suggest_command(command, command_re, polls, name, was_combination)) {
continue; continue;
} }
wasCombination = 0; was_combination = 0;
if (help_command(command)) if (help_command(command))
continue; continue;
if (polls_command(command, polls, elements)) if (polls_command(command, polls, elements))
@ -111,9 +111,6 @@ int main(int argc, char *argv[]) {
} }
} }
if (failed)
continue;
uintptr_t result; uintptr_t result;
hashmap_get(elements, command_re, strlen(command_re), &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); 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) { if (result == 0) {
wasCombination = 1; was_combination = 1;
printf("You didn't make anything; use /suggest to suggest an element.\n"); printf("You didn't make anything; use /suggest to suggest an element.\n");
continue; continue;
} }
if (failed)
continue;
char *res_str = (char *)result; char *res_str = (char *)result;
uintptr_t result2; uintptr_t result2;