refactor: changed exp and money update

This commit is contained in:
2022-02-16 16:18:25 +01:00
parent 36a9e1921c
commit 840d5edbe4
2 changed files with 14 additions and 30 deletions

View File

@@ -18,12 +18,8 @@ private:
std::uint8_t m_ID;
std::uint8_t m_GoldPerSecond;
bool m_GoldChanged;
bool m_ExpChanged;
public:
Player(std::uint8_t id = 0) : m_TeamColor(game::TeamColor::None), m_Gold(0), m_Exp(0), m_ID(id), m_GoldPerSecond(5),
m_GoldChanged(false), m_ExpChanged(false) {}
Player(std::uint8_t id = 0) : m_TeamColor(game::TeamColor::None), m_Gold(0), m_Exp(0), m_ID(id), m_GoldPerSecond(5) {}
const std::string& getName() const { return m_Name; }
void setName(const std::string& name) { m_Name = name; }
@@ -35,20 +31,14 @@ public:
void setGoldPerSecond(std::uint8_t goldPerSecond) { m_GoldPerSecond = goldPerSecond; }
std::uint32_t getGold() const { return m_Gold; }
void setGold(std::uint32_t gold) { m_GoldChanged = true; m_Gold = gold; }
void addGold(std::uint32_t gold) { m_GoldChanged = true; m_Gold += gold; }
void removeGold(std::uint32_t gold) { m_GoldChanged = true; m_Gold -= gold; }
void setGold(std::uint32_t gold) { m_Gold = gold; }
void addGold(std::uint32_t gold) { m_Gold += gold; }
void removeGold(std::uint32_t gold) { m_Gold -= gold; }
std::uint32_t getExp() const { return m_Exp; }
void setExp(std::uint32_t exp) { m_ExpChanged = true; m_Exp = exp; }
void addExp(std::uint32_t exp) { m_ExpChanged = true; m_Exp += exp; }
void removeExp(std::uint32_t exp) { m_ExpChanged = true; m_Exp -= exp; }
bool hasGoldChanged() const { return m_GoldChanged; }
bool hasExpChanged() const { return m_ExpChanged; }
void updateGold() { m_GoldChanged = false; }
void updateExp() { m_ExpChanged = false; }
void setExp(std::uint32_t exp) { m_Exp = exp; }
void addExp(std::uint32_t exp) { m_Exp += exp; }
void removeExp(std::uint32_t exp) { m_Exp -= exp; }
std::uint8_t getID() const { return m_ID; }
};

View File

@@ -37,25 +37,19 @@ void ServerGame::startGame() {
void ServerGame::updatePlayerStats() {
m_GoldMineTimer.update();
for (auto& pair : m_Server->getPlayers()) {
game::Player& player = pair.second;
if (player.hasGoldChanged()) {
protocol::UpdateMoneyPacket packet(player.getGold());
m_Server->getConnexions()[player.getID()].sendPacket(&packet);
player.updateGold();
}
if (player.hasExpChanged()) {
protocol::UpdateExpPacket packet(player.getExp());
m_Server->getConnexions()[player.getID()].sendPacket(&packet);
player.updateExp();
}
}
}
void ServerGame::updateGoldMines() {
for (auto& pair : m_Server->getPlayers()) {
game::Player* player = &pair.second;
player->addGold(player->getGoldPerSecond());
// Update player money and exp every second
protocol::UpdateMoneyPacket moneyPacket(player->getGold());
m_Server->getConnexions()[player->getID()].sendPacket(&moneyPacket);
protocol::UpdateExpPacket expPacket(player->getExp());
m_Server->getConnexions()[player->getID()].sendPacket(&expPacket);
}
}