shooter/Source.cpp

65 lines
1.5 KiB
C++
Raw Normal View History

2021-09-13 15:53:43 +03:00
//
2024-05-24 16:10:23 +03:00
// Created by Иван Ильин on 22.09.2021.
2021-09-13 15:53:43 +03:00
//
2024-05-24 16:10:23 +03:00
#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>
2021-09-13 15:53:43 +03:00
using namespace std;
2024-05-24 16:10:23 +03:00
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;
2021-09-13 15:53:43 +03:00
2024-05-24 16:10:23 +03:00
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();
}
2021-10-29 23:44:37 +03:00
int main() {
2024-05-24 16:10:23 +03:00
Time::init();
ShooterServer server{};
Log::log("Initializing the server...");
InitServer(server);
double lastTryReconnecting = 0;
2021-09-13 15:53:43 +03:00
2024-05-24 16:10:23 +03:00
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();
2021-09-13 15:53:43 +03:00
2024-05-24 16:10:23 +03:00
sleep_for(10ms);
}
2021-09-13 15:53:43 +03:00
return 0;
2024-05-24 16:10:23 +03:00
}