From 1a0ad427708a010992321efbcdca90150b9e3f8b Mon Sep 17 00:00:00 2001 From: Haru Date: Mon, 22 Feb 2016 01:09:54 +0100 Subject: Split mapif->mercenary_save() into two functions (save and create) Signed-off-by: Haru --- src/char/int_mercenary.c | 59 ++++++++++++++++++++++++++++++++---------------- src/char/mapif.c | 4 +++- src/char/mapif.h | 3 ++- 3 files changed, 44 insertions(+), 22 deletions(-) (limited to 'src/char') diff --git a/src/char/int_mercenary.c b/src/char/int_mercenary.c index 2ed45305c..133dcf565 100644 --- a/src/char/int_mercenary.c +++ b/src/char/int_mercenary.c @@ -94,32 +94,51 @@ bool inter_mercenary_owner_delete(int char_id) return true; } -bool mapif_mercenary_save(struct s_mercenary* merc) +/** + * Creates a new mercenary with the given data. + * + * @remark + * The mercenary ID is expected to be 0, and will be filled with the newly + * assigned ID. + * + * @param[in,out] merc The new mercenary's data. + * @retval false in case of errors. + */ +bool mapif_mercenary_create(struct s_mercenary *merc) { - bool flag = true; + nullpo_retr(false, merc); + Assert_retr(false, merc->mercenary_id == 0); - nullpo_ret(merc); - if( merc->mercenary_id == 0 ) - { // Create new DB entry - if( SQL_ERROR == SQL->Query(inter->sql_handle, + if (SQL_ERROR == SQL->Query(inter->sql_handle, "INSERT INTO `%s` (`char_id`,`class`,`hp`,`sp`,`kill_counter`,`life_time`) VALUES ('%d','%d','%d','%d','%u','%u')", - mercenary_db, merc->char_id, merc->class_, merc->hp, merc->sp, merc->kill_count, merc->life_time) ) - { - Sql_ShowDebug(inter->sql_handle); - flag = false; - } - else - merc->mercenary_id = (int)SQL->LastInsertId(inter->sql_handle); + mercenary_db, merc->char_id, merc->class_, merc->hp, merc->sp, merc->kill_count, merc->life_time)) { + Sql_ShowDebug(inter->sql_handle); + return false; } - else if( SQL_ERROR == SQL->Query(inter->sql_handle, - "UPDATE `%s` SET `char_id` = '%d', `class` = '%d', `hp` = '%d', `sp` = '%d', `kill_counter` = '%u', `life_time` = '%u' WHERE `mer_id` = '%d'", - mercenary_db, merc->char_id, merc->class_, merc->hp, merc->sp, merc->kill_count, merc->life_time, merc->mercenary_id) ) - { // Update DB entry + merc->mercenary_id = (int)SQL->LastInsertId(inter->sql_handle); + + return true; +} + +/** + * Saves an existing mercenary. + * + * @param merc The mercenary's data. + * @retval false in case of errors. + */ +bool mapif_mercenary_save(const struct s_mercenary *merc) +{ + nullpo_retr(false, merc); + Assert_retr(false, merc->mercenary_id > 0); + + if (SQL_ERROR == SQL->Query(inter->sql_handle, + "UPDATE `%s` SET `char_id` = '%d', `class` = '%d', `hp` = '%d', `sp` = '%d', `kill_counter` = '%u', `life_time` = '%u' WHERE `mer_id` = '%d'", + mercenary_db, merc->char_id, merc->class_, merc->hp, merc->sp, merc->kill_count, merc->life_time, merc->mercenary_id)) { Sql_ShowDebug(inter->sql_handle); - flag = false; + return false; } - return flag; + return true; } bool mapif_mercenary_load(int merc_id, int char_id, struct s_mercenary *merc) @@ -181,7 +200,7 @@ void mapif_mercenary_send(int fd, struct s_mercenary *merc, unsigned char flag) void mapif_parse_mercenary_create(int fd, struct s_mercenary* merc) { - bool result = mapif->mercenary_save(merc); + bool result = mapif->mercenary_create(merc); mapif->mercenary_send(fd, merc, result); } diff --git a/src/char/mapif.c b/src/char/mapif.c index dc8e34e36..f25df2369 100644 --- a/src/char/mapif.c +++ b/src/char/mapif.c @@ -131,7 +131,8 @@ void mapif_mail_return(int fd, int char_id, int mail_id, int new_mail); void mapif_parse_mail_return(int fd); void mapif_mail_send(int fd, struct mail_message* msg); void mapif_parse_mail_send(int fd); -bool mapif_mercenary_save(struct s_mercenary* merc); +bool mapif_mercenary_create(struct s_mercenary *merc); +bool mapif_mercenary_save(const struct s_mercenary *merc); bool mapif_mercenary_load(int merc_id, int char_id, struct s_mercenary *merc); bool mapif_mercenary_delete(int merc_id); void mapif_mercenary_send(int fd, struct s_mercenary *merc, unsigned char flag); @@ -308,6 +309,7 @@ void mapif_defaults(void) { mapif->parse_mail_return = mapif_parse_mail_return; mapif->mail_send = mapif_mail_send; mapif->parse_mail_send = mapif_parse_mail_send; + mapif->mercenary_create = mapif_mercenary_create; mapif->mercenary_save = mapif_mercenary_save; mapif->mercenary_load = mapif_mercenary_load; mapif->mercenary_delete = mapif_mercenary_delete; diff --git a/src/char/mapif.h b/src/char/mapif.h index 05bddabb4..d4fbcb945 100644 --- a/src/char/mapif.h +++ b/src/char/mapif.h @@ -125,7 +125,8 @@ struct mapif_interface { void (*parse_mail_return) (int fd); void (*mail_send) (int fd, struct mail_message* msg); void (*parse_mail_send) (int fd); - bool (*mercenary_save) (struct s_mercenary* merc); + bool (*mercenary_create) (struct s_mercenary *merc); + bool (*mercenary_save) (const struct s_mercenary *merc); bool (*mercenary_load) (int merc_id, int char_id, struct s_mercenary *merc); bool (*mercenary_delete) (int merc_id); void (*mercenary_send) (int fd, struct s_mercenary *merc, unsigned char flag); -- cgit v1.2.3-70-g09d2