From a7c72d7f2d03e672102857e3860ffee55b2d3149 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Tue, 2 Jun 2020 06:39:37 +0200 Subject: [PATCH] [ServerConfig] 'maxPlayers' added. --- CMakeLists.txt | 2 +- config/server.example.lua | 3 +++ source/server/core/ServerConfig.cpp | 5 +++++ source/server/core/ServerConfig.hpp | 5 +++++ source/server/network/Server.cpp | 6 +++--- 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 608527ea3..843606e2f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,7 +62,7 @@ if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git") option(GIT_SUBMODULE "Check submodules during build" ON) if(GIT_SUBMODULE) message(STATUS "Submodule update") - execute_process(COMMAND ${GIT_EXECUTABLE} submodule sync WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) + execute_process(COMMAND ${GIT_EXECUTABLE} submodule sync --recursive WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} RESULT_VARIABLE GIT_SUBMOD_RESULT) diff --git a/config/server.example.lua b/config/server.example.lua index 428e93514..9b04a55f5 100644 --- a/config/server.example.lua +++ b/config/server.example.lua @@ -5,3 +5,6 @@ -- Gameplay useItemDrops = false +-- Server +maxPlayers = 5 + diff --git a/source/server/core/ServerConfig.cpp b/source/server/core/ServerConfig.cpp index f03e8633b..c8f1cfe3b 100644 --- a/source/server/core/ServerConfig.cpp +++ b/source/server/core/ServerConfig.cpp @@ -29,6 +29,9 @@ // Gameplay bool ServerConfig::useItemDrops = false; +// Server +u8 ServerConfig::maxPlayers = 5; + #include #include @@ -43,6 +46,8 @@ void ServerConfig::loadConfigFromFile(const char *file) { useItemDrops = lua["useItemDrops"].get_or(useItemDrops); + maxPlayers = lua["maxPlayers"].get_or(maxPlayers); + gkInfo() << "Config file loaded successfully"; } catch (sol::error &e) { diff --git a/source/server/core/ServerConfig.hpp b/source/server/core/ServerConfig.hpp index 4ee378fa7..169de5b73 100644 --- a/source/server/core/ServerConfig.hpp +++ b/source/server/core/ServerConfig.hpp @@ -27,10 +27,15 @@ #ifndef SERVERCONFIG_HPP_ #define SERVERCONFIG_HPP_ +#include + namespace ServerConfig { // Gameplay extern bool useItemDrops; + // Server + extern u8 maxPlayers; + void loadConfigFromFile(const char *file); } diff --git a/source/server/network/Server.cpp b/source/server/network/Server.cpp index a6c206eb8..cc21794f4 100644 --- a/source/server/network/Server.cpp +++ b/source/server/network/Server.cpp @@ -25,6 +25,7 @@ * ===================================================================================== */ #include "Server.hpp" +#include "ServerConfig.hpp" void Server::init(u16 port) { if (m_tcpListener.listen(port) != sf::Socket::Done) @@ -57,9 +58,8 @@ void Server::handleNewConnections() { Network::Command command; packet >> command; if (command != Network::Command::ClientConnect) - throw EXCEPTION("Network error: Expected 'ClientConnect' packet."); - - if (m_info.clients().size() < 5) { + gkError() << "Network error: Expected 'ClientConnect' packet for new clients."; + else if (m_info.clients().size() < ServerConfig::maxPlayers) { std::string address; packet >> address;