Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge server with shooter #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ CMakeUserPresets.json

# Project exclude paths
engine.log
shooter
shooter
shooter_server
19 changes: 17 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")

add_executable(${CMAKE_PROJECT_NAME}
# game:
Source.cpp
player/Player.cpp
player/Player.h
Expand All @@ -29,8 +28,24 @@ add_executable(${CMAKE_PROJECT_NAME}
network/ShooterMsgType.cpp
network/Chat.cpp
network/Chat.h
)
)

add_executable(${CMAKE_PROJECT_NAME}_server
Server.cpp
player/Player.cpp
player/Player.h
network/ShooterServer.cpp
network/ShooterServer.h
ShooterConsts.h
network/ShooterMsgType.h
network/ShooterMsgType.cpp
network/Chat.cpp
network/Chat.h
weapon/Weapon.cpp
weapon/Weapon.h
)

# include 3dzavr engine into our project
add_subdirectory(3dzavr/engine)
target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC 3DZAVR)
target_link_libraries(${CMAKE_PROJECT_NAME}_server PUBLIC 3DZAVR)
64 changes: 64 additions & 0 deletions Server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// Created by Иван Ильин on 22.09.2021.
//

#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::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() {
Time::init();
ShooterServer server{};

Log::log("Initializing the server...");
InitServer(server);

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;
}