From 554e54afebf3eed9d6293a7891c66320af2586a0 Mon Sep 17 00:00:00 2001 From: Kenpachi Developer Date: Thu, 13 Feb 2020 08:15:39 +0100 Subject: Add enum for pet hunger levels and use its constants --- src/char/int_pet.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'src/char') diff --git a/src/char/int_pet.c b/src/char/int_pet.c index 880de668d..e7be4d762 100644 --- a/src/char/int_pet.c +++ b/src/char/int_pet.c @@ -60,7 +60,7 @@ static int inter_pet_tosql(const struct s_pet *p) nullpo_ret(p); SQL->EscapeStringLen(inter->sql_handle, esc_name, p->name, strnlen(p->name, NAME_LENGTH)); - hungry = cap_value(p->hungry, 0, 100); + hungry = cap_value(p->hungry, PET_HUNGER_STARVING, PET_HUNGER_STUFFED); intimate = cap_value(p->intimate, 0, 1000); if (p->pet_id == 0) { @@ -128,7 +128,7 @@ static int inter_pet_fromsql(int pet_id, struct s_pet *p) SQL->FreeResult(inter->sql_handle); - p->hungry = cap_value(p->hungry, 0, 100); + p->hungry = cap_value(p->hungry, PET_HUNGER_STARVING, PET_HUNGER_STUFFED); p->intimate = cap_value(p->intimate, 0, 1000); if (chr->show_save_log) @@ -177,14 +177,10 @@ static struct s_pet *inter_pet_create(int account_id, int char_id, int pet_class inter_pet->pt->egg_id = pet_egg_id; inter_pet->pt->equip = pet_equip; inter_pet->pt->intimate = intimate; - inter_pet->pt->hungry = hungry; + inter_pet->pt->hungry = cap_value(hungry, PET_HUNGER_STARVING, PET_HUNGER_STUFFED); inter_pet->pt->rename_flag = rename_flag; inter_pet->pt->incubate = incubate; - if(inter_pet->pt->hungry < 0) - inter_pet->pt->hungry = 0; - else if(inter_pet->pt->hungry > 100) - inter_pet->pt->hungry = 100; if(inter_pet->pt->intimate < 0) inter_pet->pt->intimate = 0; else if(inter_pet->pt->intimate > 1000) -- cgit v1.2.3-70-g09d2 From 09e291009156002e83566afe7a10b6509f5463f5 Mon Sep 17 00:00:00 2001 From: Kenpachi Developer Date: Thu, 13 Feb 2020 08:29:47 +0100 Subject: Add enum for pet intimacy levels and use its constants --- src/char/int_pet.c | 11 +++-------- src/common/mmo.h | 11 +++++++++++ src/map/atcommand.c | 2 +- src/map/clif.c | 4 ++-- src/map/pc.c | 6 +++--- src/map/pet.c | 26 +++++++++++++------------- src/map/script.c | 9 +++++++++ src/map/status.c | 2 +- src/map/unit.c | 6 +++--- 9 files changed, 46 insertions(+), 31 deletions(-) (limited to 'src/char') diff --git a/src/char/int_pet.c b/src/char/int_pet.c index e7be4d762..1c3ca16d6 100644 --- a/src/char/int_pet.c +++ b/src/char/int_pet.c @@ -61,7 +61,7 @@ static int inter_pet_tosql(const struct s_pet *p) SQL->EscapeStringLen(inter->sql_handle, esc_name, p->name, strnlen(p->name, NAME_LENGTH)); hungry = cap_value(p->hungry, PET_HUNGER_STARVING, PET_HUNGER_STUFFED); - intimate = cap_value(p->intimate, 0, 1000); + intimate = cap_value(p->intimate, PET_INTIMACY_NONE, PET_INTIMACY_MAX); if (p->pet_id == 0) { // New pet. @@ -129,7 +129,7 @@ static int inter_pet_fromsql(int pet_id, struct s_pet *p) SQL->FreeResult(inter->sql_handle); p->hungry = cap_value(p->hungry, PET_HUNGER_STARVING, PET_HUNGER_STUFFED); - p->intimate = cap_value(p->intimate, 0, 1000); + p->intimate = cap_value(p->intimate, PET_INTIMACY_NONE, PET_INTIMACY_MAX); if (chr->show_save_log) ShowInfo("Pet loaded (%d - %s).\n", pet_id, p->name); @@ -176,16 +176,11 @@ static struct s_pet *inter_pet_create(int account_id, int char_id, int pet_class inter_pet->pt->level = pet_lv; inter_pet->pt->egg_id = pet_egg_id; inter_pet->pt->equip = pet_equip; - inter_pet->pt->intimate = intimate; + inter_pet->pt->intimate = cap_value(intimate, PET_INTIMACY_NONE, PET_INTIMACY_MAX); inter_pet->pt->hungry = cap_value(hungry, PET_HUNGER_STARVING, PET_HUNGER_STUFFED); inter_pet->pt->rename_flag = rename_flag; inter_pet->pt->incubate = incubate; - if(inter_pet->pt->intimate < 0) - inter_pet->pt->intimate = 0; - else if(inter_pet->pt->intimate > 1000) - inter_pet->pt->intimate = 1000; - inter_pet->pt->pet_id = 0; //Signal NEW pet. if ((inter_pet->pt->pet_id = inter_pet->tosql(inter_pet->pt)) != 0) return inter_pet->pt; diff --git a/src/common/mmo.h b/src/common/mmo.h index 4886ca04b..9421f6e35 100644 --- a/src/common/mmo.h +++ b/src/common/mmo.h @@ -1388,6 +1388,17 @@ enum e_pet_hunger_level { PET_HUNGER_STUFFED = 100 }; +/** Pet intimacy level **/ +enum e_pet_intimacy_level { + PET_INTIMACY_NONE = 0, + PET_INTIMACY_AWKWARD = 1, + PET_INTIMACY_SHY = 100, + PET_INTIMACY_NEUTRAL = 250, + PET_INTIMACY_CORDIAL = 750, + PET_INTIMACY_LOYAL = 900, + PET_INTIMACY_MAX = 1000 +}; + /* packet size constant for itemlist */ #if MAX_INVENTORY > MAX_STORAGE && MAX_INVENTORY > MAX_CART #define MAX_ITEMLIST MAX_INVENTORY diff --git a/src/map/atcommand.c b/src/map/atcommand.c index 9fc552f5f..96093ffec 100644 --- a/src/map/atcommand.c +++ b/src/map/atcommand.c @@ -2817,7 +2817,7 @@ ACMD(petfriendly) return false; } - if (friendly < 0 || friendly > 1000) + if (friendly < PET_INTIMACY_NONE || friendly > PET_INTIMACY_MAX) { clif->message(fd, msg_fd(fd,37)); // An invalid number was specified. return false; diff --git a/src/map/clif.c b/src/map/clif.c index 14f5557eb..162308516 100644 --- a/src/map/clif.c +++ b/src/map/clif.c @@ -10804,7 +10804,7 @@ static void clif_parse_LoadEndAck(int fd, struct map_session_data *sd) } } - if(sd->pd && sd->pd->pet.intimate > 900) + if (sd->pd && sd->pd->pet.intimate > PET_INTIMACY_LOYAL) clif->pet_emotion(sd->pd,(sd->pd->pet.class_ - 100)*100 + 50 + pet->hungry_val(sd->pd)); if(homun_alive(sd->hd)) @@ -15232,7 +15232,7 @@ static void clif_parse_pet_evolution(int fd, struct map_session_data *sd) } // Not Loyal Yet - if (sd->pd == NULL || sd->pd->pet.intimate < 900) { + if (sd->pd == NULL || sd->pd->pet.intimate < PET_INTIMACY_LOYAL) { clif->petEvolutionResult(fd, PET_EVOL_RG_FAMILIAR); return; } diff --git a/src/map/pc.c b/src/map/pc.c index 6dba997ef..33deb780b 100644 --- a/src/map/pc.c +++ b/src/map/pc.c @@ -5920,7 +5920,7 @@ static int pc_setpos(struct map_session_data *sd, unsigned short map_index, int sd->regen.state.gc = 1; } - if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 ) { + if (sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > PET_INTIMACY_NONE) { sd->pd->bl.m = m; sd->pd->bl.x = sd->pd->ud.to_x = x; sd->pd->bl.y = sd->pd->ud.to_y = y; @@ -8046,8 +8046,8 @@ static int pc_dead(struct map_session_data *sd, struct block_list *src) struct pet_data *pd = sd->pd; if( !map->list[sd->bl.m].flag.noexppenalty ) { pet->set_intimate(pd, pd->pet.intimate - pd->petDB->die); - if( pd->pet.intimate < 0 ) - pd->pet.intimate = 0; + if (pd->pet.intimate < PET_INTIMACY_NONE) + pd->pet.intimate = PET_INTIMACY_NONE; clif->send_petdata(sd,sd->pd,1,pd->pet.intimate); } if( sd->pd->target_id ) // Unlock all targets... diff --git a/src/map/pet.c b/src/map/pet.c index 39a502d0f..592a25d3e 100644 --- a/src/map/pet.c +++ b/src/map/pet.c @@ -91,7 +91,7 @@ static void pet_set_intimate(struct pet_data *pd, int value) status_calc_pc(sd,SCO_NONE); /* Pet is lost, delete the egg */ - if (value <= 0) { + if (value <= PET_INTIMACY_NONE) { int i; ARR_FIND(0, sd->status.inventorySize, i, sd->status.inventory[i].card[0] == CARD0_PET && @@ -246,7 +246,7 @@ static int pet_hungry(int tid, int64 tick, int id, intptr_t data) } pd->pet_hungry_timer = INVALID_TIMER; - if (pd->pet.intimate <= 0) + if (pd->pet.intimate <= PET_INTIMACY_NONE) return 1; //You lost the pet already, the rest is irrelevant. pd->pet.hungry--; @@ -262,9 +262,9 @@ static int pet_hungry(int tid, int64 tick, int id, intptr_t data) pet_stop_attack(pd); pd->pet.hungry = PET_HUNGER_STARVING; pet->set_intimate(pd, pd->pet.intimate - battle_config.pet_hungry_friendly_decrease); - if( pd->pet.intimate <= 0 ) + if (pd->pet.intimate <= PET_INTIMACY_NONE) { - pd->pet.intimate = 0; + pd->pet.intimate = PET_INTIMACY_NONE; pd->status.speed = pd->db->status.speed; } status_calc_pet(pd, SCO_NONE); @@ -320,9 +320,9 @@ static int pet_performance(struct map_session_data *sd, struct pet_data *pd) int val; nullpo_retr(1, pd); - if (pd->pet.intimate > 900) + if (pd->pet.intimate > PET_INTIMACY_LOYAL) val = (pd->petDB->s_perfor > 0)? 4:3; - else if(pd->pet.intimate > 750) //TODO: this is way too high + else if (pd->pet.intimate > PET_INTIMACY_CORDIAL) //TODO: this is way too high val = 2; else val = 1; @@ -678,7 +678,7 @@ static int pet_menu(struct map_session_data *sd, int menunum) return 1; //You lost the pet already. - if(!sd->status.pet_id || sd->pd->pet.intimate <= 0 || sd->pd->pet.incubate) + if (!sd->status.pet_id || sd->pd->pet.intimate <= PET_INTIMACY_NONE || sd->pd->pet.incubate) return 1; egg_id = itemdb->exists(sd->pd->petDB->EggID); @@ -858,12 +858,12 @@ static int pet_food(struct map_session_data *sd, struct pet_data *pd) } pet->set_intimate(pd, pd->pet.intimate + add_intimate); } - if (pd->pet.intimate <= 0) { - pd->pet.intimate = 0; + if (pd->pet.intimate <= PET_INTIMACY_NONE) { + pd->pet.intimate = PET_INTIMACY_NONE; pet_stop_attack(pd); pd->status.speed = pd->db->status.speed; - } else if (pd->pet.intimate > 1000) { - pd->pet.intimate = 1000; + } else if (pd->pet.intimate > PET_INTIMACY_MAX) { + pd->pet.intimate = PET_INTIMACY_MAX; } status_calc_pet(pd, SCO_NONE); pd->pet.hungry += pd->petDB->fullness; @@ -937,7 +937,7 @@ static int pet_ai_sub_hard(struct pet_data *pd, struct map_session_data *sd, int if(pd->ud.walktimer != INVALID_TIMER && pd->ud.walkpath.path_pos <= 2) return 0; //No thinking when you just started to walk. - if(pd->pet.intimate <= 0) { + if (pd->pet.intimate <= PET_INTIMACY_NONE) { //Pet should just... well, random walk. pet->randomwalk(pd,tick); return 0; @@ -1168,7 +1168,7 @@ static int pet_skill_bonus_timer(int tid, int64 tick, int id, intptr_t data) if (pd->state.skillbonus && pd->bonus->delay > 0) { bonus = 0; duration = pd->bonus->delay*1000; // the duration until pet bonuses will be reactivated again - } else if (pd->pet.intimate) { + } else if (pd->pet.intimate > PET_INTIMACY_NONE) { bonus = 1; duration = pd->bonus->duration*1000; // the duration for pet bonuses to be in effect } else { //Lost pet... diff --git a/src/map/script.c b/src/map/script.c index 11ee56180..b6e3b8198 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -27721,6 +27721,15 @@ static void script_hardcoded_constants(void) script->set_constant("PET_HUNGER_SATISFIED", PET_HUNGER_SATISFIED, false, false); script->set_constant("PET_HUNGER_STUFFED", PET_HUNGER_STUFFED, false, false); + script->constdb_comment("Pet intimacy levels"); + script->set_constant("PET_INTIMACY_NONE", PET_INTIMACY_NONE, false, false); + script->set_constant("PET_INTIMACY_AWKWARD", PET_INTIMACY_AWKWARD, false, false); + script->set_constant("PET_INTIMACY_SHY", PET_INTIMACY_SHY, false, false); + script->set_constant("PET_INTIMACY_NEUTRAL", PET_INTIMACY_NEUTRAL, false, false); + script->set_constant("PET_INTIMACY_CORDIAL", PET_INTIMACY_CORDIAL, false, false); + script->set_constant("PET_INTIMACY_LOYAL", PET_INTIMACY_LOYAL, false, false); + script->set_constant("PET_INTIMACY_MAX", PET_INTIMACY_MAX, false, false); + script->constdb_comment("monster skill states"); script->set_constant("MSS_ANY", MSS_ANY, false, false); script->set_constant("MSS_IDLE", MSS_IDLE, false, false); diff --git a/src/map/status.c b/src/map/status.c index 4e7094569..bee5645ad 100644 --- a/src/map/status.c +++ b/src/map/status.c @@ -2583,7 +2583,7 @@ static int status_calc_pc_(struct map_session_data *sd, enum e_status_calc_opt o struct pet_data *pd = sd->pd; if( pd && pd->petDB && pd->petDB->equip_script && pd->pet.intimate >= battle_config.pet_equip_min_friendly ) script->run(pd->petDB->equip_script,0,sd->bl.id,0); - if( pd && pd->pet.intimate > 0 && (!battle_config.pet_equip_required || pd->pet.equip > 0) && pd->state.skillbonus == 1 && pd->bonus ) + if (pd && pd->pet.intimate > PET_INTIMACY_NONE && (!battle_config.pet_equip_required || pd->pet.equip > 0) && pd->state.skillbonus == 1 && pd->bonus) pc->bonus(sd,pd->bonus->type, pd->bonus->val); } diff --git a/src/map/unit.c b/src/map/unit.c index 56ddb5917..015f755bb 100644 --- a/src/map/unit.c +++ b/src/map/unit.c @@ -898,7 +898,7 @@ static int unit_movepos(struct block_list *bl, short dst_x, short dst_y, int eas } else npc->untouch_areanpc(sd, bl->m, bl->x, bl->y); - if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 ) + if (sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > PET_INTIMACY_NONE) { // Check if pet needs to be teleported. [Skotlex] int flag = 0; struct block_list* pbl = &sd->pd->bl; @@ -2722,7 +2722,7 @@ static int unit_remove_map(struct block_list *bl, enum clr_type clrtype, const c case BL_PET: { struct pet_data *pd = BL_UCAST(BL_PET, bl); - if( pd->pet.intimate <= 0 && !(pd->msd && !pd->msd->state.active) ) { + if (pd->pet.intimate <= PET_INTIMACY_NONE && !(pd->msd && !pd->msd->state.active)) { //If logging out, this is deleted on unit->free clif->clearunit_area(bl,clrtype); map->delblock(bl); @@ -2936,7 +2936,7 @@ static int unit_free(struct block_list *bl, enum clr_type clrtype) aFree (pd->loot); pd->loot = NULL; } - if (pd->pet.intimate > 0) { + if (pd->pet.intimate > PET_INTIMACY_NONE) { intif->save_petdata(pd->pet.account_id,&pd->pet); } else { //Remove pet. -- cgit v1.2.3-70-g09d2 From fd324381de7e95188f3638a6d1b9192b1f8fe256 Mon Sep 17 00:00:00 2001 From: Kenpachi Developer Date: Sat, 15 Feb 2020 09:01:34 +0100 Subject: Adjust inter_pet_tosql() function to use prepared statement --- src/char/int_pet.c | 75 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 20 deletions(-) (limited to 'src/char') diff --git a/src/char/int_pet.c b/src/char/int_pet.c index 1c3ca16d6..9ca40d555 100644 --- a/src/char/int_pet.c +++ b/src/char/int_pet.c @@ -54,37 +54,72 @@ struct inter_pet_interface *inter_pet; static int inter_pet_tosql(const struct s_pet *p) { //`pet` (`pet_id`, `class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incubate`) - char esc_name[NAME_LENGTH*2+1];// escaped pet name - int pet_id = 0, hungry = 0, intimate = 0; + int pet_id = 0; nullpo_ret(p); - SQL->EscapeStringLen(inter->sql_handle, esc_name, p->name, strnlen(p->name, NAME_LENGTH)); - hungry = cap_value(p->hungry, PET_HUNGER_STARVING, PET_HUNGER_STUFFED); - intimate = cap_value(p->intimate, PET_INTIMACY_NONE, PET_INTIMACY_MAX); - - if (p->pet_id == 0) { - // New pet. - if (SQL_ERROR == SQL->Query(inter->sql_handle, "INSERT INTO `%s` " - "(`class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incubate`, `autofeed`) " - "VALUES ('%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')", - pet_db, p->class_, esc_name, p->account_id, p->char_id, p->level, p->egg_id, - p->equip, intimate, hungry, p->rename_flag, p->incubate, p->autofeed)) { - Sql_ShowDebug(inter->sql_handle); + struct SqlStmt *stmt = SQL->StmtMalloc(inter->sql_handle); + + if (stmt == NULL) { + SqlStmt_ShowDebug(stmt); + return 0; + } + + if (p->pet_id == 0) { // New pet. + const char *query = "INSERT INTO `%s` " + "(`class`, `name`, `account_id`, `char_id`, `level`, `egg_id`, `equip`, `intimate`, `hungry`, `rename_flag`, `incubate`, `autofeed`) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + + if (SQL_ERROR == SQL->StmtPrepare(stmt, query, pet_db) || + SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_INT32, &p->class_, sizeof(p->class_)) || + SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_STRING, &p->name, strnlen(p->name, sizeof(p->name))) || + SQL_ERROR == SQL->StmtBindParam(stmt, 2, SQLDT_INT32, &p->account_id, sizeof(p->account_id)) || + SQL_ERROR == SQL->StmtBindParam(stmt, 3, SQLDT_INT32, &p->char_id, sizeof(p->char_id)) || + SQL_ERROR == SQL->StmtBindParam(stmt, 4, SQLDT_INT16, &p->level, sizeof(p->level)) || + SQL_ERROR == SQL->StmtBindParam(stmt, 5, SQLDT_INT32, &p->egg_id, sizeof(p->egg_id)) || + SQL_ERROR == SQL->StmtBindParam(stmt, 6, SQLDT_INT32, &p->equip, sizeof(p->equip)) || + SQL_ERROR == SQL->StmtBindParam(stmt, 7, SQLDT_INT16, &p->intimate, sizeof(p->intimate)) || + SQL_ERROR == SQL->StmtBindParam(stmt, 8, SQLDT_INT16, &p->hungry, sizeof(p->hungry)) || + SQL_ERROR == SQL->StmtBindParam(stmt, 9, SQLDT_INT8, &p->rename_flag, sizeof(p->rename_flag)) || + SQL_ERROR == SQL->StmtBindParam(stmt, 10, SQLDT_INT8, &p->incubate, sizeof(p->incubate)) || + SQL_ERROR == SQL->StmtBindParam(stmt, 11, SQLDT_INT32, &p->autofeed, sizeof(p->autofeed)) || + SQL_ERROR == SQL->StmtExecute(stmt)) { + SqlStmt_ShowDebug(stmt); + SQL->StmtFree(stmt); return 0; } + pet_id = (int)SQL->LastInsertId(inter->sql_handle); - } else { - // Update pet. - if (SQL_ERROR == SQL->Query(inter->sql_handle, "UPDATE `%s` SET `class`='%d',`name`='%s',`account_id`='%d',`char_id`='%d',`level`='%d',`egg_id`='%d',`equip`='%d',`intimate`='%d',`hungry`='%d',`rename_flag`='%d',`incubate`='%d', `autofeed`='%d' WHERE `pet_id`='%d'", - pet_db, p->class_, esc_name, p->account_id, p->char_id, p->level, p->egg_id, - p->equip, intimate, hungry, p->rename_flag, p->incubate, p->autofeed, p->pet_id)) { - Sql_ShowDebug(inter->sql_handle); + } else { // Update pet. + const char *query = "UPDATE `%s` SET " + "`class`=?, `name`=?, `account_id`=?, `char_id`=?, `level`=?, `egg_id`=?, `equip`=?, `intimate`=?, `hungry`=?, `rename_flag`=?, `incubate`=?, `autofeed`=? " + "WHERE `pet_id`=?"; + + if (SQL_ERROR == SQL->StmtPrepare(stmt, query, pet_db) || + SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_INT32, &p->class_, sizeof(p->class_)) || + SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_STRING, &p->name, strnlen(p->name, sizeof(p->name))) || + SQL_ERROR == SQL->StmtBindParam(stmt, 2, SQLDT_INT32, &p->account_id, sizeof(p->account_id)) || + SQL_ERROR == SQL->StmtBindParam(stmt, 3, SQLDT_INT32, &p->char_id, sizeof(p->char_id)) || + SQL_ERROR == SQL->StmtBindParam(stmt, 4, SQLDT_INT16, &p->level, sizeof(p->level)) || + SQL_ERROR == SQL->StmtBindParam(stmt, 5, SQLDT_INT32, &p->egg_id, sizeof(p->egg_id)) || + SQL_ERROR == SQL->StmtBindParam(stmt, 6, SQLDT_INT32, &p->equip, sizeof(p->equip)) || + SQL_ERROR == SQL->StmtBindParam(stmt, 7, SQLDT_INT16, &p->intimate, sizeof(p->intimate)) || + SQL_ERROR == SQL->StmtBindParam(stmt, 8, SQLDT_INT16, &p->hungry, sizeof(p->hungry)) || + SQL_ERROR == SQL->StmtBindParam(stmt, 9, SQLDT_INT8, &p->rename_flag, sizeof(p->rename_flag)) || + SQL_ERROR == SQL->StmtBindParam(stmt, 10, SQLDT_INT8, &p->incubate, sizeof(p->incubate)) || + SQL_ERROR == SQL->StmtBindParam(stmt, 11, SQLDT_INT32, &p->autofeed, sizeof(p->autofeed)) || + SQL_ERROR == SQL->StmtBindParam(stmt, 12, SQLDT_INT32, &p->pet_id, sizeof(p->pet_id)) || + SQL_ERROR == SQL->StmtExecute(stmt)) { + SqlStmt_ShowDebug(stmt); + SQL->StmtFree(stmt); return 0; } + pet_id = p->pet_id; } + SQL->StmtFree(stmt); + if (chr->show_save_log) ShowInfo("Pet saved %d - %s.\n", pet_id, p->name); -- cgit v1.2.3-70-g09d2 From 70edf6f2d4f042b470cb1f62e408c076f5c8d76b Mon Sep 17 00:00:00 2001 From: Kenpachi Developer Date: Sat, 15 Feb 2020 09:02:30 +0100 Subject: Adjust inter_pet_fromsql() function to use prepared statement --- src/char/int_pet.c | 71 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 28 deletions(-) (limited to 'src/char') diff --git a/src/char/int_pet.c b/src/char/int_pet.c index 9ca40d555..e1ecae766 100644 --- a/src/char/int_pet.c +++ b/src/char/int_pet.c @@ -128,8 +128,12 @@ static int inter_pet_tosql(const struct s_pet *p) static int inter_pet_fromsql(int pet_id, struct s_pet *p) { - char* data; - size_t len; + struct SqlStmt *stmt = SQL->StmtMalloc(inter->sql_handle); + + if (stmt == NULL) { + SqlStmt_ShowDebug(stmt); + return 0; + } #ifdef NOISY ShowInfo("Loading pet (%d)...\n",pet_id); @@ -139,36 +143,47 @@ static int inter_pet_fromsql(int pet_id, struct s_pet *p) //`pet` (`pet_id`, `class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incubate`, `autofeed`) - if( SQL_ERROR == SQL->Query(inter->sql_handle, "SELECT `pet_id`, `class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incubate`,`autofeed` FROM `%s` WHERE `pet_id`='%d'", pet_db, pet_id) ) - { - Sql_ShowDebug(inter->sql_handle); + const char *query = "SELECT " + "`class`, `name`, `account_id`, `char_id`, `level`, `egg_id`, `equip`, `intimate`, `hungry`, `rename_flag`, `incubate`, `autofeed` " + "FROM `%s` WHERE `pet_id`=?"; + + if (SQL_ERROR == SQL->StmtPrepare(stmt, query, pet_db) || + SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_INT32, &pet_id, sizeof(pet_id)) || + SQL_ERROR == SQL->StmtExecute(stmt) || + SQL_ERROR == SQL->StmtBindColumn(stmt, 0, SQLDT_INT32, &p->class_, sizeof(p->class_), NULL, NULL) || + SQL_ERROR == SQL->StmtBindColumn(stmt, 1, SQLDT_STRING, &p->name, sizeof(p->name), NULL, NULL) || + SQL_ERROR == SQL->StmtBindColumn(stmt, 2, SQLDT_INT32, &p->account_id, sizeof(p->account_id), NULL, NULL) || + SQL_ERROR == SQL->StmtBindColumn(stmt, 3, SQLDT_INT32, &p->char_id, sizeof(p->char_id), NULL, NULL) || + SQL_ERROR == SQL->StmtBindColumn(stmt, 4, SQLDT_INT16, &p->level, sizeof(p->level), NULL, NULL) || + SQL_ERROR == SQL->StmtBindColumn(stmt, 5, SQLDT_INT32, &p->egg_id, sizeof(p->egg_id), NULL, NULL) || + SQL_ERROR == SQL->StmtBindColumn(stmt, 6, SQLDT_INT32, &p->equip, sizeof(p->equip), NULL, NULL) || + SQL_ERROR == SQL->StmtBindColumn(stmt, 7, SQLDT_INT16, &p->intimate, sizeof(p->intimate), NULL, NULL) || + SQL_ERROR == SQL->StmtBindColumn(stmt, 8, SQLDT_INT16, &p->hungry, sizeof(p->hungry), NULL, NULL) || + SQL_ERROR == SQL->StmtBindColumn(stmt, 9, SQLDT_INT8, &p->rename_flag, sizeof(p->rename_flag), NULL, NULL) || + SQL_ERROR == SQL->StmtBindColumn(stmt, 10, SQLDT_INT8, &p->incubate, sizeof(p->incubate), NULL, NULL) || + SQL_ERROR == SQL->StmtBindColumn(stmt, 11, SQLDT_INT32, &p->autofeed, sizeof(p->autofeed), NULL, NULL)) { + SqlStmt_ShowDebug(stmt); + SQL->StmtFree(stmt); + return 0; + } + + if (SQL->StmtNumRows(stmt) < 1) { + ShowError("inter_pet_fromsql: Requested non-existant pet ID: %d\n", pet_id); + SQL->StmtFree(stmt); return 0; } - if( SQL_SUCCESS == SQL->NextRow(inter->sql_handle) ) - { - p->pet_id = pet_id; - SQL->GetData(inter->sql_handle, 1, &data, NULL); p->class_ = atoi(data); - SQL->GetData(inter->sql_handle, 2, &data, &len); memcpy(p->name, data, min(len, NAME_LENGTH)); - SQL->GetData(inter->sql_handle, 3, &data, NULL); p->account_id = atoi(data); - SQL->GetData(inter->sql_handle, 4, &data, NULL); p->char_id = atoi(data); - SQL->GetData(inter->sql_handle, 5, &data, NULL); p->level = atoi(data); - SQL->GetData(inter->sql_handle, 6, &data, NULL); p->egg_id = atoi(data); - SQL->GetData(inter->sql_handle, 7, &data, NULL); p->equip = atoi(data); - SQL->GetData(inter->sql_handle, 8, &data, NULL); p->intimate = atoi(data); - SQL->GetData(inter->sql_handle, 9, &data, NULL); p->hungry = atoi(data); - SQL->GetData(inter->sql_handle, 10, &data, NULL); p->rename_flag = atoi(data); - SQL->GetData(inter->sql_handle, 11, &data, NULL); p->incubate = atoi(data); - SQL->GetData(inter->sql_handle, 12, &data, NULL); p->autofeed = atoi(data); - - SQL->FreeResult(inter->sql_handle); - - p->hungry = cap_value(p->hungry, PET_HUNGER_STARVING, PET_HUNGER_STUFFED); - p->intimate = cap_value(p->intimate, PET_INTIMACY_NONE, PET_INTIMACY_MAX); - - if (chr->show_save_log) - ShowInfo("Pet loaded (%d - %s).\n", pet_id, p->name); + if (SQL_ERROR == SQL->StmtNextRow(stmt)) { + SqlStmt_ShowDebug(stmt); + SQL->StmtFree(stmt); + return 0; } + + SQL->StmtFree(stmt); + + if (chr->show_save_log) + ShowInfo("Pet loaded %d - %s.\n", pet_id, p->name); + return 0; } //---------------------------------------------- -- cgit v1.2.3-70-g09d2 From ea824742822a2e17b2b83c77d6b28bf0a12aacf4 Mon Sep 17 00:00:00 2001 From: Kenpachi Developer Date: Sat, 15 Feb 2020 09:20:50 +0100 Subject: Apply code style to inter_pet_tosql() function --- src/char/int_pet.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'src/char') diff --git a/src/char/int_pet.c b/src/char/int_pet.c index e1ecae766..2ba31b43b 100644 --- a/src/char/int_pet.c +++ b/src/char/int_pet.c @@ -43,19 +43,17 @@ struct inter_pet_interface *inter_pet; /** * Saves a pet to the SQL database. * - * @remark - * In case of newly created pet, the pet ID is not updated to reflect the - * newly assigned ID. The caller must do so. + * Table structure: + * `pet` (`pet_id`, `class`, `name`, `account_id`, `char_id`, `level`, `egg_id`, `equip`, `intimate`, `hungry`, `rename_flag`, `incubate`, `autofeed`) + * + * @remark In case of newly created pet, the pet ID is not updated to reflect the newly assigned ID. The caller must do so. * * @param p The pet data to save. - * @return The ID of the saved pet. - * @retval 0 in case of errors. - */ + * @return The ID of the saved pet, or 0 in case of errors. + * + **/ static int inter_pet_tosql(const struct s_pet *p) { - //`pet` (`pet_id`, `class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incubate`) - int pet_id = 0; - nullpo_ret(p); struct SqlStmt *stmt = SQL->StmtMalloc(inter->sql_handle); @@ -65,9 +63,12 @@ static int inter_pet_tosql(const struct s_pet *p) return 0; } + int pet_id = 0; + if (p->pet_id == 0) { // New pet. const char *query = "INSERT INTO `%s` " - "(`class`, `name`, `account_id`, `char_id`, `level`, `egg_id`, `equip`, `intimate`, `hungry`, `rename_flag`, `incubate`, `autofeed`) " + "(`class`, `name`, `account_id`, `char_id`, `level`, `egg_id`, `equip`, " + "`intimate`, `hungry`, `rename_flag`, `incubate`, `autofeed`) " "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; if (SQL_ERROR == SQL->StmtPrepare(stmt, query, pet_db) || @@ -92,7 +93,8 @@ static int inter_pet_tosql(const struct s_pet *p) pet_id = (int)SQL->LastInsertId(inter->sql_handle); } else { // Update pet. const char *query = "UPDATE `%s` SET " - "`class`=?, `name`=?, `account_id`=?, `char_id`=?, `level`=?, `egg_id`=?, `equip`=?, `intimate`=?, `hungry`=?, `rename_flag`=?, `incubate`=?, `autofeed`=? " + "`class`=?, `name`=?, `account_id`=?, `char_id`=?, `level`=?, `egg_id`=?, `equip`=?, " + "`intimate`=?, `hungry`=?, `rename_flag`=?, `incubate`=?, `autofeed`=? " "WHERE `pet_id`=?"; if (SQL_ERROR == SQL->StmtPrepare(stmt, query, pet_db) || -- cgit v1.2.3-70-g09d2 From 5c99148b945ed4fe6c01af05bcf390de8df13505 Mon Sep 17 00:00:00 2001 From: Kenpachi Developer Date: Sat, 15 Feb 2020 09:24:02 +0100 Subject: Apply code style to inter_pet_fromsql() function --- src/char/int_pet.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src/char') diff --git a/src/char/int_pet.c b/src/char/int_pet.c index 2ba31b43b..9b81869d2 100644 --- a/src/char/int_pet.c +++ b/src/char/int_pet.c @@ -128,8 +128,21 @@ static int inter_pet_tosql(const struct s_pet *p) return pet_id; } +/** + * Loads a pet's data from the SQL database. + * + * Table structure: + * `pet` (`pet_id`, `class`, `name`, `account_id`, `char_id`, `level`, `egg_id`, `equip`, `intimate`, `hungry`, `rename_flag`, `incubate`, `autofeed`) + * + * @param pet_id The pet's ID. + * @param p The pet data to save the SQL data in. + * @return Always 0. + * + **/ static int inter_pet_fromsql(int pet_id, struct s_pet *p) { + nullpo_ret(p); + struct SqlStmt *stmt = SQL->StmtMalloc(inter->sql_handle); if (stmt == NULL) { @@ -140,13 +153,12 @@ static int inter_pet_fromsql(int pet_id, struct s_pet *p) #ifdef NOISY ShowInfo("Loading pet (%d)...\n",pet_id); #endif - nullpo_ret(p); - memset(p, 0, sizeof(struct s_pet)); - //`pet` (`pet_id`, `class`,`name`,`account_id`,`char_id`,`level`,`egg_id`,`equip`,`intimate`,`hungry`,`rename_flag`,`incubate`, `autofeed`) + memset(p, 0, sizeof(struct s_pet)); const char *query = "SELECT " - "`class`, `name`, `account_id`, `char_id`, `level`, `egg_id`, `equip`, `intimate`, `hungry`, `rename_flag`, `incubate`, `autofeed` " + "`class`, `name`, `account_id`, `char_id`, `level`, `egg_id`, `equip`, " + "`intimate`, `hungry`, `rename_flag`, `incubate`, `autofeed` " "FROM `%s` WHERE `pet_id`=?"; if (SQL_ERROR == SQL->StmtPrepare(stmt, query, pet_db) || -- cgit v1.2.3-70-g09d2 From 3ec0863d06c804028cc2caf237c60bb4d7b5a6a7 Mon Sep 17 00:00:00 2001 From: Kenpachi Developer Date: Sat, 15 Feb 2020 10:08:24 +0100 Subject: Apply code style to inter_pet_create() function --- src/char/int_pet.c | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) (limited to 'src/char') diff --git a/src/char/int_pet.c b/src/char/int_pet.c index 9b81869d2..176025f13 100644 --- a/src/char/int_pet.c +++ b/src/char/int_pet.c @@ -224,18 +224,34 @@ static int inter_pet_delete(int pet_id) return 0; } //------------------------------------------------------ + +/** + * Creates a new pet and inserts its data into the `pet` SQL table. + * + * @param account_id The pet's master's account ID. + * @param char_id The pet's master's char ID. + * @param pet_class The pet's class/monster ID. + * @param pet_lv The pet's level. + * @param pet_egg_id The pet's egg's item ID. + * @param pet_equip The pet's equipment's item ID. + * @param intimate The pet's intimacy value. + * @param hungry The pet's hunger value. + * @param rename_flag The pet's rename flag. + * @param incubate The pet's incubate state. + * @param pet_name The pet's name. + * @return The created pet's data struct, or NULL in case of errors. + * + **/ static struct s_pet *inter_pet_create(int account_id, int char_id, int pet_class, int pet_lv, int pet_egg_id, - int pet_equip, short intimate, short hungry, char rename_flag, char incubate, const char *pet_name) + int pet_equip, short intimate, short hungry, char rename_flag, + char incubate, const char *pet_name) { - nullpo_ret(pet_name); + nullpo_retr(NULL, pet_name); + memset(inter_pet->pt, 0, sizeof(struct s_pet)); safestrncpy(inter_pet->pt->name, pet_name, NAME_LENGTH); - if(incubate == 1) - inter_pet->pt->account_id = inter_pet->pt->char_id = 0; - else { - inter_pet->pt->account_id = account_id; - inter_pet->pt->char_id = char_id; - } + inter_pet->pt->account_id = (incubate == 1) ? 0 : account_id; + inter_pet->pt->char_id = (incubate == 1) ? 0 : char_id; inter_pet->pt->class_ = pet_class; inter_pet->pt->level = pet_lv; inter_pet->pt->egg_id = pet_egg_id; @@ -244,12 +260,12 @@ static struct s_pet *inter_pet_create(int account_id, int char_id, int pet_class inter_pet->pt->hungry = cap_value(hungry, PET_HUNGER_STARVING, PET_HUNGER_STUFFED); inter_pet->pt->rename_flag = rename_flag; inter_pet->pt->incubate = incubate; + inter_pet->pt->pet_id = 0; // Signal NEW pet. - inter_pet->pt->pet_id = 0; //Signal NEW pet. if ((inter_pet->pt->pet_id = inter_pet->tosql(inter_pet->pt)) != 0) return inter_pet->pt; - else //Failed... - return NULL; + + return NULL; } static struct s_pet *inter_pet_load(int account_id, int char_id, int pet_id) -- cgit v1.2.3-70-g09d2