From f0e57629b127ef710c5905b44698cdd3efbaa42d Mon Sep 17 00:00:00 2001 From: KingoCor Date: Sat, 23 Jul 2022 13:07:29 +0300 Subject: [PATCH] Syncing the map with the server by FTP --- Shooter.cpp | 10 ++++++---- Shooter.h | 1 + network/ShooterClient.cpp | 39 +++++++++++++++++++++++++++++++++++++++ network/ShooterClient.h | 3 +++ 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/Shooter.cpp b/Shooter.cpp index a16b13e..ccd58c4 100644 --- a/Shooter.cpp +++ b/Shooter.cpp @@ -52,6 +52,7 @@ void Shooter::initNetwork() { server->generateBonuses(); } + client->requestMap(clientIp, ¤t_map); client->connect(clientIp, clientPort); player->setPlayerNickName(playerName); @@ -67,12 +68,15 @@ void Shooter::initNetwork() { } void Shooter::start() { + // connecting to the server + initNetwork(); + // This code executed once in the beginning: setUpdateWorld(false); screen->setMouseCursorVisible(true); - world->loadMap(ShooterConsts::MAP_OBJ, Vec3D{5, 5, 5}); + world->loadMap(current_map, Vec3D{5, 5, 5}); // TODO: encapsulate call backs inside Player player->setAddTraceCallBack([this](const Vec3D &from, const Vec3D &to) { @@ -92,9 +96,7 @@ void Shooter::start() { camera->translateToPoint(player->position() + Vec3D{0, 1.8, 0}); player->attach(camera); world->addBody(player); - - // connecting to the server - initNetwork(); + // Waiting for connect and updating server if it's same window while (client->isWorking() && !client->connected()) { client->update(); diff --git a/Shooter.h b/Shooter.h index a145ee4..bc94dde 100644 --- a/Shooter.h +++ b/Shooter.h @@ -29,6 +29,7 @@ private: bool inGame = false; int fireTraces = 0; + std::string current_map = ShooterConsts::MAP_OBJ; void start() override; void update() override; diff --git a/network/ShooterClient.cpp b/network/ShooterClient.cpp index a4dc480..1aed4b4 100644 --- a/network/ShooterClient.cpp +++ b/network/ShooterClient.cpp @@ -4,6 +4,8 @@ #include "ShooterClient.h" +#include +#include #include #include "../engine/utils/Log.h" #include "../engine/animation/Timeline.h" @@ -301,3 +303,40 @@ void ShooterClient::setChangeEnemyWeaponCallBack(std::function changeEnemyWeapon) { _changeEnemyWeaponCallBack = std::move(changeEnemyWeapon); } + +void ShooterClient::requestMap(std::string clientIp, std::string *current_map) { + Log::log("---------[FTP server]---------"); + sf::Ftp ftp; + sf::Ftp::Response connectResponse = ftp.connect(clientIp, 21); + if (connectResponse.isOk()) { + ftp.login(); + + sf::Ftp::ListingResponse dirResponse = ftp.getDirectoryListing("current_map/"); + Log::log("Response code: "+std::to_string(dirResponse.getStatus())+" | Message: "+dirResponse.getMessage()); + if (dirResponse.isOk()) { + const std::vector& listing = dirResponse.getListing(); + + if (listing.size()!=0) { + for (std::vector::const_iterator it = listing.begin(); it != listing.end(); ++it) + Log::log("- "+*it); + + sf::Ftp::Response downloadResponse = ftp.download(listing.at(0), "./obj/maps/", sf::Ftp::Ascii); + Log::log("Response code: "+std::to_string(downloadResponse.getStatus())+" | Message: "+downloadResponse.getMessage()); + + if (downloadResponse.isOk()) { + std::string map_path = listing.at(0); + map_path = "./obj/maps"+map_path.substr(map_path.find("/")); + Log::log("Map set to: "+map_path); + *current_map = map_path; + } + } else { + Log::log("there is no map file"); + } + } + + ftp.disconnect(); + } else { + Log::log("Couldn't connect to FTP server with ip: "+clientIp+" and port: 21"); + } + Log::log("------------------------------"); +} diff --git a/network/ShooterClient.h b/network/ShooterClient.h index b622469..7e389a9 100644 --- a/network/ShooterClient.h +++ b/network/ShooterClient.h @@ -7,6 +7,7 @@ #include "../engine/network/ClientUDP.h" #include "../player/Player.h" +#include class ShooterClient final : public ClientUDP { private: @@ -60,6 +61,8 @@ public: void addPlayer(sf::Uint16 id, std::shared_ptr player); + void requestMap(std::string clientIp, std::string *current_map); + [[nodiscard]] std::map> const &players() const { return _players; } [[nodiscard]] std::string lastEvent() const { return _lastEvent; }