[Client] Use processors for player packets

pull/199/head
Koncord 8 years ago
parent e98fa6ad07
commit 740bfea0b2

@ -55,6 +55,7 @@ Networking::Networking(): peer(RakNet::RakPeerInterface::GetInstance()), playerP
worldPacketController.SetStream(0, &bsOut);
connected = 0;
ProcessorInitializer();
}
Networking::~Networking()
@ -151,6 +152,7 @@ void Networking::connect(const std::string &ip, unsigned short port, std::vector
case ID_CONNECTION_REQUEST_ACCEPTED:
{
serverAddr = packet->systemAddress;
PlayerProcessor::SetServerAddr(packet->systemAddress);
connected = true;
queue = false;
@ -245,537 +247,8 @@ void Networking::preInit(std::vector<std::string> &content, Files::Collections &
void Networking::processPlayerPacket(RakNet::Packet *packet)
{
RakNet::RakNetGUID guid;
RakNet::BitStream bsIn(&packet->data[1], packet->length, false);
bsIn.Read(guid);
DedicatedPlayer *pl = 0;
static RakNet::RakNetGUID myGuid = getLocalPlayer()->guid;
if (guid != myGuid)
pl = PlayerList::getPlayer(guid);
PlayerPacket *myPacket = playerPacketController.GetPacket(packet->data[0]);
switch (packet->data[0])
{
case ID_HANDSHAKE:
{
myPacket->setPlayer(getLocalPlayer());
myPacket->Send(serverAddr);
break;
}
case ID_PLAYER_BASEINFO:
{
LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Received ID_PLAYER_BASEINFO from server");
if (guid == myGuid)
{
LOG_APPEND(Log::LOG_INFO, "- Packet was about my id");
if (packet->length == myPacket->headerSize())
{
LOG_APPEND(Log::LOG_INFO, "- Requesting info");
myPacket->setPlayer(getLocalPlayer());
myPacket->Send(serverAddr);
}
else
{
myPacket->setPlayer(getLocalPlayer());
myPacket->Packet(&bsIn, false);
LOG_APPEND(Log::LOG_INFO, "- Updating LocalPlayer");
getLocalPlayer()->updateChar();
}
}
else
{
LOG_APPEND(Log::LOG_INFO, "- Packet was about %s", pl == 0 ? "new player" : pl->npc.mName.c_str());
if (pl == 0)
{
LOG_APPEND(Log::LOG_INFO, "- Exchanging data with new player");
pl = PlayerList::newPlayer(guid);
}
myPacket->setPlayer(pl);
myPacket->Packet(&bsIn, false);
PlayerList::createPlayer(guid);
}
break;
}
case ID_PLAYER_POS:
{
if (guid == myGuid)
{
if (packet->length != myPacket->headerSize())
{
LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "ID_PLAYER_POS changed by server");
myPacket->setPlayer(getLocalPlayer());
myPacket->Packet(&bsIn, false);
getLocalPlayer()->setPosition();
}
else
getLocalPlayer()->updatePosition(true);
}
else if (pl != 0)
{
myPacket->setPlayer(pl);
myPacket->Packet(&bsIn, false);
pl->updateMarker();
}
break;
}
case ID_USER_MYID:
{
LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Received ID_USER_MYID from server");
myGuid = guid;
getLocalPlayer()->guid = guid;
break;
}
case ID_USER_DISCONNECTED:
{
if (guid == myGuid)
MWBase::Environment::get().getStateManager()->requestQuit();
else if (pl != 0)
PlayerList::disconnectPlayer(guid);
}
case ID_PLAYER_EQUIPMENT:
{
if (guid == myGuid)
{
if (packet->length == myPacket->headerSize())
{
getLocalPlayer()->updateEquipment(true);
}
else
{
myPacket->setPlayer(getLocalPlayer());
myPacket->Packet(&bsIn, false);
getLocalPlayer()->setEquipment();
}
}
else if (pl != 0)
{
myPacket->setPlayer(pl);
myPacket->Packet(&bsIn, false);
pl->updateEquipment();
}
break;
}
case ID_PLAYER_INVENTORY:
{
if (guid == myGuid)
{
if (packet->length == myPacket->headerSize())
{
getLocalPlayer()->updateInventory(true);
}
else
{
myPacket->setPlayer(getLocalPlayer());
myPacket->Packet(&bsIn, false);
int inventoryAction = getLocalPlayer()->inventoryChanges.action;
if (inventoryAction == InventoryChanges::ADD)
{
getLocalPlayer()->addItems();
}
else if (inventoryAction == InventoryChanges::REMOVE)
{
getLocalPlayer()->removeItems();
}
else // InventoryChanges::SET
{
getLocalPlayer()->setInventory();
}
}
}
break;
}
case ID_PLAYER_SPELLBOOK:
{
if (guid == myGuid)
{
if (packet->length == myPacket->headerSize())
{
getLocalPlayer()->sendSpellbook();
}
else
{
myPacket->setPlayer(getLocalPlayer());
myPacket->Packet(&bsIn, false);
int spellbookAction = getLocalPlayer()->spellbookChanges.action;
if (spellbookAction == SpellbookChanges::ADD)
{
getLocalPlayer()->addSpells();
}
else if (spellbookAction == SpellbookChanges::REMOVE)
{
getLocalPlayer()->removeSpells();
}
else // SpellbookChanges::SET
{
getLocalPlayer()->setSpellbook();
}
}
}
break;
}
case ID_PLAYER_JOURNAL:
{
if (guid == myGuid)
{
if (packet->length == myPacket->headerSize())
{
// Entire journal cannot currently be requested from players
}
else
{
myPacket->setPlayer(getLocalPlayer());
myPacket->Packet(&bsIn, false);
getLocalPlayer()->addJournalItems();
}
}
break;
}
case ID_PLAYER_ATTACK:
{
if (pl != 0)
{
myPacket->setPlayer(pl);
myPacket->Packet(&bsIn, false);
Main::get().getMechanicsHelper()->processAttack(pl->getPtr(), pl->attack);
}
break;
}
case ID_PLAYER_STATS_DYNAMIC:
{
if (guid == myGuid)
{
if (packet->length == myPacket->headerSize())
{
getLocalPlayer()->updateStatsDynamic(true);
}
else
{
myPacket->setPlayer(getLocalPlayer());
myPacket->Packet(&bsIn, false);
getLocalPlayer()->setDynamicStats();
}
}
else if (pl != 0)
{
myPacket->setPlayer(pl);
myPacket->Packet(&bsIn, false);
MWWorld::Ptr ptrPlayer = pl->getPtr();
MWMechanics::CreatureStats *ptrCreatureStats = &ptrPlayer.getClass().getCreatureStats(ptrPlayer);
MWMechanics::DynamicStat<float> value;
for (int i = 0; i < 3; ++i)
{
value.readState(pl->creatureStats.mDynamic[i]);
ptrCreatureStats->setDynamic(i, value);
}
}
break;
}
case ID_PLAYER_DEATH:
{
LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Received ID_PLAYER_DEATH from server");
if (guid == myGuid)
{
LOG_APPEND(Log::LOG_INFO, "- Packet was about me");
MWWorld::Ptr player = MWBase::Environment::get().getWorld()->getPlayerPtr();
MWMechanics::DynamicStat<float> health = player.getClass().getCreatureStats(player).getHealth();
health.setCurrent(0);
player.getClass().getCreatureStats(player).setHealth(health);
myPacket->setPlayer(getLocalPlayer());
myPacket->Send(serverAddr);
}
else if (pl != 0)
{
LOG_APPEND(Log::LOG_INFO, "- Packet was about %s", pl->npc.mName.c_str());
MWMechanics::DynamicStat<float> health;
pl->creatureStats.mDead = true;
health.readState(pl->creatureStats.mDynamic[0]);
health.setCurrent(0);
health.writeState(pl->creatureStats.mDynamic[0]);
pl->getPtr().getClass().getCreatureStats(pl->getPtr()).setHealth(health);
}
break;
}
case ID_PLAYER_RESURRECT:
{
LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Received ID_PLAYER_RESURRECT from server");
if (guid == myGuid)
{
LOG_APPEND(Log::LOG_INFO, "- Packet was about me");
MWWorld::Ptr player = MWBase::Environment::get().getWorld()->getPlayerPtr();
player.getClass().getCreatureStats(player).resurrect();
// If this player had a weapon or spell readied when dying, they will
// still have it readied but be unable to use it unless we clear it here
player.getClass().getNpcStats(player).setDrawState(MWMechanics::DrawState_Nothing);
myPacket->setPlayer(getLocalPlayer());
myPacket->Send(serverAddr);
getLocalPlayer()->updateStatsDynamic(true);
playerPacketController.GetPacket(ID_PLAYER_STATS_DYNAMIC)->setPlayer(getLocalPlayer());
playerPacketController.GetPacket(ID_PLAYER_STATS_DYNAMIC)->Send(serverAddr);
}
else if (pl != 0)
{
LOG_APPEND(Log::LOG_INFO, "- Packet was about %s", pl->npc.mName.c_str());
pl->creatureStats.mDead = false;
if (pl->creatureStats.mDynamic[0].mMod < 1)
pl->creatureStats.mDynamic[0].mMod = 1;
pl->creatureStats.mDynamic[0].mCurrent = pl->creatureStats.mDynamic[0].mMod;
pl->getPtr().getClass().getCreatureStats(pl->getPtr()).resurrect();
MWMechanics::DynamicStat<float> health;
health.readState(pl->creatureStats.mDynamic[0]);
pl->getPtr().getClass().getCreatureStats(pl->getPtr()).setHealth(health);
}
break;
}
case ID_PLAYER_CELL_CHANGE:
{
if (guid == myGuid)
{
if (packet->length == myPacket->headerSize())
getLocalPlayer()->updateCell(true);
else
{
myPacket->setPlayer(getLocalPlayer());
myPacket->Packet(&bsIn, false);
getLocalPlayer()->setCell();
}
}
else if (pl != 0)
{
myPacket->setPlayer(pl);
myPacket->Packet(&bsIn, false);
pl->updateCell();
}
break;
}
case ID_PLAYER_CELL_STATE:
{
if (guid == myGuid)
{
if (packet->length == myPacket->headerSize())
getLocalPlayer()->sendCellStates();
}
break;
}
case ID_PLAYER_ANIM_FLAGS:
{
if (guid == myGuid)
getLocalPlayer()->updateAnimFlags(true);
else if (pl != 0)
{
myPacket->setPlayer(pl);
myPacket->Packet(&bsIn, false);
pl->updateAnimFlags();
}
break;
}
case ID_CHAT_MESSAGE:
{
std::string message;
if (guid == myGuid)
{
myPacket->setPlayer(getLocalPlayer());
myPacket->Packet(&bsIn, false);
message = getLocalPlayer()->chatMessage;
}
else if (pl != 0)
{
myPacket->setPlayer(pl);
myPacket->Packet(&bsIn, false);
message = pl->chatMessage;
}
Main::get().getGUIController()->printChatMessage(message);
break;
}
case ID_PLAYER_CHARGEN:
{
if (guid == myGuid)
{
myPacket->setPlayer(getLocalPlayer());
myPacket->Packet(&bsIn, false);
}
break;
}
case ID_PLAYER_ATTRIBUTE:
{
if (guid == myGuid)
{
if (packet->length == myPacket->headerSize())
{
getLocalPlayer()->updateAttributes(true);
}
else
{
myPacket->setPlayer(getLocalPlayer());
myPacket->Packet(&bsIn, false);
getLocalPlayer()->setAttributes();
}
}
else if (pl != 0)
{
myPacket->setPlayer(pl);
myPacket->Packet(&bsIn, false);
MWWorld::Ptr ptrPlayer = pl->getPtr();
MWMechanics::CreatureStats *ptrCreatureStats = &ptrPlayer.getClass().getCreatureStats(ptrPlayer);
MWMechanics::AttributeValue attributeValue;
for (int i = 0; i < 8; ++i)
{
attributeValue.readState(pl->creatureStats.mAttributes[i]);
ptrCreatureStats->setAttribute(i, attributeValue);
}
}
break;
}
case ID_PLAYER_SKILL:
{
if (guid == myGuid)
{
if (packet->length == myPacket->headerSize())
{
getLocalPlayer()->updateSkills(true);
}
else
{
myPacket->setPlayer(getLocalPlayer());
myPacket->Packet(&bsIn, false);
getLocalPlayer()->setSkills();
}
}
else if (pl != 0)
{
myPacket->setPlayer(pl);
myPacket->Packet(&bsIn, false);
MWWorld::Ptr ptrPlayer = pl->getPtr();
MWMechanics::NpcStats *ptrNpcStats = &ptrPlayer.getClass().getNpcStats(ptrPlayer);
MWMechanics::SkillValue skillValue;
for (int i = 0; i < 27; ++i)
{
skillValue.readState(pl->npcStats.mSkills[i]);
ptrNpcStats->setSkill(i, skillValue);
}
}
break;
}
case ID_PLAYER_LEVEL:
{
if (guid == myGuid)
{
if (packet->length == myPacket->headerSize())
{
getLocalPlayer()->updateLevel(true);
}
else
{
myPacket->setPlayer(getLocalPlayer());
myPacket->Packet(&bsIn, false);
getLocalPlayer()->setLevel();
}
}
else if (pl != 0)
{
myPacket->setPlayer(pl);
myPacket->Packet(&bsIn, false);
MWWorld::Ptr ptrPlayer = pl->getPtr();
MWMechanics::CreatureStats *ptrCreatureStats = &ptrPlayer.getClass().getCreatureStats(ptrPlayer);
ptrCreatureStats->setLevel(pl->creatureStats.mLevel);
}
break;
}
case ID_GUI_MESSAGEBOX:
{
if (guid == myGuid)
{
myPacket->setPlayer(getLocalPlayer());
myPacket->Packet(&bsIn, false);
LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "ID_GUI_MESSAGEBOX, Type %d, MSG %s", getLocalPlayer()->guiMessageBox.type,
getLocalPlayer()->guiMessageBox.label.c_str());
int messageBoxType = getLocalPlayer()->guiMessageBox.type;
if (messageBoxType == BasePlayer::GUIMessageBox::MessageBox)
Main::get().getGUIController()->showMessageBox(getLocalPlayer()->guiMessageBox);
else if (messageBoxType == BasePlayer::GUIMessageBox::CustomMessageBox)
Main::get().getGUIController()->showCustomMessageBox(getLocalPlayer()->guiMessageBox);
else if (messageBoxType == BasePlayer::GUIMessageBox::InputDialog)
Main::get().getGUIController()->showInputBox(getLocalPlayer()->guiMessageBox);
else if (messageBoxType == BasePlayer::GUIMessageBox::ListBox)
Main::get().getGUIController()->showDialogList(getLocalPlayer()->guiMessageBox);
}
break;
}
case ID_PLAYER_CHARCLASS:
{
if (guid == myGuid)
{
if (packet->length == myPacket->headerSize())
getLocalPlayer()->sendClass();
else
{
myPacket->setPlayer(getLocalPlayer());
myPacket->Packet(&bsIn, false);
getLocalPlayer()->setClass();
}
}
break;
}
case ID_GAME_TIME:
{
if (guid == myGuid)
{
myPacket->setPlayer(getLocalPlayer());
myPacket->Packet(&bsIn, false);
MWBase::World *world = MWBase::Environment::get().getWorld();
if (getLocalPlayer()->hour != -1)
world->setHour(getLocalPlayer()->hour);
else if (getLocalPlayer()->day != -1)
world->setDay(getLocalPlayer()->day);
else if (getLocalPlayer()->month != -1)
world->setMonth(getLocalPlayer()->month);
}
break;
}
case ID_GAME_CONSOLE:
{
myPacket->setPlayer(getLocalPlayer());
myPacket->Packet(&bsIn, false);
break;
}
default:
LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Unhandled PlayerPacket with identifier %i has arrived", packet->data[0]);
}
if(!PlayerProcessor::Process(*packet))
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Unhandled PlayerPacket with identifier %i has arrived", packet->data[0]);
}
void Networking::processActorPacket(RakNet::Packet *packet)

Loading…
Cancel
Save