From 5bb4216d7e327e93b28313f395ec1840038d5d4a Mon Sep 17 00:00:00 2001 From: anon Date: Thu, 3 Aug 2023 14:52:24 +0200 Subject: [PATCH] get random assignment support --- include/stmt.h | 8 ++++++++ src/api.c | 15 +++++++++++++++ src/irc.c | 3 +++ 3 files changed, 26 insertions(+) diff --git a/include/stmt.h b/include/stmt.h index 09ab864..3d5836a 100644 --- a/include/stmt.h +++ b/include/stmt.h @@ -34,3 +34,11 @@ static const char get_nth_id_stmt_template[] = "LIMIT 1 " "OFFSET ?;" ; + +static sqlite3_stmt* new_assignment_stmt; +static const char new_assignment_stmt_template[] = + "INSERT INTO assignment " + "(who, project)" + " VALUES " + "(?, ?);" +; diff --git a/src/api.c b/src/api.c index 4b1907d..b432b08 100644 --- a/src/api.c +++ b/src/api.c @@ -1,4 +1,5 @@ #include +#include #include @@ -164,3 +165,17 @@ get_nth_id(const int i){ r = sqlite3_column_int(get_nth_id_stmt, 0); return r; } + +DECL void +new_assignment(const char * const who, const int project){ + DBERR(sqlite3_bind_text(new_assignment_stmt, 1, who, -1, SQLITE_STATIC)); + DBERR(sqlite3_bind_int(new_assignment_stmt, 2, project)); + DBERR(sqlite3_step(new_assignment_stmt)); +} + +DECL void +random_assign(const char * const who){ + int i = rand() % get_project_count(); + i = get_nth_id(i); + new_assignment(who, i); +} diff --git a/src/irc.c b/src/irc.c index 1817712..716d009 100644 --- a/src/irc.c +++ b/src/irc.c @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include "irc.h" #include "parse.h" @@ -120,6 +122,7 @@ event_channel(irc_session_t * session, int init(void) { + srand(time(NULL)); if(api_init()) { ERR(DB_ERROR, "Error initializing database."); } memset(&callbacks, 0, sizeof(callbacks));