Merge pull request #13 from KingoCor/map-download

Syncing the map with the server by FTP
master
Vectozavr 2023-05-21 14:38:55 +03:00 committed by GitHub
commit 1dccac0085
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 4 deletions

View File

@ -52,6 +52,7 @@ void Shooter::initNetwork() {
server->generateBonuses();
}
client->requestMap(clientIp, &current_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();

View File

@ -29,6 +29,7 @@ private:
bool inGame = false;
int fireTraces = 0;
std::string current_map = ShooterConsts::MAP_OBJ;
void start() override;
void update() override;

View File

@ -4,6 +4,8 @@
#include "ShooterClient.h"
#include <SFML/Network/Ftp.hpp>
#include <string>
#include <utility>
#include "../3dzavr/engine/utils/Log.h"
#include "../3dzavr/engine/animation/Timeline.h"
@ -301,3 +303,40 @@ void
ShooterClient::setChangeEnemyWeaponCallBack(std::function<void(const std::string &, sf::Uint16)> 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<std::string>& listing = dirResponse.getListing();
if (listing.size()!=0) {
for (std::vector<std::string>::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("------------------------------");
}

View File

@ -7,6 +7,7 @@
#include "../3dzavr/engine/network/ClientUDP.h"
#include "../player/Player.h"
#include <SFML/Config.hpp>
class ShooterClient final : public ClientUDP {
private:
@ -60,6 +61,8 @@ public:
void addPlayer(sf::Uint16 id, std::shared_ptr<Player> player);
void requestMap(std::string clientIp, std::string *current_map);
[[nodiscard]] std::map<sf::Uint16, std::shared_ptr<Player>> const &players() const { return _players; }
[[nodiscard]] std::string lastEvent() const { return _lastEvent; }