From f5fdb19e7aac292cca31ec23250587e0d97d0ff6 Mon Sep 17 00:00:00 2001 From: Chuck Miller Date: Thu, 1 Oct 2009 00:26:38 -0400 Subject: Adds code for saving and getting status effects from the database --- src/account-server/character.hpp | 16 ++++++++ src/account-server/dalstorage.cpp | 77 +++++++++++++++++++++++++++++++++++++++ src/account-server/dalstorage.hpp | 8 ++++ 3 files changed, 101 insertions(+) (limited to 'src/account-server') diff --git a/src/account-server/character.hpp b/src/account-server/character.hpp index d38d0211..32d8a8a6 100644 --- a/src/account-server/character.hpp +++ b/src/account-server/character.hpp @@ -124,6 +124,21 @@ class Character void receiveExperience(int skill, int value) { mExperience[skill] += value; } + /** + * Get / Set a status effects + */ + void applyStatusEffect(int id, int time) + { mStatusEffects[id] = time; } + + int getStatusEffectSize() const + { return mStatusEffects.size(); } + + const std::map::const_iterator getStatusEffectBegin() const + { return mStatusEffects.begin(); } + + const std::map::const_iterator getStatusEffectEnd() const + { return mStatusEffects.end(); } + /** * Gets the Id of the map that the character is on. */ @@ -180,6 +195,7 @@ class Character Point mPos; //!< Position the being is at. unsigned short mAttributes[CHAR_ATTR_NB]; //!< Attributes. std::map mExperience; //!< Skill Experience. + std::map mStatusEffects; //!< Status Effects unsigned short mMapId; //!< Map the being is on. unsigned char mGender; //!< Gender of the being. unsigned char mHairStyle; //!< Hair style of the being. diff --git a/src/account-server/dalstorage.cpp b/src/account-server/dalstorage.cpp index 4628eca5..1f71d40c 100644 --- a/src/account-server/dalstorage.cpp +++ b/src/account-server/dalstorage.cpp @@ -314,6 +314,22 @@ Character *DALStorage::getCharacterBySQL(Account *owner) toUint(skillInfo(row, 1))); // experience } } + s.clear(); + s.str(""); + // Load the status effect + s << "select status_id, status_time FROM " << CHAR_STATUS_EFFECTS_TBL_NAME + << " WHERE char_id = " << character->getDatabaseID(); + const dal::RecordSet &statusInfo = mDb->execSql(s.str()); + if (!statusInfo.isEmpty()) + { + const unsigned int nRows = statusInfo.rows(); + for (unsigned int row = 0; row < nRows; row++) + { + character->applyStatusEffect( + toUint(statusInfo(row, 0)), // Statusid + toUint(statusInfo(row, 1))); // Time + } + } } catch (const dal::DbSqlQueryExecFailure &e) { @@ -670,6 +686,48 @@ bool DALStorage::updateCharacter(Character *character, return false; } + /** + * Update char status effects + */ + try + { + // Delete the old status effects first + std::ostringstream sql; + + sql << "delete from " << CHAR_STATUS_EFFECTS_TBL_NAME + << " where char_id = '" << character->getDatabaseID() << "';"; + + mDb->execSql(sql.str()); + } + catch (const dal::DbSqlQueryExecFailure& e) + { + // TODO: throw an exception. + if (startTransaction) + { + mDb->rollbackTransaction(); + } + LOG_ERROR("(DALStorage::updateCharacter #5) SQL query failure: " << e.what()); + return false; + } + try + { + std::map::const_iterator status_it; + for (status_it = character->getStatusEffectBegin(); + status_it != character->getStatusEffectEnd(); status_it++) + { + insertStatusEffect(character->getDatabaseID(), status_it->first, status_it->second); + } + } + catch (const dal::DbSqlQueryExecFailure& e) + { + // TODO: throw an exception + if (startTransaction) + { + mDb->rollbackTransaction(); + } + LOG_ERROR("(DALStorage::updateCharacter #6) SQL query failure: " << e.what()); + return false; + } if (startTransaction) { mDb->commitTransaction(); @@ -972,6 +1030,25 @@ void DALStorage::updateExperience(const int CharId, const int SkillId, } } +void DALStorage::insertStatusEffect(const int charId, const int statusId, const int time) +{ + try + { + std::ostringstream sql; + + sql << "insert into " << CHAR_STATUS_EFFECTS_TBL_NAME + << " (char_id, status_id, status_time) VALUES ( " + << charId << ", " + << statusId << ", " + << time << ")"; + mDb->execSql(sql.str()); + } + catch (const dal::DbSqlQueryExecFailure &e) + { + LOG_ERROR("DALStorage::insertStatusEffect: " << e.what()); + throw; + } +} /** diff --git a/src/account-server/dalstorage.hpp b/src/account-server/dalstorage.hpp index 77222377..36bb1e39 100644 --- a/src/account-server/dalstorage.hpp +++ b/src/account-server/dalstorage.hpp @@ -146,6 +146,14 @@ class DALStorage void updateExperience(const int CharId, const int SkillId, const int SkillValue); + /** + * Inserts a record about a status effect into the database + * @param charId ID of the character in the database + * @param statusId ID of the status effect + * @param time Time left on the status effect + */ + void insertStatusEffect(const int charId, const int statusId, const int time); + /** * Sets a ban on an account (hence on all its characters). * -- cgit v1.2.3-60-g2f50