From c7189a5cefabd5b0bb365e0497aa60aa30f1fa50 Mon Sep 17 00:00:00 2001 From: kamilprzyb Date: Sun, 13 Sep 2020 20:45:51 +0200 Subject: [PATCH] added saving missions --- src/Database.cpp | 56 ++++++++++++++++++++++++++++++++++++------ src/Database.hpp | 1 + src/MissionManager.cpp | 14 +++++++++++ src/MissionManager.hpp | 2 ++ src/Player.hpp | 1 + src/PlayerManager.cpp | 5 ++++ 6 files changed, 71 insertions(+), 8 deletions(-) diff --git a/src/Database.cpp b/src/Database.cpp index 88cbda7..bcd2f1a 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -8,6 +8,7 @@ #include "Player.hpp" #include "CNStructs.hpp" #include "contrib/sqlite/sqlite_orm.h" +#include "MissionManager.hpp" using namespace sqlite_orm; @@ -50,7 +51,7 @@ auto db = make_storage("database.db", make_column("isGM", &Database::DbPlayer::isGM), make_column("FusionMatter", &Database::DbPlayer::FusionMatter), make_column("Taros", &Database::DbPlayer::Taros), - make_column("PCState", &Database::DbPlayer::PCState) + make_column("Quests", &Database::DbPlayer::QuestFlag) ), make_table("Inventory", make_column("PlayerId", &Database::Inventory::playerId), @@ -156,6 +157,7 @@ int Database::createCharacter(sP_CL2LS_REQ_SAVE_CHAR_NAME* save, int AccountID) create.y_coordinates= settings::SPAWN_Y; create.z_coordinates= settings::SPAWN_Z; create.angle = settings::SPAWN_ANGLE; + create.QuestFlag = std::vector(); return db.insert(create); } @@ -201,9 +203,9 @@ void Database::finishCharacter(sP_CL2LS_REQ_CHAR_CREATE* character) void Database::finishTutorial(int PlayerID) { - //set flag - DbPlayer finish = getDbPlayerById(PlayerID); - finish.TutorialFlag = 1; + Player finish = getPlayer(PlayerID); + //set flag + finish.PCStyle2.iTutorialFlag= 1; //add Gun Inventory LightningGun = {}; LightningGun.playerId = PlayerID; @@ -218,9 +220,13 @@ void Database::finishTutorial(int PlayerID) Buttercup.iID = 1; Buttercup.iSkillID = 1; Buttercup.iStamina = 150; - finish.Nano1 = 1; + finish.equippedNanos[0] = 1; db.insert(Buttercup); - db.update(finish); + //save missions + MissionManager::saveMission(&finish, 0); + MissionManager::saveMission(&finish, 1); + + db.update(playerToDb(finish)); } int Database::deleteCharacter(int characterID) @@ -303,6 +309,23 @@ Database::DbPlayer Database::playerToDb(Player player) result.Nano1 = player.equippedNanos[0]; result.Nano2 = player.equippedNanos[1]; result.Nano3 = player.equippedNanos[2]; + + //quests + result.QuestFlag = std::vector(); + //parsing long array to char vector + for (int i=0; i<16; i++) + { + int64_t temp = player.aQuestFlag[i]; + for (int j = 0; j < 8; j++) { + int64_t check2 = (temp >> (8 * (7 - j))); + char toadd = check2; + result.QuestFlag.push_back( + toadd + ); + } + } + + return result; } @@ -345,6 +368,23 @@ Player Database::DbToPlayer(DbPlayer player) { Database::getInventory(&result); Database::getNanos(&result); + + std::vector::iterator it = player.QuestFlag.begin(); + for (int i = 0; i < 16; i++) + { + if (it == player.QuestFlag.end()) + break; + + int64_t toAdd = 0; + for (int j = 0; j < 8; j++) { + int64_t temp = *it; + int64_t check2 = (temp << (8 * (7 - j))); + toAdd += check2; + it++; + } + result.aQuestFlag[i] = toAdd; + } + return result; } @@ -370,10 +410,10 @@ void Database::updatePlayer(Player player) { updateNanos(player); } -void Database::updateInventory(Player player) { +void Database::updateInventory(Player player){ //start transaction db.begin_transaction(); - //remove all previous items + //remove all db.remove_all( where(c(&Inventory::playerId) == player.iID) ); diff --git a/src/Database.hpp b/src/Database.hpp index 88d79f4..c3fc191 100644 --- a/src/Database.hpp +++ b/src/Database.hpp @@ -62,6 +62,7 @@ namespace Database { int z_coordinates; int angle; short int PCState; + std::vector QuestFlag; }; diff --git a/src/MissionManager.cpp b/src/MissionManager.cpp index a298568..ed39106 100644 --- a/src/MissionManager.cpp +++ b/src/MissionManager.cpp @@ -100,6 +100,13 @@ void MissionManager::taskEnd(CNSocket* sock, CNPacketData* data) { std::cout << "[WARN] Player completed non-active mission!?" << std::endl; } + // if it's the last task + if (task["m_iSUOutgoingTask"] == 0) + { + //save completed mission on player + saveMission(plr, (int)(task["m_iHMissionID"])-1); + } + sock->sendPacket((void*)&response, P_FE2CL_REP_PC_TASK_END_SUCC, sizeof(sP_FE2CL_REP_PC_TASK_END_SUCC)); } @@ -323,3 +330,10 @@ void MissionManager::mobKilled(CNSocket *sock, int mobid) { sock->sendPacket((void*)&kill, P_FE2CL_REP_PC_KILL_QUEST_NPCs_SUCC, sizeof(sP_FE2CL_REP_PC_KILL_QUEST_NPCs_SUCC)); } } + +void MissionManager::saveMission(Player* player, int missionId) { + //Missions are stored in int_64t array + int row = missionId / 64; + int collumn = missionId % 64; + player->aQuestFlag[row] |= (1LL << collumn); +} diff --git a/src/MissionManager.hpp b/src/MissionManager.hpp index 704aada..4105514 100644 --- a/src/MissionManager.hpp +++ b/src/MissionManager.hpp @@ -50,4 +50,6 @@ namespace MissionManager { int giveMissionReward(CNSocket *sock, int task); void mobKilled(CNSocket *sock, int mobid); + + void saveMission(Player* player, int missionId); } diff --git a/src/Player.hpp b/src/Player.hpp index 938b690..7bb514c 100644 --- a/src/Player.hpp +++ b/src/Player.hpp @@ -38,6 +38,7 @@ struct Player { bool isTradeConfirm; bool IsGM; + int64_t aQuestFlag[16]; int tasks[ACTIVE_MISSION_COUNT]; sItemBase QInven[AQINVEN_COUNT]; }; diff --git a/src/PlayerManager.cpp b/src/PlayerManager.cpp index dcb6ae8..f6416c2 100644 --- a/src/PlayerManager.cpp +++ b/src/PlayerManager.cpp @@ -235,6 +235,11 @@ void PlayerManager::enterPlayer(CNSocket* sock, CNPacketData* data) { } response.PCLoadData2CL.aQuestFlag[0] = -1; + //missions + for (int i = 0; i < 16; i++) { + response.PCLoadData2CL.aQuestFlag[i] = plr.aQuestFlag[i]; + } + // shut Computress up response.PCLoadData2CL.iFirstUseFlag1 = UINT64_MAX; response.PCLoadData2CL.iFirstUseFlag2 = UINT64_MAX;