Skip to content

Commit

Permalink
player_id added to constants
Browse files Browse the repository at this point in the history
  • Loading branch information
ISPlatonov committed Jan 9, 2024
1 parent a092eb8 commit 7bd3016
Show file tree
Hide file tree
Showing 16 changed files with 203 additions and 124 deletions.
29 changes: 2 additions & 27 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,14 @@ project(SFML_project LANGUAGES CXX)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# Fetch SFML
include(FetchContent)
FetchContent_Declare(SFML
GIT_REPOSITORY https://github.com/SFML/SFML.git
GIT_TAG 2.6.x)
FetchContent_MakeAvailable(SFML)

include_directories(include)
add_subdirectory(src)

file(GLOB SOURCE_FILES CONFIGURE_DEPENDS src/*.cpp)
add_executable(SFML_project ${SOURCE_FILES})
target_compile_definitions(SFML_project PRIVATE CLIENT)
target_link_libraries(SFML_project sfml-graphics sfml-window sfml-audio sfml-network sfml-system)
target_compile_features(SFML_project PRIVATE cxx_std_17)

set(SERVER_SOURCE_FILES src/server/server.cpp src/Multiplayer.cpp src/Object.cpp)
add_executable(server ${SERVER_SOURCE_FILES})
target_link_libraries(server sfml-graphics sfml-window sfml-network sfml-system)
target_compile_features(server PRIVATE cxx_std_17)

if(WIN32)
add_custom_command(
TARGET SFML_project
COMMENT "Copy OpenAL DLL"
PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${SFML_SOURCE_DIR}/extlibs/bin/$<IF:$<BOOL:${ARCH_64BITS}>,x64,x86>/openal32.dll $<TARGET_FILE_DIR:SFML_project>
VERBATIM)
endif()

add_custom_command(TARGET SFML_project POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/textures $<TARGET_FILE_DIR:SFML_project>/textures)
add_custom_command(TARGET server POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/textures $<TARGET_FILE_DIR:server>/textures)

install(TARGETS SFML_project server DESTINATION ${CMAKE_SOURCE_DIR}/bin)
install(DIRECTORY ${CMAKE_SOURCE_DIR}/textures DESTINATION ${CMAKE_SOURCE_DIR}/bin)
10 changes: 8 additions & 2 deletions include/Actor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@

namespace Actor
{
enum class Direction {
left,
right
};


/**
* @brief Base class for all actors
*/
Expand All @@ -34,8 +40,8 @@ namespace Actor

protected:
sf::Sprite sprite;
static inline std::unordered_map<std::string, sf::Texture> textures{};
std::string direction_x;
static inline std::unordered_map<Direction, sf::Texture> textures{};
Direction direction_x;
Multiplayer::Inventory inventory;

virtual void draw(sf::RenderTarget& target, sf::RenderStates states=sf::RenderStates()) const;
Expand Down
61 changes: 61 additions & 0 deletions include/Constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,42 @@
#ifdef CLIENT
#include <SFML/Window.hpp>
#endif
#include <SFML/Network.hpp>
#include <string>
#include <fstream>
#include <random>
#include <chrono>


/**
* @brief This class is used to store player unique id
*/
class PlayerId {
public:
PlayerId(size_t ip, size_t port, size_t timestamp, size_t key) : ip(ip), port(port), timestamp(timestamp), key(key) {}

inline size_t getIp() const { return ip; }
inline size_t getPort() const { return port; }
inline size_t getTimestamp() const { return timestamp; }
inline size_t getKey() const { return key; }

inline bool operator==(const PlayerId& other) const {
return ip == other.ip && port == other.port && timestamp == other.timestamp && key == other.key;
}

private:
size_t ip;
size_t port;
size_t timestamp;
size_t key;
};


inline sf::Packet& operator <<(sf::Packet& packet, const PlayerId& id)
{
packet << id.getIp() << id.getPort() << id.getTimestamp() << id.getKey();
return packet;
}


/**
Expand Down Expand Up @@ -34,6 +68,26 @@ class Constants
infile >> SERVER_IP;
infile >> MAX_PING;
infile >> UDP_PACKETS_GAP;
infile.close();
std::ifstream infile_id("textures/id.txt");
if (!infile_id.is_open()) {
std::ofstream outfile("textures/id.txt");
sf::IpAddress ip_global = sf::IpAddress::getPublicAddress();
auto socket = sf::UdpSocket();
while (socket.bind(sf::Socket::AnyPort) != sf::Socket::Done);
auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
size_t random_key = std::random_device()();
outfile << ip_global.toInteger() << std::endl
<< socket.getLocalPort() << std::endl
<< timestamp << std::endl
<< random_key << std::endl;
outfile.close();
infile_id.open("textures/id.txt");
}
size_t id_ip, id_port, id_timestamp, id_key;
infile_id >> id_ip >> id_port >> id_timestamp >> id_key;
id = {id_ip, id_port, id_timestamp, id_key};
infile_id.close();
loaded = true;
}
static inline const size_t& getPIXEL_SIZE()
Expand Down Expand Up @@ -134,6 +188,12 @@ class Constants
load_constants();
return UDP_PACKETS_GAP;
}
static inline const PlayerId& getID() {
while (!loaded)
load_constants();
return id;
}


private:
Constants();
Expand All @@ -158,4 +218,5 @@ class Constants
static inline std::string SERVER_IP = "77.73.71.158";
static inline int MAX_PING = 5000;
static inline size_t UDP_PACKETS_GAP = 1000;
static inline PlayerId id = {0, 0, 0, 0};
};
4 changes: 2 additions & 2 deletions include/Multiplayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ namespace Multiplayer
public:
Transportable() {}
Transportable(sf::Vector2f pos, Time sent) : position(pos), sent_time(sent) {}
void setPosition(const sf::Vector2f& new_position);
void setTime(const Time& new_time);
inline void setPosition(const sf::Vector2f& new_position) { position = new_position; };
inline void setTime(const Time& new_time) { sent_time = new_time; }
inline const sf::Vector2f& getPosition() const { return position; }
inline const Time& getTime() const { return sent_time; }

Expand Down
33 changes: 33 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
add_subdirectory(common)

# Server build
set(SERVER_SOURCE_FILES server/server.cpp ${SRC_COMMON_SERVER})
add_executable(server ${SERVER_SOURCE_FILES})
target_link_libraries(server sfml-graphics sfml-window sfml-network sfml-system)
target_compile_features(server PRIVATE cxx_std_17)

add_custom_command(TARGET server POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/textures $<TARGET_FILE_DIR:server>/textures)

# Client build
file(GLOB SOURCE_FILES CONFIGURE_DEPENDS client/main.cpp ${SRC_COMMON_CLIENT})
add_executable(SFML_project ${SOURCE_FILES})
target_compile_definitions(SFML_project PRIVATE CLIENT)
target_link_libraries(SFML_project sfml-graphics sfml-window sfml-audio sfml-network sfml-system)
target_compile_features(SFML_project PRIVATE cxx_std_17)

if(WIN32)
add_custom_command(
TARGET SFML_project
COMMENT "Copy OpenAL DLL"
PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${SFML_SOURCE_DIR}/extlibs/bin/$<IF:$<BOOL:${ARCH_64BITS}>,x64,x86>/openal32.dll $<TARGET_FILE_DIR:SFML_project>
VERBATIM)
endif()

add_custom_command(TARGET SFML_project POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/textures $<TARGET_FILE_DIR:SFML_project>/textures)

# Installing instructions
install(TARGETS SFML_project server DESTINATION ${CMAKE_SOURCE_DIR}/bin)
File renamed without changes.
18 changes: 9 additions & 9 deletions src/Actor.cpp → src/common/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Actor
{
if (!Actor::textures.size())
load_textures("textures/actors/Guy_16x32");
direction_x = "right";
direction_x = Direction::right;
sprite.setPosition(position);
sprite.setTexture(Actor::textures.at(direction_x));
sprite.setScale(Constants::getPIXEL_SIZE(), Constants::getPIXEL_SIZE());
Expand All @@ -19,7 +19,7 @@ namespace Actor
{
if (!Actor::textures.size())
load_textures("textures/actors/Guy_16x32");
direction_x = "right";
direction_x = Direction::right;
sprite.setPosition(position);
sprite.setTexture(Actor::textures.at(direction_x));
sprite.setScale(Constants::getPIXEL_SIZE(), Constants::getPIXEL_SIZE());
Expand All @@ -29,10 +29,10 @@ namespace Actor

void Actor::check_direction(const sf::Vector2f& direction)
{
if (direction.x > 0 && direction_x != "right")
direction_x = "right";
else if (direction.x < 0 && direction_x != "left")
direction_x = "left";
if (direction.x > 0 && direction_x != Direction::right)
direction_x = Direction::right;
else if (direction.x < 0 && direction_x != Direction::left)
direction_x = Direction::left;
else
return;
sprite.setTexture(Actor::textures.at(direction_x));
Expand Down Expand Up @@ -130,7 +130,7 @@ namespace Actor
{
auto position = user.getPosition() / static_cast<float>(Constants::getPIXEL_SIZE());
sf::Uint32 time = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
packet << position.x << position.y << user.getIp() << user.getPort() << time;
packet << position.x << position.y << Constants::getID() << time;
packet << static_cast<sf::Uint32>(user.getInventory().size());
for (auto iter : user.getInventory())
{
Expand Down Expand Up @@ -159,8 +159,8 @@ namespace Actor
left_texture.loadFromFile(texture_dir_path + "/left.png");
auto right_texture = sf::Texture();
right_texture.loadFromFile(texture_dir_path + "/right.png");
Actor::textures["left"] = std::move(left_texture);
Actor::textures["right"] = std::move(right_texture);
Actor::textures[Direction::left] = std::move(left_texture);
Actor::textures[Direction::right] = std::move(right_texture);
}


Expand Down
11 changes: 11 additions & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
add_subdirectory(Multiplayer)

# Client dependencies
file(GLOB SRC_COMMON_CLIENT CONFIGURE_DEPENDS ${SRC_COMMON_MULTIPLAYER} *.cpp)
message(STATUS "SRC_COMMON_CLIENT: ${SRC_COMMON_CLIENT}")
set(SRC_COMMON_CLIENT ${SRC_COMMON_CLIENT} PARENT_SCOPE)

# Server dependencies
file(GLOB SRC_COMMON_SERVER CONFIGURE_DEPENDS ${SRC_COMMON_MULTIPLAYER} Object.cpp)
message(STATUS "SRC_COMMON_SERVER: ${SRC_COMMON_SERVER}")
set(SRC_COMMON_SERVER ${SRC_COMMON_SERVER} PARENT_SCOPE)
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions src/common/Multiplayer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
file(GLOB SRC_COMMON_MULTIPLAYER CONFIGURE_DEPENDS *.cpp)
set(SRC_COMMON_MULTIPLAYER ${SRC_COMMON_MULTIPLAYER} PARENT_SCOPE)
87 changes: 3 additions & 84 deletions src/Multiplayer.cpp → src/common/Multiplayer/Multiplayer.cpp
Original file line number Diff line number Diff line change
@@ -1,92 +1,9 @@
#include "Multiplayer.hpp"


sf::Packet& operator <<(sf::Packet& packet, Multiplayer::ObjectData& object_data)
{
//#ifndef CLIENT
object_data.setTime(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
//#endif
packet << object_data.getPosition().x << object_data.getPosition().y << static_cast<sf::Uint32>(object_data.getTime()) << object_data.getName() << object_data.getPassability();
return packet;
}


Object::Object& operator <<(Object::Object& object, const Multiplayer::ObjectData& object_data)
{
auto position = object_data.getPosition() * static_cast<float>(Constants::getPIXEL_SIZE());
auto& name = object_data.getName();
auto& passability = object_data.getPassability();
object = Object::Object(name, position, passability);
return object;
}


sf::Packet& operator >>(sf::Packet& data, Multiplayer::ObjectData& object_data)
{
sf::Vector2f position;
Object::ObjectName object_name;
Object::Passability passability;
sf::Uint32 object_name_enum;
sf::Uint32 passability_enum;
sf::Uint32 sent_time;
data >> position.x >> position.y >> sent_time >> object_name_enum >> passability_enum;
object_name = static_cast<Object::ObjectName>(object_name_enum);
passability = static_cast<Object::Passability>(passability_enum);
object_data = Multiplayer::ObjectData(std::move(position), std::move(sent_time), std::move(object_name), std::move(passability));
return data;
}


sf::Packet& operator <<(sf::Packet& packet, const Multiplayer::PlayerData& player_data)
{
packet << player_data.getPosition().x << player_data.getPosition().y << player_data.getIp() << player_data.getPort() << player_data.getTime();
packet << static_cast<sf::Uint32>(player_data.getInventory().size());
for (auto pair : player_data.getInventory())
{
packet << pair.first << static_cast<sf::Uint32>(pair.second);
}
return packet;
}


sf::Packet& operator >>(sf::Packet& packet, Multiplayer::PlayerData& player_data)
{
int msg_ip;
sf::Vector2f position;
sf::Uint32 sent_time;
unsigned int msg_port;
packet >> position.x >> position.y >> msg_ip >> msg_port >> sent_time;
sf::Uint32 inventory_size_uint32;
packet >> inventory_size_uint32;
size_t inventory_size = static_cast<size_t>(inventory_size_uint32);
Multiplayer::Inventory inventory;
for (size_t i = 0; i < inventory_size; ++i)
{
sf::Uint32 object_name_enum;
sf::Uint32 object_num_uint32;
packet >> object_name_enum >> object_num_uint32;
size_t object_num = static_cast<size_t>(object_num_uint32);
inventory[static_cast<Object::ObjectName>(object_name_enum)] = std::move(object_num);
}
player_data = Multiplayer::PlayerData(std::move(position), std::move(msg_ip), std::move(msg_port), std::move(sent_time), std::move(inventory)); // no inventory needed!
return packet;
}
#include <iostream>


namespace Multiplayer
{
void Transportable::setPosition(const sf::Vector2f& new_position)
{
position = new_position;
}


void Transportable::setTime(const Time& new_time)
{
sent_time = new_time;
}


const size_t PlayerData::objectNumber(Object::ObjectName name) const
{
if (!inventory.count(name))
Expand Down Expand Up @@ -133,6 +50,8 @@ namespace Multiplayer
// bind the socket to a port
while (socket.bind(port) != sf::Socket::Status::Done);
port = socket.getLocalPort();
std::cout << "ip: " << sf::IpAddress::getLocalAddress() << std::endl;
std::cout << "port: " << port << std::endl;
}


Expand Down
Loading

0 comments on commit 7bd3016

Please sign in to comment.