add ServerMain

This commit is contained in:
2024-07-23 01:14:42 +02:00
parent d5d459b658
commit acbc00c6c6

56
src/ServerMain.cpp Normal file
View File

@@ -0,0 +1,56 @@
#include <server/Server.h>
#include <Nazara/Core/Application.hpp>
#include <Nazara/Core/EntitySystemAppComponent.hpp>
#include <Nazara/Network/Network.hpp>
#include <iostream>
void ConsoleLoop(Nz::Application<Nz::Network>& app, blitz::server::Server& server) {
std::string line;
while (true) {
getline(std::cin, line);
if (line == "stop") {
std::cout << "Exiting ...\n";
server.CloseServer();
app.Quit();
break;
}
}
}
static std::uint16_t DefaultPort = 25565;
int main(int argc, char* args[]) {
Nz::Application<Nz::Network> app(argc, args);
const auto& params = app.GetCommandLineParameters();
std::string_view portString;
std::uint16_t port = DefaultPort;
if (params.GetParameter("port", &portString)) {
std::cout << "Selected port : " << portString << "\n";
bool ok;
port = Nz::StringToNumber(portString, 10, &ok);
if (!ok) {
std::cerr << "Failed to parse port !\n";
port = DefaultPort;
}
}
auto& ecs = app.AddComponent<Nz::EntitySystemAppComponent>();
auto& serverWorld = ecs.AddWorld<Nz::EnttWorld>();
blitz::server::Server server(port, serverWorld);
std::cout << "Server running on port " << port << " ...\n";
std::jthread consoleThread([&app, &server]() { ConsoleLoop(app, server); });
// tick 20 times per seconds
app.AddUpdaterFunc([]() { std::this_thread::sleep_for(std::chrono::milliseconds(50)); });
return app.Run();
}