1er commit

This commit is contained in:
2021-08-21 10:14:47 +02:00
commit a99ecf7c2d
99 changed files with 66605 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
#pragma once
#include <vector>
#include "misc/Time.h"
namespace td {
namespace server {
class Server;
class Lobby{
private:
Server* m_Server;
bool m_GameStarted = false;
std::uint64_t m_StartTimerTime = 0;
std::vector<std::uint8_t> m_Players;
utils::Timer m_Timer;
public:
Lobby(Server* server);
void OnPlayerJoin(std::uint8_t playerID);
void OnPlayerLeave(std::uint8_t playerID);
void sendTimeRemaining();
void tick();
};
} // namespace server
} // namespace td

View File

@@ -0,0 +1,96 @@
#pragma once
#include <cstdint>
#include <map>
#include "network/TCPListener.h"
#include "protocol/Protocol.h"
#include "protocol/PacketDispatcher.h"
#include "protocol/PacketHandler.h"
#include "ServerGame.h"
#include "ServerConnexion.h"
#include "Lobby.h"
//#define LOBBY_WAITING_TIME 2 * 60 * 1000
#define LOBBY_WAITING_TIME 5 * 1000
#define SERVER_TPS 20
#define SERVER_TICK 1000 / SERVER_TPS
namespace td {
namespace server {
typedef std::map<std::uint8_t, ServerConnexion> ConnexionMap;
class TickCounter{
private:
float m_TPS;
std::uint64_t m_LastTPSTime;
std::uint8_t m_TickCount;
public:
TickCounter(){}
void reset(){
m_TPS = SERVER_TPS;
m_LastTPSTime = utils::getTime();
m_TickCount = 0;
}
bool update(){ // return true when tps is updated
m_TickCount++;
if (m_TickCount >= SERVER_TPS){
std::uint64_t timeElapsedSinceLast20Ticks = td::utils::getTime() - m_LastTPSTime;
m_TPS = (float)SERVER_TPS / (float)(timeElapsedSinceLast20Ticks / 1000.0f);
m_TickCount = 0;
m_LastTPSTime = td::utils::getTime();
return true;
}
return false;
}
float getTPS() const{ return m_TPS; }
};
class Server{
private:
network::TCPListener m_Listener;
ConnexionMap m_Connections;
ServerGame m_Game{this};
Lobby m_Lobby{this};
TickCounter m_TickCounter;
public:
Server(const std::string& worldFilePath);
virtual ~Server(){}
bool start(std::uint16_t port);
void tick(std::uint64_t delta);
void stop();
void lauchGame();
void removeConnexion(std::uint8_t connexionID);
void broadcastPacket(protocol::Packet* packet);
float getTPS() const{ return m_TickCounter.getTPS(); }
const ServerGame& getGame() const{ return m_Game; }
ServerGame& getGame() { return m_Game; }
const Lobby& getLobby() const{ return m_Lobby; }
const ConnexionMap& getConnexions() const{ return m_Connections; }
ConnexionMap& getConnexions(){ return m_Connections; }
const game::PlayerList& getPlayers() const{ return m_Game.getPlayers(); }
game::PlayerList& getPlayers(){ return m_Game.getPlayers(); }
private:
void accept();
void updateSockets();
void OnPlayerJoin(std::uint8_t id);
void OnPlayerLeave(std::uint8_t id);
};
} // namespace server
} // namespace td

View File

@@ -0,0 +1,56 @@
#pragma once
#include "network/TCPSocket.h"
#include "protocol/PacketHandler.h"
#include "protocol/PacketDispatcher.h"
#include "game/Player.h"
#include "game/Connexion.h"
namespace td{
namespace server{
class Server;
struct KeepAlive
{
std::uint64_t keepAliveID = 0;
std::uint64_t sendTime;
bool recievedResponse = false;
};
class ServerConnexion : public protocol::Connexion{
private:
Server* m_Server = nullptr;
std::uint8_t m_ID;
KeepAlive m_KeepAlive;
game::Player* m_Player;
public:
ServerConnexion();
ServerConnexion(network::TCPSocket& socket, std::uint8_t id);
ServerConnexion(ServerConnexion&& move);
virtual ~ServerConnexion();
void setServer(Server* server);
virtual void HandlePacket(protocol::PlayerLoginPacket* packet);
virtual void HandlePacket(protocol::KeepAlivePacket* packet);
virtual void HandlePacket(protocol::SelectTeamPacket* packet);
virtual void HandlePacket(protocol::DisconnectPacket* packet);
std::uint8_t getID() const{return m_ID;}
const game::Player* getPlayer() const{return m_Player;}
game::Player* getPlayer() {return m_Player;}
virtual bool updateSocket();
REMOVE_COPY(ServerConnexion);
private:
void registerHandlers();
void checkKeepAlive();
void sendKeepAlive();
void initConnection();
};
} // namespace server
} // namespace td

View File

@@ -0,0 +1,29 @@
#pragma once
#include "game/BaseGame.h"
#include "misc/Time.h"
#include "ServerWorld.h"
namespace td {
namespace server {
class Server;
class ServerGame : public game::Game{
private:
Server* m_Server;
ServerWorld m_ServerWorld;
utils::Timer m_GoldMineTimer{1000, std::bind(&ServerGame::updateGoldMines, this)};
public:
ServerGame(Server* server);
~ServerGame(){}
virtual void tick(std::uint64_t delta);
void startGame();
private:
void balanceTeams();
void updateGoldMines();
};
} // namespace game
} // namespace td

View File

@@ -0,0 +1,21 @@
#pragma once
#include "game/World.h"
namespace td {
namespace server {
class Server;
class ServerGame;
class ServerWorld : public game::World{
private:
game::MobID m_CurrentMobID = 0;
Server* m_Server;
public:
ServerWorld(Server* server, ServerGame* game);
void spawnMobs(game::MobType type, std::uint8_t level, game::PlayerID sender, std::uint8_t count);
};
} // namespace server
} // namespace td