DungeonCrawl
Loading...
Searching...
No Matches
ability_database.c
Go to the documentation of this file.
1
5#include "ability_database.h"
6
8
9#include <string.h>
10
11#define SQL_SELECT_ALL_ABILITIES "SELECT AB_NUMBER, AB_NAME, AB_ROLLAMOUNT, AB_ACCURACY, AB_RESSOURCECOST, AB_DICESIZE, DA_NUMBER FROM ability, damage JOIN ability_has_damage ON AD_AB_ID = AB_ID AND AD_DA_ID = DA_ID"
12
14 // Check if the database connection is open
15 if (!db_is_open(db_connection)) {
16 log_msg(ERROR, "Ability", "Database connection is not open");
17 return NULL;
18 }
19
20 // Prepare the SQL statement
21 sqlite3_stmt* stmt;
22 int rc = sqlite3_prepare_v2(db_connection->db, SQL_SELECT_ALL_ABILITIES, -1, &stmt, NULL);
23 if (rc != SQLITE_OK) {
24 log_msg(ERROR, "Ability", "Failed to prepare statement: %s", sqlite3_errmsg(db_connection->db));
25 return NULL;
26 }
27
28 // Allocate memory for the ability table
29 ability_init_t* ability_init_table = malloc(sizeof(ability_init_t) * MAX_ABILITIES);
30 if (ability_init_table == NULL) {
31 log_msg(ERROR, "Ability", "Failed to allocate memory for ability table");
32 sqlite3_finalize(stmt);
33 return NULL;
34 }
35
36 memset(ability_init_table, 0, sizeof(ability_init_t) * MAX_ABILITIES);
37
38 // Execute the statement and fetch the results
39 int index = 0;
40 while ((rc = sqlite3_step(stmt)) == SQLITE_ROW && index < MAX_ABILITIES) {
41 ability_init_table[index].ability_number = sqlite3_column_int(stmt, 0);
42 ability_init_table[index].name = strdup((const char*) sqlite3_column_text(stmt, 1));
43 ability_init_table[index].roll_amount = sqlite3_column_int(stmt, 2);
44 ability_init_table[index].accuracy = sqlite3_column_int(stmt, 3);
45 ability_init_table[index].resource_cost = sqlite3_column_int(stmt, 4);
46 ability_init_table[index].dice_size = (dice_size_t) sqlite3_column_int(stmt, 5);
47 ability_init_table[index].damage_type = (damage_type_t) sqlite3_column_int(stmt, 6);
48 index++;
49 }
50
51 if (rc != SQLITE_DONE) {
52 log_msg(ERROR, "Ability", "Failed to execute statement: %s", sqlite3_errmsg(db_connection->db));
53 free(ability_init_table);
54 sqlite3_finalize(stmt);
55 return NULL;
56 }
57
58 // Finalize the statement
59 sqlite3_finalize(stmt);
60
61 return ability_init_table;
62}
63
64void free_ability_table_from_db(ability_init_t* ability_init_table) {
65 if (ability_init_table == NULL) {
66 return;
67 }
68
69 for (int i = 0; i < MAX_ABILITIES; i++) {
70 free(ability_init_table[i].name);
71 ability_init_table[i].name = NULL;
72 }
73 free(ability_init_table);
74}
ability_init_t * get_ability_table_from_db(const db_connection_t *db_connection)
Get the ability table from the database.
void free_ability_table_from_db(ability_init_t *ability_init_table)
Clean up the ability table Call this function to free the memory allocated for the ability table.
Declares functions to load and free ability definitions from the game database.
int db_is_open(const db_connection_t *db_connection)
This function is to check if the database is open.
Definition database.c:27
void log_msg(const log_level_t level, const char *module, const char *format,...)
Logs a formatted message with a specified log level and module.
Definition logger.c:246
Header file for logging functionality of the game.
To get the ability table from the database, we need to define a struct This struct is for the initial...
This struct is used for the database connection in SQLite.
Definition database.h:22