This function saves the character's inventory to the database.
268 {
269
270 sqlite3_stmt* stmt;
271 int rc = sqlite3_prepare_v2(db_connection->db, SQL_INSERT_INVENTORY, -1, &stmt, NULL);
272 if (rc != SQLITE_OK) {
273 log_msg(ERROR,
"Character",
"Failed to prepare statement: %s", sqlite3_errmsg(db_connection->db));
274 return;
275 }
276
277 rc = sqlite3_bind_int(stmt, 1, 0);
278 if (rc != SQLITE_OK) {
279 log_msg(ERROR,
"Character",
"Failed to bind inventory type: %s", sqlite3_errmsg(db_connection->db));
280 sqlite3_finalize(stmt);
281 return;
282 }
283
284 rc = sqlite3_step(stmt);
285
286
287 const sqlite3_int64 inventory_gear_id = sqlite3_last_insert_rowid(db_connection->db);
288 if (rc != SQLITE_DONE) {
289 log_msg(ERROR,
"Character",
"Failed to execute statement: %s", sqlite3_errmsg(db_connection->db));
290 sqlite3_finalize(stmt);
291 return;
292 }
293
294 sqlite3_finalize(stmt);
295
296
297 sqlite3_stmt* stmt_potion;
298 rc = sqlite3_prepare_v2(db_connection->db, SQL_INSERT_INVENTORY, -1, &stmt_potion, NULL);
299 if (rc != SQLITE_OK) {
300 log_msg(ERROR,
"Character",
"Failed to prepare statement: %s", sqlite3_errmsg(db_connection->db));
301 return;
302 }
303
304 rc = sqlite3_bind_int(stmt_potion, 1, 1);
305 if (rc != SQLITE_OK) {
306 log_msg(ERROR,
"Character",
"Failed to bind inventory type: %s", sqlite3_errmsg(db_connection->db));
307 sqlite3_finalize(stmt_potion);
308 return;
309 }
310
311 rc = sqlite3_step(stmt_potion);
312
313 const sqlite3_int64 inventory_potion_id = sqlite3_last_insert_rowid(db_connection->db);
314 if (rc != SQLITE_DONE) {
315 log_msg(ERROR,
"Character",
"Failed to execute statement: %s", sqlite3_errmsg(db_connection->db));
316 sqlite3_finalize(stmt_potion);
317 return;
318 }
319
320 sqlite3_finalize(stmt_potion);
321
322
323 for (int i = 0; i < character.gear_count; i++) {
324
325 sqlite3_stmt* stmt_gear_save;
326 rc = sqlite3_prepare_v2(db_connection->db, SQL_INSERT_INVENTORY_GEAR, -1, &stmt_gear_save, NULL);
327 if (rc != SQLITE_OK) {
328 log_msg(ERROR,
"Character",
"Failed to prepare statement: %s", sqlite3_errmsg(db_connection->db));
329 return;
330 }
331
332 rc = sqlite3_bind_int64(stmt_gear_save, 1, inventory_gear_id);
333 if (rc != SQLITE_OK) {
334 log_msg(ERROR,
"Character",
"Failed to bind inventory ID: %s", sqlite3_errmsg(db_connection->db));
335 sqlite3_finalize(stmt_gear_save);
336 return;
337 }
338
339 rc = sqlite3_bind_int(stmt_gear_save, 2, character.gear_inventory[i]->gear_identifier);
340 if (rc != SQLITE_OK) {
341 log_msg(ERROR,
"Character",
"Failed to bind gear type: %s", sqlite3_errmsg(db_connection->db));
342 sqlite3_finalize(stmt_gear_save);
343 return;
344 }
345
346 rc = sqlite3_bind_int(stmt_gear_save, 3, 0);
347
348 if (rc != SQLITE_OK) {
349 log_msg(ERROR,
"Character",
"Failed to bind equipped status: %s", sqlite3_errmsg(db_connection->db));
350 sqlite3_finalize(stmt_gear_save);
351 return;
352 }
353
354 rc = sqlite3_step(stmt_gear_save);
355 if (rc != SQLITE_DONE) {
356 log_msg(ERROR,
"Character",
"Failed to execute statement: %s", sqlite3_errmsg(db_connection->db));
357 sqlite3_finalize(stmt_gear_save);
358 return;
359 }
360
361 sqlite3_finalize(stmt_gear_save);
362 }
363
364
365 for (int i = 0; i < MAX_SLOT; i++) {
366 if (character.equipment[i] != NULL) {
367
368 sqlite3_stmt* stmt_gear_save;
369 rc = sqlite3_prepare_v2(db_connection->db, SQL_INSERT_INVENTORY_GEAR, -1, &stmt_gear_save, NULL);
370 if (rc != SQLITE_OK) {
371 log_msg(ERROR,
"Character",
"Failed to prepare statement: %s", sqlite3_errmsg(db_connection->db));
372 return;
373 }
374
375 rc = sqlite3_bind_int64(stmt_gear_save, 1, inventory_gear_id);
376 if (rc != SQLITE_OK) {
377 log_msg(ERROR,
"Character",
"Failed to bind inventory ID: %s", sqlite3_errmsg(db_connection->db));
378 sqlite3_finalize(stmt_gear_save);
379 return;
380 }
381
382 rc = sqlite3_bind_int(stmt_gear_save, 2, character.equipment[i]->gear_identifier);
383 if (rc != SQLITE_OK) {
384 log_msg(ERROR,
"Character",
"Failed to bind gear type: %s", sqlite3_errmsg(db_connection->db));
385 sqlite3_finalize(stmt_gear_save);
386 return;
387 }
388
389 rc = sqlite3_bind_int(stmt_gear_save, 3, 1);
390
391 if (rc != SQLITE_OK) {
392 log_msg(ERROR,
"Character",
"Failed to bind equipped status: %s", sqlite3_errmsg(db_connection->db));
393 sqlite3_finalize(stmt_gear_save);
394 return;
395 }
396
397 rc = sqlite3_step(stmt_gear_save);
398 if (rc != SQLITE_DONE) {
399 log_msg(ERROR,
"Character",
"Failed to execute statement: %s", sqlite3_errmsg(db_connection->db));
400 sqlite3_finalize(stmt_gear_save);
401 return;
402 }
403
404 sqlite3_finalize(stmt_gear_save);
405 }
406 }
407
408
409 for (int i = 0; i < character.potion_count; i++) {
410
411 sqlite3_stmt* stmt_potion_save;
412 rc = sqlite3_prepare_v2(db_connection->db, SQL_INSERT_INVENTORY_POTION, -1, &stmt_potion_save, NULL);
413 if (rc != SQLITE_OK) {
414 log_msg(ERROR,
"Character",
"Failed to prepare statement: %s", sqlite3_errmsg(db_connection->db));
415 return;
416 }
417
418 rc = sqlite3_bind_int64(stmt_potion_save, 1, inventory_potion_id);
419 if (rc != SQLITE_OK) {
420 log_msg(ERROR,
"Character",
"Failed to bind inventory ID: %s", sqlite3_errmsg(db_connection->db));
421 sqlite3_finalize(stmt_potion_save);
422 return;
423 }
424
425 rc = sqlite3_bind_int(stmt_potion_save, 2, character.potion_inventory[i]->effectType);
426 if (rc != SQLITE_OK) {
427 log_msg(ERROR,
"Character",
"Failed to bind potion type: %s", sqlite3_errmsg(db_connection->db));
428 sqlite3_finalize(stmt_potion_save);
429 return;
430 }
431
432 rc = sqlite3_step(stmt_potion_save);
433 if (rc != SQLITE_DONE) {
434 log_msg(ERROR,
"Character",
"Failed to execute statement: %s", sqlite3_errmsg(db_connection->db));
435 sqlite3_finalize(stmt_potion_save);
436 return;
437 }
438
439 sqlite3_finalize(stmt_potion_save);
440 }
441
442
443 sqlite3_stmt* stmt_character_inventory;
444 rc = sqlite3_prepare_v2(db_connection->db, SQL_INSERT_CHARACTER_INVENTORY, -1, &stmt_character_inventory, NULL);
445 if (rc != SQLITE_OK) {
446 log_msg(ERROR,
"Character",
"Failed to prepare statement: %s", sqlite3_errmsg(db_connection->db));
447 return;
448 }
449
450 rc = sqlite3_bind_int64(stmt_character_inventory, 1, character_id);
451 if (rc != SQLITE_OK) {
452 log_msg(ERROR,
"Character",
"Failed to bind character ID: %s", sqlite3_errmsg(db_connection->db));
453 sqlite3_finalize(stmt_character_inventory);
454 return;
455 }
456
457 rc = sqlite3_bind_int64(stmt_character_inventory, 2, inventory_gear_id);
458 if (rc != SQLITE_OK) {
459 log_msg(ERROR,
"Character",
"Failed to bind inventory ID: %s", sqlite3_errmsg(db_connection->db));
460 sqlite3_finalize(stmt_character_inventory);
461 return;
462 }
463
464 rc = sqlite3_step(stmt_character_inventory);
465 if (rc != SQLITE_DONE) {
466 log_msg(ERROR,
"Character",
"Failed to execute statement: %s", sqlite3_errmsg(db_connection->db));
467 sqlite3_finalize(stmt_character_inventory);
468 return;
469 }
470
471 sqlite3_finalize(stmt_character_inventory);
472
473
474 sqlite3_stmt* stmt_character_inventory_potion;
475 rc = sqlite3_prepare_v2(db_connection->db, SQL_INSERT_CHARACTER_INVENTORY, -1, &stmt_character_inventory_potion, NULL);
476 if (rc != SQLITE_OK) {
477 log_msg(ERROR,
"Character",
"Failed to prepare statement: %s", sqlite3_errmsg(db_connection->db));
478 return;
479 }
480
481 rc = sqlite3_bind_int64(stmt_character_inventory_potion, 1, character_id);
482 if (rc != SQLITE_OK) {
483 log_msg(ERROR,
"Character",
"Failed to bind character ID: %s", sqlite3_errmsg(db_connection->db));
484 sqlite3_finalize(stmt_character_inventory_potion);
485 return;
486 }
487
488 rc = sqlite3_bind_int64(stmt_character_inventory_potion, 2, inventory_potion_id);
489 if (rc != SQLITE_OK) {
490 log_msg(ERROR,
"Character",
"Failed to bind inventory ID: %s", sqlite3_errmsg(db_connection->db));
491 sqlite3_finalize(stmt_character_inventory_potion);
492 return;
493 }
494
495 rc = sqlite3_step(stmt_character_inventory_potion);
496 if (rc != SQLITE_DONE) {
497 log_msg(ERROR,
"Character",
"Failed to execute statement: %s", sqlite3_errmsg(db_connection->db));
498 sqlite3_finalize(stmt_character_inventory_potion);
499 return;
500 }
501
502 sqlite3_finalize(stmt_character_inventory_potion);
503}