Skip to content

Commit

Permalink
Misc: updated some cycles to C++11 style... Replaced method to append…
Browse files Browse the repository at this point in the history
… buffer with operator
  • Loading branch information
Arcidev committed Aug 17, 2015
1 parent 96620bc commit 4532bfc
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 49 deletions.
8 changes: 4 additions & 4 deletions Server/Server/Cards/RangedCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ RangedCard::RangedCard(uint64_t const& guid, Card const* card, Player* owner) :
bool RangedCard::CanAttackCard(uint64_t const& guid, std::vector<PlayableCard*> const& opponentCards, uint32_t const& /*position*/)
{
bool canAttack = true;
for (std::vector<PlayableCard*>::const_iterator iter = opponentCards.begin(); iter != opponentCards.end(); ++iter)
for (PlayableCard const* card : opponentCards)
{
if ((*iter)->GetGuid() == guid)
if (card->GetGuid() == guid)
{
if ((*iter)->GetType() == CARD_TYPE_DEFENSE)
if (card->GetType() == CARD_TYPE_DEFENSE)
return true;
}
else if ((*iter)->GetType() == CARD_TYPE_DEFENSE)
else if (card->GetType() == CARD_TYPE_DEFENSE)
canAttack = false;
}

Expand Down
41 changes: 24 additions & 17 deletions Server/Server/PacketHandlers/ByteBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,51 +79,51 @@ void ByteBuffer::ReadGuidByte(uint8_t& byte)
void ByteBuffer::WriteGuidBitStreamInOrder(Guid const& guid, std::vector<uint8_t> indexOrder)
{
uint8_t size = (indexOrder.size() > 8) ? 8 : (uint8_t)indexOrder.size();
for (uint8_t i = 0; i < size; i++)
for (uint8_t const& val : indexOrder)
{
if (indexOrder[i] > 7)
throw std::out_of_range("Index must be lower than 8. You've requested " + indexOrder[i]);
if (val > 7)
throw std::out_of_range("Index must be lower than 8. You've requested " + val);

WriteBit(guid[indexOrder[i]]);
WriteBit(guid[val]);
}
}

// Writes byte values of guid if bytes has value. Bytes without value are ignored
void ByteBuffer::WriteGuidByteStreamInOrder(Guid const& guid, std::vector<uint8_t> indexOrder)
{
uint8_t size = (indexOrder.size() > 8) ? 8 : (uint8_t)indexOrder.size();
for (uint8_t i = 0; i < size; i++)
for (uint8_t const& val : indexOrder)
{
if (indexOrder[i] > 7)
throw std::out_of_range("Index must be lower than 8. You've requested " + indexOrder[i]);
if (val > 7)
throw std::out_of_range("Index must be lower than 8. You've requested " + val);

WriteGuidByte(guid[indexOrder[i]]);
WriteGuidByte(guid[val]);
}
}

// Reads if exist byte values of guid in passed order
void ByteBuffer::ReadGuidBitStreamInOrder(Guid& guid, std::vector<uint8_t> indexOrder)
{
uint8_t size = (indexOrder.size() > 8) ? 8 : (uint8_t)indexOrder.size();
for (uint8_t i = 0; i < size; i++)
for (uint8_t const& val : indexOrder)
{
if (indexOrder[i] > 7)
throw std::out_of_range("Index must be lower than 8. You've requested " + indexOrder[i]);
if (val > 7)
throw std::out_of_range("Index must be lower than 8. You've requested " + val);

guid[indexOrder[i]] = ReadBit();
guid[val] = ReadBit();
}
}

// Reads byte values of guid if bytes has value. Bytes without value are ignored
void ByteBuffer::ReadGuidByteStreamInOrder(Guid& guid, std::vector<uint8_t> indexOrder)
{
uint8_t size = (indexOrder.size() > 8) ? 8 : (uint8_t)indexOrder.size();
for (uint8_t i = 0; i < size; i++)
for (uint8_t const& val : indexOrder)
{
if (indexOrder[i] > 7)
throw std::out_of_range("Index must be lower than 8. You've requested " + indexOrder[i]);
if (val > 7)
throw std::out_of_range("Index must be lower than 8. You've requested " + val);

ReadGuidByte(guid[indexOrder[i]]);
ReadGuidByte(guid[val]);
}
}

Expand Down Expand Up @@ -172,7 +172,14 @@ ByteBuffer& ByteBuffer::operator << (float const& value)
ByteBuffer& ByteBuffer::operator << (std::string const& value)
{
*this << (uint16_t)value.size();
append((uint8_t*)value.c_str(), value.size());
append((uint8_t const*)value.c_str(), value.size());
return *this;
}

// Stores float value in stream
ByteBuffer& ByteBuffer::operator << (ByteBuffer const& value)
{
append((uint8_t const*)&value.GetStorage()[0], value.GetStorage().size());
return *this;
}

Expand Down
1 change: 1 addition & 0 deletions Server/Server/PacketHandlers/ByteBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ByteBuffer
ByteBuffer& operator << (int8_t const& value) { return operator<<((uint8_t)value); }
ByteBuffer& operator << (float const& value);
ByteBuffer& operator << (std::string const& value);
ByteBuffer& operator << (ByteBuffer const& value);

ByteBuffer& operator >> (uint32_t& value);
ByteBuffer& operator >> (uint16_t& value);
Expand Down
4 changes: 2 additions & 2 deletions Server/Server/PacketHandlers/CardHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ void PacketHandler::handleSelectedCardsPacket(Player* player, Packet* packet)

pck.FlushBits();
pck << player->GetId();
pck.AppendBuffer(playerBuffer);
pck.AppendBuffer(opponentBuffer);
pck << playerBuffer;
pck << opponentBuffer;

player->GetGame()->BroadcastPacket(&pck);

Expand Down
1 change: 0 additions & 1 deletion Server/Server/PacketHandlers/Packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,4 @@ class Packet : public ByteBuffer
Packet(std::string const& data);

void Initialize(uint32_t opcodeNumber);
void AppendBuffer(ByteBuffer const& value) { append((uint8_t const*)&value.GetStorage()[0], value.GetStorage().size()); }
};
30 changes: 15 additions & 15 deletions Server/Server/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,30 +111,30 @@ void Player::SpellAttack(std::list<PlayableCard*> const& targets, uint8_t const&
ByteBuffer buffer;

packet << (uint8_t)targets.size();
for (std::list<PlayableCard*>::const_iterator iter = targets.begin(); iter != targets.end(); ++iter)
for (PlayableCard* target : targets)
{
(*iter)->DealDamage(damage);
if ((*iter)->IsAlive())
target->DealDamage(damage);
if (target->IsAlive())
packet.WriteBit(true);
else
{
if (!sendOpponentCardDeck)
sendOpponentCardDeck = true;
victim->destroyCard((*iter)->GetGuid());
victim->destroyCard(target->GetGuid());

packet.WriteBit(false);
}

packet.WriteGuidBitStreamInOrder((*iter)->GetGuid(), std::vector<uint8_t> { 6, 3, 1, 7, 0, 2, 5, 4 });
packet.WriteGuidBitStreamInOrder(target->GetGuid(), std::vector<uint8_t> { 6, 3, 1, 7, 0, 2, 5, 4 });

buffer.WriteGuidByteStreamInOrder((*iter)->GetGuid(), std::vector<uint8_t> { 4, 3, 5 });
buffer.WriteGuidByteStreamInOrder(target->GetGuid(), std::vector<uint8_t> { 4, 3, 5 });
buffer << damage;
buffer.WriteGuidByteStreamInOrder((*iter)->GetGuid(), std::vector<uint8_t> { 2, 0, 1, 6, 7 });
buffer.WriteGuidByteStreamInOrder(target->GetGuid(), std::vector<uint8_t> { 2, 0, 1, 6, 7 });
}

packet.FlushBits();
packet << m_id;
packet.AppendBuffer(buffer);
packet << buffer;

GetGame()->BroadcastPacket(&packet);

Expand Down Expand Up @@ -312,7 +312,7 @@ void Player::SendAvailableCards() const
}

packet.FlushBits();
packet.AppendBuffer(buffer);
packet << buffer;

SendPacket(&packet);
}
Expand Down Expand Up @@ -494,18 +494,18 @@ void Player::replenishMana()
buffer << m_id;
buffer << (uint8_t)MANA_REPLENISHMENT_VALUE;

for (std::vector<PlayableCard*>::iterator iter = m_currentCards.begin(); iter != m_currentCards.end(); ++iter)
for (PlayableCard* currentCard : m_currentCards)
{
(*iter)->ModifyMana(MANA_REPLENISHMENT_VALUE);
currentCard->ModifyMana(MANA_REPLENISHMENT_VALUE);

packet.WriteGuidBitStreamInOrder((*iter)->GetGuid(), std::vector<uint8_t> { 5, 0, 1, 2, 3, 7, 4, 6 });
packet.WriteGuidBitStreamInOrder(currentCard->GetGuid(), std::vector<uint8_t> { 5, 0, 1, 2, 3, 7, 4, 6 });

buffer.WriteGuidByteStreamInOrder((*iter)->GetGuid(), std::vector<uint8_t> { 2, 6, 0, 7, 1, 4, 3, 5 });
buffer << (*iter)->GetMana();
buffer.WriteGuidByteStreamInOrder(currentCard->GetGuid(), std::vector<uint8_t> { 2, 6, 0, 7, 1, 4, 3, 5 });
buffer << currentCard->GetMana();
}

packet.FlushBits();
packet.AppendBuffer(buffer);
packet << buffer;

GetGame()->BroadcastPacket(&packet);
}
Expand Down
4 changes: 2 additions & 2 deletions Server/Server/Spells/Spell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ uint8_t Spell::Cast(Player* caster, Player* victim, uint64_t const& selectedCard
return SPELL_CAST_RESULT_FAIL_NOT_ENOUGH_MANA;

bool failed = true;
for (std::list<SpellEffectPair>::const_iterator iter = m_spellEffects.begin(); iter != m_spellEffects.end(); ++iter)
if (iter->first(caster, victim, selectedCardGuid, &iter->second) && failed)
for (SpellEffectPair const& effectPair : m_spellEffects)
if (effectPair.first(caster, victim, selectedCardGuid, &effectPair.second) && failed)
failed = false;

return failed ? SPELL_CAST_RESULT_FAIL_INVALID_TARGET : SPELL_CAST_RESULT_SUCCESS;
Expand Down
10 changes: 5 additions & 5 deletions Server/Server/Spells/SpellEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ bool SpellEffect::handleApplyAura(Player* attacker, Player* victim, uint64_t tar
if (targets.empty())
return false;

for (std::list<PlayableCard*>::const_iterator iter = targets.begin(); iter != targets.end(); ++iter)
for (PlayableCard* target : targets)
{
SpellAuraEffect auraEffect(*iter, effectValues->SpellId, effectValues->Value1, effectValues->Value2, effectValues->Value3, effectValues->Value4);
(*iter)->ApplyAura(auraEffect);
SpellAuraEffect auraEffect(target, effectValues->SpellId, effectValues->Value1, effectValues->Value2, effectValues->Value3, effectValues->Value4);
target->ApplyAura(auraEffect);
}

return true;
Expand All @@ -41,8 +41,8 @@ bool SpellEffect::handleHeal(Player* attacker, Player* victim, uint64_t targetGu
if (targets.empty())
return false;

for (std::list<PlayableCard*>::iterator iter = targets.begin(); iter != targets.end(); ++iter)
(*iter)->Heal(effectValues->Value1);
for (PlayableCard* target : targets)
target->Heal(effectValues->Value1);

return true;
}
Expand Down
6 changes: 3 additions & 3 deletions Server/Server/Spells/SpellTargetSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ SpellTargetSelectorFunc const SpellTargetSelector::m_spellTargetSelectors[] =

std::list<PlayableCard*> SpellTargetSelector::getTargetFromDeck(Player* player, uint64_t const& targetGuid)
{
for (std::vector<PlayableCard*>::const_iterator iter = player->GetCurrentCards().begin(); iter != player->GetCurrentCards().end(); ++iter)
for (PlayableCard* card : player->GetCurrentCards())
{
if (targetGuid == (*iter)->GetGuid())
return std::list<PlayableCard*>{ (*iter) };
if (targetGuid == card->GetGuid())
return std::list<PlayableCard*>{ card };
}

return std::list<PlayableCard*>();
Expand Down

0 comments on commit 4532bfc

Please sign in to comment.