added saving character when a connection with shard is killed

This commit is contained in:
kamilprzyb 2020-09-03 00:22:00 +02:00 committed by dongresource
parent 3cfec7aab3
commit fc57cae37d
6 changed files with 70 additions and 45 deletions

View File

@ -100,7 +100,7 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
resp.iPaymentFlag = 1;
resp.iOpenBetaFlag = 0;
resp.uiSvrTime = getTime();
// send the resp in with original key
sock->sendPacket((void*)&resp, P_LS2CL_REP_LOGIN_SUCC, sizeof(sP_LS2CL_REP_LOGIN_SUCC));
@ -135,13 +135,6 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
charInfo.aEquip[i] = it->Equip[i];
}
for (int i = 5; i < AEQUIP_COUNT; i++) {
// empty equips
charInfo.aEquip[i].iID = 0;
charInfo.aEquip[i].iType = 0;
charInfo.aEquip[i].iOpt = 0;
}
// set default to the first character
if (it == characters.begin())
loginSessions[sock].selectedChar = UID;
@ -291,8 +284,7 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
DEBUGLOG(
std::cout << "P_CL2LS_REQ_CHAR_SELECT:" << std::endl;
std::cout << "\tPC_UID: " << chararacter->iPC_UID << std::endl;
)
)
loginSessions[sock].selectedChar = chararacter->iPC_UID;
Database::updateSelected(loginSessions[sock].userID, loginSessions[sock].characters[chararacter->iPC_UID].slot);
sock->sendPacket((void*)&resp, P_LS2CL_REP_CHAR_SELECT_SUCC, sizeof(sP_LS2CL_REP_CHAR_SELECT_SUCC));

View File

@ -4,6 +4,7 @@
#include "PlayerManager.hpp"
#include "CNShared.hpp"
#include "settings.hpp"
#include "Database.hpp"
#include <iostream>
#include <sstream>
@ -52,6 +53,9 @@ void CNShardServer::killConnection(CNSocket* cns) {
if (PlayerManager::players.find(cns) == PlayerManager::players.end())
return;
//save player to DB
Database::updatePlayer(*PlayerManager::players[cns].plr);
// remove from CNSharedData
int64_t key = PlayerManager::getPlayer(cns)->SerialKey;
PlayerManager::removePlayer(cns);

View File

@ -31,7 +31,8 @@ auto db = make_storage("database.db",
make_column("PayZoneFlag", &Database::DbPlayer::PayZoneFlag),
make_column("XCoordinates", &Database::DbPlayer::x_coordinates),
make_column("YCoordinates", &Database::DbPlayer::y_coordinates),
make_column("ZCoordinates", &Database::DbPlayer::z_coordinates),
make_column("ZCoordinates", &Database::DbPlayer::z_coordinates),
make_column("Angle", &Database::DbPlayer::angle),
make_column("Body", &Database::DbPlayer::Body),
make_column("Class", &Database::DbPlayer::Class),
make_column("EquipFoot", &Database::DbPlayer::EquipFoot),
@ -49,7 +50,8 @@ auto db = make_storage("database.db",
make_column("SkinColor", &Database::DbPlayer::SkinColor),
make_column("isGM", &Database::DbPlayer::isGM),
make_column("FusionMatter", &Database::DbPlayer::FusionMatter),
make_column("Taros", &Database::DbPlayer::Taros)
make_column("Taros", &Database::DbPlayer::Taros),
make_column("PCState", &Database::DbPlayer::PCState)
),
make_table("Inventory",
make_column("AccountID", &Database::Inventory::AccountID, primary_key())
@ -152,9 +154,11 @@ int Database::createCharacter(sP_CL2LS_REQ_SAVE_CHAR_NAME* save, int AccountID)
create.FusionMatter= 0;
create.Taros= 0;
create.PCState = 0;
create.x_coordinates = settings::SPAWN_X;
create.y_coordinates= settings::SPAWN_Y;
create.z_coordinates= settings::SPAWN_Z;
create.angle = 0;
return db.insert(create);
}
@ -238,11 +242,10 @@ Database::DbPlayer Database::playerToDb(Player player)
result.AppearanceFlag = player.PCStyle2.iAppearanceFlag;
result.Body = player.PCStyle.iBody;
result.Class = player.PCStyle.iClass;
//equipment
result.EyeColor = player.PCStyle.iEyeColor;
result.FaceStyle = player.PCStyle.iFaceStyle;
result.FirstName = U16toU8( player.PCStyle.szFirstName);
//fm
result.FusionMatter = player.fusionmatter;
result.Gender = player.PCStyle.iGender;
result.HairColor = player.PCStyle.iHairColor;
result.HairStyle = player.PCStyle.iHairStyle;
@ -256,11 +259,19 @@ Database::DbPlayer Database::playerToDb(Player player)
result.PlayerID = player.PCStyle.iPC_UID;
result.SkinColor = player.PCStyle.iSkinColor;
result.slot = player.slot;
//taros
result.Taros = player.money;
result.TutorialFlag = player.PCStyle2.iTutorialFlag;
result.x_coordinates = player.x;
result.y_coordinates = player.y;
result.z_coordinates = player.z;
result.angle = player.angle;
result.PCState = player.iPCState;
//temporary inventory stuff
result.EquipWeapon1 = player.Equip[0].iID;
result.EquipUB = player.Equip[1].iID;
result.EquipLB = player.Equip[2].iID;
result.EquipFoot = player.Equip[3].iID;
return result;
}
@ -268,6 +279,7 @@ Database::DbPlayer Database::playerToDb(Player player)
Player Database::DbToPlayer(DbPlayer player) {
Player result = {}; // fixes some weird memory errors, this zeros out the members (not the padding inbetween though)
result.iID = player.PlayerID;
result.accountId = player.AccountID;
result.PCStyle2.iAppearanceFlag = player.AppearanceFlag;
result.PCStyle.iBody = player.Body;
@ -293,11 +305,12 @@ Player Database::DbToPlayer(DbPlayer player) {
result.x = player.x_coordinates;
result.y = player.y_coordinates;
result.z = player.z_coordinates;
result.angle = player.angle;
result.money = player.Taros;
result.fusionmatter = player.FusionMatter;
//TODO:: implement all of below
result.SerialKey = 0;
result.money = 0;
result.fusionmatter = 0;
result.SerialKey = 0;
result.activeNano = 0;
result.iPCState = 0;
result.equippedNanos[0] = 1;
@ -305,17 +318,25 @@ Player Database::DbToPlayer(DbPlayer player) {
result.equippedNanos[2] = 0;
result.isTrading = false;
result.isTradeConfirm = false;
//TODO:: implement all of below
//Nanos
result.activeNano = -1;
result.equippedNanos[0] = 0;
result.equippedNanos[1] = 0;
result.equippedNanos[2] = 0;
result.Nanos[1].iID = 1;
result.Nanos[1].iSkillID = 1;
result.Nanos[1].iStamina = 150;
result.Nanos[0].iID = 0;
result.Nanos[0].iSkillID = 0;
result.Nanos[0].iStamina = 0;
for (int i = 0; i < 37; i++) {
result.Nanos[i].iID = 0;
result.Nanos[i].iSkillID = 0;
result.Nanos[i].iStamina = 0;
for (int i = 1; i < 37; i++) {
result.Nanos[i].iID = i;
result.Nanos[i].iSkillID = 1;
result.Nanos[i].iStamina = 150;
}
//equip
result.Equip[0].iID = player.EquipWeapon1;
result.Equip[0].iType = 0;
(player.EquipWeapon1) ? result.Equip[0].iOpt = 1 : result.Equip[0].iOpt = 0;
@ -332,8 +353,6 @@ Player Database::DbToPlayer(DbPlayer player) {
result.Equip[3].iType = 3;
(player.EquipFoot) ? result.Equip[3].iOpt = 1 : result.Equip[3].iOpt = 0;
for (int i = 4; i < AEQUIP_COUNT; i++) {
// empty equips
result.Equip[i].iID = 0;
@ -355,3 +374,8 @@ Database::DbPlayer Database::getDbPlayerById(int id) {
}
#pragma endregion LoginServer
void Database::updatePlayer(Player player) {
DbPlayer toUpdate = playerToDb(player);
db.update(toUpdate);
}

View File

@ -51,6 +51,8 @@ namespace Database {
int x_coordinates;
int y_coordinates;
int z_coordinates;
int angle;
short int PCState;
};
@ -88,4 +90,5 @@ namespace Database {
//getting players
DbPlayer getDbPlayerById(int id);
void updatePlayer(Player player);
}

View File

@ -75,6 +75,10 @@ void PlayerManager::removePlayer(CNSocket* key) {
players.erase(key);
}
void PlayerManager::updatePlayerPosition(CNSocket* sock, int X, int Y, int Z, int angle) {
players[sock].plr->angle = angle;
updatePlayerPosition(sock, X, Y, Z);
}
void PlayerManager::updatePlayerPosition(CNSocket* sock, int X, int Y, int Z) {
players[sock].plr->x = X;
players[sock].plr->y = Y;
@ -164,6 +168,7 @@ void PlayerManager::updatePlayerPosition(CNSocket* sock, int X, int Y, int Z) {
NPCManager::updatePlayerNPCS(sock, players[sock]);
}
std::list<CNSocket*> PlayerManager::getNearbyPlayers(int x, int y, int dist) {
std::list<CNSocket*> plrs;
@ -195,12 +200,12 @@ void PlayerManager::enterPlayer(CNSocket* sock, CNPacketData* data) {
std::cout << "\tSerial: " << enter->iEnterSerialKey << std::endl;
std::cout << "\tTemp: " << enter->iTempValue << std::endl;
std::cout << "\tPC_UID: " << plr.PCStyle.iPC_UID << std::endl;
)
)
response.iID = rand();
response.iID = plr.iID;
response.uiSvrTime = getTime();
response.PCLoadData2CL.iUserLevel = 1;
response.PCLoadData2CL.iHP = 3625; //TODO: Check player levelupdata and get this right
response.PCLoadData2CL.iHP = plr.HP;
response.PCLoadData2CL.iLevel = plr.level;
response.PCLoadData2CL.iCandy = plr.money;
response.PCLoadData2CL.iMentor = 5; // Computress
@ -209,8 +214,8 @@ void PlayerManager::enterPlayer(CNSocket* sock, CNPacketData* data) {
response.PCLoadData2CL.iX = plr.x;
response.PCLoadData2CL.iY = plr.y;
response.PCLoadData2CL.iZ = plr.z;
response.PCLoadData2CL.iAngle = 130;
response.PCLoadData2CL.iActiveNanoSlotNum = -1;
response.PCLoadData2CL.iAngle = plr.angle;
response.PCLoadData2CL.iActiveNanoSlotNum = plr.activeNano;
response.PCLoadData2CL.iFatigue = 50;
response.PCLoadData2CL.PCStyle = plr.PCStyle;
response.PCLoadData2CL.PCStyle2 = plr.PCStyle2;
@ -223,9 +228,7 @@ void PlayerManager::enterPlayer(CNSocket* sock, CNPacketData* data) {
// don't ask..
for (int i = 1; i < 37; i++) {
response.PCLoadData2CL.aNanoBank[i].iID = i;
response.PCLoadData2CL.aNanoBank[i].iSkillID = 1;
response.PCLoadData2CL.aNanoBank[i].iStamina = 150;
response.PCLoadData2CL.aNanoBank[i] = plr.Nanos[i];
}
// temporarily not add nanos for nano add test through commands
@ -237,9 +240,7 @@ void PlayerManager::enterPlayer(CNSocket* sock, CNPacketData* data) {
response.PCLoadData2CL.iFirstUseFlag1 = UINT64_MAX;
response.PCLoadData2CL.iFirstUseFlag2 = UINT64_MAX;
plr.iID = response.iID;
plr.SerialKey = enter->iEnterSerialKey;
plr.HP = response.PCLoadData2CL.iHP;
motd.iType = 1;
U8toU16(settings::MOTDSTRING, (char16_t*)motd.szSystemMsg);
@ -272,7 +273,7 @@ void PlayerManager::loadPlayer(CNSocket* sock, CNPacketData* data) {
response.iPC_ID = complete->iPC_ID;
// reload players & NPCs
updatePlayerPosition(sock, plr->x, plr->y, plr->z);
updatePlayerPosition(sock, plr->x, plr->y, plr->z, plr->angle);
sock->sendPacket((void*)&response, P_FE2CL_REP_PC_LOADING_COMPLETE_SUCC, sizeof(sP_FE2CL_REP_PC_LOADING_COMPLETE_SUCC));
}
@ -282,7 +283,7 @@ void PlayerManager::movePlayer(CNSocket* sock, CNPacketData* data) {
return; // ignore the malformed packet
sP_CL2FE_REQ_PC_MOVE* moveData = (sP_CL2FE_REQ_PC_MOVE*)data->buf;
updatePlayerPosition(sock, moveData->iX, moveData->iY, moveData->iZ);
updatePlayerPosition(sock, moveData->iX, moveData->iY, moveData->iZ, moveData->iAngle);
players[sock].plr->angle = moveData->iAngle;
uint64_t tm = getTime();
@ -339,7 +340,7 @@ void PlayerManager::jumpPlayer(CNSocket* sock, CNPacketData* data) {
return; // ignore the malformed packet
sP_CL2FE_REQ_PC_JUMP* jumpData = (sP_CL2FE_REQ_PC_JUMP*)data->buf;
updatePlayerPosition(sock, jumpData->iX, jumpData->iY, jumpData->iZ);
updatePlayerPosition(sock, jumpData->iX, jumpData->iY, jumpData->iZ, jumpData->iAngle);
uint64_t tm = getTime();
@ -370,7 +371,7 @@ void PlayerManager::jumppadPlayer(CNSocket* sock, CNPacketData* data) {
return; // ignore the malformed packet
sP_CL2FE_REQ_PC_JUMPPAD* jumppadData = (sP_CL2FE_REQ_PC_JUMPPAD*)data->buf;
updatePlayerPosition(sock, jumppadData->iX, jumppadData->iY, jumppadData->iZ);
updatePlayerPosition(sock, jumppadData->iX, jumppadData->iY, jumppadData->iZ, jumppadData->iAngle);
uint64_t tm = getTime();
@ -399,7 +400,7 @@ void PlayerManager::launchPlayer(CNSocket* sock, CNPacketData* data) {
return; // ignore the malformed packet
sP_CL2FE_REQ_PC_LAUNCHER* launchData = (sP_CL2FE_REQ_PC_LAUNCHER*)data->buf;
updatePlayerPosition(sock, launchData->iX, launchData->iY, launchData->iZ);
updatePlayerPosition(sock, launchData->iX, launchData->iY, launchData->iZ, launchData->iAngle);
uint64_t tm = getTime();
@ -429,7 +430,7 @@ void PlayerManager::ziplinePlayer(CNSocket* sock, CNPacketData* data) {
return; // ignore the malformed packet
sP_CL2FE_REQ_PC_ZIPLINE* ziplineData = (sP_CL2FE_REQ_PC_ZIPLINE*)data->buf;
updatePlayerPosition(sock, ziplineData->iX, ziplineData->iY, ziplineData->iZ);
updatePlayerPosition(sock, ziplineData->iX, ziplineData->iY, ziplineData->iZ, ziplineData->iAngle);
uint64_t tm = getTime();
@ -466,7 +467,7 @@ void PlayerManager::movePlatformPlayer(CNSocket* sock, CNPacketData* data) {
return; // ignore the malformed packet
sP_CL2FE_REQ_PC_MOVEPLATFORM* platformData = (sP_CL2FE_REQ_PC_MOVEPLATFORM*)data->buf;
updatePlayerPosition(sock, platformData->iX, platformData->iY, platformData->iZ);
updatePlayerPosition(sock, platformData->iX, platformData->iY, platformData->iZ, platformData->iAngle);
uint64_t tm = getTime();
@ -500,7 +501,7 @@ void PlayerManager::moveSlopePlayer(CNSocket* sock, CNPacketData* data) {
return; // ignore the malformed packet
sP_CL2FE_REQ_PC_SLOPE* slopeData = (sP_CL2FE_REQ_PC_SLOPE*)data->buf;
updatePlayerPosition(sock, slopeData->iX, slopeData->iY, slopeData->iZ);
updatePlayerPosition(sock, slopeData->iX, slopeData->iY, slopeData->iZ,slopeData->iAngle);
uint64_t tm = getTime();

View File

@ -26,6 +26,7 @@ namespace PlayerManager {
void removePlayer(CNSocket* key);
void updatePlayerPosition(CNSocket* sock, int X, int Y, int Z);
void updatePlayerPosition(CNSocket* sock, int X, int Y, int Z, int angle);
std::list<CNSocket*> getNearbyPlayers(int X, int Y, int dist);
void enterPlayer(CNSocket* sock, CNPacketData* data);