From 0a814233a4fe057e69146b50cbfab1a1a01061dc Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sat, 12 Aug 2023 12:22:36 +0200 Subject: [PATCH] Change Lobby timer --- include/game/BaseGame.h | 4 ++++ include/game/client/ClientGame.h | 4 ++-- include/game/server/Lobby.h | 2 +- include/protocol/packets/UpdateLobbyTimePacket.h | 6 +++--- src/game/client/ClientGame.cpp | 6 ++---- src/game/server/Lobby.cpp | 15 +++++++-------- src/protocol/packets/UpdateLobbyTimePacket.cpp | 4 ++-- src/render/gui/GameMenu.cpp | 10 ++++++---- 8 files changed, 27 insertions(+), 24 deletions(-) diff --git a/include/game/BaseGame.h b/include/game/BaseGame.h index dc961d6..a8d1141 100644 --- a/include/game/BaseGame.h +++ b/include/game/BaseGame.h @@ -36,6 +36,8 @@ protected: TeamList m_Teams = { Team{TeamColor::Red}, Team{TeamColor::Blue} }; GameState m_GameState = GameState::Lobby; PlayerList m_Players; + + std::uint64_t m_GameStartTime = 0; public: Game(World* world); virtual ~Game(); @@ -65,6 +67,8 @@ public: const TeamList& GetTeams() const { return m_Teams; } + std::uint64_t GetGameStartTime() const { return m_GameStartTime; } + }; } // namespace game diff --git a/include/game/client/ClientGame.h b/include/game/client/ClientGame.h index c5c40c9..1505aae 100644 --- a/include/game/client/ClientGame.h +++ b/include/game/client/ClientGame.h @@ -18,7 +18,7 @@ class ClientGame : public protocol::PacketHandler, public game::Game { private: Client* m_Client; std::uint8_t m_ConnexionID; - std::uint32_t m_LobbyTime = 0; + std::uint64_t m_LobbyStartTime = 0; game::Player* m_Player = nullptr; render::Renderer* m_Renderer; client::WorldClient m_WorldClient; @@ -31,7 +31,7 @@ public: void RenderWorld(); - std::uint32_t GetLobbyTime() const { return m_LobbyTime; } + std::uint64_t GetLobbyStartTime() const { return m_LobbyStartTime; } const game::Player* GetPlayer() const { return m_Player; } const WorldClient& GetWorld() const { return m_WorldClient; } Client* GetClient() const { return m_Client; } diff --git a/include/game/server/Lobby.h b/include/game/server/Lobby.h index f59d3ff..a525da8 100644 --- a/include/game/server/Lobby.h +++ b/include/game/server/Lobby.h @@ -13,7 +13,7 @@ class Lobby { private: Server* m_Server; bool m_GameStarted = false; - std::uint64_t m_StartTimerTime = 0; + std::uint64_t m_StartTime = 0; std::vector m_Players; utils::AutoTimer m_Timer; public: diff --git a/include/protocol/packets/UpdateLobbyTimePacket.h b/include/protocol/packets/UpdateLobbyTimePacket.h index e910c59..226d77e 100644 --- a/include/protocol/packets/UpdateLobbyTimePacket.h +++ b/include/protocol/packets/UpdateLobbyTimePacket.h @@ -7,17 +7,17 @@ namespace protocol { class UpdateLobbyTimePacket : public Packet { private: - std::uint32_t m_RemainingTime; + std::uint64_t m_StartTime; // unix millis public: UpdateLobbyTimePacket() {} - UpdateLobbyTimePacket(std::uint32_t remainingTime) : m_RemainingTime(remainingTime) {} + UpdateLobbyTimePacket(std::uint64_t startTime) : m_StartTime(startTime) {} virtual ~UpdateLobbyTimePacket() {} virtual DataBuffer Serialize(bool packetID = true) const; virtual void Deserialize(DataBuffer& data); virtual void Dispatch(PacketHandler* handler) const; - std::uint32_t GetRemainingTime() const { return m_RemainingTime; } + std::uint64_t GetStartTime() const { return m_StartTime; } virtual PacketType GetType() const { return PacketType::UpdateLobbyTime; } }; diff --git a/src/game/client/ClientGame.cpp b/src/game/client/ClientGame.cpp index 572d52c..2f25e79 100644 --- a/src/game/client/ClientGame.cpp +++ b/src/game/client/ClientGame.cpp @@ -40,9 +40,6 @@ ClientGame::~ClientGame() { void ClientGame::Tick(std::uint64_t delta) { game::Game::Tick(delta); m_WorldRenderer.Update(); - if (m_GameState == game::GameState::Lobby && m_LobbyTime > 0) { - m_LobbyTime -= delta; - } } void ClientGame::HandlePacket(const protocol::PlayerJoinPacket* packet) { @@ -97,7 +94,8 @@ void ClientGame::HandlePacket(const protocol::ConnexionInfoPacket* packet) { } void ClientGame::HandlePacket(const protocol::UpdateLobbyTimePacket* packet) { - m_LobbyTime = packet->GetRemainingTime(); + m_GameStartTime = packet->GetStartTime(); + m_LobbyStartTime = utils::GetTime(); } void ClientGame::HandlePacket(const protocol::UpdateMoneyPacket* packet) { diff --git a/src/game/server/Lobby.cpp b/src/game/server/Lobby.cpp index 3c6ced0..1a3e7c3 100644 --- a/src/game/server/Lobby.cpp +++ b/src/game/server/Lobby.cpp @@ -33,20 +33,20 @@ Lobby::Lobby(Server* server) : m_Server(server), m_Timer(1000, std::bind(&Lobby: } void Lobby::Tick() { - if (m_GameStarted || m_StartTimerTime == 0) + if (m_GameStarted || m_StartTime == 0) return; - if (utils::GetTime() - m_StartTimerTime >= LobbyWaitingTime) { + if (utils::GetTime() >= m_StartTime) { m_Server->GetGame().NotifyListeners(&game::GameListener::OnGameBegin); m_GameStarted = true; return; } - m_Timer.Update(); + //m_Timer.Update(); } void Lobby::SendTimeRemaining() { - protocol::UpdateLobbyTimePacket packet(LobbyWaitingTime - (utils::GetTime() - m_StartTimerTime)); // converting second to millis + protocol::UpdateLobbyTimePacket packet(m_StartTime); // converting second to millis m_Server->BroadcastPacket(&packet); } @@ -56,7 +56,7 @@ void Lobby::OnPlayerJoin(std::uint8_t playerID) { utils::LOG("(Server) Player Joined Lobby !"); m_Players.push_back(playerID); if (m_Players.size() == MIN_PLAYER_WAITING) { // start timer if a second player join the match - m_StartTimerTime = utils::GetTime(); + m_StartTime = utils::GetTime() + static_cast(LobbyWaitingTime); m_Timer.Reset(); SendTimeRemaining(); } @@ -73,9 +73,8 @@ void Lobby::OnPlayerLeave(std::uint8_t playerID) { m_Players.erase(it); if (m_Players.size() == 1) { - protocol::UpdateLobbyTimePacket packet(0); - m_Server->BroadcastPacket(&packet); - m_StartTimerTime = 0; // reset timer if there is only one player left + m_StartTime = 0; // reset timer if there is only one player left + SendTimeRemaining(); } } diff --git a/src/protocol/packets/UpdateLobbyTimePacket.cpp b/src/protocol/packets/UpdateLobbyTimePacket.cpp index f0f8bde..99c13c0 100644 --- a/src/protocol/packets/UpdateLobbyTimePacket.cpp +++ b/src/protocol/packets/UpdateLobbyTimePacket.cpp @@ -7,12 +7,12 @@ DataBuffer UpdateLobbyTimePacket::Serialize(bool packetID) const { DataBuffer data; WritePacketID(data, packetID); - data << m_RemainingTime; + data << m_StartTime; return data; } void UpdateLobbyTimePacket::Deserialize(DataBuffer& data) { - data >> m_RemainingTime; + data >> m_StartTime; } } // namespace protocol diff --git a/src/render/gui/GameMenu.cpp b/src/render/gui/GameMenu.cpp index 78ed984..8066b6e 100644 --- a/src/render/gui/GameMenu.cpp +++ b/src/render/gui/GameMenu.cpp @@ -73,10 +73,12 @@ void GameMenu::ShowTeamSelection() { } void GameMenu::ShowLobbyProgress() { - const int timePassed = server::Lobby::LobbyWaitingTime - GetClient()->GetGame().GetLobbyTime(); - const float progress = (float)timePassed / (float)(server::Lobby::LobbyWaitingTime); + const std::uint64_t waitTime = GetClient()->GetGame().GetGameStartTime() - GetClient()->GetGame().GetLobbyStartTime(); + const std::uint64_t timeRemaining = GetClient()->GetGame().GetGameStartTime() - utils::GetTime(); + const float progress = static_cast(timeRemaining) / static_cast(waitTime); + if (progress > 0 && progress < 1) { - ImGui::ProgressBar(progress, ImVec2(0.0f, 0.0f), std::string(std::to_string(GetClient()->GetGame().GetLobbyTime() / 1000) + "s").c_str()); + ImGui::ProgressBar(1.0f - progress, ImVec2(0.0f, 0.0f), std::string(std::to_string(timeRemaining / 1000) + "s").c_str()); ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x); ImGui::Text("Time Remaining"); } else { @@ -86,7 +88,7 @@ void GameMenu::ShowLobbyProgress() { void GameMenu::ShowTPS() { ImGui::Text("Server TPS : %.1f", GetClient()->GetConnexion().GetServerTPS()); - ImGui::Text("Server MSPT : %i", (int)GetClient()->GetConnexion().GetServerMSPT()); + ImGui::Text("Server MSPT : %i", static_cast(GetClient()->GetConnexion().GetServerMSPT())); ImGui::Text("Server Ping : %i", GetClient()->GetConnexion().GetServerPing()); }