Save the game state.
31 {
32
34 log_msg(ERROR,
"GameState",
"Database connection is not open");
35 return 0;
36 }
37
38
39
41 if (current_time == NULL) {
42 log_msg(ERROR,
"GameState",
"Failed to get current time");
43 return 0;
44 }
45
46
47 sqlite3_stmt* stmt;
48 int rc = sqlite3_prepare_v2(db_connection->db, SQL_INSERT_GAME_STATE, -1, &stmt, NULL);
49 if (rc != SQLITE_OK) {
50 log_msg(ERROR,
"GameState",
"Failed to prepare statement: %s", sqlite3_errmsg(db_connection->db));
51 free(current_time);
52 return 0;
53 }
54
55
56 rc = sqlite3_bind_text(stmt, 1, current_time, -1, SQLITE_TRANSIENT);
57 if (rc != SQLITE_OK) {
58 log_msg(ERROR,
"GameState",
"Failed to bind time: %s", sqlite3_errmsg(db_connection->db));
59 sqlite3_finalize(stmt);
60 free(current_time);
61 return 0;
62 }
63
64
65 free(current_time);
66
67
68 rc = sqlite3_bind_text(stmt, 2, save_name, -1, SQLITE_TRANSIENT);
69 if (rc != SQLITE_OK) {
70 log_msg(ERROR,
"GameState",
"Failed to bind save name: %s", sqlite3_errmsg(db_connection->db));
71 sqlite3_finalize(stmt);
72 return 0;
73 }
74
75
76 rc = sqlite3_step(stmt);
77 if (rc != SQLITE_DONE) {
78 log_msg(ERROR,
"GameState",
"Failed to execute statement: %s", sqlite3_errmsg(db_connection->db));
79 }
80
81 const sqlite3_int64 game_state_id = sqlite3_last_insert_rowid(db_connection->db);
82
83
84 sqlite3_finalize(stmt);
85
86
89 if (map_json == NULL || revealed_map_json == NULL) {
90 free(map_json);
91 free(revealed_map_json);
92 return 0;
93 }
94
95
96 sqlite3_stmt* stmt_map;
97 rc = sqlite3_prepare_v2(db_connection->db, SQL_INSERT_MAP_STATE, -1, &stmt_map, NULL);
98 if (rc != SQLITE_OK) {
99 log_msg(ERROR,
"GameState",
"Failed to prepare statement: %s", sqlite3_errmsg(db_connection->db));
100 free(map_json);
101 free(revealed_map_json);
102 return 0;
103 }
104
105
106 rc = sqlite3_bind_text(stmt_map, 1, map_json, -1, SQLITE_TRANSIENT);
107 if (rc != SQLITE_OK) {
108 log_msg(ERROR,
"GameState",
"Failed to bind map: %s", sqlite3_errmsg(db_connection->db));
109 sqlite3_finalize(stmt_map);
110 free(map_json);
111 free(revealed_map_json);
112 return 0;
113 }
114
115 rc = sqlite3_bind_text(stmt_map, 2, revealed_map_json, -1, SQLITE_TRANSIENT);
116 if (rc != SQLITE_OK) {
117 log_msg(ERROR,
"GameState",
"Failed to bind revealed map: %s", sqlite3_errmsg(db_connection->db));
118 sqlite3_finalize(stmt_map);
119 free(map_json);
120 free(revealed_map_json);
121 return 0;
122 }
123
124
125 free(map_json);
126 free(revealed_map_json);
127
128 rc = sqlite3_bind_int(stmt_map, 3, height);
129 if (rc != SQLITE_OK) {
130 log_msg(ERROR,
"GameState",
"Failed to bind height: %s", sqlite3_errmsg(db_connection->db));
131 sqlite3_finalize(stmt_map);
132 return 0;
133 }
134 rc = sqlite3_bind_int(stmt_map, 4, width);
135 if (rc != SQLITE_OK) {
136 log_msg(ERROR,
"GameState",
"Failed to bind width: %s", sqlite3_errmsg(db_connection->db));
137 sqlite3_finalize(stmt_map);
138 return 0;
139 }
140 rc = sqlite3_bind_int64(stmt_map, 5, game_state_id);
141 if (rc != SQLITE_OK) {
142 log_msg(ERROR,
"GameState",
"Failed to bind game state ID: %s", sqlite3_errmsg(db_connection->db));
143 sqlite3_finalize(stmt_map);
144 return 0;
145 }
146
147 rc = sqlite3_step(stmt_map);
148 if (rc != SQLITE_DONE) {
149 log_msg(ERROR,
"GameState",
"Failed to execute statement: %s", sqlite3_errmsg(db_connection->db));
150 }
151
152 sqlite3_finalize(stmt_map);
153
154
155 sqlite3_stmt* stmt_player;
156 rc = sqlite3_prepare_v2(db_connection->db, SQL_INSERT_PLAYER_STATE, -1, &stmt_player, NULL);
157 if (rc != SQLITE_OK) {
158 log_msg(ERROR,
"GameState",
"Failed to prepare statement: %s", sqlite3_errmsg(db_connection->db));
159 return 0;
160 }
161
162 rc = sqlite3_bind_int(stmt_player, 1, player.dx);
163 if (rc != SQLITE_OK) {
164 log_msg(ERROR,
"GameState",
"Failed to bind player x: %s", sqlite3_errmsg(db_connection->db));
165 sqlite3_finalize(stmt_player);
166 return 0;
167 }
168 rc = sqlite3_bind_int(stmt_player, 2, player.dy);
169 if (rc != SQLITE_OK) {
170 log_msg(ERROR,
"GameState",
"Failed to bind player y: %s", sqlite3_errmsg(db_connection->db));
171 sqlite3_finalize(stmt_player);
172 return 0;
173 }
174 rc = sqlite3_bind_int64(stmt_player, 3, game_state_id);
175 if (rc != SQLITE_OK) {
176 log_msg(ERROR,
"GameState",
"Failed to bind game state ID: %s", sqlite3_errmsg(db_connection->db));
177 sqlite3_finalize(stmt_player);
178 return 0;
179 }
180
181 rc = sqlite3_step(stmt_player);
182 if (rc != SQLITE_DONE) {
183 log_msg(ERROR,
"GameState",
"Failed to execute statement: %s", sqlite3_errmsg(db_connection->db));
184 }
185
186 sqlite3_finalize(stmt_player);
187 return game_state_id;
188}
char * arr2D_to_flat_json(const int *arr, const int width, const int height)
Convert a 2D array to a Json-Array.
char * get_iso8601_time()
Get the current time in ISO 8601 format.