Replaced HandleCharacterSet*() with HandleCharacterMovement()

This new method rolls three otherwise similar methods together. There is
still a conditional which handles room movements separately, but it's much
smoother, and PumpPacketProximity is utilized much more.

I've also added a stub for graphical attack data via
HandleCharacterAttack()
This commit is contained in:
Kayne Ruse
2015-01-13 02:04:48 +11:00
parent e752dd7b0f
commit 42662c3f61
3 changed files with 50 additions and 119 deletions
+2 -3
View File
@@ -101,9 +101,8 @@ private:
void HandleCharacterUnload(CharacterPacket* const); void HandleCharacterUnload(CharacterPacket* const);
//character movement //character movement
void HandleCharacterSetRoom(CharacterPacket* const); void HandleCharacterMovement(CharacterPacket* const);
void HandleCharacterSetOrigin(CharacterPacket* const); void HandleCharacterAttack(CharacterPacket* const);
void HandleCharacterSetMotion(CharacterPacket* const);
//utility methods //utility methods
void PumpPacket(SerialPacket* const); void PumpPacket(SerialPacket* const);
+36 -101
View File
@@ -175,105 +175,15 @@ void ServerApplication::HandleCharacterUnload(CharacterPacket* const argPacket)
//character movement //character movement
//------------------------- //-------------------------
//TODO: ? Could replace this verbosity with a "verify" method, taking a client, account and character ptr as arguments //TODO: Could replace this verbosity with a "verify" method, taking a client, account and character ptr as arguments
void ServerApplication::HandleCharacterSetRoom(CharacterPacket* const argPacket) { void ServerApplication::HandleCharacterMovement(CharacterPacket* const argPacket) {
//get the specified objects //get the specified objects
AccountData* accountData = accountMgr.Get(argPacket->accountIndex); AccountData* accountData = accountMgr.Get(argPacket->accountIndex);
CharacterData* characterData = characterMgr.Get(argPacket->characterIndex); CharacterData* characterData = characterMgr.Get(argPacket->characterIndex);
if (!accountData || !characterData) { if (!accountData || !characterData) {
throw(std::runtime_error("Failed to set character room, missing data")); throw(std::runtime_error("Failed to move a character, missing data"));
}
//get this account's client
ClientData* clientData = clientMgr.Get(accountData->GetClientIndex());
//check for fraud
if (clientData->GetAddress() != argPacket->srcAddress) {
std::cerr << "Falsified set character origin targeting uid(" << argPacket->characterIndex << ")" << std::endl;
return;
}
//check if allowed
if (characterData->GetOwner() != argPacket->accountIndex && !accountData->GetModerator() && !accountData->GetAdministrator()) {
//TODO: send to the client?
std::cerr << "Failed to set character room due to lack of permissions targeting uid(" << argPacket->characterIndex << ")" << std::endl;
return;
}
//pop from the old room
roomMgr.PopCharacter(characterData);
//set the character's room, zero it's origin, zero it's motion
characterData->SetRoomIndex(argPacket->roomIndex);
characterData->SetOrigin({0, 0});
characterData->SetMotion({0, 0});
//push to the new room
roomMgr.PushCharacter(characterData);
//update the clients
CharacterPacket newPacket;
CopyCharacterToPacket(&newPacket, argPacket->characterIndex);
newPacket.type = SerialPacketType::CHARACTER_SET_ROOM;
PumpPacket(&newPacket);
}
void ServerApplication::HandleCharacterSetOrigin(CharacterPacket* const argPacket) {
//get the specified objects
AccountData* accountData = accountMgr.Get(argPacket->accountIndex);
CharacterData* characterData = characterMgr.Get(argPacket->characterIndex);
if (!accountData || !characterData) {
throw(std::runtime_error("Failed to set character origin, missing data"));
}
//get this account's client
ClientData* clientData = clientMgr.Get(accountData->GetClientIndex());
//check for fraud
if (clientData->GetAddress() != argPacket->srcAddress) {
std::cerr << "Falsified set character origin targeting uid(" << argPacket->characterIndex << ")" << std::endl;
return;
}
//check if allowed
if (characterData->GetOwner() != argPacket->accountIndex && !accountData->GetModerator() && !accountData->GetAdministrator()) {
//TODO: send to the client?
std::cerr << "Failed to set character origin due to lack of permissions targeting uid(" << argPacket->characterIndex << ")" << std::endl;
return;
}
//set the character's origin, zero it's motion
characterData->SetOrigin(argPacket->origin);
characterData->SetMotion({0, 0});
//update the clients
CharacterPacket newPacket;
CopyCharacterToPacket(&newPacket, argPacket->characterIndex);
newPacket.type = SerialPacketType::CHARACTER_SET_ORIGIN;
PumpPacket(&newPacket);
}
void ServerApplication::HandleCharacterSetMotion(CharacterPacket* const argPacket) {
//get the specified objects
AccountData* accountData = accountMgr.Get(argPacket->accountIndex);
if (!accountData) {
std::ostringstream msg;
msg << "Failed to set character motion, missing account: Index " << argPacket->accountIndex << "; ";
msg << "Number of accounts loaded: " << accountMgr.GetContainer()->size();
throw(std::runtime_error(msg.str()));
}
CharacterData* characterData = characterMgr.Get(argPacket->characterIndex);
if (!characterData) {
std::ostringstream msg;
msg << "Failed to set character motion, missing character: Index " << argPacket->characterIndex << "; ";
msg << "Number of characters loaded: " << characterMgr.GetContainer()->size();
throw(std::runtime_error(msg.str()));
} }
//get this account's client //get this account's client
@@ -292,13 +202,38 @@ void ServerApplication::HandleCharacterSetMotion(CharacterPacket* const argPacke
return; return;
} }
//set the character's origin and motion //check if moving rooms
characterData->SetOrigin(argPacket->origin); if (characterData->GetRoomIndex() != argPacket->roomIndex) {
characterData->SetMotion(argPacket->motion); //delete from the old room
CharacterPacket newPacket;
CopyCharacterToPacket(&newPacket, argPacket->characterIndex);
newPacket.type = SerialPacketType::CHARACTER_DELETE;
PumpPacketProximity(&newPacket, characterData->GetRoomIndex(), characterData->GetOrigin(), -1);
//update the clients //move the character between rooms
CharacterPacket newPacket; roomMgr.PopCharacter(characterData);
CopyCharacterToPacket(&newPacket, argPacket->characterIndex); characterData->SetRoomIndex(argPacket->roomIndex);
newPacket.type = SerialPacketType::CHARACTER_SET_MOTION; roomMgr.PushCharacter(characterData);
PumpPacketProximity(&newPacket, characterData->GetRoomIndex(), characterData->GetOrigin(), -1);
//create in the new room
CopyCharacterToPacket(&newPacket, argPacket->characterIndex);
newPacket.type = SerialPacketType::CHARACTER_CREATE;
PumpPacketProximity(&newPacket, characterData->GetRoomIndex(), characterData->GetOrigin(), -1);
}
//if not moving between rooms
else {
//set the character's origin and motion
characterData->SetOrigin(argPacket->origin);
characterData->SetMotion(argPacket->motion);
//update the clients
CharacterPacket newPacket;
CopyCharacterToPacket(&newPacket, argPacket->characterIndex);
newPacket.type = SerialPacketType::CHARACTER_MOVEMENT;
PumpPacketProximity(&newPacket, characterData->GetRoomIndex(), characterData->GetOrigin(), -1);
}
} }
void ServerApplication::HandleCharacterAttack(CharacterPacket* const) {
//TODO: bounce graphical attack data
}
+12 -15
View File
@@ -277,27 +277,24 @@ void ServerApplication::HandlePacket(SerialPacket* const argPacket) {
HandleCharacterUnload(static_cast<CharacterPacket*>(argPacket)); HandleCharacterUnload(static_cast<CharacterPacket*>(argPacket));
break; break;
//character movement /* case SerialPacketType::QUERY_CHARACTER_EXISTS:
case SerialPacketType::CHARACTER_SET_ROOM: //
HandleCharacterSetRoom(static_cast<CharacterPacket*>(argPacket));
break; break;
case SerialPacketType::CHARACTER_SET_ORIGIN:
HandleCharacterSetOrigin(static_cast<CharacterPacket*>(argPacket));
break;
case SerialPacketType::CHARACTER_SET_MOTION:
HandleCharacterSetMotion(static_cast<CharacterPacket*>(argPacket));
break;
/*
case SerialPacketType::QUERY_CHARACTER_STATS: case SerialPacketType::QUERY_CHARACTER_STATS:
// HandleCharacterStatsRequest(static_cast<RegionPacket*>(argPacket)); //
break; break;
case SerialPacketType::QUERY_CHARACTER_LOCATION: case SerialPacketType::QUERY_CHARACTER_LOCATION:
// HandleCharacterStatsRequest(static_cast<RegionPacket*>(argPacket)); //
break; break;
case SerialPacketType::TEXT_BROADCAST: */
// HandleCharacterStatsRequest(static_cast<RegionPacket*>(argPacket)); //character movement
case SerialPacketType::CHARACTER_MOVEMENT:
HandleCharacterMovement(static_cast<CharacterPacket*>(argPacket));
break; break;
case SerialPacketType::CHARACTER_ATTACK:
HandleCharacterAttack(static_cast<CharacterPacket*>(argPacket));
break;
/*
//enemy management //enemy management
//TODO: enemy management //TODO: enemy management