Prevent DB players from occupying same slot

This commit is contained in:
Gent S 2020-11-25 19:15:30 -05:00
parent ea5b7104be
commit 872425640d
3 changed files with 25 additions and 13 deletions

View File

@ -214,6 +214,17 @@ void CNLoginServer::nameCheck(CNSocket* sock, CNPacketData* data) {
loginSessions[sock].lastHeartbeat = getTime(); loginSessions[sock].lastHeartbeat = getTime();
} }
void invalidCharacter(CNSocket* sock) {
INITSTRUCT(sP_LS2CL_REP_SHARD_SELECT_FAIL, fail);
fail.iErrorCode = 2;
sock->sendPacket((void*)&fail, P_LS2CL_REP_SHARD_SELECT_FAIL, sizeof(sP_LS2CL_REP_SHARD_SELECT_FAIL));
DEBUGLOG(
std::cout << "Login Server: Selected character error" << std::endl;
)
return;
}
void CNLoginServer::nameSave(CNSocket* sock, CNPacketData* data) { void CNLoginServer::nameSave(CNSocket* sock, CNPacketData* data) {
if (data->size != sizeof(sP_CL2LS_REQ_SAVE_CHAR_NAME)) if (data->size != sizeof(sP_CL2LS_REQ_SAVE_CHAR_NAME))
return; return;
@ -224,9 +235,10 @@ void CNLoginServer::nameSave(CNSocket* sock, CNPacketData* data) {
int errorCode = 0; int errorCode = 0;
if (!CNLoginServer::isCharacterNameGood(U16toU8(save->szFirstName), U16toU8(save->szLastName))) { if (!CNLoginServer::isCharacterNameGood(U16toU8(save->szFirstName), U16toU8(save->szLastName))) {
errorCode = 4; errorCode = 4;
} } else if (!Database::isNameFree(U16toU8(save->szFirstName), U16toU8(save->szLastName))) {
else if (!Database::isNameFree(U16toU8(save->szFirstName), U16toU8(save->szLastName))) {
errorCode = 1; errorCode = 1;
} else if (!Database::isSlotFree(loginSessions[sock].userID, save->iSlotNum)) {
return invalidCharacter(sock);
} }
if (errorCode != 0) { if (errorCode != 0) {
@ -260,17 +272,6 @@ void CNLoginServer::nameSave(CNSocket* sock, CNPacketData* data) {
) )
} }
void invalidCharacter(CNSocket* sock) {
INITSTRUCT(sP_LS2CL_REP_SHARD_SELECT_FAIL, fail);
fail.iErrorCode = 2;
sock->sendPacket((void*)&fail, P_LS2CL_REP_SHARD_SELECT_FAIL, sizeof(sP_LS2CL_REP_SHARD_SELECT_FAIL));
DEBUGLOG(
std::cout << "Login Server: Selected character error" << std::endl;
)
return;
}
bool validateCharacterCreation(sP_CL2LS_REQ_CHAR_CREATE* character) { bool validateCharacterCreation(sP_CL2LS_REQ_CHAR_CREATE* character) {
// all the values have been determined from analyzing client code and xdt // all the values have been determined from analyzing client code and xdt

View File

@ -208,6 +208,16 @@ bool Database::isNameFree(std::string firstName, std::string lastName) {
.empty()); .empty());
} }
bool Database::isSlotFree(int accountId, int slotNum) {
std::lock_guard<std::mutex> lock(dbCrit);
return
(db.get_all<DbPlayer>
(where((c(&DbPlayer::AccountID) == accountId)
and (c(&DbPlayer::slot) == slotNum)))
.empty());
}
int Database::createCharacter(sP_CL2LS_REQ_SAVE_CHAR_NAME* save, int AccountID) { int Database::createCharacter(sP_CL2LS_REQ_SAVE_CHAR_NAME* save, int AccountID) {
std::lock_guard<std::mutex> lock(dbCrit); std::lock_guard<std::mutex> lock(dbCrit);

View File

@ -121,6 +121,7 @@ namespace Database {
std::unique_ptr<Account> findAccount(std::string login); std::unique_ptr<Account> findAccount(std::string login);
bool validateCharacter(int characterID, int userID); bool validateCharacter(int characterID, int userID);
bool isNameFree(std::string firstName, std::string lastName); bool isNameFree(std::string firstName, std::string lastName);
bool isSlotFree(int accountId, int slotNum);
// called after chosing name, returns ID // called after chosing name, returns ID
int createCharacter(sP_CL2LS_REQ_SAVE_CHAR_NAME* save, int AccountID); int createCharacter(sP_CL2LS_REQ_SAVE_CHAR_NAME* save, int AccountID);
// called after finishing creation // called after finishing creation