From 95e6e3e2437dafcaa270a3891821d13a54107c94 Mon Sep 17 00:00:00 2001 From: Haru Date: Mon, 22 Feb 2016 01:03:59 +0100 Subject: Split mapif->homunculus_save() into two functions (save and create) Signed-off-by: Haru --- src/char/int_homun.c | 97 ++++++++++++++++++++++++++++++---------------------- src/char/mapif.c | 4 ++- src/char/mapif.h | 3 +- 3 files changed, 61 insertions(+), 43 deletions(-) (limited to 'src') diff --git a/src/char/int_homun.c b/src/char/int_homun.c index d633dc7e0..95374a978 100644 --- a/src/char/int_homun.c +++ b/src/char/int_homun.c @@ -109,64 +109,79 @@ void mapif_homunculus_renamed(int fd, int account_id, int char_id, unsigned char WFIFOSET(fd, NAME_LENGTH+12); } -bool mapif_homunculus_save(struct s_homunculus* hd) +/** + * Creates a new homunculus with the given data. + * + * @remark + * The homunculus ID is expected to be 0, and will be filled with the newly + * assigned ID. + * + * @param[in,out] hd The new homunculus' data. + * @retval false in case of errors. + */ +bool mapif_homunculus_create(struct s_homunculus *hd) { - bool flag = true; char esc_name[NAME_LENGTH*2+1]; - nullpo_ret(hd); + nullpo_retr(false, hd); + Assert_retr(false, hd->hom_id == 0); + SQL->EscapeStringLen(inter->sql_handle, esc_name, hd->name, strnlen(hd->name, NAME_LENGTH)); - if( hd->hom_id == 0 ) - {// new homunculus - if( SQL_ERROR == SQL->Query(inter->sql_handle, "INSERT INTO `%s` " + if (SQL_ERROR == SQL->Query(inter->sql_handle, "INSERT INTO `%s` " "(`char_id`, `class`,`prev_class`,`name`,`level`,`exp`,`intimacy`,`hunger`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `hp`,`max_hp`,`sp`,`max_sp`,`skill_point`, `rename_flag`, `vaporize`) " "VALUES ('%d', '%d', '%d', '%s', '%d', '%u', '%u', '%d', '%d', %d, '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')", homunculus_db, hd->char_id, hd->class_, hd->prev_class, esc_name, hd->level, hd->exp, hd->intimacy, hd->hunger, hd->str, hd->agi, hd->vit, hd->int_, hd->dex, hd->luk, - hd->hp, hd->max_hp, hd->sp, hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize) ) - { - Sql_ShowDebug(inter->sql_handle); - flag = false; - } - else - { - hd->hom_id = (int)SQL->LastInsertId(inter->sql_handle); - } + hd->hp, hd->max_hp, hd->sp, hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize)) { + Sql_ShowDebug(inter->sql_handle); + return false; } - else - { - if( SQL_ERROR == SQL->Query(inter->sql_handle, "UPDATE `%s` SET `char_id`='%d', `class`='%d',`prev_class`='%d',`name`='%s',`level`='%d',`exp`='%u',`intimacy`='%u',`hunger`='%d', `str`='%d', `agi`='%d', `vit`='%d', `int`='%d', `dex`='%d', `luk`='%d', `hp`='%d',`max_hp`='%d',`sp`='%d',`max_sp`='%d',`skill_point`='%d', `rename_flag`='%d', `vaporize`='%d' WHERE `homun_id`='%d'", + hd->hom_id = (int)SQL->LastInsertId(inter->sql_handle); + return true; +} + +/** + * Saves an existing homunculus. + * + * @param hd The homunculus' data. + * @retval false in case of errors. + */ +bool mapif_homunculus_save(const struct s_homunculus *hd) +{ + bool flag = true; + char esc_name[NAME_LENGTH*2+1]; + + nullpo_retr(false, hd); + Assert_retr(false, hd->hom_id > 0); + + SQL->EscapeStringLen(inter->sql_handle, esc_name, hd->name, strnlen(hd->name, NAME_LENGTH)); + + if (SQL_ERROR == SQL->Query(inter->sql_handle, "UPDATE `%s` SET `char_id`='%d', `class`='%d',`prev_class`='%d',`name`='%s',`level`='%d',`exp`='%u',`intimacy`='%u',`hunger`='%d', `str`='%d', `agi`='%d', `vit`='%d', `int`='%d', `dex`='%d', `luk`='%d', `hp`='%d',`max_hp`='%d',`sp`='%d',`max_sp`='%d',`skill_point`='%d', `rename_flag`='%d', `vaporize`='%d' WHERE `homun_id`='%d'", homunculus_db, hd->char_id, hd->class_, hd->prev_class, esc_name, hd->level, hd->exp, hd->intimacy, hd->hunger, hd->str, hd->agi, hd->vit, hd->int_, hd->dex, hd->luk, - hd->hp, hd->max_hp, hd->sp, hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize, hd->hom_id) ) - { - Sql_ShowDebug(inter->sql_handle); + hd->hp, hd->max_hp, hd->sp, hd->max_sp, hd->skillpts, hd->rename_flag, hd->vaporize, hd->hom_id)) { + Sql_ShowDebug(inter->sql_handle); + flag = false; + } else { + int i; + SqlStmt *stmt = SQL->StmtMalloc(inter->sql_handle); + + if (SQL_ERROR == SQL->StmtPrepare(stmt, "REPLACE INTO `%s` (`homun_id`, `id`, `lv`) VALUES (%d, ?, ?)", skill_homunculus_db, hd->hom_id)) { + SqlStmt_ShowDebug(stmt); flag = false; - } - else - { - SqlStmt* stmt; - int i; - - stmt = SQL->StmtMalloc(inter->sql_handle); - if( SQL_ERROR == SQL->StmtPrepare(stmt, "REPLACE INTO `%s` (`homun_id`, `id`, `lv`) VALUES (%d, ?, ?)", skill_homunculus_db, hd->hom_id) ) - SqlStmt_ShowDebug(stmt); - for( i = 0; i < MAX_HOMUNSKILL; ++i ) - { - if( hd->hskill[i].id > 0 && hd->hskill[i].lv != 0 ) - { - SQL->StmtBindParam(stmt, 0, SQLDT_USHORT, &hd->hskill[i].id, 0); - SQL->StmtBindParam(stmt, 1, SQLDT_USHORT, &hd->hskill[i].lv, 0); - if( SQL_ERROR == SQL->StmtExecute(stmt) ) - { + } else { + for (i = 0; i < MAX_HOMUNSKILL; ++i) { + if (hd->hskill[i].id > 0 && hd->hskill[i].lv != 0) { + SQL->StmtBindParam(stmt, 0, SQLDT_USHORT, (void*)&hd->hskill[i].id, 0); // FIXME: StmtBindParam should take const void + SQL->StmtBindParam(stmt, 1, SQLDT_USHORT, (void*)&hd->hskill[i].lv, 0); // FIXME: StmtBindParam should take const void + if (SQL_ERROR == SQL->StmtExecute(stmt)) { SqlStmt_ShowDebug(stmt); - SQL->StmtFree(stmt); flag = false; break; } } } - SQL->StmtFree(stmt); } + SQL->StmtFree(stmt); } return flag; @@ -289,7 +304,7 @@ bool mapif_homunculus_rename(char *name) void mapif_parse_homunculus_create(int fd, int len, int account_id, struct s_homunculus* phd) { - bool result = mapif->homunculus_save(phd); + bool result = mapif->homunculus_create(phd); mapif->homunculus_created(fd, account_id, phd, result); } diff --git a/src/char/mapif.c b/src/char/mapif.c index 4edcb6027..dc8e34e36 100644 --- a/src/char/mapif.c +++ b/src/char/mapif.c @@ -108,7 +108,8 @@ void mapif_homunculus_deleted(int fd, int flag); void mapif_homunculus_loaded(int fd, int account_id, struct s_homunculus *hd); void mapif_homunculus_saved(int fd, int account_id, bool flag); void mapif_homunculus_renamed(int fd, int account_id, int char_id, unsigned char flag, char* name); -bool mapif_homunculus_save(struct s_homunculus* hd); +bool mapif_homunculus_create(struct s_homunculus *hd); +bool mapif_homunculus_save(const struct s_homunculus *hd); bool mapif_homunculus_load(int homun_id, struct s_homunculus* hd); bool mapif_homunculus_delete(int homun_id); bool mapif_homunculus_rename(char *name); @@ -284,6 +285,7 @@ void mapif_defaults(void) { mapif->homunculus_loaded = mapif_homunculus_loaded; mapif->homunculus_saved = mapif_homunculus_saved; mapif->homunculus_renamed = mapif_homunculus_renamed; + mapif->homunculus_create = mapif_homunculus_create; mapif->homunculus_save = mapif_homunculus_save; mapif->homunculus_load = mapif_homunculus_load; mapif->homunculus_delete = mapif_homunculus_delete; diff --git a/src/char/mapif.h b/src/char/mapif.h index e5767bf05..05bddabb4 100644 --- a/src/char/mapif.h +++ b/src/char/mapif.h @@ -102,7 +102,8 @@ struct mapif_interface { void (*homunculus_loaded) (int fd, int account_id, struct s_homunculus *hd); void (*homunculus_saved) (int fd, int account_id, bool flag); void (*homunculus_renamed) (int fd, int account_id, int char_id, unsigned char flag, char* name); - bool (*homunculus_save) (struct s_homunculus* hd); + bool (*homunculus_create) (struct s_homunculus *hd); + bool (*homunculus_save) (const struct s_homunculus *hd); bool (*homunculus_load) (int homun_id, struct s_homunculus* hd); bool (*homunculus_delete) (int homun_id); bool (*homunculus_rename) (char *name); -- cgit v1.2.3-60-g2f50