summaryrefslogtreecommitdiff
path: root/src/char/int_homun.c
diff options
context:
space:
mode:
authorHaru <haru@dotalux.com>2016-02-22 01:03:59 +0100
committerHaru <haru@dotalux.com>2016-02-24 19:40:31 +0100
commit95e6e3e2437dafcaa270a3891821d13a54107c94 (patch)
treed4a098feab7e3e1d495b124b88cc7a9c30b4a49d /src/char/int_homun.c
parentd736b2ef87d2b331f2e8fca9c42784d481778a94 (diff)
downloadhercules-95e6e3e2437dafcaa270a3891821d13a54107c94.tar.gz
hercules-95e6e3e2437dafcaa270a3891821d13a54107c94.tar.bz2
hercules-95e6e3e2437dafcaa270a3891821d13a54107c94.tar.xz
hercules-95e6e3e2437dafcaa270a3891821d13a54107c94.zip
Split mapif->homunculus_save() into two functions (save and create)
Signed-off-by: Haru <haru@dotalux.com>
Diffstat (limited to 'src/char/int_homun.c')
-rw-r--r--src/char/int_homun.c97
1 files changed, 56 insertions, 41 deletions
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);
}