Convert to server
parent
0db33018c5
commit
ac53da60b7
|
@ -1,5 +1,5 @@
|
||||||
cmake_minimum_required(VERSION 3.17)
|
cmake_minimum_required(VERSION 3.17)
|
||||||
project(shooter)
|
project(shooter_server)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
|
||||||
|
|
63
Source.cpp
63
Source.cpp
|
@ -1,23 +1,64 @@
|
||||||
//
|
//
|
||||||
// Created by Ivan Ilin on 06.02.2021.
|
// Created by Иван Ильин on 22.09.2021.
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "Shooter.h"
|
#include <fstream>
|
||||||
|
#include <utility>
|
||||||
|
#include <iostream>
|
||||||
|
#include "network/ShooterServer.h"
|
||||||
|
#include <utils/Time.h>
|
||||||
|
#include <utils/Log.h>
|
||||||
|
#include <Consts.h>
|
||||||
|
#include <chrono>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
using namespace std;
|
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() {
|
int main() {
|
||||||
Shooter game;
|
Time::init();
|
||||||
|
ShooterServer server{};
|
||||||
|
|
||||||
// Optimal for standard monitors:
|
Log::log("Initializing the server...");
|
||||||
//game.create(720, 480, ShooterConsts::PROJECT_NAME, true);
|
InitServer(server);
|
||||||
game.create(1920, 1080, ShooterConsts::PROJECT_NAME, true);
|
|
||||||
//game.create(1920, 1080, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::Fullscreen);
|
|
||||||
|
|
||||||
// Optimal for MacBook Pro 16 display:
|
double lastTryReconnecting = 0;
|
||||||
//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);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue