DungeonCrawl
Loading...
Searching...
No Matches
ability_database.h File Reference

Declares functions to load and free ability definitions from the game database. More...

#include "../../combat/ability.h"
#include "../database.h"

Go to the source code of this file.

Data Structures

struct  ability_init_t
 To get the ability table from the database, we need to define a struct This struct is for the initialization of the ability table The values of the struct corresponds to the init_ability() method in. More...

Typedefs

typedef struct ability_init_t ability_init_t
 To get the ability table from the database, we need to define a struct This struct is for the initialization of the ability table The values of the struct corresponds to the init_ability() method in.

Functions

ability_init_tget_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.

Detailed Description

Declares functions to load and free ability definitions from the game database.

Definition in file ability_database.h.

Typedef Documentation

◆ ability_init_t

typedef struct ability_init_t ability_init_t

To get the ability table from the database, we need to define a struct This struct is for the initialization of the ability table The values of the struct corresponds to the init_ability() method in.

See also
{ability.h} &
{ability.c}

Function Documentation

◆ free_ability_table_from_db()

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.

Definition at line 64 of file ability_database.c.

64 {
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}

◆ get_ability_table_from_db()

ability_init_t * get_ability_table_from_db ( const db_connection_t * db_connection)

Get the ability table from the database.

Parameters
db_connectionPointer to the database connection
Returns
ability_init_t* pointer to the ability table

Definition at line 13 of file ability_database.c.

13 {
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}
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
To get the ability table from the database, we need to define a struct This struct is for the initial...