Skip to content

Commit

Permalink
[ServerConfig] 'maxPlayers' added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jun 2, 2020
1 parent 742b1f2 commit a7c72d7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions config/server.example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
-- Gameplay
useItemDrops = false

-- Server
maxPlayers = 5

5 changes: 5 additions & 0 deletions source/server/core/ServerConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
// Gameplay
bool ServerConfig::useItemDrops = false;

// Server
u8 ServerConfig::maxPlayers = 5;

#include <gk/core/Debug.hpp>
#include <gk/core/Filesystem.hpp>

Expand All @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions source/server/core/ServerConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@
#ifndef SERVERCONFIG_HPP_
#define SERVERCONFIG_HPP_

#include <gk/core/IntTypes.hpp>

namespace ServerConfig {
// Gameplay
extern bool useItemDrops;

// Server
extern u8 maxPlayers;

void loadConfigFromFile(const char *file);
}

Expand Down
6 changes: 3 additions & 3 deletions source/server/network/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* =====================================================================================
*/
#include "Server.hpp"
#include "ServerConfig.hpp"

void Server::init(u16 port) {
if (m_tcpListener.listen(port) != sf::Socket::Done)
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit a7c72d7

Please sign in to comment.