Change Lobby timer

This commit is contained in:
2023-08-12 12:22:36 +02:00
parent b4836847f5
commit 0a814233a4
8 changed files with 27 additions and 24 deletions

View File

@@ -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

View File

@@ -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; }

View File

@@ -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<std::uint8_t> m_Players;
utils::AutoTimer m_Timer;
public:

View File

@@ -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; }
};

View File

@@ -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) {

View File

@@ -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<std::uint64_t>(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();
}
}

View File

@@ -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

View File

@@ -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<float>(timeRemaining) / static_cast<float>(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<int>(GetClient()->GetConnexion().GetServerMSPT()));
ImGui::Text("Server Ping : %i", GetClient()->GetConnexion().GetServerPing());
}