add suggestions
This commit is contained in:
parent
e6165f3af8
commit
34d2d97c82
6 changed files with 177 additions and 15 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -1,3 +1,5 @@
|
|||
/bin/elem
|
||||
/bin/elem.exe
|
||||
/inv.txt
|
||||
/inv.txt
|
||||
/inv_users.txt
|
||||
/polls.txt
|
134
src/command.c
134
src/command.c
|
@ -7,11 +7,26 @@
|
|||
|
||||
#define MAX_BUF_LENGTH 1024
|
||||
#define MAX_COMBO_LENGTH 1024
|
||||
#define poll_file "polls.txt"
|
||||
#define polls_lock_file "polls_lock.txt"
|
||||
#define combo_file "combos.txt"
|
||||
#define UPVOTE_IN 2
|
||||
|
||||
struct pager {
|
||||
int page;
|
||||
int i;
|
||||
};
|
||||
|
||||
struct verifier {
|
||||
char *name;
|
||||
char *sugg;
|
||||
};
|
||||
|
||||
struct succ {
|
||||
char *sugg;
|
||||
int *points;
|
||||
};
|
||||
|
||||
long stoi(const char *s) {
|
||||
long i;
|
||||
i = 0;
|
||||
|
@ -64,6 +79,123 @@ int inv_handler(const void *key, unsigned long long size,
|
|||
return 1;
|
||||
}
|
||||
|
||||
#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || \
|
||||
(defined(__APPLE__) && defined(__MACH__)))
|
||||
|
||||
int polls_handler(const void *key, unsigned long size, unsigned long val,
|
||||
void *usr)
|
||||
#else
|
||||
int polls_handler(const void *key, unsigned long long size,
|
||||
unsigned long long val, void *usr)
|
||||
#endif
|
||||
{
|
||||
struct verifier *verified = (struct verifier *)usr;
|
||||
|
||||
if (((char*) val)[strlen(((char *)val)) - 1] == '\n') {
|
||||
((char *)val)[strlen(((char *)val)) - 1] = '\0';
|
||||
}
|
||||
|
||||
if (strncmp(verified->name, key, strlen(verified->name)) == 0 &&
|
||||
strcmp(verified->sugg, (char *)val) == 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || \
|
||||
(defined(__APPLE__) && defined(__MACH__)))
|
||||
|
||||
int success_handler(const void *key, unsigned long size, unsigned long val,
|
||||
void *usr)
|
||||
#else
|
||||
int success_handler(const void *key, unsigned long long size,
|
||||
unsigned long long val, void *usr)
|
||||
#endif
|
||||
{
|
||||
struct succ *verified = (struct succ *)usr;
|
||||
|
||||
if ((char *)val != verified->sugg) {
|
||||
//((char *)val)[strlen(((char *)val)) - 1] = '\0';
|
||||
}
|
||||
|
||||
if (strcmp(verified->sugg, (char *)val) == 0) {
|
||||
verified->points[0]++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int suggest_command(char *command, char *command_re, hashmap *polls,
|
||||
char *name) {
|
||||
if (command[0] == '/') {
|
||||
command++;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *invs = "suggest ";
|
||||
|
||||
char *page;
|
||||
|
||||
if (strncmp(command, invs, strlen(invs)) == 0) {
|
||||
page = &command[strlen(invs)];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
page[strlen(page) - 1] = '\0';
|
||||
|
||||
char *val = calloc(MAX_BUF_LENGTH, sizeof(char));
|
||||
sprintf(val, "%s;%s", page, command_re);
|
||||
|
||||
char *key = calloc(MAX_BUF_LENGTH, sizeof(char));
|
||||
sprintf(key, "%s_%i", name, (int)rand());
|
||||
|
||||
struct verifier verified = {.name = name, .sugg = val};
|
||||
|
||||
if (hashmap_iterate(polls, polls_handler, (void *)&verified) == -1) {
|
||||
printf("You already suggested this!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (point_thing == UPVOTE_IN) {
|
||||
|
||||
printf("Poll was added into the game!\n");
|
||||
FILE *fptr;
|
||||
|
||||
fptr = fopen("../elem_data/" combo_file, "a");
|
||||
if (fptr == NULL)
|
||||
return 1;
|
||||
fwrite(val, sizeof(char), strlen(val), fptr);
|
||||
fwrite("\n", sizeof(char), 1, fptr);
|
||||
fclose(fptr);
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Suggested %s = %s.\n", command_re, page);
|
||||
|
||||
// todo: clean old polls
|
||||
|
||||
FILE *fptr;
|
||||
|
||||
fptr = fopen("../elem_data/" poll_file, "a");
|
||||
if (fptr == NULL)
|
||||
return 1;
|
||||
fwrite(key, sizeof(char), strlen(key), fptr);
|
||||
fwrite(";", sizeof(char), 1, fptr);
|
||||
fwrite(val, sizeof(char), strlen(val), fptr);
|
||||
fwrite("\n", sizeof(char), 1, fptr);
|
||||
fclose(fptr);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int slash_command(char *command, hashmap *inv) {
|
||||
|
||||
if (command[0] == '/') {
|
||||
|
@ -75,7 +207,7 @@ int slash_command(char *command, hashmap *inv) {
|
|||
char *invs = "inv ";
|
||||
|
||||
int page;
|
||||
|
||||
|
||||
if (strncmp(command, invs, strlen(invs)) == 0) {
|
||||
page = stoi(&command[strlen(invs)]);
|
||||
} else if (strncmp(command, invs, strlen(invs) - 1) == 0) {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#include "map.h"
|
||||
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,
|
||||
char *name);
|
|
@ -27,7 +27,7 @@ int load_elements(hashmap *m, char *table, int use_inv) {
|
|||
|
||||
did_something = 1;
|
||||
|
||||
if (use_inv) {
|
||||
if (use_inv == 1) {
|
||||
hashmap_set(m, str, strlen(str) - 1, (uintptr_t)1, 0);
|
||||
continue;
|
||||
}
|
||||
|
@ -36,6 +36,11 @@ int load_elements(hashmap *m, char *table, int use_inv) {
|
|||
combo[0] = '\0';
|
||||
combo++;
|
||||
|
||||
if (use_inv == 2) {
|
||||
hashmap_set(m, str, strlen(str) - 1, (uintptr_t)combo, 0);
|
||||
continue;
|
||||
}
|
||||
|
||||
hashmap_set(m, combo, strlen(combo) - 1, (uintptr_t)str, 1);
|
||||
}
|
||||
|
||||
|
|
42
src/main.c
42
src/main.c
|
@ -8,44 +8,63 @@
|
|||
#define inv_file "inv.txt"
|
||||
#define inv_base_file "inv_base.txt"
|
||||
#define combo_file "combos.txt"
|
||||
#define poll_file "polls.txt"
|
||||
#define polls_lock_file "polls_lock.txt"
|
||||
|
||||
#define MAX_BUF_LENGTH 1024
|
||||
#define MAX_COMBO_LENGTH 1024
|
||||
|
||||
void init_tables(hashmap *elements, hashmap *inv, int do_inv) {
|
||||
|
||||
load_elements(elements, "../elem_data/" combo_file, 0) ||
|
||||
load_elements(elements, combo_file, 0) ||
|
||||
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) ||
|
||||
load_elements(elements, "bin/" combo_file, 0);
|
||||
|
||||
load_elements(polls, "../elem_data/" poll_file, 2);
|
||||
|
||||
if (!do_inv)
|
||||
return;
|
||||
|
||||
load_elements(elements, "../elem_data/" inv_base_file, 1) ||
|
||||
load_elements(inv, inv_base_file, 1) ||
|
||||
load_elements(inv, "bin/" inv_base_file, 1);
|
||||
|
||||
load_elements(inv, inv_file, 1);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
char *name;
|
||||
if (argc < 2) {
|
||||
name = "guest";
|
||||
} else {
|
||||
name = argv[1];
|
||||
}
|
||||
|
||||
hashmap *elements = hashmap_create();
|
||||
hashmap *inv = hashmap_create();
|
||||
hashmap *polls = hashmap_create();
|
||||
|
||||
char *command = calloc(MAX_BUF_LENGTH, sizeof(char));
|
||||
char *command_re = calloc(MAX_BUF_LENGTH, sizeof(char));
|
||||
char **sort_tmp = calloc(MAX_COMBO_LENGTH, sizeof(char **));
|
||||
|
||||
init_tables(elements, inv, 1);
|
||||
int wasCombination = 0;
|
||||
|
||||
load_elements(inv, inv_file, 1);
|
||||
init_tables(elements, inv, polls, 1);
|
||||
|
||||
printf("Welcome to Elemental on Command Line!\n");
|
||||
printf("%s, welcome to Elemental on Command Line!\n", name);
|
||||
while (1) {
|
||||
// todo: separate into functions
|
||||
|
||||
printf("\n");
|
||||
fgets(command, MAX_BUF_LENGTH - 1, stdin);
|
||||
int slashRan = slash_command(command, inv);
|
||||
if (slashRan)
|
||||
|
||||
if (wasCombination && suggest_command(command, command_re, polls, name)) {
|
||||
wasCombination = 0;
|
||||
continue;
|
||||
}
|
||||
wasCombination = 0;
|
||||
|
||||
if (slash_command(command, inv))
|
||||
continue;
|
||||
|
||||
int combos = get_command(command, command_re, sort_tmp);
|
||||
|
@ -68,13 +87,14 @@ int main(int argc, char *argv[]) {
|
|||
hashmap_get(elements, command_re, strlen(command_re), &result);
|
||||
|
||||
if (result == 0) {
|
||||
init_tables(elements, inv, 0);
|
||||
init_tables(elements, inv, polls, 0);
|
||||
|
||||
hashmap_get(elements, command_re, strlen(command_re), &result);
|
||||
}
|
||||
|
||||
if (result == 0) {
|
||||
printf("You didn't make anything.\n");
|
||||
wasCombination = 1;
|
||||
printf("You didn't make anything; use /suggest to suggest an element.\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
// map.h
|
||||
//
|
||||
// Created by Mashpoe on 1/15/21.
|
||||
// Modifications by BiglyDerv
|
||||
//
|
||||
|
||||
#include "map.h"
|
||||
|
|
Loading…
Reference in a new issue