diff options
Diffstat (limited to 'src/account-server')
-rw-r--r-- | src/account-server/dalstorage.cpp | 131 | ||||
-rw-r--r-- | src/account-server/dalstorage.hpp | 21 | ||||
-rw-r--r-- | src/account-server/serverhandler.cpp | 41 | ||||
-rw-r--r-- | src/account-server/serverhandler.hpp | 8 |
4 files changed, 148 insertions, 53 deletions
diff --git a/src/account-server/dalstorage.cpp b/src/account-server/dalstorage.cpp index 06a1c83a..20815a7d 100644 --- a/src/account-server/dalstorage.cpp +++ b/src/account-server/dalstorage.cpp @@ -67,11 +67,6 @@ DALStorage::~DALStorage() /** * Connect to the database and initialize it if necessary. * - * TODO: <b>Exceptionfault:</b> after connecting to the database, we have to - * verify if the version matches a supported version. Maybe implement a - * "version table" to check after connect. Raise an error with verbose - * informations about the discrepancy between the versions. - * */ void DALStorage::open() { @@ -538,7 +533,8 @@ bool DALStorage::updateCharacter(Character *character, { for (unsigned int skill_id = 0; skill_id < CHAR_SKILL_NB; skill_id++) { - flushSkill(character, skill_id); + updateExperience(character->getDatabaseID(), skill_id, + character->getExperience(skill_id)); } } catch (const dal::DbSqlQueryExecFailure& e) @@ -635,57 +631,15 @@ bool DALStorage::updateCharacter(Character *character, /** * Save changes of a skill to the database permanently. + * @deprecated Use DALStorage::updateExperience instead!!! */ void DALStorage::flushSkill(const Character* const character, const int skill_id ) { - try - { - const unsigned int exp = character->getExperience(skill_id); - - // if experience has decreased to 0 we don't store is anymore, - // its the default - if (exp == 0) - { - std::ostringstream sql; - sql << "DELETE FROM " << CHAR_SKILLS_TBL_NAME << " " - << "WHERE char_id = '" << character->getDatabaseID() << "' " - << "AND skill_id = '" << skill_id << "'"; - mDb->execSql(sql.str()); - return; - } - - // try to update the skill - std::ostringstream sql; - sql << "UPDATE " << CHAR_SKILLS_TBL_NAME << " " - << "SET skill_exp = '" << exp << "' " - << "WHERE char_id = '" << character->getDatabaseID() << "' " - << "AND skill_id = '" << skill_id << "'"; - mDb->execSql(sql.str()); - - // check if the update has modified a row - if (mDb->getModifiedRows() > 0) - { - return; - } - - sql.clear(); - sql.str(""); - sql << "INSERT INTO " << CHAR_SKILLS_TBL_NAME << " " - << "(char_id, skill_id, skill_exp) VALUES ( " - << "'" << character->getDatabaseID() << "', " - << "'" << skill_id << "', " - << "'" << exp << "' )"; - mDb->execSql(sql.str()); - } - catch (const dal::DbSqlQueryExecFailure &e) - { - LOG_ERROR("DALStorage::flushSkill: " << e.what()); - throw; - } + updateExperience(character->getDatabaseID(), skill_id, + character->getExperience(skill_id)); } - /** * Add an account to the database. */ @@ -804,7 +758,8 @@ void DALStorage::flush(Account *account) // update the characters skills for (unsigned int skill_id = 0; skill_id < CHAR_SKILL_NB; skill_id++) { - flushSkill((*it), skill_id); + updateExperience((*it)->getDatabaseID(), skill_id, + (*it)->getExperience(skill_id)); } } } // @@ -886,6 +841,78 @@ void DALStorage::updateLastLogin(const Account *account) mDb->execSql(sql.str()); } +void DALStorage::updateCharacterPoints(const int CharId, const int CharPoints, + const int CorrPoints, const int AttribId, const int AttribValue ) +{ + std::ostringstream sql; + sql << "UPDATE " << CHARACTERS_TBL_NAME + << " SET char_pts = " << CharPoints << ", " + << " correct_pts = " << CorrPoints << ", "; + + switch (AttribId) + { + case CHAR_ATTR_STRENGTH: sql << "str = "; break; + case CHAR_ATTR_AGILITY: sql << "agi = "; break; + case CHAR_ATTR_DEXTERITY: sql << "dex = "; break; + case CHAR_ATTR_VITALITY: sql << "vit = "; break; + case CHAR_ATTR_INTELLIGENCE: sql << "int = "; break; + case CHAR_ATTR_WILLPOWER: sql << "will = "; break; + } + sql << AttribValue + << " WHERE id = " << CharId; + + mDb->execSql(sql.str()); +} + +void DALStorage::updateExperience(const int CharId, const int SkillId, + const int SkillValue) +{ + try + { + // if experience has decreased to 0 we don't store it anymore, + // its the default + if (SkillValue == 0) + { + std::ostringstream sql; + sql << "DELETE FROM " << CHAR_SKILLS_TBL_NAME + << " WHERE char_id = " << CharId + << " AND skill_id = " << SkillId; + mDb->execSql(sql.str()); + return; + } + + // try to update the skill + std::ostringstream sql; + sql << "UPDATE " << CHAR_SKILLS_TBL_NAME + << " SET skill_exp = " << SkillValue + << " WHERE char_id = " << CharId + << " AND skill_id = " << SkillId; + mDb->execSql(sql.str()); + + // check if the update has modified a row + if (mDb->getModifiedRows() > 0) + { + return; + } + + sql.clear(); + sql.str(""); + sql << "INSERT INTO " << CHAR_SKILLS_TBL_NAME << " " + << "(char_id, skill_id, skill_exp) VALUES ( " + << CharId << ", " + << SkillId << ", " + << SkillValue << ")"; + mDb->execSql(sql.str()); + } + catch (const dal::DbSqlQueryExecFailure &e) + { + LOG_ERROR("DALStorage::updateExperience: " << e.what()); + throw; + } +} + + + /** * Add a guild */ diff --git a/src/account-server/dalstorage.hpp b/src/account-server/dalstorage.hpp index e0cfead5..896151d1 100644 --- a/src/account-server/dalstorage.hpp +++ b/src/account-server/dalstorage.hpp @@ -123,6 +123,27 @@ class DALStorage void updateLastLogin(const Account *account); /** + * Write a modification message about Character points to the database. + * + * @param CharId ID of the character + * @param CharPoints Number of character points left for the character + * @param CorrPoints Number of correction points left for the character + * @param AttribId ID of the modified attribute + * @param AttribValue New value of the modified attribute + */ + void updateCharacterPoints(const int CharId, const int CharPoints, + const int CorrPoints, const int AttribId, const int AttribValue ); + + /** + * Write a modification message about character skills to the database. + * @param CharId ID of the character + * @param SkillId ID of the skill + * @param SkillValue new skill points + */ + void updateExperience(const int CharId, const int SkillId, + const int SkillValue); + + /** * Sets a ban on an account (hence on all its characters). * * @param id character identifier. diff --git a/src/account-server/serverhandler.cpp b/src/account-server/serverhandler.cpp index bdbdb1f6..1c4a5219 100644 --- a/src/account-server/serverhandler.cpp +++ b/src/account-server/serverhandler.cpp @@ -31,7 +31,6 @@ #include "account-server/dalstorage.hpp" #include "chat-server/post.hpp" #include "net/connectionhandler.hpp" -#include "net/messagein.hpp" #include "net/messageout.hpp" #include "net/netcomputer.hpp" #include "serialize/characterdata.hpp" @@ -241,6 +240,12 @@ void ServerHandler::processMessage(NetComputer *comp, MessageIn &msg) } } break; + case GAMSG_PLAYER_SYNC: + { + LOG_DEBUG("GAMSG_PLAYER_SYNC"); + GameServerHandler::syncDatabase(msg); + } break; + case GAMSG_REDIRECT: { LOG_DEBUG("GAMSG_REDIRECT"); @@ -511,3 +516,37 @@ void GameServerHandler::sendPartyChange(Character *ptr, int partyId) s->send(msg); } } + +void GameServerHandler::syncDatabase(MessageIn &msg) +{ + int msgType = msg.readByte(); + while( msgType != SYNC_END_OF_BUFFER ) + { + switch (msgType) + { + case SYNC_CHARACTER_POINTS: + { + LOG_DEBUG("received SYNC_CHARACTER_POINTS"); + int CharId = msg.readLong(); + int CharPoints = msg.readLong(); + int CorrPoints = msg.readLong(); + int AttribId = msg.readByte(); + int AttribValue = msg.readLong(); + storage->updateCharacterPoints(CharId, CharPoints, CorrPoints, + AttribId, AttribValue); + } break; + + case SYNC_CHARACTER_SKILL: + { + LOG_DEBUG("received SYNC_CHARACTER_SKILL"); + int CharId = msg.readLong(); + int SkillId = msg.readByte(); + int SkillValue = msg.readLong(); + storage->updateExperience(CharId, SkillId, SkillValue); + } break; + } + + // read next message type from buffer + msgType = msg.readByte(); + } +} diff --git a/src/account-server/serverhandler.hpp b/src/account-server/serverhandler.hpp index 0d75219c..646ebf4c 100644 --- a/src/account-server/serverhandler.hpp +++ b/src/account-server/serverhandler.hpp @@ -25,6 +25,8 @@ #include <iosfwd> #include <string> +#include "net/messagein.hpp" + class Character; namespace GameServerHandler @@ -64,6 +66,12 @@ namespace GameServerHandler * Sends chat party information */ void sendPartyChange(Character *ptr, int partyId); + + /** + * Takes a GAMSG_PLAYER_SYNC from the gameserver and stores all changes in + * the database. + */ + void syncDatabase(MessageIn &msg); } #endif |