From ac53da60b7dbcdc91cf600498b27e7d2c6e21e7e Mon Sep 17 00:00:00 2001 From: Nakidai Date: Fri, 24 May 2024 16:10:23 +0300 Subject: [PATCH] Convert to server --- CMakeLists.txt | 2 +- Source.cpp | 63 +++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0091a24..3568f8b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.17) -project(shooter) +project(shooter_server) set(CMAKE_CXX_STANDARD 20) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") diff --git a/Source.cpp b/Source.cpp index bb97154..4cc97f1 100644 --- a/Source.cpp +++ b/Source.cpp @@ -1,23 +1,64 @@ // -// Created by Ivan Ilin on 06.02.2021. +// Created by Иван Ильин on 22.09.2021. // -#include "Shooter.h" +#include +#include +#include +#include "network/ShooterServer.h" +#include +#include +#include +#include +#include using namespace std; +using namespace std::this_thread; // sleep_for, sleep_until +using namespace std::chrono_literals; // ns, us, ms, s, h, etc. +using std::chrono::system_clock; +void InitServer(ShooterServer& server) +{ + sf::Uint16 serverPort; + std::ifstream connectFile("server.txt", std::ifstream::in); + + if (!connectFile.is_open() || !(connectFile >> serverPort)) + { + connectFile.close(); + // Create file and write default settings + serverPort = 54000; + std::ofstream temp("server.txt", std::ofstream::out); + temp << serverPort; + temp.close(); + } + connectFile.close(); + + server.start(serverPort); + if(server.isWorking()) + server.generateBonuses(); +} int main() { - Shooter game; + Time::init(); + ShooterServer server{}; - // Optimal for standard monitors: - //game.create(720, 480, ShooterConsts::PROJECT_NAME, true); - game.create(1920, 1080, ShooterConsts::PROJECT_NAME, true); - //game.create(1920, 1080, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::Fullscreen); + Log::log("Initializing the server..."); + InitServer(server); - // Optimal for MacBook Pro 16 display: - //game.create(2048, 1152, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR); - //game.create(3072, 1920, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::Fullscreen); + double lastTryReconnecting = 0; + + while (true) { + if(!server.isWorking() && (Time::time() - lastTryReconnecting > Consts::NETWORK_TIMEOUT)) { + lastTryReconnecting = Time::time(); + server.stop(); + Log::log("Restarting the server..."); + InitServer(server); + } + Time::update(); + server.update(); + + sleep_for(10ms); + } return 0; -} \ No newline at end of file +}